Skip to content

3D engine that AI agents drive

Keep cutter geometries out of the unused list and leave a bad far plane to the camera (#2534)

Nguyen Xuan Anfc081d8b20f6
diff --git a/changes/2534.md b/changes/2534.md
new file mode 100644
index 00000000..9cf7d8dc
--- /dev/null
+++ b/changes/2534.md
@@ -0,0 +1 @@
+- Inspect no longer calls a geometry used only as a boolean cutter unused, and the fitted near plane leaves an invalid far plane to the camera check (#2534, #2535)
diff --git a/changes/2534.size.md b/changes/2534.size.md
new file mode 100644
index 00000000..4636a0b9
--- /dev/null
+++ b/changes/2534.size.md
@@ -0,0 +1,3 @@
+- scene:agent-scene: about 105 bytes gzip for following boolean cutter references, so inspect stops telling an agent to remove a cutter geometry that a mesh still needs.
+- scene:agent-scene+lazy: same code as scene:agent-scene.
+- goal:agent-scene: the scene was already over its goal; the 105 bytes keep a diagnostic from giving advice that breaks a mesh.
diff --git a/src/agent-diagnostics.js b/src/agent-diagnostics.js
index 41374f0f..a1aa97e9 100644
--- a/src/agent-diagnostics.js
+++ b/src/agent-diagnostics.js
@@ -61,11 +61,23 @@ function materialProblems(material) {
return problems;
}
+// A mesh geometry may cut with another recipe geometry that no node draws (`edits[].cutter.geometry`), and that one with a third.
+function geometryIdsInUse({ geometries, nodes }) {
+ const inUse = new Set();
+ const visit = id => {
+ if (id === undefined || inUse.has(id)) return;
+ inUse.add(id);
+ geometries.find(geometry => geometry.id === id)?.edits?.forEach(edit => visit(edit?.cutter?.geometry));
+ };
+ nodes.forEach(node => visit(node.geometry));
+ return inUse;
+}
+
/** Material and resource diagnostics for a normalized procedural recipe. */
export function recipeDiagnostics(recipe) {
const problems = recipe.materials.flatMap(materialProblems);
const usedMaterials = new Set(recipe.nodes.map(node => node.material).filter(Boolean));
- const usedGeometries = new Set(recipe.nodes.map(node => node.geometry).filter(Boolean));
+ const usedGeometries = geometryIdsInUse(recipe);
for (const material of recipe.materials.filter(candidate => !usedMaterials.has(candidate.id))) {
problems.push(diagnostic('unused-material', 'info', material.id,
`Material ${material.id} is used by no node.`,
diff --git a/src/near-plane-fit.js b/src/near-plane-fit.js
index eb7cdad9..b4c4ef64 100644
--- a/src/near-plane-fit.js
+++ b/src/near-plane-fit.js
@@ -49,6 +49,8 @@ export function nearestDepthOfBounds(bounds, view) {
/** The near plane for a frame whose camera left near at its default: the default, or further out when the scene is far from the eye. */
export function fittedNearPlane(bounds, view, far) {
+ // A far plane that is missing, NaN or no farther than the default near plane is the camera's error to report, not one to hide behind a fitted near.
+ if (!(far > DEFAULT_NEAR)) return DEFAULT_NEAR;
const nearestDepth = nearestDepthOfBounds(bounds, view);
if (!(nearestDepth > DEFAULT_NEAR / NEAREST_DEPTH_SHARE)) return DEFAULT_NEAR;
return Math.min(nearestDepth * NEAREST_DEPTH_SHARE, far * FAR_SHARE_LIMIT);
diff --git a/tests/agent-diagnostics-cutters.test.js b/tests/agent-diagnostics-cutters.test.js
new file mode 100644
index 00000000..424a42bc
--- /dev/null
+++ b/tests/agent-diagnostics-cutters.test.js
@@ -0,0 +1,32 @@
+import test from 'node:test';
+import assert from 'node:assert/strict';
+import { recipeDiagnostics } from '../src/agent-diagnostics.js';
+
+function cuttingMesh(id, cutterId) {
+ return { id, type: 'mesh', base: { type: 'box' }, edits: [{ op: 'boolean', operation: 'difference', name: 'bore', cutter: { geometry: cutterId } }] };
+}
+
+function recipeWith(geometries) {
+ return { materials: [{ id: 'steel' }], geometries, nodes: [{ id: 'block', geometry: geometries[0].id, material: 'steel' }] };
+}
+
+function unusedGeometryIds(recipe) {
+ return recipeDiagnostics(recipe).filter(entry => entry.kind === 'unused-geometry').map(entry => entry.target);
+}
+
+test('a geometry that only serves as a boolean cutter is not reported as unused', () => {
+ const recipe = recipeWith([cuttingMesh('block', 'tool'), { id: 'tool', type: 'cylinder' }]);
+ assert.deepEqual(unusedGeometryIds(recipe), []);
+});
+
+test('a cutter of a cutter counts as used, and a cutter of an unused mesh does not', () => {
+ const nested = recipeWith([cuttingMesh('block', 'tool'), cuttingMesh('tool', 'inner'), { id: 'inner', type: 'sphere' }]);
+ assert.deepEqual(unusedGeometryIds(nested), []);
+ const orphan = recipeWith([{ id: 'block', type: 'box' }, cuttingMesh('spare', 'tool'), { id: 'tool', type: 'cylinder' }]);
+ assert.deepEqual(unusedGeometryIds(orphan), ['spare', 'tool']);
+});
+
+test('a cycle of cutters ends and an unrelated geometry is still reported', () => {
+ const recipe = recipeWith([cuttingMesh('block', 'tool'), cuttingMesh('tool', 'block'), { id: 'spare', type: 'sphere' }]);
+ assert.deepEqual(unusedGeometryIds(recipe), ['spare']);
+});
diff --git a/tests/near-plane-fit-far.test.js b/tests/near-plane-fit-far.test.js
new file mode 100644
index 00000000..acaccc22
--- /dev/null
+++ b/tests/near-plane-fit-far.test.js
@@ -0,0 +1,17 @@
+import test from 'node:test';
+import assert from 'node:assert/strict';
+import { mat4 } from 'gl-matrix';
+import { DEFAULT_NEAR, fittedNearPlane, sceneBoundsOfParts } from '../src/near-plane-fit.js';
+
+const distantBounds = sceneBoundsOfParts([{ geometry: { positions: new Float32Array([-1, -1, -1, 1, 1, 1]) }, matrix: mat4.fromTranslation(mat4.create(), [0, 0, -80]) }]);
+const view = mat4.lookAt(mat4.create(), [0, 0, 0], [0, 0, -1], [0, 1, 0]);
+
+test('a far plane that is not a positive finite number leaves the default near plane for the camera to reject', () => {
+ for (const far of [NaN, undefined, 0, -5, 0.05]) {
+ assert.equal(fittedNearPlane(distantBounds, view, far), DEFAULT_NEAR, `far ${far}`);
+ }
+});
+
+test('a usable far plane still moves the near plane out', () => {
+ assert.ok(fittedNearPlane(distantBounds, view, 10000) > 50);
+});