Skip to content

3D engine that AI agents drive

Fix box emitter radius default and particle parts after an earlier batch (#2203, #2204)

Nguyen Xuan An3c6524a91436
diff --git a/changes/2203.md b/changes/2203.md
new file mode 100644
index 00000000..949b2239
--- /dev/null
+++ b/changes/2203.md
@@ -0,0 +1 @@
+- A box particle emitter no longer gets a `radius: 0.5` written in (a stored one is dropped on load), and `set-particles` on a node added in an earlier batch now renders instead of failing with "Unknown part identity" (#2203, #2204).
diff --git a/src/agent-particles.js b/src/agent-particles.js
index 05f3835d..fc0975c8 100644
--- a/src/agent-particles.js
+++ b/src/agent-particles.js
@@ -96,6 +96,24 @@ function normalizeVelocitySource(input, emitter, from, path) {
emitter.velocityScale = input.velocityScale === undefined ? 1 : finiteNumber(input.velocityScale, `${path}.velocityScale`, -1000, 1000);
}
+const DEFAULT_EMITTER_RADIUS = 0.5;
+
+// Only a sphere and a disc are sized by a radius, so only they get the default. A box takes its size from halfExtents and
+// rejects a radius, except the 0.5 earlier versions wrote into every box they stored: that one is dropped so the recipe still loads.
+function assignEmitterRadius(input, emitter, shape, path) {
+ if (shape === 'box') {
+ if (input.radius !== undefined && input.radius !== DEFAULT_EMITTER_RADIUS) {
+ throw invalidField(`${path}.radius`, input.radius, `left out on a box, sized by ${path}.halfExtents`);
+ }
+ return;
+ }
+ if (input.radius !== undefined) {
+ emitter.radius = finiteNumber(input.radius, `${path}.radius`, 0, POSITION_LIMIT);
+ } else if (shape === 'sphere' || shape === 'disc') {
+ emitter.radius = DEFAULT_EMITTER_RADIUS;
+ }
+}
+
function normalizeEmitter(input, context, path) {
knownKeys(input, EMITTER_FIELDS, path);
const shape = input.shape ?? 'point';
@@ -106,11 +124,8 @@ function normalizeEmitter(input, context, path) {
shape,
position: input.position === undefined ? [0, 0, 0] : vectorOfThree(input.position, `${path}.position`, POSITION_LIMIT),
halfExtents: input.halfExtents === undefined ? [0.5, 0.5, 0.5] : vectorOfThree(input.halfExtents, `${path}.halfExtents`, POSITION_LIMIT).map(Math.abs),
- radius: input.radius === undefined ? 0.5 : finiteNumber(input.radius, `${path}.radius`, 0, POSITION_LIMIT),
};
- if (shape === 'box' && input.radius !== undefined) {
- throw invalidField(`${path}.radius`, input.radius, `left out on a box, sized by ${path}.halfExtents`);
- }
+ assignEmitterRadius(input, emitter, shape, path);
if (shape === 'mesh') {
normalizeMeshEmitter(input, emitter, context, path);
} else {
diff --git a/src/agent-scene.js b/src/agent-scene.js
index ab95198a..2d54c762 100644
--- a/src/agent-scene.js
+++ b/src/agent-scene.js
@@ -2063,8 +2063,10 @@ export function createAgentScene(input, inputLimits = {}, options = {}) {
}
function commit(stagedState, incrementRevision) {
- publishPose(stagedState, stagedState.initialPose);
- if (stagedState.topologyChanged) {
+ // A pose that swaps in new parts (particles, cloth) is a topology change too: a renderer holding the old parts would
+ // otherwise try to update parts it never saw, which failed with "Unknown part identity" (#2204).
+ const posedPartsChanged = publishPose(stagedState, stagedState.initialPose);
+ if (stagedState.topologyChanged || posedPartsChanged) {
topologyRevision++;
}
if (incrementRevision && state) {
diff --git a/tests/particle-batches.test.js b/tests/particle-batches.test.js
new file mode 100644
index 00000000..fae1cbcb
--- /dev/null
+++ b/tests/particle-batches.test.js
@@ -0,0 +1,45 @@
+import './support/load-agent-scene-modules.mjs';
+import test from 'node:test';
+import assert from 'node:assert/strict';
+import { createAgentScene } from '../src/agent-scene.js';
+
+function emptyRecipe() {
+ return {
+ format: 'crucible.scene', schemaVersion: 1,
+ geometries: [{ id: 'speck', type: 'box', bevel: 0 }],
+ materials: [{ id: 'glow', color: [1, 0.5, 0.1], emission: 4 }],
+ nodes: [],
+ };
+}
+
+function applyBatch(scene, operations) {
+ return scene.apply(operations, { expectedRevision: scene.revision });
+}
+
+test('a box emitter keeps no radius, and later batches still apply (#2203)', () => {
+ const scene = createAgentScene(emptyRecipe());
+ applyBatch(scene, [{ type: 'add-node', node: { id: 'cloud', geometry: 'speck', material: 'glow' } }]);
+ applyBatch(scene, [{ type: 'set-particles', particles: { systems: [{ node: 'cloud', emitter: { shape: 'box', halfExtents: [1, 2, 1] } }] } }]);
+ assert.equal('radius' in scene.exportRecipe().particles.systems[0].emitter, false, 'the stored box emitter has no radius');
+ applyBatch(scene, [{ type: 'set-particles', particles: { systems: [{ node: 'cloud', count: 5, emitter: { shape: 'box', halfExtents: [1, 2, 1] } }] } }]);
+ assert.equal(scene.inspect().particles.systems[0].particles, 5);
+});
+
+test('a stored box emitter holding the old radius default still loads (#2203)', () => {
+ const recipe = emptyRecipe();
+ recipe.nodes.push({ id: 'cloud', geometry: 'speck', material: 'glow' });
+ recipe.particles = { systems: [{ node: 'cloud', emitter: { shape: 'box', halfExtents: [1, 1, 1], radius: 0.5 } }] };
+ const scene = createAgentScene(recipe);
+ assert.equal('radius' in scene.exportRecipe().particles.systems[0].emitter, false);
+ assert.throws(() => createAgentScene({ ...recipe, particles: { systems: [{ node: 'cloud', emitter: { shape: 'box', radius: 2 } }] } }), /left out on a box/);
+});
+
+test('set-particles resolves a node added in an earlier batch (#2204)', () => {
+ const scene = createAgentScene(emptyRecipe());
+ applyBatch(scene, [{ type: 'add-node', node: { id: 'spray0', geometry: 'speck', material: 'glow' } }]);
+ const topologyBefore = scene.topologyRevision;
+ applyBatch(scene, [{ type: 'set-particles', particles: { systems: [{ node: 'spray0', seed: 11, count: 700, emitter: { shape: 'sphere', position: [-6.5, 0.4, 6], radius: 2.2 } }] } }]);
+ assert.equal(scene.inspect().particles.systems[0].particles, 700);
+ assert.ok(scene.topologyRevision > topologyBefore, 'the particle parts are new, so a renderer must replace its parts instead of updating them');
+ assert.ok(scene.parts.some(part => part.name === 'spray0' && part.geometry.positions.length > 0));
+});