Files
vibemake/js/assembly-framing.js

31 lines
1.6 KiB
JavaScript

import { Box3, Vector3 } from '../vendor/three/build/three.module.min.js';
export function buildWorkBounds(finalBounds, visible, contextBounds, entries = []) {
const box = new Box3();
for (const id of visible) if (finalBounds.has(id)) box.union(finalBounds.get(id));
for (const { id, offset } of entries) if (finalBounds.has(id)) box.union(finalBounds.get(id).clone().translate(offset));
if (box.isEmpty()) return contextBounds.clone();
// First screw/base still has enough context, but never frame invisible future
// parts at the far end of the leg or torso.
const minimum = contextBounds.getSize(new Vector3()).length() * 0.16;
const size = box.getSize(new Vector3()), center = box.getCenter(new Vector3());
return box.setFromCenterAndSize(center, size.max(new Vector3(minimum, minimum, minimum)));
}
export function componentViewDirection(component, whole) {
const side = component.getCenter(new Vector3()).sub(whole.getCenter(new Vector3()));
// Keep the same front and elevation, looking from the outer side of paired
// components. This angle is chosen once per component, not per moving screw.
const z = Math.abs(side.z) > whole.getSize(new Vector3()).z * .12 ? Math.sign(side.z) * .75 : -.65;
return new Vector3(1, .48, z).normalize();
}
export function workBoundsFit(bounds, camera) {
camera.updateMatrixWorld(true);
for (const x of [bounds.min.x,bounds.max.x]) for (const y of [bounds.min.y,bounds.max.y]) for (const z of [bounds.min.z,bounds.max.z]) {
const p = new Vector3(x,y,z).project(camera);
if (Math.abs(p.x) > .84 || Math.abs(p.y) > .84 || p.z < -1 || p.z > 1) return false;
}
return true;
}