Skip to content

3D engine that AI agents drive

Build the per-node line art table for nodes with any number of object IDs (#2478)

Nguyen Xuan An308407d04595
diff --git a/changes/2478.md b/changes/2478.md
new file mode 100644
index 00000000..ff1e7e26
--- /dev/null
+++ b/changes/2478.md
@@ -0,0 +1 @@
+- Per-node line art no longer throws "Maximum call stack size exceeded" when one node owns over 100,000 object IDs (#2478).
diff --git a/src/line-art.js b/src/line-art.js
index e27a72cb..16110b57 100644
--- a/src/line-art.js
+++ b/src/line-art.js
@@ -190,7 +190,11 @@ export function resolveLineArt(input = {}) {
export function lineNodeTable(lines, groups) {
if (!lines.nodes || Object.keys(lines.nodes).length === 0) return { count: 0, data: null, width: 0, height: 0 };
const names = Object.keys(groups);
- const count = names.reduce((highest, name) => Math.max(highest, ...groups[name]), 0) + 1;
+ let highestIdentifier = 0;
+ for (const name of names) {
+ for (const identifier of groups[name]) highestIdentifier = Math.max(highestIdentifier, identifier);
+ }
+ const count = highestIdentifier + 1;
const height = Math.ceil(count / NODE_TABLE_WIDTH);
const data = new Float32Array(NODE_TABLE_WIDTH * height * 4);
names.forEach((name, nodeIndex) => {
diff --git a/tests/line-art-nodes.test.js b/tests/line-art-nodes.test.js
index da78dc44..73e19d37 100644
--- a/tests/line-art-nodes.test.js
+++ b/tests/line-art-nodes.test.js
@@ -141,3 +141,12 @@ test('removing a node that lines.nodes names says to drop its entry in the same
assert.throws(() => scene.apply([{ type: 'remove-node', id: 'rug' }], { expectedRevision: 0 }), /rug.*drop that node's entry from lines\.nodes in the same call/s);
scene.apply([{ type: 'remove-node', id: 'rug' }, { type: 'set-environment', environment: { lines: { nodes: { floor: { silhouetteOnly: true } } } } }], { expectedRevision: 0 });
});
+
+test('a node with hundreds of thousands of object IDs still gets its table', () => {
+ const crowdSize = 400_000;
+ const crowd = Array.from({ length: crowdSize }, (unused, index) => index + 1);
+ const lines = resolveLineArt({ nodes: { crowd: { silhouetteOnly: true } } });
+ const table = lineNodeTable(lines, { crowd });
+ assert.equal(table.count, crowdSize + 1);
+ assert.equal(table.data[crowdSize * 4 + 3], 2 + 4 * 1, 'the last ID carries the mode and the node index');
+});