1c837766cd
Add backend-driven assembly and simulation experiences, MAKE branding, mobile rendering budgets and desktop-only CAD/PCB routes. Improve logo loading progress, document image recovery and URDF request handling; include dev packaging and regression coverage.
61 lines
3.2 KiB
JavaScript
61 lines
3.2 KiB
JavaScript
// Read-only geometry for offline assembly checks. Render assets are never rewritten.
|
|
import { readFileSync } from 'node:fs';
|
|
import { join } from 'node:path';
|
|
import { createHash } from 'node:crypto';
|
|
import assert from 'node:assert/strict';
|
|
import { NodeIO } from '@gltf-transform/core';
|
|
import { ALL_EXTENSIONS } from '@gltf-transform/extensions';
|
|
import { BufferGeometry, BufferAttribute, Matrix4, Quaternion, Vector3 } from 'three';
|
|
import { validateEmbeddedGlb } from '../../js/experience-packages.js';
|
|
import { collider } from './assembly-axis-planner.mjs';
|
|
|
|
export function assemblyColliders(config, assets) {
|
|
const io = new NodeIO().registerExtensions(ALL_EXTENSIONS);
|
|
const display = new Matrix4().makeRotationFromQuaternion(new Quaternion(...config.displayQuaternion));
|
|
const documents = new Map(), items = new Map();
|
|
return async function get(part) {
|
|
assert.equal(part.parentId, null, 'Only flat rigid assemblies are supported');
|
|
if (items.has(part.id)) return items.get(part.id);
|
|
if (!documents.has(part.file)) {
|
|
const bytes = readFileSync(join(assets, part.file));
|
|
const file = config.files.get(part.file);
|
|
assert.equal(bytes.length, file.bytes);
|
|
assert.equal(createHash('sha256').update(bytes).digest('hex'), file.sha256);
|
|
validateEmbeddedGlb(bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength));
|
|
documents.set(part.file, await io.readBinary(bytes));
|
|
}
|
|
const doc = documents.get(part.file);
|
|
const scene = doc.getRoot().getDefaultScene() ?? doc.getRoot().listScenes()[0];
|
|
assert.ok(scene, `Missing scene: ${part.file}`);
|
|
const placement = display.clone().multiply(new Matrix4().compose(
|
|
new Vector3(...part.position), new Quaternion(...part.quaternion), new Vector3(...part.scale)));
|
|
const chunks = [], indices = [];
|
|
let count = 0;
|
|
scene.traverse(node => {
|
|
assert.ok(!node.getSkin() && !node.getWeights().length, 'Rigid geometry required');
|
|
const matrix = placement.clone().multiply(new Matrix4().fromArray(node.getWorldMatrix()));
|
|
for (const primitive of node.getMesh()?.listPrimitives() ?? []) {
|
|
assert.equal(primitive.getMode(), 4);
|
|
assert.equal(primitive.listTargets().length, 0);
|
|
const source = primitive.getAttribute('POSITION');
|
|
assert.ok(source && !source.getNormalized());
|
|
const position = new BufferAttribute(new Float32Array(source.getArray()), 3);
|
|
position.applyMatrix4(matrix);
|
|
chunks.push(position.array);
|
|
const idx = primitive.getIndices()?.getArray() ?? Array.from({ length: position.count }, (_, i) => i);
|
|
for (const index of idx) indices.push(index + count);
|
|
count += position.count;
|
|
}
|
|
});
|
|
assert.ok(count && indices.length % 3 === 0);
|
|
const positions = new Float32Array(count * 3);
|
|
let offset = 0;
|
|
for (const chunk of chunks) { positions.set(chunk, offset); offset += chunk.length; }
|
|
const geometry = new BufferGeometry().setAttribute('position', new BufferAttribute(positions, 3));
|
|
geometry.setIndex(new BufferAttribute(new Uint32Array(indices), 1));
|
|
const item = collider(part.id, geometry);
|
|
items.set(part.id, item);
|
|
return item;
|
|
};
|
|
}
|