Files
vibemake/js/assembly-explode.js
Jerry 1c837766cd feat: integrate robot assembly and simulation with resilient loading
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.
2026-10-30 18:38:39 +08:00

66 lines
3.6 KiB
JavaScript

import { Box3, Matrix3, Matrix4, Vector3 } from '../vendor/three/build/three.module.min.js';
// Build once from assembled geometry. Only instance translations change during
// interaction; geometry, materials, rotations and the source JSON stay intact.
export function createExplodedLayout(config, instances) {
const records = config.parts.map((part) => {
const instance = instances.get(part.id);
instance.updateWorldMatrix(true, true);
// The first child is this part's GLB, excluding nested assembly parts/tools.
const bounds = new Box3().setFromObject(instance.children[0]);
if (bounds.isEmpty()) bounds.setFromCenterAndSize(instance.getWorldPosition(new Vector3()), new Vector3());
return { part, instance, bounds, center: bounds.getCenter(new Vector3()), worldOffset: new Vector3() };
});
const originalBounds = new Box3();
records.forEach(({ bounds }) => originalBounds.union(bounds));
const center = originalBounds.getCenter(new Vector3());
const size = originalBounds.getSize(new Vector3());
const span = Math.max(size.x, size.y, size.z, 0.001);
const coincident = new Map();
for (const record of records) {
const key = record.center.toArray().map((n) => Math.round(n / span * 1e6)).join(',');
if (!coincident.has(key)) coincident.set(key, []);
coincident.get(key).push(record);
record.worldOffset.copy(record.center).sub(center).multiplyScalar(0.85);
const insertion = new Vector3(...record.part.installOffset)
.applyMatrix3(new Matrix3().setFromMatrix4(record.instance.parent.matrixWorld));
if (insertion.lengthSq() > 1e-14) {
const clearance = Math.min(span * 0.25, Math.max(span * 0.07, record.bounds.getSize(new Vector3()).length() * 0.35));
record.worldOffset.addScaledVector(insertion.normalize(), clearance);
}
}
// Coincident pivots/centers must not remain stacked in the exploded view.
for (const group of coincident.values()) if (group.length > 1) {
group.sort((a, b) => a.part.id.localeCompare(b.part.id));
group.forEach((record, i) => {
const angle = i * Math.PI * (3 - Math.sqrt(5));
record.worldOffset.add(new Vector3(Math.cos(angle), (i / (group.length - 1) - 0.5) * 0.5, Math.sin(angle)).multiplyScalar(span * 0.09));
});
}
// Keep the lowest part on/above the original floor throughout the transition.
const lowest = Math.min(...records.map(({ bounds, worldOffset }) => bounds.min.y + worldOffset.y));
const lift = Math.max(0, originalBounds.min.y - lowest);
records.forEach(({ worldOffset }) => { worldOffset.y += lift; });
const byId = new Map(records.map((record) => [record.part.id, record]));
for (const record of records) {
const parentOffset = byId.get(record.part.parentId)?.worldOffset || new Vector3();
const inverseParent = new Matrix3().setFromMatrix4(new Matrix4().copy(record.instance.parent.matrixWorld).invert());
// Compensate the parent's displacement so nested parts don't move twice.
record.offset = record.worldOffset.clone().sub(parentOffset).applyMatrix3(inverseParent);
}
const clamp = (value) => Number.isFinite(value) ? Math.max(0, Math.min(1, value)) : 0;
return {
records,
originalBounds,
apply(value) {
const amount = clamp(value);
for (const { part, instance, offset } of records) instance.position.fromArray(part.position).addScaledVector(offset, amount);
},
boundsAt(value) {
const amount = clamp(value), bounds = new Box3();
for (const record of records) bounds.union(record.bounds.clone().translate(record.worldOffset.clone().multiplyScalar(amount)));
return bounds;
},
};
}