3D engine that AI agents drive
Release undo revisions before the post-generation vertex budget refuses a mesh (closes #818)
Nguyen Xuan Anf09707b1bd02
diff --git a/changes/818.md b/changes/818.mdnew file mode 100644index 00000000..ffadf7c2--- /dev/null+++ b/changes/818.md@@ -0,0 +1 @@+- Fix mesh edits such as `mesh_stack` failing with "render vertices exceed maxVertices across the scene" after a few re-evaluations of a large mesh: undo revisions are released before the budget refuses, and a scene that is really over the limit is still rejected (#818)diff --git a/src/agent-scene.js b/src/agent-scene.jsindex e3be67fe..664dac4c 100644--- a/src/agent-scene.js+++ b/src/agent-scene.js@@ -1398,7 +1398,14 @@ export function createAgentScene(input, inputLimits = {}, options = {}) {const geometryBytes = Object.values(geometry).reduce((total, array) => total + array.byteLength, 0);const vertices = geometry.positions.length / 3;if (plan.definition.type === 'mesh') {- if (residentStats.vertices + pendingVertices - plan.estimatedVertices + vertices > limits.maxVertices) {+ // Edits can grow a mesh past its estimate. Undo history never blocks an edit, so release the oldest revisions before refusing.+ let residentVertices = residentStats.vertices;+ while (residentVertices + pendingVertices - plan.estimatedVertices + vertices > limits.maxVertices && revisionHistory.length > 0) {+ revisionHistory.shift();+ const stillResident = new Set([...ownedGeometryRecords(), ...plans.map(candidate => candidate.existing).filter(Boolean)]);+ residentVertices = geometryStats(stillResident).vertices;+ }+ if (residentVertices + pendingVertices - plan.estimatedVertices + vertices > limits.maxVertices) {throw new RangeError(`Mesh ${plan.definition.id} has ${vertices} render vertices after its edits, which exceeds maxVertices across the scene. Reduce detail or edits.`);}} else if (vertices > plan.estimatedVertices || geometryBytes > plan.estimatedVertices * (56 + TERRAIN_SKY_VISIBILITY_BYTES)) {diff --git a/tests/mesh-stack-budget.test.js b/tests/mesh-stack-budget.test.jsnew file mode 100644index 00000000..01bc732f--- /dev/null+++ b/tests/mesh-stack-budget.test.js@@ -0,0 +1,36 @@+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 subdivided box grows far past the preflight estimate, so the budget is checked again after generation.+function seaRecipe(copies) {+ return {+ format: '67js.scene', schemaVersion: 1,+ geometries: [{+ id: 'sea', type: 'mesh', base: { type: 'box', bevel: 0 },+ edits: [{ op: 'subdivide_edges', edges: { all: true }, cuts: 16 }],+ stack: [{ op: 'array', count: copies, relativeOffset: [1, 0, 0] }],+ }],+ materials: [{ id: 'water', color: [0.1, 0.3, 0.6] }],+ nodes: [{ id: 'sea', geometry: 'sea', material: 'water' }],+ };+}++function setStack(scene, revision, copies, offset) {+ scene.apply([{ type: 'set-mesh-stack', geometry: 'sea', stack: [{ op: 'array', count: copies, relativeOffset: [offset, 0, 0] }] }], { expectedRevision: revision });+}++test('re-evaluating a 70k-vertex stack 10 times in one scene stays inside the default vertex budget (#818)', () => {+ const scene = createAgentScene(seaRecipe(38));+ for (let revision = 0; revision < 10; revision += 1) {+ setStack(scene, revision, 38, 1 + (revision + 1) * 0.01);+ }+ const [report] = scene.inspectMeshes().meshes;+ assert.equal(report.ok, true, JSON.stringify(report.problems));+});++test('the vertex budget still rejects a stack that is over the limit by itself (#818)', () => {+ const scene = createAgentScene(seaRecipe(2), { maxVertices: 50_000 });+ assert.throws(() => setStack(scene, 0, 38, 1), /maxVertices/);+});