Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Add deterministic per-component explosion offsets and export helper

Nguyen Xuan An14b1a7d1332a
diff --git a/src/assembly.js b/src/assembly.js
new file mode 100644
index 00000000..e0e88075
--- /dev/null
+++ b/src/assembly.js
@@ -0,0 +1,84 @@
+// Components spread in their section's mounting plane, then lift off in ordered rows.
+// The base matrices and shared geometry remain assembled and never change.
+function sectionLayout(section) {
+ const side = section.startsWith('Left ') ? -1 : 1;
+ const family = section.replace(/^(Left|Right) /, '');
+ const layouts = {
+ 'Structural frame': { axis: 1, sign: 1, spread: 0.9, tier: 0.14, lift: 0 },
+ 'Battery cassette': { axis: 1, sign: 1, spread: 1.6, tier: 0.14, lift: 0 },
+ 'Control electronics': { axis: 1, sign: 1, spread: 1.7, tier: 0.14, lift: 0.1 },
+ 'Differential drive': { axis: 2, sign: -1, spread: 1.2, tier: 0.14, lift: 0.55 },
+ 'Front compactor panel': { axis: 2, sign: 1, spread: 1, tier: 0.05, lift: 0.2 },
+ 'Rear service panel': { axis: 2, sign: -1, spread: 1, tier: 0.08, lift: 0.2 },
+ 'Roof panel': { axis: 1, sign: 1, spread: 1, tier: 0.18, lift: 0 },
+ 'body panel': { axis: 0, sign: side, spread: 1, tier: 0.12, lift: 0.4 },
+ 'track assembly': { axis: 0, sign: side, spread: 1.7, tier: 0.1, lift: 0.72 },
+ 'arm and claw': { axis: 0, sign: side, spread: 1.3, tier: 0.1, lift: 0.5 },
+ 'Neck linkage': { axis: 1, sign: 1, spread: 1.4, tier: 0.14, lift: 0 },
+ 'eye housing': { axis: 2, sign: -1, spread: 1.4, tier: 0.14, lift: 0.08 },
+ 'optical mechanism': { axis: 2, sign: 1, spread: 1.4, tier: 0.14, lift: 0.15 },
+ 'lens assembly': { axis: 2, sign: 1, spread: 1.8, tier: 0.14, lift: 0.1 },
+ };
+ const layout = layouts[family];
+ if (!layout) throw new Error(`No detail layout for assembly section: ${section}`);
+ return layout;
+}
+
+// Steel shoes and wide rubber pads need separate axial lanes; the wheel stack stays nearby.
+const trackLanes = {
+ 'Caterpillar steel shoe': 0.18,
+ 'Caterpillar rubber traction pad': 0.65,
+ 'Track sprocket drum': 0.12,
+ 'Track exposed wheel disc': 0.49,
+ 'Track wheel rim': 0.58,
+ 'Track wheel hub': 0.72,
+ 'Track hub retaining bolt': 0.86,
+ 'Track wheel bolt': 1,
+ 'Track tension beam': 1.2,
+};
+
+export function assemblyOffset(part, amount = 0) {
+ if (!Number.isFinite(amount)) throw new TypeError('Assembly amount must be finite');
+ const progress = Math.max(0, Math.min(1, amount));
+ if (progress === 0) return [0, 0, 0];
+ const detailProgress = Math.max(0, Math.min(1, (progress - 0.3) / 0.7));
+ const detailAmount = detailProgress * detailProgress * (3 - 2 * detailProgress);
+ const offset = part.assembly?.offset ?? [0, 0, 0];
+ const detailOffset = part.assembly?.detailOffset ?? [0, 0, 0];
+ return offset.map((value, axis) => value * progress + detailOffset[axis] * detailAmount);
+}
+
+export function addDetailOffsets(parts) {
+ const sections = new Map();
+ for (const part of parts) {
+ const section = part.assembly.section;
+ if (!sections.has(section)) {
+ sections.set(section, { minimum: [Infinity, Infinity, Infinity], maximum: [-Infinity, -Infinity, -Infinity], families: new Map(), layout: sectionLayout(section) });
+ }
+ const state = sections.get(section);
+ for (let axis = 0; axis < 3; axis++) {
+ state.minimum[axis] = Math.min(state.minimum[axis], part.matrix[12 + axis]);
+ state.maximum[axis] = Math.max(state.maximum[axis], part.matrix[12 + axis]);
+ }
+ }
+ return parts.map(part => {
+ const state = sections.get(part.assembly.section);
+ const { layout } = state;
+ if (!state.families.has(part.name)) {
+ state.families.set(part.name, { tier: state.families.size, count: 0 });
+ }
+ const family = state.families.get(part.name);
+ const detailOffset = [0, 0, 0];
+ for (let axis = 0; axis < 3; axis++) {
+ if (axis !== layout.axis) {
+ const center = (state.minimum[axis] + state.maximum[axis]) / 2;
+ detailOffset[axis] = (part.matrix[12 + axis] - center) * layout.spread;
+ }
+ }
+ const lane = part.assembly.section.endsWith('track assembly') ? trackLanes[part.name] : undefined;
+ detailOffset[layout.axis] = layout.sign * ((lane ?? 0.12 + family.tier * layout.tier) + family.count * 0.018);
+ detailOffset[1] += layout.lift;
+ family.count++;
+ return { ...part, assembly: { ...part.assembly, detailOffset } };
+ });
+}
diff --git a/src/model.js b/src/model.js
index 362fa0a5..cefc01fe 100644
--- a/src/model.js
+++ b/src/model.js
@@ -1,5 +1,6 @@
import { box, cylinder, sphere, torus } from './geometry.js';
import { mat4, quat } from 'gl-matrix';
+import { addDetailOffsets } from './assembly.js';
const QUARTER_TURN = Math.PI / 2;
const materials = {
@@ -255,5 +256,5 @@ export function createModel() {
}
wire('Head sensor lead', [[horizontal, 2.44, 0.02], [horizontal * 0.8, 2.37, 0.02], [horizontal * 0.35, 2.33, 0.05]], 0.015, 'copper', headCore);
}
- return parts;
+ return addDetailOffsets(parts);
}
diff --git a/tests/assembly.test.js b/tests/assembly.test.js
new file mode 100644
index 00000000..bbc640a6
--- /dev/null
+++ b/tests/assembly.test.js
@@ -0,0 +1,90 @@
+import assert from 'node:assert/strict';
+import test from 'node:test';
+import { addDetailOffsets, assemblyOffset } from '../src/assembly.js';
+import { createModel } from '../src/model.js';
+
+test('detail planning is deterministic and leaves assembled source objects untouched', () => {
+ const sharedAssembly = { layer: 'interior', section: 'Structural frame', offset: [0, 0, 0] };
+ const source = [0, 1, 2].map(index => ({ name: 'Frame screw', matrix: new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, index * 0.1, 1, 0, 1]), geometry: {}, material: {}, assembly: sharedAssembly }));
+ const originalMatrices = source.map(part => Array.from(part.matrix));
+ const planned = addDetailOffsets(source);
+ const repeat = addDetailOffsets(source);
+ assert.deepEqual(planned.map(part => part.assembly.detailOffset), repeat.map(part => part.assembly.detailOffset));
+ assert.equal(sharedAssembly.detailOffset, undefined);
+ planned.forEach((part, index) => {
+ assert.equal(part.matrix, source[index].matrix);
+ assert.equal(part.geometry, source[index].geometry);
+ assert.equal(part.material, source[index].material);
+ assert.notEqual(part.assembly, sharedAssembly);
+ assert.deepEqual(Array.from(source[index].matrix), originalMatrices[index]);
+ });
+});
+
+test('all individual accessories have distinct finite movement and reversible full-explosion bounds', () => {
+ const parts = createModel();
+ const translations = new Set();
+ const sectionDetails = new Map();
+ const minimum = [Infinity, Infinity, Infinity];
+ const maximum = [-Infinity, -Infinity, -Infinity];
+ for (const part of parts) {
+ const { offset, detailOffset, section } = part.assembly;
+ assert.equal(detailOffset.length, 3);
+ assert.ok(detailOffset.every(Number.isFinite));
+ assert.ok(Math.hypot(...detailOffset) > 0.1, 'every component moves, including the stationary structural frame');
+ const translation = assemblyOffset(part, 1);
+ assert.deepEqual(translation, offset.map((value, axis) => value + detailOffset[axis]));
+ assert.deepEqual(assemblyOffset(part, 0), [0, 0, 0]);
+ const key = translation.map(value => value.toFixed(6)).join(',');
+ assert.ok(!translations.has(key), `individual full translation: ${section} / ${part.name}`);
+ translations.add(key);
+ if (!sectionDetails.has(section)) sectionDetails.set(section, new Set());
+ const detailKey = detailOffset.map(value => value.toFixed(6)).join(',');
+ assert.ok(!sectionDetails.get(section).has(detailKey), 'details within each section do not travel as a single block');
+ sectionDetails.get(section).add(detailKey);
+ const matrix = part.matrix;
+ const positions = part.geometry.positions;
+ for (let vertex = 0; vertex < positions.length; vertex += 3) {
+ for (let axis = 0; axis < 3; axis++) {
+ const assembled = matrix[axis] * positions[vertex] + matrix[axis + 4] * positions[vertex + 1] + matrix[axis + 8] * positions[vertex + 2] + matrix[axis + 12];
+ const expanded = assembled + translation[axis];
+ assert.ok(Math.abs(expanded - translation[axis] - assembled) < 1e-12, 'translation reverses exactly within floating-point tolerance');
+ minimum[axis] = Math.min(minimum[axis], expanded);
+ maximum[axis] = Math.max(maximum[axis], expanded);
+ }
+ }
+ }
+ assert.equal(translations.size, 574);
+ assert.equal(sectionDetails.size, 20);
+ assert.ok(minimum[1] >= 0.045);
+ assert.ok(maximum[1] <= 5.5);
+ assert.ok(Math.max(Math.abs(minimum[0]), Math.abs(maximum[0])) <= 4.8);
+ assert.ok(Math.max(Math.abs(minimum[2]), Math.abs(maximum[2])) <= 3.4);
+ console.log(JSON.stringify({ individuallyExplodableParts: translations.size, sections: sectionDetails.size, minimum, maximum }));
+});
+
+test('export displacement exactly matches the staged shader smoothstep', () => {
+ const part = { assembly: { offset: [1, 2, -3], detailOffset: [0.5, -0.4, 0.2] } };
+ assert.deepEqual(assemblyOffset(part, -1), [0, 0, 0]);
+ assert.deepEqual(assemblyOffset(part, 0.2), [0.2, 0.4, -0.6000000000000001]);
+ assert.deepEqual(assemblyOffset(part, 0.3), [0.3, 0.6, -0.8999999999999999]);
+ const halfway = assemblyOffset(part, 0.65);
+ const expected = [0.9, 1.1, -1.85];
+ halfway.forEach((value, axis) => assert.ok(Math.abs(value - expected[axis]) < 1e-12));
+ assert.deepEqual(assemblyOffset(part, 1), [1.5, 1.6, -2.8]);
+ assert.deepEqual(assemblyOffset(part, 2), assemblyOffset(part, 1));
+ assert.deepEqual(assemblyOffset({}, 0.8), [0, 0, 0]);
+ assert.throws(() => assemblyOffset(part, NaN), /finite/);
+ assert.throws(() => assemblyOffset(part, Infinity), /finite/);
+});
+
+test('coincident rubber pads and steel shoes detach into visibly separate axial lanes', () => {
+ const parts = createModel();
+ for (const section of ['Left track assembly', 'Right track assembly']) {
+ const shoes = parts.filter(part => part.assembly.section === section && part.name === 'Caterpillar steel shoe');
+ const pads = parts.filter(part => part.assembly.section === section && part.name === 'Caterpillar rubber traction pad');
+ assert.equal(shoes.length, pads.length);
+ shoes.forEach((shoe, index) => {
+ assert.ok(Math.abs(shoe.assembly.detailOffset[0] - pads[index].assembly.detailOffset[0]) > 0.4);
+ });
+ }
+});
diff --git a/tests/model.test.js b/tests/model.test.js
index 4ffa491b..2b1dae83 100644
--- a/tests/model.test.js
+++ b/tests/model.test.js
@@ -1,5 +1,6 @@
import assert from 'node:assert/strict';
import test from 'node:test';
+import { createHash } from 'node:crypto';
import { box, cylinder, sphere, torus } from '../src/geometry.js';
import { createModel } from '../src/model.js';
@@ -122,7 +123,8 @@ test('model respects the renderer contract, instancing and authored bounds', ()
}
const section = sections.get(part.assembly.section);
if (section) {
- assert.deepEqual(part.assembly, section, 'components in a mechanical section travel together');
+ assert.equal(part.assembly.layer, section.layer);
+ assert.deepEqual(part.assembly.offset, section.offset, 'components share their section translation before detail expansion');
} else {
sections.set(part.assembly.section, part.assembly);
}
@@ -155,3 +157,18 @@ test('model respects the renderer contract, instancing and authored bounds', ()
assert.ok(meshBytes < 1500000, 'geometry remains suitable for a compact export');
console.log(JSON.stringify({ parts: parts.length, geometries: geometries.size, triangleCount, meshBytes, sections: sections.size, bounds: { minimum, maximum }, expandedBounds: { minimum: expandedMinimum, maximum: expandedMaximum } }));
});
+
+test('detail metadata preserves the assembled WALL-E appearance byte for byte', () => {
+ const parts = createModel();
+ const hash = createHash('sha256');
+ for (const part of parts) {
+ hash.update(JSON.stringify({ name: part.name, matrix: Array.from(part.matrix), material: part.material }));
+ }
+ for (const mesh of new Set(parts.map(part => part.geometry))) {
+ for (const field of [mesh.positions, mesh.normals, mesh.indices]) {
+ hash.update(new Uint8Array(field.buffer, field.byteOffset, field.byteLength));
+ }
+ }
+ assert.equal(parts.length, 574);
+ assert.equal(hash.digest('hex'), '8dc74e090405d078bdde805232da69fd5a5ed20431544d53eb30968e03d9da91');
+});