Files
vibemake/js/assembly-motion.js
T

119 lines
5.6 KiB
JavaScript

// Same Three module as the import map; this math also runs in Node regression tests.
import { Vector3, Quaternion, Triangle, Box3 } from '../vendor/three/build/three.module.min.js';
import { assemblyEase as ease } from './assembly-animation.js';
const clamp = (t) => Math.max(0, Math.min(1, t));
export function screwDuration(motion) {
return motion.approachDuration + motion.engageDuration + motion.tightenDuration + motion.retractDuration;
}
// Legacy manifests did not declare the tool's working direction. Measure the
// outward normal at its declared tip once at load time, including GLB node
// transforms. This is the end-cap direction, not the long axis of an L-key.
export function inferToolTipAxis(template, tipPosition) {
template.updateMatrixWorld(true);
const tip = new Vector3(...tipPosition);
const outward = tip.clone().sub(new Box3().setFromObject(template).getCenter(new Vector3()));
const triangle = new Triangle(), nearest = new Vector3(), normal = new Vector3();
let best = Infinity, axis;
template.traverse((mesh) => {
if (!mesh.isMesh) return;
const positions = mesh.geometry.getAttribute('position'), indices = mesh.geometry.index;
if (!positions) return;
const count = indices?.count ?? positions.count;
for (let i = 0; i + 2 < count; i += 3) {
[triangle.a, triangle.b, triangle.c].forEach((v, j) => v.fromBufferAttribute(positions, indices ? indices.getX(i + j) : i + j).applyMatrix4(mesh.matrixWorld));
triangle.getNormal(normal);
if (normal.lengthSq() < 0.5) continue;
triangle.closestPointToPoint(tip, nearest);
const distance = nearest.distanceToSquared(tip);
if (distance >= best) continue;
best = distance;
axis = normal.clone();
if (axis.dot(outward) < 0) axis.negate();
}
});
return axis?.toArray();
}
export function alignedToolQuaternion(motion, toolConfig) {
const rotation = new Quaternion(...motion.toolQuaternion);
if (toolConfig.tipAxis) {
const current = new Vector3(...toolConfig.tipAxis).applyQuaternion(rotation).normalize();
const inward = new Vector3(...motion.axis).normalize();
rotation.premultiply(new Quaternion().setFromUnitVectors(current, inward)).normalize();
}
return rotation;
}
// Coordinates: axis/socket are in the GLB part's local frame, installOffset is
// in its parent's frame. Clockwise is viewed from the tool looking into the hole.
// No geometry or material is modified. A child tool follows the screw rigidly.
export class ScrewTransition {
constructor(instance, part, tool, toolConfig, delay = 0) {
this.instance = instance;
this.part = part;
this.motion = part.installation;
this.tool = tool;
this.delay = delay;
this.elapsed = 0;
this.duration = delay + screwDuration(this.motion);
this.target = new Vector3(...part.position);
this.rotation = new Quaternion(...part.quaternion);
this.axis = new Vector3(...this.motion.axis).normalize();
this.socket = new Vector3(...this.motion.socketPosition);
this.startOffset = new Vector3(...part.installOffset);
this.seatOffset = this.axis.clone().multiplyScalar(-this.motion.travel)
.multiply(new Vector3(...part.scale)).applyQuaternion(this.rotation);
this.spin = new Quaternion();
this.pivotOffset = new Vector3();
this.offset = new Vector3();
tool.quaternion.copy(alignedToolQuaternion(this.motion, toolConfig));
const toolScale = this.motion.toolScale ?? 1;
tool.scale.multiplyScalar(toolScale);
this.toolDock = this.socket.clone().sub(new Vector3(...toolConfig.tipPosition).multiplyScalar(toolScale).applyQuaternion(tool.quaternion));
instance.add(tool);
this.sample(0);
}
sample(elapsed) {
const { instance, motion: m, tool } = this;
const t = elapsed - this.delay;
this.phase = t < 0 ? 'waiting' : 'done';
instance.visible = t >= 0;
tool.visible = false;
instance.position.copy(this.target);
instance.quaternion.copy(this.rotation);
if (t < 0) return;
if (elapsed >= this.duration) return; // exact final pose, no residual rotation
const engageStart = m.approachDuration;
const tightenStart = engageStart + m.engageDuration;
const retractStart = tightenStart + m.tightenDuration;
if (t < engageStart) {
this.phase = 'approach';
this.offset.lerpVectors(this.startOffset, this.seatOffset, ease(clamp(t / engageStart)));
} else {
this.phase = t < tightenStart ? 'engage' : t < retractStart ? 'tighten' : 'retract';
const progress = ease(clamp((t - tightenStart) / m.tightenDuration));
this.offset.copy(this.seatOffset).multiplyScalar(1 - progress);
const angle = (m.direction === 'clockwise' ? 1 : -1) * Math.PI * 2 * m.turns * progress;
this.spin.setFromAxisAngle(this.axis, angle);
if (progress < 1) instance.quaternion.copy(this.rotation).multiply(this.spin);
// Rotate around the socket axis even when the GLB origin is not on it.
this.pivotOffset.copy(this.socket).applyQuaternion(this.spin).negate().add(this.socket)
.multiply(instance.scale).applyQuaternion(this.rotation);
if (progress < 1) this.offset.add(this.pivotOffset);
tool.visible = true;
let clearance = 0;
if (t < tightenStart) clearance = m.toolClearance * (1 - ease(clamp((t - engageStart) / m.engageDuration)));
if (t >= retractStart) clearance = m.toolClearance * ease(clamp((t - retractStart) / m.retractDuration));
tool.position.copy(this.toolDock).addScaledVector(this.axis, -clearance);
}
instance.position.add(this.offset);
}
update(dt) { this.elapsed += dt; this.sample(this.elapsed); }
dispose() { this.tool.removeFromParent(); }
}