Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Give a hidden spun particle a finite angle so its vertices stay finite (#839)

Nguyen Xuan Anf874d37b6076
diff --git a/changes/839-spin-angle.md b/changes/839-spin-angle.md
new file mode 100644
index 00000000..cb0294ce
--- /dev/null
+++ b/changes/839-spin-angle.md
@@ -0,0 +1 @@
+- A particle system with `spin` no longer turns every vertex to NaN while its first particle is unborn or dead, which made the coplanar diagnostics of a video render throw at the frame where that happened (#839)
diff --git a/src/particles.js b/src/particles.js
index a1e609cf..f0d0826c 100644
--- a/src/particles.js
+++ b/src/particles.js
@@ -380,6 +380,8 @@ export function particleStateAt(launches, index, forces, scaleMode, time, target
if (age < 0 || age >= lifetime) {
target.scale = 0;
target.motionAge = 0;
+ // A spun particle is still rotated while it is hidden, so the angle must be a number, not whatever the last call left.
+ target.angle = launches.hasSpin ? launches.spinStart[index] : 0;
for (let component = 0; component < 3; component++) target.position[component] = launches.origin[index * 3 + component];
return target;
}
diff --git a/tests/particles.test.js b/tests/particles.test.js
index 4aa9cf88..0c7de640 100644
--- a/tests/particles.test.js
+++ b/tests/particles.test.js
@@ -364,3 +364,15 @@ test('hair limits the vertices it draws, and an empty surface is refused', () =>
flat.nodes[0].scale = [0, 0, 0];
assert.throws(() => createAgentScene(flat), /no triangle with area|scale/);
});
+
+test('a spinning system keeps every vertex finite once its first particle has died', () => {
+ const recipe = sparkRecipe({ systems: [{ node: 'sparks', seed: 7, count: 40, rate: 40, lifetime: [0.2, 0.4], speed: [1, 2], spin: [10, 200], size: 0.05 }] });
+ const scene = createAgentScene(recipe);
+ for (const time of [0, 0.1, 0.5, 1, 1.5]) {
+ scene.sampleMotion(time);
+ const geometry = scene.parts.find(part => part.name === 'sparks').geometry;
+ for (const values of [geometry.positions, geometry.normals]) {
+ assert.ok(values.every(Number.isFinite), `a vertex is not finite at ${time} s`);
+ }
+ }
+});