3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
Refuse bad dense LOD options and key vegetation exclusions by their real position (#2552)
Nguyen Xuan An433008241c03
diff --git a/changes/2552.md b/changes/2552.mdnew file mode 100644index 00000000..f835dcc4--- /dev/null+++ b/changes/2552.md@@ -0,0 +1 @@+- Scene file density options refuse a pixel ratio outside 0.1 to 16 and NaN triangle thresholds by name, and a vegetation scatter that excludes around a node regrows when that node's parent or the ground moves (#2552, #2553).diff --git a/docs/culling-lod.md b/docs/culling-lod.mdindex d98aead6..471851e0 100644--- a/docs/culling-lod.md+++ b/docs/culling-lod.md@@ -60,7 +60,7 @@ A scan with about one triangle per pixel or more is shaded once per sample at ne`addLodsToDenseParts(parts, { pixels })` from `src/dense-lods.js` gives levels to the parts of a scene that has more than one triangle per pixel (`pixels` is the canvas width times its height times the pixel ratio squared). A part gets levels when it has at least `minimumTriangles` (default 4000, so a scene of many meshes with a few thousand triangles each gets them too, #2144, #2157) triangles and can take them; parts with the same geometry share one set. It returns `parts` itself for a scene under the density. It is not applied by itself, and it takes seconds on a million triangles, so call it once while loading. On the Monster scene (1.6 million triangles, 1280 by 720, 4 samples) called with the canvas size, the frame went from 5.8 to 1.9 ms, and Lone Monk (5.6 million) from 15.4 to 9.5 ms (measured before the error metric was changed to a distance); the picture changes slightly (root mean square 2.0 of 255), because far parts draw from coarser levels (#1163).-A scene file (`scene.json`, rendered by `scripts/render-media.mjs --scene file`) gets the same levels by default; `"renderer": { "denseLods": false }` keeps every triangle of a dense scan. A scene file also picks levels at `lodPixelError` 0.5, not the renderer's 1, so far detail shimmers less; `"renderer": { "lodPixelError": 1 }` restores the old choice (#2157). With the option on, `partsForRendererOptions` in `src/dense-lods.js` calls `addLodsToDenseParts` at the canvas size and pixel ratio before the renderer is built (#1163).+A scene file (`scene.json`, rendered by `scripts/render-media.mjs --scene file`) gets the same levels by default; `"renderer": { "denseLods": false }` keeps every triangle of a dense scan. A scene file also picks levels at `lodPixelError` 0.5, not the renderer's 1, so far detail shimmers less; `"renderer": { "lodPixelError": 1 }` restores the old choice (#2157). With the option on, `partsForRendererOptions` in `src/dense-lods.js` calls `addLodsToDenseParts` at the canvas size and pixel ratio before the renderer is built (#1163). A `pixelRatio` outside 0.1 to 16, and a `minimumTriangles` or `minimumTrianglesPerPixel` that is not a number from 0 to 1e9, throws a RangeError that names it (#2552).On Lone Monk seen from the courtyard (a camera that shows the scene, 1280 by 720, 4 samples), drawing from the levels of parts of 4000 triangles and up, not 20000 and up, sent 1.06 million triangles instead of 1.80 million and cut the GPU frame from 4.36 to 3.38 ms (1500 and up: 3.51 to 3.62 ms, 8000 and up: 3.30 ms) (no levels at all: 3.26 million triangles, 7.12 ms). The picture differs from the 20000 floor by a root mean square of 1.74 of 255 (2.51 from no levels). `node bench/glaze-passes/run.mjs <out.json> <scene directories> whole '{"denseLods":true,"denseLodsMinimumTriangles":4000}'` measures it; `'{"engine":"three"}'` draws the same file with three.js, and `bench/glaze-passes/still-pair.mjs` writes the picture pair and the RMSE (#2144).diff --git a/src/agent-vegetation.js b/src/agent-vegetation.jsindex 6f6965a5..ac9f978d 100644--- a/src/agent-vegetation.js+++ b/src/agent-vegetation.js@@ -261,10 +261,16 @@ function grow(recipe, vegetation) {return { variants: Object.values(scattered.variants).flat(), instances: scattered.instances };}+// Where a named exclusion stands in the ground's space, or null while the node or the ground is missing (growing reports that).+function namedPlanePosition(recipe, scatter, nodeId) {+ const hasNode = id => recipe.nodes.some(node => node.id === id);+ return hasNode(nodeId) && hasNode(scatter.ground) ? planePositionInGround(recipe, scatter, nodeId) : null;+}+function cacheKey(recipe, node) {const { vegetation } = node;const ground = vegetation.scatter ? recipe.geometries.find(geometry => geometry.id === recipe.nodes.find(candidate => candidate.id === vegetation.scatter.ground)?.geometry) : null;- const named = vegetation.scatter?.exclude.filter(entry => entry.node).map(entry => recipe.nodes.find(candidate => candidate.id === entry.node)?.translation);+ const named = vegetation.scatter?.exclude.filter(entry => entry.node).map(entry => namedPlanePosition(recipe, vegetation.scatter, entry.node));const { season, cullDistance, distanceScale, ...growth } = vegetation;return JSON.stringify([growth, ground, named]);}diff --git a/src/dense-lods.js b/src/dense-lods.jsindex 3005f2a3..8e29d81e 100644--- a/src/dense-lods.js+++ b/src/dense-lods.js@@ -1,3 +1,4 @@+import { requireFiniteInRange } from './modeling-limits.js';import { generateGeometryLods, checkRatios, DEFAULT_LOD_RATIOS } from './mesh-lod.js';import { MAXIMUM_SIMPLIFY_TRIANGLES } from './geometry-simplify.js';import { DENSE_TRIANGLES_PER_PIXEL, partTakesLods } from './renderer.js';@@ -34,6 +35,8 @@ export function addLodsToDenseParts(parts, { pixels, minimumTrianglesPerPixel =if (!Array.isArray(parts)) throw new TypeError('parts must be the array of parts that goes to CrucibleRenderer.');// Checked here so bad ratios fail on a sparse scene too, not only on the frame a scene turns dense.checkRatios(ratios);+ requireFiniteInRange('addLodsToDenseParts', 'minimumTrianglesPerPixel', minimumTrianglesPerPixel, 0, 1e9);+ requireFiniteInRange('addLodsToDenseParts', 'minimumTriangles', minimumTriangles, 0, 1e9);if (trianglesPerPixel(parts, pixels) <= minimumTrianglesPerPixel) return parts;const levelsByGeometry = new Map();return parts.map(part => {@@ -53,6 +56,7 @@ export function partsForRendererOptions(parts, canvas, rendererOptions = {}) {const { denseLods = true, pixelRatio = 1.5 } = rendererOptions;if (typeof denseLods !== 'boolean') throw new TypeError('renderer.denseLods must be true or false.');if (!denseLods) return parts;+ requireFiniteInRange('partsForRendererOptions', 'pixelRatio', pixelRatio, 0.1, 16);const pixels = Math.max(1, canvas.clientWidth) * Math.max(1, canvas.clientHeight) * pixelRatio * pixelRatio;return addLodsToDenseParts(parts, { pixels });}diff --git a/tests/agent-scene-vegetation.test.js b/tests/agent-scene-vegetation.test.jsindex 62ffc245..a8db2c7f 100644--- a/tests/agent-scene-vegetation.test.js+++ b/tests/agent-scene-vegetation.test.js@@ -164,3 +164,21 @@ test('an imported tree with a foliage override gets the swaying tag and shares iassert.notEqual(material('plain').tag, 'foliage');assert.throws(() => createAgentScene({ ...recipe, nodes: [{ id: 'a', asset: { id, materials: [{ name: 'Leaves', tag: 'water' }] } }] }, {}, { assets }), /^RangeError: nodes\[0\]\.asset\.materials\[0\]\.tag: got "water"; expected one of surface, foliage, grass/);});++test('moving the parent of a node the scatter excludes around regrows the forest like a fresh scene', () => {+ const recipe = pondParentX => ({+ ...stillRecipe(),+ nodes: [+ { id: 'hills', geometry: 'ground', material: 'grass' },+ { id: 'pondgroup', translation: [pondParentX, 0, 0] },+ { id: 'pond', parent: 'pondgroup', geometry: 'marker', material: 'steel' },+ { id: 'forest', vegetation: { species: 'broadleaf', seed: 11, scatter: { ground: 'hills', density: 0.02, exclude: [{ node: 'pond', radius: 9 }], variants: 2 } } },+ ],+ });+ const treesWithin = (scene, centerX) => scene.parts.filter(part => part.name.startsWith('forest/') && Math.hypot(part.matrix[12] - centerX, part.matrix[14]) <= 9).length;+ const edited = createAgentScene(recipe(0));+ assert.equal(treesWithin(edited, 0), 0);+ edited.apply([{ type: 'set-node', id: 'pondgroup', translation: [15, 0, 0] }], { expectedRevision: 0 });+ assert.equal(treesWithin(edited, 15), 0, 'no tree stands in the new clearing');+ assert.deepEqual(vegetationMatrices(edited), vegetationMatrices(createAgentScene(recipe(15))));+});diff --git a/tests/dense-lods-option.test.js b/tests/dense-lods-option.test.jsindex 95eee6e5..f55b1eba 100644--- a/tests/dense-lods-option.test.js+++ b/tests/dense-lods-option.test.js@@ -49,3 +49,13 @@ test('a scene file picks levels at 0.5 pixels of error unless its renderer saysassert.equal(withSceneFileRendererDefaults({ lodPixelError: 2 }).lodPixelError, 2);assert.equal(withSceneFileRendererDefaults({ samples: 2 }).samples, 2);});++test('a pixel ratio that is not a usable number is refused by name, and NaN thresholds are not read as "dense"', async () => {+ const { addLodsToDenseParts } = await import('../src/dense-lods.js');+ for (const pixelRatio of [0, -1, NaN, '1']) {+ assert.throws(() => partsForRendererOptions([scanPart()], SMALL_CANVAS, { pixelRatio }), /partsForRendererOptions: pixelRatio was .*; pass a number from 0\.1 to 16/);+ }+ for (const options of [{ minimumTriangles: NaN }, { minimumTrianglesPerPixel: NaN }, { minimumTrianglesPerPixel: 'x' }]) {+ assert.throws(() => addLodsToDenseParts([scanPart()], { pixels: 1e9, ...options }), /addLodsToDenseParts: minimumTriangle/);+ }+});