- Структура графа: Вот как выглядит структура графа:
< /ol>Код: Выделить всё
{ "1": { "coordinates": [0.1, 0.4, -1], "connections": [3, 5] }, "2": { "coordinates": [0.2, -0.4, 0.2], "connections": [1] }, "3": { "coordinates": [1, -0.3, 0.9], "connections": [1, 2] }, "4": { "coordinates": [-0.5, 0.4, 0.9], "connections": [] }, "5": { "coordinates": [0.0, 0.1, -0.2], "connections": [4, 2] } } - Класс FloodFill: я использую класс FloodFill с двумя асинхронными методами: animateImpulse и заполните.
Код: Выделить всё
function startImpulseFromAttributes() {
const graph = parseGraphDict();
const floodFill = new FloodFill(graph, animationSpeed);
floodFill.fill(startVertex, impulsePower, (impulse, vertex) => {
post("Impulse", JSON.stringify(impulse), " active at vertex ", JSON.stringify(vertex));
});
}
var activeObjects = []; // List to track created objects
function cleanupAllObjects() {
activeObjects.forEach(obj => obj.freepeer());
activeObjects = []; // Clear the list
}
class FloodFill {
constructor(graph, animationSpeed) {
this.graph = graph;
this.animationSpeed = animationSpeed;
}
async animateImpulse(startCoord, endCoord, speed) {
return new Promise((resolve) => {
const gridshape = new JitterObject("jit.gl.gridshape", contextName);
gridshape.shape = "cone";
gridshape.color = [Math.random(), Math.random(), Math.random()];
const animNode = new JitterObject("jit.anim.node");
gridshape.anim = animNode.name;
animNode.movemode = "local";
animNode.scale = impulseScale;
animNode.position = startCoord;
animNode.lookat = endCoord;
const animDrive = new JitterObject("jit.anim.drive");
animDrive.targetname = animNode.name;
animDrive.move(0, 0, speed);
const goalVector = endCoord.map((v, i) => v - startCoord[i]);
const checkPosition = new Task(() => {
const pos = animNode.worldpos;
const currentVector = pos.map((v, i) => v - startCoord[i]);
if (veclength(currentVector) >= veclength(goalVector)) {
post("Animation complete\n");
gridshape.freepeer();
animNode.freepeer();
animDrive.freepeer();
checkPosition.cancel();
resolve();
// post("Resolved promise\n");
}
});
checkPosition.interval = worldposQueryInterval;
checkPosition.repeat();
})
}
async fill(startVertexId, stepsLeft, actionCallback) {
if (stepsLeft
Подробнее здесь: [url]https://stackoverflow.com/questions/79381497/cant-figure-out-the-cause-of-unexpected-behaviour-of-my-async-recursive-animate[/url]