3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
Refuse null for an optional modifier field (#2514)
Nguyen Xuan An0fdbf769be7d
diff --git a/changes/2514.md b/changes/2514.mdnew file mode 100644index 00000000..91045aae--- /dev/null+++ b/changes/2514.md@@ -0,0 +1 @@+- Mesh modifier edits now refuse null for an optional field (axis, cyclic, replace, falloff and the like) instead of silently using the default.diff --git a/src/mesh-build-modifier.js b/src/mesh-build-modifier.jsindex 38a8b9f2..e2199ae5 100644--- a/src/mesh-build-modifier.js+++ b/src/mesh-build-modifier.js@@ -2,7 +2,7 @@ import { seededRandom } from './mesh-fields.js';import { faceCentroids } from './mesh-normals.js';import { registerModifierNormalizer, registerModifierRunner } from './mesh-modifiers.js';import { MeshBuilder } from './mesh-stack.js';-import { editBoolean, editField } from './mesh-edit-field.js';+import { editBoolean, editField, editDefault } from './mesh-edit-field.js';// Build modifier, as Blender's Build: the faces of the mesh appear one by one over a run of frames. `frame` is the frame to// show, `start` and `length` give the run, and `order` says which face comes next: by index, by a seeded shuffle or along@@ -63,7 +63,7 @@ export function buildMesh(mesh, edit) {}function normalizeBuild(edit, normalized, index, { finiteNumber, integerInRange }) {- normalized.order = edit.order ?? 'index';+ normalized.order = editDefault(edit.order, 'index');if (!BUILD_ORDERS.includes(normalized.order)) {throw editField(index, 'order', edit.order, `one of ${BUILD_ORDERS.join(', ')}, such as "random" with a seed`);}@@ -77,7 +77,7 @@ function normalizeBuild(edit, normalized, index, { finiteNumber, integerInRangethrow editField(index, 'seed', edit.seed, 'left out, because only order "random" uses a seed');}if (normalized.order === 'axis') {- normalized.axis = edit.axis ?? 'y';+ normalized.axis = editDefault(edit.axis, 'y');if (!Object.hasOwn(AXIS_INDEX, normalized.axis)) throw editField(index, 'axis', edit.axis, 'one of "x", "y", "z"');} else if (edit.axis !== undefined) {throw editField(index, 'axis', edit.axis, 'left out, because only order "axis" uses an axis');diff --git a/src/mesh-deform-modifiers.js b/src/mesh-deform-modifiers.jsindex 664c7b28..6966628b 100644--- a/src/mesh-deform-modifiers.js+++ b/src/mesh-deform-modifiers.js@@ -1,7 +1,7 @@import { withPositions } from './mesh-field-edits.js';import { vertexNormals } from './mesh-normals.js';import { invalidField } from './field-error.js';-import { editField, editInteger, editBoolean, editAxis } from './mesh-edit-field.js';+import { editField, editInteger, editBoolean, editAxis, editDefault } from './mesh-edit-field.js';import { vertexGroupWeights } from './mesh-data.js';// Modifiers that only move vertices, each from a few numbers: lattice (a coarse grid of moved control points), curve (bend@@ -328,10 +328,10 @@ function checkedVector(value, index, label, finiteNumber) {}function normalizeLattice(edit, normalized, index, { finiteNumber }) {- const divisions = edit.divisions ?? [2, 2, 2];+ const divisions = editDefault(edit.divisions, [2, 2, 2]);if (!Array.isArray(divisions) || divisions.length !== 3) throw editField(index, 'divisions', divisions, `an array of three whole numbers from 2 to ${MAX_LATTICE_DIVISIONS}, such as [2, 2, 2]`);normalized.divisions = divisions.map(count => editInteger(count, index, 'divisions', 2, MAX_LATTICE_DIVISIONS));- const moves = edit.moves ?? [];+ const moves = editDefault(edit.moves, []);if (!Array.isArray(moves) || moves.length > MAX_LATTICE_MOVES) throw editField(index, 'moves', moves, `a list of at most ${MAX_LATTICE_MOVES} moves, such as [{ "point": [0, 0, 0], "offset": [0, 0, 1] }]`, RangeError);normalized.moves = moves.map((move, moveIndex) => {if (!move || typeof move !== 'object' || Array.isArray(move)) throw editField(index, `moves[${moveIndex}]`, move, 'an object such as { "point": [0, 0, 0], "offset": [0, 0, 1] }');@@ -346,16 +346,16 @@ function normalizeCurve(edit, normalized, index, { finiteNumber }) {throw editField(index, 'path', edit.path, `an array of 2 to ${MAX_CURVE_POINTS} points of three numbers, such as [[0, 0, 0], [0, 0, 2]]`);}normalized.path = edit.path.map(point => checkedVector(point, index, 'path', finiteNumber));- normalized.axis = edit.axis ?? 'x';+ normalized.axis = editDefault(edit.axis, 'x');editAxis(normalized.axis, index);}function normalizeHook(edit, normalized, index, { finiteNumber }) {- normalized.center = checkedVector(edit.center ?? [0, 0, 0], index, 'center', finiteNumber);+ normalized.center = checkedVector(editDefault(edit.center, [0, 0, 0]), index, 'center', finiteNumber);normalized.radius = edit.radius === undefined ? 1 : finiteNumber(edit.radius, index, 'radius', { minimum: 0, maximum: 1e4, exclusiveMinimum: true });- normalized.translation = checkedVector(edit.translation ?? [0, 0, 0], index, 'translation', finiteNumber);+ normalized.translation = checkedVector(editDefault(edit.translation, [0, 0, 0]), index, 'translation', finiteNumber);normalized.strength = edit.strength === undefined ? 1 : finiteNumber(edit.strength, index, 'strength', { minimum: 0, maximum: 1 });- normalized.falloff = edit.falloff ?? 'smooth';+ normalized.falloff = editDefault(edit.falloff, 'smooth');if (!HOOK_FALLOFFS.includes(normalized.falloff)) throw editField(index, 'falloff', normalized.falloff, `one of ${HOOK_FALLOFFS.join(', ')}`);}@@ -366,22 +366,22 @@ function normalizeWave(edit, normalized, index, { finiteNumber }) {normalized.speed = edit.speed === undefined ? 0.25 : finiteNumber(edit.speed, index, 'speed', { minimum: -1e4, maximum: 1e4 });normalized.time = edit.time === undefined ? 0 : finiteNumber(edit.time, index, 'time', { minimum: -1e6, maximum: 1e6 });normalized.timeOffset = edit.timeOffset === undefined ? 0 : finiteNumber(edit.timeOffset, index, 'timeOffset', { minimum: -1e6, maximum: 1e6 });- const start = edit.start ?? [0, 0];+ const start = editDefault(edit.start, [0, 0]);if (!Array.isArray(start) || start.length !== 2) throw editField(index, 'start', start, 'an array of two numbers, x and y, such as [0, 0]');normalized.start = start.map(value => finiteNumber(value, index, 'start'));- normalized.cyclic = edit.cyclic ?? true;- normalized.normal = edit.normal ?? false;+ normalized.cyclic = editDefault(edit.cyclic, true);+ normalized.normal = editDefault(edit.normal, false);editBoolean(normalized.cyclic, index, 'cyclic');editBoolean(normalized.normal, index, 'normal');- normalized.axes = edit.axes ?? 'xy';+ normalized.axes = editDefault(edit.axes, 'xy');if (!WAVE_AXES.includes(normalized.axes)) throw editField(index, 'axes', normalized.axes, `one of ${WAVE_AXES.join(', ')}`);}function normalizeSmoothLaplacian(edit, normalized, index, { finiteNumber }) {normalized.factor = edit.factor === undefined ? 0.5 : finiteNumber(edit.factor, index, 'factor', { minimum: 0, maximum: 1, exclusiveMinimum: true });normalized.iterations = edit.iterations === undefined ? 1 : editInteger(edit.iterations, index, 'iterations', 1, MAX_LAPLACIAN_ITERATIONS);- normalized.preserveVolume = edit.preserveVolume ?? true;- normalized.pinBoundary = edit.pinBoundary ?? true;+ normalized.preserveVolume = editDefault(edit.preserveVolume, true);+ normalized.pinBoundary = editDefault(edit.pinBoundary, true);editBoolean(normalized.preserveVolume, index, 'preserveVolume');editBoolean(normalized.pinBoundary, index, 'pinBoundary');if (edit.group === undefined) return;@@ -395,7 +395,7 @@ function normalizeWarp(edit, normalized, index, { finiteNumber }) {normalized.from = checkedVector(edit.from, index, 'from', finiteNumber);normalized.to = checkedVector(edit.to, index, 'to', finiteNumber);normalized.radius = edit.radius === undefined ? 1 : finiteNumber(edit.radius, index, 'radius', { minimum: 0, maximum: 1e4, exclusiveMinimum: true });- normalized.curve = edit.curve ?? 'smooth';+ normalized.curve = editDefault(edit.curve, 'smooth');if (!HOOK_FALLOFFS.includes(normalized.curve)) throw editField(index, 'curve', normalized.curve, `one of ${HOOK_FALLOFFS.join(', ')}`);normalized.twist = edit.twist === undefined ? 0 : finiteNumber(edit.twist, index, 'twist', { minimum: -3600, maximum: 3600 });const apart = Math.hypot(...normalized.to.map((value, axis) => value - normalized.from[axis])) > MINIMUM_SPAN;diff --git a/src/mesh-edit-field.js b/src/mesh-edit-field.jsindex d38dbf81..ca9db79a 100644--- a/src/mesh-edit-field.js+++ b/src/mesh-edit-field.js@@ -30,3 +30,8 @@ export function editRuleObject(value, index, label, hint) {if (!value || typeof value !== 'object' || Array.isArray(value)) throw editField(index, label, value, hint);return value;}++/** The field's value, or the fallback only when the field is left out; null is a wrong value that the check after this call rejects. */+export function editDefault(value, fallback) {+ return value === undefined ? fallback : value;+}diff --git a/src/mesh-generator-modifiers.js b/src/mesh-generator-modifiers.jsindex 2f75715d..5b7038fc 100644--- a/src/mesh-generator-modifiers.js+++ b/src/mesh-generator-modifiers.js@@ -2,7 +2,7 @@ import { createMeshData } from './mesh-data.js';import { vertexNormals, faceNormals } from './mesh-normals.js';import { evaluateField, meshFieldContext } from './mesh-fields.js';import { invalidField } from './field-error.js';-import { editInteger, editBoolean, editAxis } from './mesh-edit-field.js';+import { editInteger, editBoolean, editAxis, editDefault } from './mesh-edit-field.js';// Modifiers that build new surface from the mesh they are given: screw revolves its edges, wireframe turns its faces into// struts and skin puts a square tube on every edge. Each one checks the size of its result against the limits before it@@ -232,7 +232,7 @@ export function skinMesh(mesh, { radius }, limits) {export function normalizeGeneratorEdit(edit, normalized, index, { finiteNumber, checkedField }) {switch (edit.op) {case 'screw':- normalized.axis = edit.axis ?? 'z';+ normalized.axis = editDefault(edit.axis, 'z');editAxis(normalized.axis, index);normalized.angle = edit.angle === undefined ? 360 : finiteNumber(edit.angle, index, 'angle', { minimum: -3600, maximum: 3600 });normalized.steps = edit.steps === undefined ? 16 : editInteger(edit.steps, index, 'steps', 1, MAX_SCREW_STEPS);@@ -242,8 +242,8 @@ export function normalizeGeneratorEdit(edit, normalized, index, { finiteNumber,case 'wireframe':normalized.thickness = edit.thickness === undefined ? 0.02 : finiteNumber(edit.thickness, index, 'thickness', { minimum: 0, maximum: 1e3, exclusiveMinimum: true });normalized.offset = edit.offset === undefined ? 0 : finiteNumber(edit.offset, index, 'offset', { minimum: -1, maximum: 1 });- normalized.evenOffset = edit.evenOffset ?? true;- normalized.replace = edit.replace ?? true;+ normalized.evenOffset = editDefault(edit.evenOffset, true);+ normalized.replace = editDefault(edit.replace, true);editBoolean(normalized.evenOffset, index, 'evenOffset');editBoolean(normalized.replace, index, 'replace');break;diff --git a/src/mesh-modifiers.js b/src/mesh-modifiers.jsindex 2a0182fd..52df17b2 100644--- a/src/mesh-modifiers.js+++ b/src/mesh-modifiers.js@@ -2,7 +2,7 @@ import { withPositions } from './mesh-field-edits.js';import { mergeByDistance } from './mesh-ops.js';import { triangulateFaces, splitSharpEdges } from './mesh-restructure.js';import { MODIFIER_OPERATIONS } from './mesh-operations.js';-import { editField, editInteger, editAxis } from './mesh-edit-field.js';+import { editField, editInteger, editAxis, editDefault } from './mesh-edit-field.js';import { normalizeGeneratorEdit, GENERATOR_RUNNERS } from './mesh-generator-modifiers.js';import { normalizeNormalEdit, NORMAL_RUNNERS } from './mesh-normal-modifiers.js';import { isDeformModifier, normalizeDeformEdit, runDeformEdit } from './mesh-deform-modifiers.js';@@ -28,7 +28,7 @@ function normalizeSimpleDeform(edit, normalized, index, finiteNumber) {throw editField(index, 'mode', edit.mode, `one of ${SIMPLE_DEFORM_MODES.join(', ')}`);}normalized.mode = edit.mode;- normalized.axis = edit.axis ?? 'z';+ normalized.axis = editDefault(edit.axis, 'z');editAxis(normalized.axis, index);if (edit.mode === 'twist' || edit.mode === 'bend') {rejectField(edit, index, 'factor', 'angle in degrees, such as "angle": 45');diff --git a/tests/mesh-modifiers.test.js b/tests/mesh-modifiers.test.jsindex ba78a858..8601dd71 100644--- a/tests/mesh-modifiers.test.js+++ b/tests/mesh-modifiers.test.js@@ -194,3 +194,22 @@ test('modifiers run as stack nodes, reuse the cache and report their counts', ()assert.ok(outcome.results[0].triangulated > 0);assert.ok(outcome.results[0].after.faces > drum().faceCount);});++test('a null optional field is refused instead of silently taking its default', () => {+ const nullFields = [+ ['screw', 'axis'], ['wireframe', 'evenOffset'], ['wireframe', 'replace'], ['simple_deform', 'axis', { mode: 'twist' }],+ ['lattice', 'divisions'], ['lattice', 'moves'], ['curve', 'axis', { path: [[0, 0, 0], [0, 0, 2]] }], ['hook', 'center'],+ ['hook', 'translation'], ['hook', 'falloff'], ['wave', 'start'], ['wave', 'cyclic'], ['wave', 'normal'], ['wave', 'axes'],+ ['smooth_laplacian', 'preserveVolume'], ['smooth_laplacian', 'pinBoundary'], ['warp', 'curve', { from: [0, 0, 0], to: [1, 0, 0] }],+ ['build', 'order'], ['build', 'axis', { order: 'axis' }],+ ];+ for (const [op, field, rest = {}] of nullFields) {+ assert.throws(() => run(cube(), { op, ...rest, [field]: null }), error => error.message.startsWith(`edits[0].${field}: got null`), `${op}.${field}`);+ }+});++test('a modifier field that is left out still takes its default', () => {+ assert.equal(run(cube(), { op: 'wireframe' }).mesh.faceCount, 48);+ assert.equal(run(cube(), { op: 'wave' }).mesh.vertexCount, 8);+ assert.equal(run(cube(), { op: 'build', order: 'axis', frame: 60 }).mesh.vertexCount, 8);+});