I am currently developing an application where users can utilize a dat.gui menu to customize their Three.js animation. Specifically, I am implementing the 3d-force-graph library.
Whenever a user makes a change, the following steps are taken:
- Swap out the old .json file used for their animation with the new one required to display their selection.
- Execute a function called
vertexControl
that adjusts the nodes of their new animation (e.g., changing node colors).
Here's a simple example:
const settings = {
num : 5,
}
const gui = new dat.GUI();
gui.add(settings, 'num', 1, 10).step(1).onChange( function update(n) {
Graph.jsonUrl('new_json'+ n.toString() +'.json'); //built-in method of 3d-force-graph for updating the .json file
vertexControl() //Once Graph is updated, adjust the vertices
}
The issue: The vertexControl
function runs before the Graph
object is updated with the new .json file. Consequently, the desired changes to the nodes do not take effect.
Attempted solutions: I have tried using the .refresh()
method provided by 3d-force-graph, but it doesn't seem to work in my scenario. Additionally, I experimented with callbacks and other approaches from StackOverflow discussions related to executing functions sequentially. Unfortunately, these methods were unsuccessful. Resorting to a cumbersome setTimeout
workaround to guess when the Graph
is updated works, but it seems impractical for such a basic task. There must be a more logical way to achieve this, right?