3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
Refuse non-finite vertex updates and name the failing field in mesh errors (#2531)
Nguyen Xuan An8db192ddabcb
diff --git a/changes/2531.md b/changes/2531.mdnew file mode 100644index 00000000..75cb5ba5--- /dev/null+++ b/changes/2531.md@@ -0,0 +1 @@+- Refuse non-finite positions in updatePartVertices, name the shape key in its edit errors and say so when an edit leaves no faces (#2531, #2532, #2533).diff --git a/src/mesh-polygons.js b/src/mesh-polygons.jsindex f9e47c14..c64cea21 100644--- a/src/mesh-polygons.js+++ b/src/mesh-polygons.js@@ -26,6 +26,7 @@ export function facePolygons(mesh) {* vertex copies the values of `extraSources[index]` (a source vertex number) when that is given.*/export function buildPolygonMesh(mesh, polygons, extraPositions = [], extraSources = []) {+ if (polygons.length === 0) throw new RangeError('The edit left no faces. Select fewer faces or edges.');const used = new Set();for (const polygon of polygons) {for (const vertex of polygon.vertices) used.add(vertex);diff --git a/src/mesh-shape-keys.js b/src/mesh-shape-keys.jsindex 11add362..e98f26ae 100644--- a/src/mesh-shape-keys.js+++ b/src/mesh-shape-keys.js@@ -47,7 +47,11 @@ export function normalizeShapeKeys(shapeKeys) {if (typeof value !== 'number' || !(value >= 0 && value <= 1)) throw keyError(index, 'value is the starting weight, from 0 to 1');const edits = key.edits ?? (Array.isArray(key.offsets) ? key.offsets.map(entry => offsetEdit(entry, index)) : null);if (!edits) throw keyError(index, 'offsets must be an array');- normalizeMeshEdits(edits);+ try {+ normalizeMeshEdits(edits);+ } catch (error) {+ throw keyError(index, `${key.name}: ${error.message}`);+ }return { name: key.name, value, edits };});}diff --git a/src/update-part-vertices.js b/src/update-part-vertices.jsindex 07bfad8b..f7fe02c7 100644--- a/src/update-part-vertices.js+++ b/src/update-part-vertices.js@@ -8,6 +8,9 @@ function checkVertices(name, values, expectedLength, label) {if (!(values instanceof Float32Array) || values.length !== expectedLength) {throw new TypeError(`updatePartVertices ${name} must be a Float32Array of ${expectedLength} numbers (3 per vertex, the vertex count of ${label}).`);}+ for (let index = 0; index < values.length; index++) {+ if (!Number.isFinite(values[index])) throw new RangeError(`updatePartVertices ${name}[${index}] is ${values[index]}; pass finite numbers.`);+ }}// The sphere around the box of the new positions: culling and shadow fitting read it, so a sheet that swings out of itsdiff --git a/tests/mesh-polygons.test.js b/tests/mesh-polygons.test.jsnew file mode 100644index 00000000..ffad0827--- /dev/null+++ b/tests/mesh-polygons.test.js@@ -0,0 +1,14 @@+import assert from 'node:assert/strict';+import test from 'node:test';+import { createMeshData } from '../src/mesh-data.js';+import { buildPolygonMesh } from '../src/mesh-polygons.js';++const quad = createMeshData({+ positions: new Float32Array([0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0]),+ faceOffsets: new Uint32Array([0, 4]),+ cornerVertices: new Uint32Array([0, 1, 2, 3]),+});++test('buildPolygonMesh says the edit left no faces instead of blaming the positions', () => {+ assert.throws(() => buildPolygonMesh(quad, []), /^RangeError: The edit left no faces/);+});diff --git a/tests/mesh-shape-keys.test.js b/tests/mesh-shape-keys.test.jsindex 83cec356..83aadb89 100644--- a/tests/mesh-shape-keys.test.js+++ b/tests/mesh-shape-keys.test.js@@ -113,3 +113,12 @@ test('the starting value of a key is the weight without motion, and a GLB exportassert.deepEqual(document.meshes[0].extras.targetNames, ['lift', 'widen']);assert.deepEqual(document.nodes.find(node => node.mesh === 0).weights, [0.75, 0]);});++test('an error inside a shape key names the key', async () => {+ const { normalizeShapeKeys } = await import('../src/mesh-shape-keys.js');+ const keys = [+ { name: 'ok', offsets: [{ offset: [0, 1, 0] }] },+ { name: 'blink', offsets: [{ offset: [Number.NaN, 1, 0] }] },+ ];+ assert.throws(() => normalizeShapeKeys(keys), /^TypeError: Shape key 1 \(blink: edits\[0\]\.offset/);+});diff --git a/tests/update-part-vertices.test.js b/tests/update-part-vertices.test.jsindex 6d87487e..003d606f 100644--- a/tests/update-part-vertices.test.js+++ b/tests/update-part-vertices.test.js@@ -71,3 +71,18 @@ test('worldBounds follows vertices moved by updatePartVertices after bounds wereassert.equal(after.minimum[2], 50, 'bounds must not stay stale after an in-place vertex rewrite');assert.equal(after.maximum[2], 50);});++test('updatePartVertices refuses non-finite positions and normals before it writes anything', () => {+ for (const bad of [NaN, Infinity, -Infinity]) {+ const { renderer, part, geometry, batch, calls } = fakeRenderer();+ const positionsBefore = Array.from(geometry.positions);+ const boundsBefore = batch.bounds;+ const badPositions = new Float32Array([0, 0, 0, 1, bad, 0, 0, 1, 0]);+ assert.throws(() => updatePartVertices(renderer, part, badPositions), /positions\[4\] is .*pass finite numbers/);+ const badNormals = new Float32Array([0, 0, 1, 0, 0, bad, 0, 0, 1]);+ assert.throws(() => updatePartVertices(renderer, part, geometry.positions.slice(), badNormals), /normals\[5\] is .*pass finite numbers/);+ assert.deepEqual(Array.from(geometry.positions), positionsBefore, 'the shared geometry is untouched');+ assert.equal(batch.bounds, boundsBefore, 'the culling bounds are untouched');+ assert.deepEqual(calls, [], 'no buffer was written');+ }+});