3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
Honour subdivisions on a sharp mesh box base (#2201)
Nguyen Xuan An9aedd032416f
diff --git a/changes/2201.md b/changes/2201.mdnew file mode 100644index 00000000..e8c33986--- /dev/null+++ b/changes/2201.md@@ -0,0 +1 @@+- A mesh `box` base with `bevel` 0 now honours `subdivisions`: each face becomes an n by n grid welded at the edges, instead of 8 vertices (#2201).diff --git a/changes/2201.size.md b/changes/2201.size.mdnew file mode 100644index 00000000..285a2522--- /dev/null+++ b/changes/2201.size.md@@ -0,0 +1,2 @@+- scene:agent-scene: 79 bytes gzip so a documented box field (subdivisions) is honoured on a mesh base instead of silently giving 8 vertices.+- scene:agent-scene+lazy: the same 79 bytes.diff --git a/docs/mesh.md b/docs/mesh.mdindex 6d95af91..79974f08 100644--- a/docs/mesh.md+++ b/docs/mesh.md@@ -512,7 +512,7 @@ faces, so a tag confines them. Neither adds or removes faces, and the topology sis an error (`onMiss` "error") or stays put (`keep`). The queries use the BVH of `src/mesh-query.js`, at most 20million triangle tests per edit.-A mesh base is a primitive or profile record, or a `grid` or `plane` sheet (`segments` 1 to 64). A decal is a grid base shrunk onto a curved target and then solidified:+A mesh base is a primitive or profile record, or a `grid` or `plane` sheet (`segments` 1 to 64). A `box` base with `bevel` 0 and `subdivisions` (1 to 128) splits each face into an n by n grid, welded across the edges (6 n^2 + 2 vertices), so `set_position`, `displace` and `smoothAngle` have vertices to work on; without `subdivisions` it stays 8 vertices. A decal is a grid base shrunk onto a curved target and then solidified:```json{ "id": "badge", "type": "mesh", "base": { "type": "grid", "segments": 16 }, "edits": [diff --git a/src/agent-scene.js b/src/agent-scene.jsindex ab95198a..df90e4e0 100644--- a/src/agent-scene.js+++ b/src/agent-scene.js@@ -573,6 +573,11 @@ function lineEnds(value) {return ends;}+// A sharp box under a mesh keeps its subdivisions as a grid on each face, welded by the mesh, so edits have vertices to move (#2201).+function meshBaseSplitsBox(base) {+ return base.type === 'box' && base.bevel === 0 && base.subdivisions !== undefined && base.subdivisions !== null;+}+function geometryEstimate(value, scope = PENDING_CUTTER_SCOPE) {if (!Object.hasOwn(GEOMETRY_FIELDS, value.type)) {throw invalidField('type', value.type, `one of ${Object.keys(GEOMETRY_FIELDS).join(', ')}`, TypeError);@@ -684,7 +689,7 @@ function geometryEstimate(value, scope = PENDING_CUTTER_SCOPE) {if (!base || typeof base !== 'object' || Array.isArray(base) || base.type === 'mesh' || Object.hasOwn(base, 'id')) {throw invalidField('base', base, 'a primitive or profile record without an id, such as { "type": "box" } (box, cylinder, sphere, torus, lathe, extrude, tube, grid or plane)', TypeError);}- let estimate = geometryEstimate({ id: 'base', ...base });+ let estimate = meshBaseSplitsBox(base) ? 6 * (integer(base.subdivisions, 1, 128, 'subdivisions') + 1) ** 2 : geometryEstimate({ id: 'base', ...base });const cutterScope = meshCutterScope(value, scope);let referencedWork = 0;for (const edit of agentSceneModule('mesh').normalizeMeshEdits(value.edits ?? [])) {@@ -874,7 +879,7 @@ function generateGeometry(value, maxVertices, scope) {case 'mesh': {const cutterScope = meshCutterScope(value, scope);const dependencies = scope ? checkedCutterDependencies(value, scope.definitions) : '';- return agentSceneModule('mesh').generateMeshGeometry(() => generateGeometry(value.base, maxVertices), value.edits ?? [], {+ return agentSceneModule('mesh').generateMeshGeometry(() => meshBaseSplitsBox(value.base) ? box(0, value.base.subdivisions, true) : generateGeometry(value.base, maxVertices), value.edits ?? [], {maxVertices, smoothAngle: value.smoothAngle,stack: value.stack ?? [], shapeKeys: value.shapeKeys ?? [],// The edits only name a cutter geometry, so that geometry's own record joins the key: editing it changes the key.diff --git a/src/geometry.js b/src/geometry.jsindex 165b4520..2f316ec4 100644--- a/src/geometry.js+++ b/src/geometry.js@@ -314,21 +314,23 @@ export function cylinder(segments = 80, bevel = 0.025, bevelSegments = 6) {return geometry(positions, normals, indices);}-export function box(bevel = 0.08, subdivisions = 6) {+/** `splitFlatFaces` cuts a sharp box (bevel 0) into an n by n grid per face, so a mesh built on it has vertices to edit (#2201). */+export function box(bevel = 0.08, subdivisions = 6, splitFlatFaces = false) {finiteFloat(bevel, 'bevel');// A bevel below about 3e-17 leaves the core at exactly 0.5, so every face normal would divide by zero; 1e-16 is the round floor.if (bevel < 0 || bevel > 0.5 || bevel > 0 && bevel < 1e-16) {throw new RangeError('Box bevel must be 0 or from 1e-16 to 0.5: a smaller bevel underflows and gives NaN normals.');}integer(subdivisions, 1, 128, 'subdivisions');- primitiveBudget(bevel === 0 ? 24 : 6 * (2 * subdivisions + 2) ** 2);+ primitiveBudget(bevel === 0 ? (splitFlatFaces ? 6 * (subdivisions + 1) ** 2 : 24) : 6 * (2 * subdivisions + 2) ** 2);const positions = [];const normals = [];const indices = [];const coreExtent = 0.5 - bevel;const coordinates = [];if (bevel === 0) {- coordinates.push(-0.5, 0.5);+ const spans = splitFlatFaces ? subdivisions : 1;+ for (let index = 0; index <= spans; index++) coordinates.push(index / spans - 0.5);} else {// Sample the curved border in angular steps, leaving the planar center as one span.// Uniform grids miss narrow fillets and interpolate across the whole face instead.diff --git a/tests/mesh-box-subdivisions.test.js b/tests/mesh-box-subdivisions.test.jsnew file mode 100644index 00000000..dcbd9e9d--- /dev/null+++ b/tests/mesh-box-subdivisions.test.js@@ -0,0 +1,48 @@+import './support/load-agent-scene-modules.mjs';+import assert from 'node:assert/strict';+import test from 'node:test';+import { createAgentScene } from '../src/agent-scene.js';++// A sharp mesh box base keeps `subdivisions` as an n by n grid on each face, welded across the edges (#2201).+function boxRecipe(base, edits = []) {+ return {+ format: 'crucible.scene', schemaVersion: 1,+ geometries: [{ id: 'stack', type: 'mesh', base, edits }],+ materials: [{ id: 'rock', color: [0.5, 0.5, 0.5] }],+ nodes: [{ id: 'stack', geometry: 'stack', material: 'rock' }],+ };+}++function report(base, edits) {+ return createAgentScene(boxRecipe(base, edits)).inspectMeshes().meshes[0];+}++test('a sharp mesh box splits each face by its subdivisions and welds the edges', () => {+ const expectedVertices = subdivisions => 6 * subdivisions ** 2 + 2;+ for (const subdivisions of [1, 2, 64]) {+ const result = report({ type: 'box', bevel: 0, subdivisions });+ assert.equal(result.counts.vertices, expectedVertices(subdivisions), `subdivisions ${subdivisions}`);+ assert.equal(result.counts.faces, 6 * subdivisions ** 2);+ }+ assert.equal(report({ type: 'box', bevel: 0 }).counts.vertices, 8, 'no subdivisions keeps the plain box');+});++test('the same box recipe builds identical meshes', () => {+ const base = { type: 'box', bevel: 0, subdivisions: 16 };+ const edits = [{ op: 'displace', field: 1, strength: 0.05 }];+ assert.equal(JSON.stringify(report(base, edits)), JSON.stringify(report(base, edits)));+});++test('edits move the extra vertices and smoothAngle still applies', () => {+ const base = { type: 'box', bevel: 0, subdivisions: 8 };+ const flat = report(base);+ const displaced = report(base, [{ op: 'displace', field: 1, strength: 0.2, midlevel: 0 }]);+ assert.equal(displaced.counts.vertices, flat.counts.vertices);+ assert.notDeepEqual(displaced.bounds ?? displaced, flat.bounds ?? flat);+ const scene = createAgentScene({ ...boxRecipe(base), geometries: [{ id: 'stack', type: 'mesh', base, smoothAngle: 50 }] });+ assert.equal(scene.inspectMeshes().meshes[0].ok, true);+});++test('subdivisions out of range on a sharp mesh box names the accepted range', () => {+ assert.throws(() => report({ type: 'box', bevel: 0, subdivisions: 129 }), /subdivisions/);+});