194 lines
7.2 KiB
JavaScript
194 lines
7.2 KiB
JavaScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import * as THREE from "three";
|
|
import { HDRLoader } from "three/addons/loaders/HDRLoader.js";
|
|
import {
|
|
createAssemblyCameraControls,
|
|
stopAssemblyCameraMotion,
|
|
} from "../src/scene/assembly-camera.ts";
|
|
import {
|
|
placeAssemblyOnGround,
|
|
assemblyLinkPose,
|
|
} from "../src/scene/assembly-presentation.ts";
|
|
import { AssemblySceneEnvironment } from "../src/scene/assembly-scene-environment.ts";
|
|
import { createAssemblyRevealEffect } from "../src/scene/assembly-reveal-effect.ts";
|
|
|
|
const close = (actual, expected, label = "value") =>
|
|
assert.ok(
|
|
Math.abs(actual - expected) < 1e-9,
|
|
`${label}: ${actual} != ${expected}`,
|
|
);
|
|
const equalMatrix = (actual, expected) =>
|
|
actual.elements.forEach((value, index) =>
|
|
close(value, expected.elements[index], `matrix[${index}]`),
|
|
);
|
|
const createView = () => {
|
|
const camera = new THREE.PerspectiveCamera(35, 1, 0.01, 1000);
|
|
camera.position.set(2.3, -3.1, 1.65);
|
|
const controls = createAssemblyCameraControls(camera, null);
|
|
return { camera, controls };
|
|
};
|
|
|
|
test("horizontal orbit keeps Z up, a level horizon, and the model center fixed", () => {
|
|
const { camera, controls } = createView();
|
|
controls.target.set(0.4, -0.2, 0.8);
|
|
controls.update();
|
|
const target = controls.target.clone();
|
|
const height = camera.position.z;
|
|
const distance = camera.position.distanceTo(target);
|
|
controls.rotateLeft(0.8);
|
|
for (let frame = 0; frame < 120; frame++) controls.update();
|
|
close(camera.position.z, height);
|
|
close(camera.position.distanceTo(target), distance);
|
|
assert.ok(controls.target.distanceTo(target) < 1e-9);
|
|
assert.deepEqual(camera.up.toArray(), [0, 0, 1]);
|
|
const screenRight = new THREE.Vector3(1, 0, 0).applyQuaternion(
|
|
camera.quaternion,
|
|
);
|
|
close(screenRight.z, 0, "horizon roll");
|
|
});
|
|
|
|
test("vertical orbit crosses below the floor and remains finite at both poles", () => {
|
|
const { camera, controls } = createView();
|
|
controls.enableDamping = false;
|
|
controls.rotateUp(-Math.PI * 2);
|
|
assert.ok(camera.position.z < controls.target.z);
|
|
assert.ok(controls.getPolarAngle() > Math.PI / 2);
|
|
controls.rotateUp(Math.PI * 2);
|
|
assert.ok(camera.position.z > controls.target.z);
|
|
assert.ok(
|
|
[...camera.position.toArray(), ...camera.quaternion.toArray()].every(
|
|
Number.isFinite,
|
|
),
|
|
);
|
|
});
|
|
|
|
test("stopping orbit inertia prevents drift after a fit or axis view change", () => {
|
|
const { camera, controls } = createView();
|
|
controls.rotateLeft(0.8);
|
|
controls.rotateUp(0.2);
|
|
const beforeStop = camera.position.clone();
|
|
stopAssemblyCameraMotion(controls);
|
|
assert.ok(camera.position.distanceTo(beforeStop) < 1e-9);
|
|
camera.position.set(-2, 3, 2);
|
|
controls.target.set(0.2, 0.1, 0.5);
|
|
controls.update();
|
|
const frame = camera.position.clone();
|
|
const target = controls.target.clone();
|
|
for (let index = 0; index < 120; index++) controls.update();
|
|
assert.ok(camera.position.distanceTo(frame) < 1e-9);
|
|
assert.ok(controls.target.distanceTo(target) < 1e-9);
|
|
assert.equal(controls.enableDamping, true);
|
|
});
|
|
|
|
const createModel = () => {
|
|
const display = new THREE.Group();
|
|
const root = new THREE.Group();
|
|
const joint = new THREE.Group();
|
|
const link = new THREE.Mesh(
|
|
new THREE.BoxGeometry(1, 2, 3),
|
|
new THREE.MeshStandardMaterial(),
|
|
);
|
|
display.add(root);
|
|
root.add(joint);
|
|
joint.add(link);
|
|
joint.position.set(0.1, 0.2, -2);
|
|
link.position.set(0.3, -0.1, 0.4);
|
|
return { display, root, joint, link };
|
|
};
|
|
const disposeModel = (link) => {
|
|
link.geometry.dispose();
|
|
link.material.dispose();
|
|
};
|
|
|
|
test("grounding moves only the display container and is stable when repeated", () => {
|
|
const { display, root, joint, link } = createModel();
|
|
display.updateWorldMatrix(true, true);
|
|
const transforms = [root, joint, link].map((object) => object.matrix.clone());
|
|
const bounds = placeAssemblyOnGround(display);
|
|
close(bounds.min.z, 0);
|
|
assert.ok(display.position.z > 0);
|
|
[root, joint, link].forEach((object, index) =>
|
|
equalMatrix(object.matrix, transforms[index]),
|
|
);
|
|
const height = display.position.z;
|
|
close(placeAssemblyOnGround(display).min.z, 0);
|
|
close(display.position.z, height);
|
|
disposeModel(link);
|
|
});
|
|
|
|
test("saved assembly poses ignore display grounding while retaining joint motion", () => {
|
|
const { display, root, joint, link } = createModel();
|
|
display.updateWorldMatrix(true, true);
|
|
const before = assemblyLinkPose(link, root.matrixWorld.clone().invert());
|
|
placeAssemblyOnGround(display);
|
|
const grounded = assemblyLinkPose(link, root.matrixWorld.clone().invert());
|
|
grounded.xyz.forEach((value, index) => close(value, before.xyz[index]));
|
|
grounded.rpy.forEach((value, index) => close(value, before.rpy[index]));
|
|
joint.rotation.z = 0.7;
|
|
display.updateWorldMatrix(true, true);
|
|
const rotated = assemblyLinkPose(link, root.matrixWorld.clone().invert());
|
|
const local = new THREE.Matrix4()
|
|
.makeRotationFromEuler(new THREE.Euler(...rotated.rpy, "ZYX"))
|
|
.setPosition(...rotated.xyz);
|
|
equalMatrix(local, joint.matrix.clone().multiply(link.matrix));
|
|
assert.notDeepEqual(rotated.xyz, before.xyz);
|
|
disposeModel(link);
|
|
});
|
|
|
|
test("scan outlines match meshes under a grounded display container and after joint rotation", () => {
|
|
const { display, root, joint, link } = createModel();
|
|
placeAssemblyOnGround(display);
|
|
const material = link.material;
|
|
const effect = createAssemblyRevealEffect(root);
|
|
const wires = display.getObjectByName("assembly-reveal-wireframe");
|
|
const wire = wires.children[0];
|
|
display.updateWorldMatrix(true, true);
|
|
equalMatrix(wire.matrixWorld, link.matrixWorld);
|
|
joint.rotation.y = 0.3;
|
|
effect.syncTransforms();
|
|
display.updateWorldMatrix(true, true);
|
|
equalMatrix(wire.matrixWorld, link.matrixWorld);
|
|
effect.dispose();
|
|
assert.equal(link.material, material);
|
|
assert.equal(wires.parent, null);
|
|
disposeModel(link);
|
|
});
|
|
|
|
test("the studio is initialized before HDR and models; loading keeps its floor height and objects", (t) => {
|
|
// Leave HDR pending to inspect the very first frame without WebGL or network.
|
|
t.mock.method(HDRLoader.prototype, "loadAsync", () => new Promise(() => {}));
|
|
const scene = new THREE.Scene();
|
|
const camera = new THREE.PerspectiveCamera();
|
|
const renderer = { shadowMap: {}, setClearColor() {} };
|
|
const environment = new AssemblySceneEnvironment(
|
|
scene,
|
|
renderer,
|
|
camera,
|
|
"light",
|
|
);
|
|
const floor = scene.getObjectByName("assembly-studio-ground");
|
|
const spot = scene.getObjectByName("assembly-studio-key-light");
|
|
const fog = scene.fog;
|
|
assert.ok(floor.scale.x >= 15 && floor.scale.y >= 15);
|
|
assert.ok(spot.position.length() > 0 && spot.intensity > 1);
|
|
assert.ok(fog.near > 0 && fog.far > fog.near);
|
|
close(floor.position.z, -0.003);
|
|
environment.fitToBounds(
|
|
new THREE.Box3(
|
|
new THREE.Vector3(-0.5, -0.5, 0),
|
|
new THREE.Vector3(0.5, 0.5, 1),
|
|
),
|
|
);
|
|
assert.equal(scene.getObjectByName(floor.name), floor);
|
|
assert.equal(scene.fog, fog);
|
|
close(floor.position.z, -0.003);
|
|
environment.fitToBounds(
|
|
new THREE.Box3(new THREE.Vector3(-2, -2, -4), new THREE.Vector3(2, 2, 8)),
|
|
);
|
|
close(floor.position.z, -0.003);
|
|
environment.dispose();
|
|
assert.equal(scene.getObjectByName(floor.name), undefined);
|
|
assert.equal(scene.fog, null);
|
|
});
|