3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
Refuse sdf bounds past Float32 range and sparse modeling arrays (#2484)
Nguyen Xuan An3c1066be23aa
diff --git a/changes/2484.md b/changes/2484.mdnew file mode 100644index 00000000..3ddb8d09--- /dev/null+++ b/changes/2484.md@@ -0,0 +1 @@+- sdf geometry refuses sizes, positions, scale, offsets and blends beyond Float32 range instead of returning NaN positions.diff --git a/changes/2484.size.md b/changes/2484.size.mdnew file mode 100644index 00000000..7ce3a5f4--- /dev/null+++ b/changes/2484.size.md@@ -0,0 +1 @@+- scene:agent-scene+lazy: 75 bytes gzip for refusing sdf bounds that give NaN positions and for reading sparse arrays as the missing values they are, so agents get an error naming the field.diff --git a/changes/2485.md b/changes/2485.mdnew file mode 100644index 00000000..2b41daca--- /dev/null+++ b/changes/2485.md@@ -0,0 +1 @@+- Sparse arrays in sdf vectors and children, curve points, handles, taper and profile, and tube or extrude point lists are refused with an error that names the field.diff --git a/src/modeling-sdf.js b/src/modeling-sdf.jsindex f3d08628..7120e1ef 100644--- a/src/modeling-sdf.js+++ b/src/modeling-sdf.js@@ -30,7 +30,7 @@ function positiveNumber(node, field, fallback, path) {function vectorOf(value, fallback, path, { positive = false } = {}) {const vector = value ?? fallback;- const valid = Array.isArray(vector) && vector.length === 3 && vector.every(component => Number.isFinite(component) && (!positive || component > 0));+ const valid = Array.isArray(vector) && vector.length === 3 && [...vector].every(component => Number.isFinite(component) && (!positive || component > 0));if (!valid) throw fail(path, vector, positive ? 'three positive numbers, such as [1, 1, 1]' : 'three numbers, such as [0, 0, 0]');return vector;}@@ -202,7 +202,7 @@ function compileNode(node, path, depth, counter) {if (!Array.isArray(children) || children.length < 2 || children.length > SDF_LIMITS.maxChildren) {throw fail(`${path}.children`, Array.isArray(children) ? `${children.length} children` : children, `2 to ${SDF_LIMITS.maxChildren} nodes`);}- const compiled = children.map((child, index) => compileNode(child, `${path}.children[${index}]`, depth + 1, counter));+ const compiled = Array.from(children, (child, index) => compileNode(child, `${path}.children[${index}]`, depth + 1, counter));const join = combine(node.op, smooth);const distance = (x, y, z) => {let result = compiled[0].distance(x, y, z);@@ -232,8 +232,15 @@ export function planSdf(value) {return planDistanceField(compileNode(value.root, 'sdf.root', 0, { count: 0 }), resolution);}+// Bounds past Float32 range or NaN make positions NaN, so the grid is refused instead.+function checkReach({ min, max }) {+ const reach = Math.max(...[...min, ...max].map(Math.abs));+ if (!(reach <= 1e38)) throw fail('sdf.root bounds', reach, 'values that keep the shape within 1e38');+}+/** The sampling grid of a compiled field { distance, bounds }: resolution cells along its longest side, plus padding. */export function planDistanceField(root, resolution) {+ checkReach(root.bounds);const extent = root.bounds.max.map((high, axis) => high - root.bounds.min[axis]);const edge = Math.max(...extent) / resolution;const points = extent.map(length => Math.ceil(length / edge - 1e-9) + 1 + PADDING_VOXELS * 2);diff --git a/src/modeling.js b/src/modeling.jsindex 971641ae..e68176bc 100644--- a/src/modeling.js+++ b/src/modeling.js@@ -20,7 +20,7 @@ function integer(value, minimum, maximum, name) {function pointsFrom(input, size, minimum, name) {if (!Array.isArray(input) || input.length < minimum || input.length > MAX_INPUT_POINTS) throw new RangeError(`${name} needs ${minimum} to ${MAX_INPUT_POINTS} points.`);- const points = input.map(point => {+ const points = Array.from(input, point => {if ((!Array.isArray(point) && !ArrayBuffer.isView(point)) || point.length !== size) throw new TypeError(`${name} points need ${size} components.`);return Array.from(point, value => finite(value, name));});@@ -36,7 +36,7 @@ function pointsFrom(input, size, minimum, name) {function vectorFrom(input, name) {if (!Array.isArray(input) || input.length !== 3) throw new TypeError(`${name} needs three components.`);- return input.map(value => finite(value, name));+ return Array.from(input, value => finite(value, name));}function knotFrom(input, index) {@@ -55,7 +55,7 @@ function knotFrom(input, index) {function taperFrom(taper) {if (taper === undefined) return null;if (!Array.isArray(taper) || taper.length < 2 || taper.length > 64) throw new RangeError('taper needs 2 to 64 radius factors.');- return taper.map(factor => {+ return Array.from(taper, factor => {finite(factor, 'taper');if (factor < 0) throw new RangeError('taper factors cannot be negative.');return factor;@@ -387,7 +387,7 @@ function splineFrom(input, label) {for (const key of ['weights', 'order', 'knots', 'knotMode']) {if (kind !== 'nurbs' && input[key] !== undefined) throw new TypeError(`${label}.${key} is not allowed here: weights apply to kind nurbs only, as do order, knots and knotMode.`);}- const resolved = kind === 'bezier' ? points.map(knotFrom) : points.map(point => vectorFrom(point, 'point'));+ const resolved = kind === 'bezier' ? Array.from(points, knotFrom) : Array.from(points, point => vectorFrom(point, 'point'));const weights = kind === 'nurbs' ? weightsFrom(input.weights, points.length, `${label}.weights`) : undefined;const basis = kind === 'nurbs' ? nurbsBasisFrom(input, points.length, closed, label) : undefined;return { type: kind, closed, points: resolved, weights, basis, trim: trimFrom(input.trim, `${label}.trim`) };diff --git a/tests/modeling-sdf.test.js b/tests/modeling-sdf.test.jsindex 97224e22..9ad3a33f 100644--- a/tests/modeling-sdf.test.js+++ b/tests/modeling-sdf.test.js@@ -219,3 +219,24 @@ test('a recipe sdf geometry becomes a part and respects the vertex budget', () =assert.throws(() => recipe({ root: CHARACTER, resolution: 128 }, { maxVertices: 1000 }), /vertices|maxVertices/);assert.throws(() => recipe({ root: { shape: 'cone' } }), /sdf\.root\.shape/);});++test('sdf refuses sizes, positions, scale, offsets and blends beyond Float32 range instead of returning NaN', () => {+ const sphere = { shape: 'sphere' };+ const roots = {+ radius: { shape: 'sphere', radius: 1e200 },+ position: { shape: 'sphere', position: [1e300, 0, 0] },+ scale: { shape: 'sphere', scale: 1e300 },+ offset: { op: 'offset', distance: 1e300, child: sphere },+ smooth: { op: 'union', smooth: 1e300, children: [sphere, { shape: 'sphere', position: [1, 0, 0] }] },+ };+ for (const [name, root] of Object.entries(roots)) {+ assert.throws(() => sdfGeometry({ resolution: 16, root }), /sdf\.root bounds is .*within 1e38/, name);+ }+});++test('sdf names a sparse vector or child instead of reporting an empty surface', () => {+ assert.throws(() => sdfGeometry({ resolution: 16, root: { shape: 'box', size: [1, , 1] } }), /sdf\.root\.size is/);+ assert.throws(() => sdfGeometry({ resolution: 16, root: { shape: 'sphere', position: [0, , 0] } }), /sdf\.root\.position is/);+ const children = [{ shape: 'sphere' }, , { shape: 'sphere' }];+ assert.throws(() => sdfGeometry({ resolution: 16, root: { op: 'union', children } }), /sdf\.root\.children\[1\] is/);+});diff --git a/tests/modeling-sparse-input.test.js b/tests/modeling-sparse-input.test.jsnew file mode 100644index 00000000..96d8d0a0--- /dev/null+++ b/tests/modeling-sparse-input.test.js@@ -0,0 +1,17 @@+import assert from 'node:assert/strict';+import test from 'node:test';+import { curve, extrude, tube } from '../src/modeling.js';++const path = [[0, 0, 0], [1, 0, 0], [1, 1, 0]];++test('curve names a hole in taper, points, handles and profile instead of failing later', () => {+ assert.throws(() => curve(path, { taper: [1, , 1] }), /taper must be a finite float/);+ assert.throws(() => curve([[0, 0, 0], [1, , 0], [2, 0, 0]]), /point must be a finite float/);+ assert.throws(() => curve([{ position: [0, 0, 0], handleOut: [1, , 0] }, { position: [1, 0, 0] }]), /handleOut must be a finite float/);+ assert.throws(() => curve(path, { profile: [[0, 0], , [1, 0], [0, 1]] }), /profile points need 2 components/);+});++test('a hole in a point list is refused by tube and extrude', () => {+ assert.throws(() => tube([[0, 0, 0], , [1, 0, 0]]), /path points need 3 components/);+ assert.throws(() => extrude([[0, 0], [1, 0], , [0, 1]]), /contour points need 2 components/);+});