Skip to content

3D engine that AI agents drive

Check cloth options and triangle indices, fix the path error example (#2339)

Nguyen Xuan Andaddbe4edc88
diff --git a/changes/2339.md b/changes/2339.md
new file mode 100644
index 00000000..51904e01
--- /dev/null
+++ b/changes/2339.md
@@ -0,0 +1 @@
+- Cloth refuses out-of-range damping, friction, thickness, stiffness, bend and density and a triangle that names a missing particle with an error that says which, and the scene_inspect path error shows a position the query accepts.
diff --git a/changes/2339.size.md b/changes/2339.size.md
new file mode 100644
index 00000000..550fefb6
--- /dev/null
+++ b/changes/2339.size.md
@@ -0,0 +1 @@
+- scene:agent-scene+lazy: 113 bytes gzip for range checks on cloth damping, friction, thickness, stiffness, bend and density and a check that every triangle index names a particle; damping -50 blew a bake up to -1.2e8 and a bad index was blamed on the wind.
diff --git a/src/agent-navigation.js b/src/agent-navigation.js
index 51d83a6a..2703049e 100644
--- a/src/agent-navigation.js
+++ b/src/agent-navigation.js
@@ -67,7 +67,7 @@ function requirePoint(value, label) {
/** The path option of scene_inspect: checks the query, resolves the ground and avoided node IDs through `nodeGeometry` and finds the route. */
export function resolvePathQuery(query, nodeGeometry, boundsFor) {
- if (query === null || typeof query !== 'object' || Array.isArray(query)) throw invalidField('path', query, 'an object such as { "ground": "floor", "from": [0, 0, 0], "to": [5, 0, 5] }', TypeError);
+ if (query === null || typeof query !== 'object' || Array.isArray(query)) throw invalidField('path', query, 'an object such as { "ground": "floor", "from": [0, 0], "to": [5, 5] }', TypeError);
for (const key of Object.keys(query)) {
if (!NAVIGATION_FIELDS.includes(key)) throw invalidField(`path.${key}`, query[key], `a known field, one of ${NAVIGATION_FIELDS.join(', ')}`, TypeError);
}
diff --git a/src/cloth.js b/src/cloth.js
index 0460e014..434f7f3b 100644
--- a/src/cloth.js
+++ b/src/cloth.js
@@ -460,6 +460,13 @@ function checkTimeStep(deltaSeconds) {
return deltaSeconds;
}
+function checkTriangles(triangles, particleCount) {
+ for (let corner = 0; corner < triangles.length; corner++) {
+ if (!(triangles[corner] >= 0 && triangles[corner] < particleCount)) throw new RangeError(`Cloth triangle ${Math.floor(corner / 3)} uses particle ${triangles[corner]}; the sheet has ${particleCount}.`);
+ }
+ if (triangles.length % 3) throw new RangeError('Cloth triangles must hold 3 indices each.');
+}
+
function checkUnit(value, field, maximum) {
if (!Number.isFinite(value) || value < 0 || value > maximum) {
throw new RangeError(`Cloth ${field} must be a number from 0 to ${maximum}; found ${value}.`);
@@ -568,6 +575,7 @@ export function createCloth(positions, triangles, options = {}) {
const particleCount = positions.length / 3;
const colliders = options.colliders ?? [];
const moving = options.moving ?? null;
+ checkTriangles(triangles, particleCount);
for (const collider of colliders) {
if (!Object.hasOwn(PUSH_OUT, collider?.kind ?? '')) throw new TypeError(`Cloth collider kind must be one of ${Object.keys(PUSH_OUT).join(', ')}.`);
}
@@ -579,7 +587,7 @@ export function createCloth(positions, triangles, options = {}) {
if (!BEND_MODELS.includes(bendModel)) {
throw new RangeError(`Cloth bendModel must be one of ${BEND_MODELS.join(', ')}; found ${bendModel}.`);
}
- const constraints = buildConstraints(positions, triangles, particleCount, options.bend ?? 0.1, options.stiffness ?? 0.9, stiffnessWeights, bendModel);
+ const constraints = buildConstraints(positions, triangles, particleCount, checkUnit(options.bend ?? 0.1, 'bend', 1), checkUnit(options.stiffness ?? 0.9, 'stiffness', 1), stiffnessWeights, bendModel);
const bendYield = options.bendYield === undefined ? null : checkUnit(options.bendYield, 'bendYield', Math.PI);
const bendSet = checkUnit(options.bendSet ?? DEFAULT_BEND_SET, 'bendSet', 1);
if (bendYield !== null && !constraints.dihedrals) {
@@ -587,7 +595,7 @@ export function createCloth(positions, triangles, options = {}) {
}
const extras = prepareExtras(options, positions, triangles, particleCount, constraints);
checkWork(particleCount, constraints.count, triangles.length / 3, colliders.length + (moving?.colliderCount ?? 0), options.frameCount ?? 1, extras.workPerIteration + (constraints.dihedrals?.count ?? 0) * DIHEDRAL_WORK);
- const density = options.density ?? 0.2;
+ const density = checkUnit(options.density ?? 0.2, 'density', 1000);
const areas = particleAreas(positions, triangles, particleCount);
const totalArea = areas.reduce((sum, area) => sum + area, 0);
const cloth = {
@@ -613,10 +621,10 @@ export function createCloth(positions, triangles, options = {}) {
velocities: new Float64Array(positions.length), windScratch: new Float64Array(positions.length),
};
const gravity = options.gravity ?? GRAVITY_DEFAULT;
- const damping = options.damping ?? 0.3;
- const friction = options.friction ?? 0.3;
+ const damping = checkUnit(options.damping ?? 0.3, 'damping', 100);
+ const friction = checkUnit(options.friction ?? 0.3, 'friction', 1);
const staticFriction = checkUnit(options.staticFriction ?? 0, 'staticFriction', 1);
- const thickness = options.thickness ?? 0.01;
+ const thickness = checkUnit(options.thickness ?? 0.01, 'thickness', 0.5);
const previous = new Float64Array(positions.length);
const contactNormals = extras.bounce > 0 ? new Float64Array(positions.length) : null;
const reflectBody = extras.bounce > 0 ? createBodyBounce(extras.bounce, particleCount) : null;
diff --git a/tests/agent-navigation-errors.test.js b/tests/agent-navigation-errors.test.js
new file mode 100644
index 00000000..e3f1d5f4
--- /dev/null
+++ b/tests/agent-navigation-errors.test.js
@@ -0,0 +1,18 @@
+import test from 'node:test';
+import assert from 'node:assert/strict';
+import { resolvePathQuery } from '../src/agent-navigation.js';
+
+const floor = { positions: new Float32Array([-10, 0, -10, 10, 0, -10, 10, 0, 10, -10, 0, 10]), indices: new Uint32Array([0, 2, 1, 0, 3, 2]) };
+const identity = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
+const nodeGeometry = () => ({ geometry: floor, worldMatrix: identity });
+
+test('the example in the path error is a query the path check accepts', () => {
+ let message = '';
+ try {
+ resolvePathQuery(null, nodeGeometry, () => null);
+ } catch (error) {
+ message = error.message;
+ }
+ const example = JSON.parse(message.match(/such as (\{.*\})\./)[1]);
+ assert.doesNotThrow(() => resolvePathQuery(example, nodeGeometry, () => null));
+});
diff --git a/tests/cloth-input-checks.test.js b/tests/cloth-input-checks.test.js
new file mode 100644
index 00000000..deee8032
--- /dev/null
+++ b/tests/cloth-input-checks.test.js
@@ -0,0 +1,32 @@
+import test from 'node:test';
+import assert from 'node:assert/strict';
+import { bakeCloth, createCloth } from '../src/cloth.js';
+
+function smallSheet() {
+ const positions = Float32Array.from([0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1]);
+ const triangles = Uint32Array.from([0, 2, 1, 1, 2, 3]);
+ return { positions, triangles };
+}
+
+test('createCloth refuses out-of-range numeric options and names the option', () => {
+ const { positions, triangles } = smallSheet();
+ const bad = [
+ ['damping', -50], ['damping', Number.NaN], ['friction', Number.NaN], ['friction', 2], ['thickness', Number.NaN], ['thickness', -1],
+ ['stiffness', Number.NaN], ['stiffness', 2], ['bend', -1], ['density', -1],
+ ];
+ for (const [option, value] of bad) {
+ assert.throws(() => createCloth(positions, triangles, { [option]: value }), new RegExp(`Cloth ${option} must be`), `${option} ${value}`);
+ }
+});
+
+test('createCloth still takes the recipe ranges', () => {
+ const { positions, triangles } = smallSheet();
+ const options = { damping: 100, friction: 1, thickness: 0.5, stiffness: 1, bend: 0, density: 10 };
+ assert.doesNotThrow(() => bakeCloth(positions, triangles, { ...options, frameCount: 3 }));
+});
+
+test('createCloth names a triangle index that is not a particle instead of blaming the wind', () => {
+ const { positions } = smallSheet();
+ assert.throws(() => createCloth(positions, Uint32Array.from([0, 1, 999])), /triangle 0 uses particle 999; the sheet has 4/);
+ assert.throws(() => createCloth(positions, Uint32Array.from([0, 1, 2, 3])), /3 indices each/);
+});