Files
Spellcast3d/code/tests/web/viewer3d.test.mjs

454 lines
25 KiB
JavaScript

// The 3D viewer's lights in "All" without a GPU (viewer3d.js; three.js objects need no renderer): six point lights
// start evenly spread on a sphere round the model and go round it on great circles at right angles to each other
// (not all round the equator), at a distance tied to the model's size; they never bunch up, pass near every side of
// the model in a turn, are the same for a seed, and their glows are never picked by a ray. Then the viewer itself, on
// models already in its cache: a model shown in another's place keeps the camera, and a model that has left the
// screen leaves the graphics card a little later.
// node --test code/tests/web/viewer3d.test.mjs
import assert from 'node:assert/strict';
import { registerHooks } from 'node:module';
import { after, test } from 'node:test';
// The page's import map (index.html), for Node.
const STATIC = new URL('../../spellcast/static/', import.meta.url);
registerHooks({
resolve(specifier, context, next) {
if (specifier === 'three') return { url: new URL('vendor/three/build/three.module.js', STATIC).href, shortCircuit: true };
if (specifier.startsWith('three/addons/')) {
return { url: new URL(`vendor/three/examples/jsm/${specifier.slice('three/addons/'.length)}`, STATIC).href, shortCircuit: true };
}
return next(specifier, context);
},
});
const THREE = await import('three');
const { LENS_REACH, LensCamera, OrbitLights, orbitPaths, orbitPlace, surfaceCloud } = await import(new URL('js/viewer3d.js', STATIC));
const { depthRow, lineOfSight } = await import(new URL('js/paint3d.js', STATIC));
const dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
const cross = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
const length = (v) => Math.hypot(...v);
const degrees = (a, b) => (Math.acos(Math.max(-1, Math.min(1, dot(a, b) / (length(a) * length(b))))) * 180) / Math.PI;
const near = (a, b, eps = 1e-9) => Math.abs(a - b) < eps;
const STEPS = 720; // places looked at in one turn
const turn = (fn) => { for (let s = 0; s < STEPS; s++) fn((s / STEPS) * Math.PI * 2); };
test('the lights start evenly spread: the six corners of an octahedron, each a quarter turn from its four neighbours', () => {
const paths = orbitPaths();
assert.equal(paths.length, 6);
for (const { start } of paths) {
assert.ok(near(length(start), 1));
const angles = paths.map((p) => degrees(start, p.start)).sort((a, b) => a - b);
assert.deepEqual(angles.map((a) => Math.round(a)), [0, 90, 90, 90, 90, 180]);
}
});
test('each goes round a great circle: always as far from the centre, back where it began after a turn', () => {
for (const path of orbitPaths()) {
assert.ok(near(length(path.travel), 1) && near(dot(path.start, path.travel), 0), 'it sets off at right angles');
turn((a) => assert.ok(near(length(orbitPlace(path, a)), 1)));
orbitPlace(path, Math.PI * 2).forEach((v, i) => assert.ok(near(v, path.start[i])));
}
});
test('three circles at right angles to each other, none the equator: one goes over the top and under the bottom', () => {
const axes = orbitPaths().map(({ start, travel }) => cross(start, travel));
const circles = axes.filter((a, i) => axes.findIndex((b) => near(Math.abs(dot(a, b)), 1)) === i);
assert.equal(circles.length, 3, 'two lights to a circle');
for (let i = 0; i < 3; i++) for (let j = i + 1; j < 3; j++) assert.ok(near(dot(circles[i], circles[j]), 0));
for (const axis of circles) assert.ok(Math.abs(axis[1]) < 0.75, `a circle leans ${degrees(axis, [0, 1, 0]).toFixed(0)} degrees`);
let top = -1;
let bottom = 1;
for (const path of orbitPaths()) turn((a) => { const y = orbitPlace(path, a)[1]; top = Math.max(top, y); bottom = Math.min(bottom, y); });
assert.ok(top > 0.999 && bottom < -0.999);
});
test('they never bunch up: no two lights closer than 60 degrees at any moment of a turn', () => {
for (const seed of [1, 2, 3, 42]) {
const paths = orbitPaths(seed);
let closest = 180;
turn((a) => {
const places = paths.map((p) => orbitPlace(p, a));
for (let i = 0; i < places.length; i++) for (let j = i + 1; j < places.length; j++) closest = Math.min(closest, degrees(places[i], places[j]));
});
assert.ok(closest > 59.9, `seed ${seed}: ${closest.toFixed(1)} degrees`);
}
});
test('in a turn a light passes within 36 degrees of every side of the model, its top and bottom too', () => {
const paths = orbitPaths();
const sides = Array.from({ length: 400 }, (_, i) => { // a Fibonacci sphere of directions
const y = 1 - (2 * i + 1) / 400;
const r = Math.sqrt(1 - y * y);
const phi = i * Math.PI * (3 - Math.sqrt(5));
return [Math.cos(phi) * r, y, Math.sin(phi) * r];
});
const nearest = sides.map(() => 180);
turn((a) => {
const places = paths.map((p) => orbitPlace(p, a));
sides.forEach((side, k) => { for (const place of places) nearest[k] = Math.min(nearest[k], degrees(side, place)); });
});
assert.ok(Math.max(...nearest) < 36, `${Math.max(...nearest).toFixed(1)} degrees`);
});
test('the same seed gives the same paths, other seeds other ones', () => {
assert.deepEqual(orbitPaths(7), orbitPaths(7));
assert.deepEqual(orbitPaths(), orbitPaths());
assert.notDeepEqual(orbitPaths(1), orbitPaths(2));
});
/** A scene with a box of `size` at `at`, and the lights fitted round it. */
function boxScene(size, at = [0, 0, 0]) {
const scene = new THREE.Scene();
const box = new THREE.Mesh(new THREE.BoxGeometry(size, size, size), new THREE.MeshStandardMaterial());
box.position.fromArray(at);
scene.add(box);
const lights = new OrbitLights(scene);
lights.fit(box);
return { scene, box, lights, centre: new THREE.Vector3().fromArray(at) };
}
test('the lights go round the model at the same multiple of its radius, whatever its size and place', () => {
const ratios = [];
for (const [size, at] of [[2, [0, 0, 0]], [40, [5, -3, 12]]]) {
const { lights, centre } = boxScene(size, at);
const radius = (size * Math.sqrt(3)) / 2; // half the box's diagonal
for (let step = 0; step < 50; step++) {
for (const light of lights.lights) ratios.push(light.position.distanceTo(centre) / radius);
lights.turn(0.37);
}
}
assert.ok(ratios.every((r) => near(r, ratios[0], 1e-6)), 'one distance for every light, size and moment');
assert.ok(ratios[0] > 1.2, 'outside the model');
});
test('dark and without glows until lit; lit, each fades as a lamp does within its reach, the two of a pair unlike, every model lit alike', () => {
const { lights } = boxScene(2);
assert.equal(lights.lights.length, 6);
assert.ok(lights.lights.every((l) => l.isPointLight && l.intensity === 0 && l.decay === 2 && !l.castShadow));
assert.ok(lights.glows.every((g) => !g.visible));
lights.light(true);
const radius = Math.sqrt(3); // half the box's diagonal
const strong = lights.lights[0].intensity;
// Opposite lights of one strength would cancel each other's shading out: one of each pair is twice the other.
assert.deepEqual(lights.lights.map((l) => Math.round((l.intensity / strong) * 100) / 100), [1, 0.5, 1, 0.5, 1, 0.5]);
for (const k of [0, 2, 4]) {
assert.ok(lights.lights[k].position.clone().add(lights.lights[k + 1].position).length() < 1e-9, 'a pair: opposite each other');
}
// A light reaches past the model's centre, not across the whole model: the far side is the others'.
const reach = lights.lights[0].distance;
const away = lights.lights[0].position.length();
assert.ok(lights.lights.every((l) => l.distance === reach) && reach > away && reach < away + radius);
assert.ok(lights.glows.every((g) => g.visible));
// A model ten times the size: lit as brightly at the same share of its size (a lamp's light falls off by the
// distance squared), the lights' reach ten times as far.
const big = boxScene(20).lights;
big.light(true);
assert.ok(near(big.lights[0].intensity / strong, 100, 1e-6) && near(big.lights[0].distance / reach, 10, 1e-9));
lights.light(false);
assert.ok(lights.lights.every((l) => l.intensity === 0) && lights.glows.every((g) => !g.visible));
// Fitted to another model while lit, they keep shining, by that model's size.
big.fit(boxScene(2).box);
assert.ok(near(big.lights[0].intensity, strong, 1e-9) && near(big.lights[0].distance, reach, 1e-9));
});
test('a ray through a glow finds the model, never the glow; a glow near the camera stays a dot', () => {
const { scene, box, lights } = boxScene(2);
lights.light(true);
const camera = new THREE.PerspectiveCamera(32, 1, 0.01, 100);
camera.position.set(0, 0, 8);
camera.lookAt(0, 0, 0);
camera.updateMatrixWorld();
scene.updateMatrixWorld(true);
const ray = new THREE.Raycaster();
ray.camera = camera;
for (const light of lights.lights) {
ray.set(camera.position, light.position.clone().sub(camera.position).normalize());
for (const hit of ray.intersectObject(scene, true)) assert.equal(hit.object, box);
}
lights.faceCamera(camera);
const far = lights.glows.map((g) => g.scale.x);
assert.ok(far.every((s) => s > 0 && near(s, far[0])), 'far off, all one size, tied to the model');
camera.position.copy(lights.lights[0].position).add(new THREE.Vector3(0, 0, 0.01));
lights.faceCamera(camera);
assert.ok(lights.glows[0].scale.x < far[0] / 10, 'close by, a dot');
});
// ---- the shared viewer itself, with stand-ins for what needs a graphics card -----------------------------------
const { viewer } = await import(new URL('js/viewer3d.js', STATIC));
/** The viewer with a camera and controls but no renderer, and `urls` (two: all its cache keeps) in it as models. */
function rig(urls) {
Object.assign(viewer, {
renderer: {}, root: new THREE.Group(), orbit: new OrbitLights(new THREE.Scene()), current: null, place: null,
scene: { userData: { key: {}, rim: {}, ground: [{}, {}], ambient: {}, fill: {} } },
camera: new THREE.PerspectiveCamera(32, 1, 0.01, 1000),
controls: { target: new THREE.Vector3(), enabled: true, update() {} },
});
viewer.cache.clear();
forgetTimers();
viewer.offloaded = [];
for (const [i, url] of urls.entries()) {
const box = new THREE.Mesh(new THREE.BoxGeometry(2, 2, 2), new THREE.MeshStandardMaterial());
box.geometry.translate(3 + i, 1, 0); // (frame() moves each to the origin: a place of its own)
const pivot = new THREE.Group().add(box);
pivot.userData.views = { setExtraMaps() {}, apply() {}, setWire() {}, dispose() {}, edges: null,
offload: () => viewer.offloaded.push(url) };
viewer.cache.set(url, pivot);
}
}
/** The timers that would take the rig's models off the card: dropped (a test's own clock, or none, runs them). */
function forgetTimers() {
for (const timer of viewer.leaving.values()) clearTimeout(timer);
viewer.leaving.clear();
}
after(forgetTimers);
const modelAt = () => viewer.current.children[0].position.toArray();
test('a model shown with keepView stands where the last one stood and the camera stays - the one on screen staying there until it is loaded', async () => {
rig(['a.glb', 'b.glb']);
assert.equal(await viewer.show('a.glb', 'glb'), true);
const place = modelAt();
viewer.camera.position.set(1, 2, 3); // the artist goes round it
const camera = viewer.camera.position.toArray();
assert.equal(await viewer.show('b.glb', 'glb', { keepView: true }), true);
assert.deepEqual([modelAt(), viewer.camera.position.toArray()], [place, camera]);
// A model not in the cache is asked for, and another before it is there: a job's end does this (the old result
// under its new address, then the new result). The model on screen stays there meanwhile.
const superseded = viewer.show('not-loaded.glb', 'glb', { keepView: true });
assert.equal(viewer.current, viewer.cache.get('b.glb'), 'the model on screen stays while the next one loads');
assert.equal(await viewer.show('a.glb', 'glb', { keepView: true }), true);
assert.deepEqual([modelAt(), viewer.camera.position.toArray()], [place, camera]);
assert.equal(await superseded, false);
// Another object's model (no keepView) takes the one on screen off while it loads; a model shown with keepView
// after it still stands where that one stood.
const other = viewer.show('not-loaded.glb', 'glb');
assert.equal(viewer.current, null, 'nothing on screen while it loads');
assert.equal(await viewer.show('b.glb', 'glb', { keepView: true }), true);
assert.deepEqual([modelAt(), viewer.camera.position.toArray()], [place, camera]);
assert.equal(await other, false);
// Without keepView a model is framed anew.
assert.equal(await viewer.show('a.glb', 'glb'), true);
assert.notDeepEqual(viewer.camera.position.toArray(), camera);
});
test('a model that has left the screen leaves the graphics card five seconds later, unless it is back by then', async (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] });
rig(['a.glb', 'b.glb']);
viewer.container = {}; // the viewer is in a pane
await viewer.show('a.glb', 'glb');
await viewer.show('b.glb', 'glb', { keepView: true }); // a.glb leaves the screen ...
t.mock.timers.tick(4999);
await viewer.show('a.glb', 'glb', { keepView: true }); // ... and is back in time (two models compared); b.glb left
t.mock.timers.tick(4999);
assert.deepEqual(viewer.offloaded, [], 'a model back in time keeps its pictures on the card');
t.mock.timers.tick(1);
assert.deepEqual(viewer.offloaded, ['b.glb'], 'the one that stayed away is taken off');
t.mock.timers.tick(60_000);
assert.deepEqual(viewer.offloaded, ['b.glb'], 'the model on screen stays on the card');
// The viewer in no pane (another stage without a model, a job at work): the model it showed leaves the card too.
viewer.container = null;
viewer.offloadLater();
t.mock.timers.tick(4999);
assert.equal(viewer.offloaded.includes('a.glb'), false);
t.mock.timers.tick(1);
assert.equal(viewer.offloaded.includes('a.glb'), true);
// Back in a pane, on screen again before the time is up: it stays.
viewer.offloaded.length = 0;
viewer.offloadLater();
viewer.container = {};
await viewer.show('a.glb', 'glb', { keepView: true });
t.mock.timers.tick(5000);
assert.equal(viewer.offloaded.includes('a.glb'), false);
viewer.container = null;
});
test('the camera\'s lens: its perspective turned down and inside out about the point orbited, what lies there keeping its place', () => {
const camera = new LensCamera(32, 1.5, 0.1, 100);
camera.position.set(0, 0, 4); // looks down -z at the origin, the point it turns about
camera.updateMatrixWorld();
const half = Math.tan(THREE.MathUtils.degToRad(16));
const at = (x, y, z) => new THREE.Vector3(x, y, z).project(camera);
const close = (a, b) => near(a, b, 1e-6);
assert.deepEqual(camera.lens(), { tan: half, wide: 0, far: 100 });
camera.zoom = 2;
assert.ok(close(camera.lens().tan, half / 2), 'zoomed in twice, it spans half the angle');
camera.zoom = 1;
const usual = at(1, 0.5, 0);
const usualBehind = at(1, 0.5, -1);
// None: what lies at the origin's depth stays where it was, and depth no longer moves anything.
camera.perspective = 0;
camera.updateMatrixWorld();
assert.ok(close(camera.lens().tan, 0) && close(camera.lens().wide, half * 4));
assert.ok(close(at(1, 0.5, 0).x, usual.x) && close(at(1, 0.5, 0).y, usual.y));
assert.ok(close(at(1, 0.5, -1).x, at(1, 0.5, 1).x));
// Inverse: the farther, the larger; nothing shows deeper than LENS_REACH of the way to where the lines meet.
camera.perspective = -1;
camera.updateMatrixWorld();
const { tan, wide, far } = camera.lens();
assert.ok(close(tan, -half) && close(wide, half * 8) && close(far, LENS_REACH * 8));
assert.ok(close(at(1, 0.5, 0).x, usual.x) && close(at(1, 0.5, 0).y, usual.y));
assert.ok(at(1, 0.5, -1).x > at(1, 0.5, 0).x && at(1, 0.5, 0).x > at(1, 0.5, 1).x);
const behind = at(1, 0.5, -1); // depth 5: half the picture's height there is wide + tan * 5
assert.ok(close(behind.x, 1 / (1.5 * (wide + tan * 5))) && close(behind.y, 0.5 / (wide + tan * 5)));
assert.ok(close(at(0, 0, 4 - 0.1).z, -1) && close(at(0, 0, 4 - far).z, 1) && at(0, 0, 1).z < at(0, 0, 0).z);
// A place in the picture is one straight line of sight, and they all meet at depth wide / -tan = 8.
const from = new THREE.Vector3(0.5, -0.5, -1).unproject(camera);
const along = new THREE.Vector3(0.5, -0.5, 1).unproject(camera).sub(from);
const meet = from.clone().addScaledVector(along, (4 - 8 - from.z) / along.z);
assert.ok(close(meet.x, 0) && close(meet.y, 0));
assert.ok(close(new THREE.Vector3(0.5, -0.5, 0).unproject(camera).sub(from).normalize().dot(along.clone().normalize()), 1));
// The viewer sets it, and sets it back.
rig(['a.glb', 'b.glb']);
viewer.camera = camera;
viewer.dirty = false;
viewer.setPerspective(1);
assert.ok(viewer.dirty && close(at(1, 0.5, -1).x, usualBehind.x) && close(at(1, 0.5, 0).x, usual.x));
});
test('an inverse lens is held back for what its picture holds: its lines of sight meet behind the deepest part of it', () => {
const camera = new LensCamera(32, 1.5, 0.1, 100);
camera.position.set(0, 0, 4); // 4 from the point it turns about
camera.updateMatrixWorld();
const half = Math.tan(THREE.MathUtils.degToRad(16));
const asked = [];
let deep = 1; // the model reaches this far behind that point
camera.deepest = (halfHeight) => { asked.push(halfHeight); return deep; };
camera.perspective = 0;
assert.ok(near(camera.lens().wide, half * 4) && !asked.length, 'no lines of sight meet: nothing is asked');
// As far inside out as asked, while that part stays within the lens's reach.
camera.perspective = -1;
assert.ok(near(camera.lens().tan, -half) && near(asked.at(-1), half * 4), 'asked with half the height of the picture at that point');
// Asked further: held where the part lies at the far end of what is drawn - so none of it is cut off.
camera.perspective = -7;
const held = camera.lens();
assert.ok(near(held.far, 4 + 1) && held.tan > -7 * half && held.tan < -half);
assert.ok(near(held.wide + held.tan * 4, half * 4), 'what lies at the point it turns about keeps its size');
camera.updateMatrixWorld();
assert.ok(near(new THREE.Vector3(0, 0, -1).project(camera).z, 1, 1e-6), 'that part is the last thing drawn');
// The deeper the model reaches, the less the lens bends; nothing of it behind that point: as asked.
deep = 3;
const less = camera.lens();
assert.ok(less.tan > held.tan && less.tan < 0 && near(less.far, 4 + 3));
deep = 0;
assert.ok(near(camera.lens().tan, -7 * half));
});
test('the surface of a model as a cloud of points: every part of it near one of them, a few for many triangles, in its own space', () => {
const model = new THREE.Group();
const box = new THREE.Mesh(new THREE.BoxGeometry(2, 1, 0.5)); // (twelve triangles, each far longer than a cell)
box.position.set(0.5, 0, 0);
box.rotation.z = 0.3;
model.add(box);
model.position.set(10, -3, 2); // where the model stands and how large does not count
model.scale.setScalar(2);
const { points, pad } = surfaceCloud(model, 16);
box.updateMatrix();
const toBox = box.matrix.clone().invert();
const cloud = [];
for (let i = 0; i < points.length; i += 3) cloud.push(new THREE.Vector3(points[i], points[i + 1], points[i + 2]));
assert.ok(cloud.length > 100 && cloud.length < 16 ** 3 / 2);
assert.ok(pad > 0 && pad < 0.4);
// Every point of the cloud lies near the box's surface ...
const sides = new THREE.Vector3(1, 0.5, 0.25);
for (const p of cloud) {
const q = p.clone().applyMatrix4(toBox);
const outside = new THREE.Vector3(Math.max(Math.abs(q.x) - sides.x, 0), Math.max(Math.abs(q.y) - sides.y, 0), Math.max(Math.abs(q.z) - sides.z, 0)).length();
const inside = Math.min(sides.x - Math.abs(q.x), sides.y - Math.abs(q.y), sides.z - Math.abs(q.z));
assert.ok(Math.max(outside, inside) <= pad, 'a point off the surface');
}
// ... and every place on the surface near a point of the cloud: the middles of its faces too, far from any corner.
for (let u = -1; u <= 1; u += 0.25) {
for (let v = -1; v <= 1; v += 0.25) {
for (const at of [[u, v * 0.5, 0.25], [u, v * 0.5, -0.25], [u, 0.5, v * 0.25], [1, u * 0.5, v * 0.25]]) {
const place = new THREE.Vector3(...at).applyMatrix4(box.matrix);
assert.ok(Math.min(...cloud.map((p) => p.distanceTo(place))) <= pad, 'a place on the surface far from every point');
}
}
}
// A fine mesh: far fewer points than it has corners.
const ball = new THREE.Mesh(new THREE.SphereGeometry(1, 96, 64));
assert.ok(surfaceCloud(ball, 16).points.length / 3 < ball.geometry.getAttribute('position').count / 4);
assert.deepEqual(surfaceCloud(new THREE.Group()), { points: new Float32Array(0), pad: 0 });
});
test('how deep the model reaches behind the point orbited: what the picture holds of it, a part beside the picture less and less', () => {
const camera = new LensCamera(32, 1, 0.1, 100);
camera.position.set(0, 0, 4);
const model = new THREE.Group().add(new THREE.Mesh(new THREE.BoxGeometry(0.2, 0.2, 0.2)));
const v = Object.assign(Object.create(Object.getPrototypeOf(viewer)), { camera, current: new THREE.Group().add(model) });
const halfHeight = Math.tan(THREE.MathUtils.degToRad(16)) * 4;
const deepAt = (x, y, z) => {
model.position.set(x, y, z);
v.current.updateMatrixWorld(true);
camera.updateMatrixWorld();
return v.deepBehind(halfHeight);
};
const { pad } = surfaceCloud(model);
const behind = deepAt(0, 0, -1); // its far side 1.1 behind the point the camera turns about
assert.ok(behind >= 1.1 && behind <= 1.1 + 2 * pad, `${behind}`);
assert.equal(deepAt(0, 0, 1), 0, 'in front of that point: nothing lies behind it');
assert.ok(near(deepAt(0.5, -0.5, -1), behind, 1e-6), 'anywhere in the picture: the same');
// Beside the picture: less and less, and nothing from a quarter of the picture's half size out.
const edge = deepAt(halfHeight * 1.125 + 0.1 + pad, 0, -1);
assert.ok(edge > behind * 0.4 && edge < behind * 0.6, `${edge}`);
assert.equal(deepAt(halfHeight * 1.25 + 0.1 + pad + 0.01, 0, -1), 0);
assert.equal(deepAt(0, -(halfHeight * 1.25 + 0.1 + pad + 0.01), -1), 0);
// The camera holds its lens back by it.
camera.deepest = (h) => v.deepBehind(h);
camera.perspective = -7;
deepAt(0, 0, -1);
assert.ok(near(camera.lens().far, 4 + behind, 1e-6));
assert.equal(Object.getPrototypeOf(viewer).deepBehind.call({ current: null }, halfHeight), 0, 'no model: nothing');
});
test('the painter reads the camera through its lens: a pointer\'s line of sight, and how deep a point lies', () => {
const camera = new LensCamera(32, 1.5, 0.1, 100);
camera.position.set(1, 2, 4);
camera.lookAt(0, 0, 0); // the point it turns about
const close = (a, b) => near(a, b, 1e-6);
for (const perspective of [1, 0, -2]) {
camera.perspective = perspective;
camera.updateMatrixWorld();
const forward = camera.getWorldDirection(new THREE.Vector3());
const deep = (point) => point.clone().sub(camera.position).dot(forward);
for (const point of [new THREE.Vector3(0.4, -0.3, 0.2), new THREE.Vector3(-0.6, 0.5, -0.4)]) {
assert.ok(close(depthRow(camera).dot(new THREE.Vector4(point.x, point.y, point.z, 1)), deep(point)), perspective);
const seen = point.clone().project(camera);
const { origin, direction, far } = lineOfSight(camera, seen.x, seen.y);
const to = point.clone().sub(origin);
assert.ok(close(direction.length(), 1) && close(to.clone().cross(direction).length(), 0), 'it runs through the point');
assert.ok(close(deep(origin), camera.near) && to.dot(direction) > 0 && to.dot(direction) < far);
}
// It ends where the camera stops seeing: its far plane, or an inverse lens's reach before that.
const { origin, direction, far } = lineOfSight(camera, 0.3, -0.2);
assert.ok(close(deep(origin.addScaledVector(direction, far)), camera.lens().far), perspective);
}
assert.ok(camera.lens().far < 10, 'the inverse lens sees less deep than the camera\'s far plane');
});
test('"All" in Export is lit by two lights shining down that hold still; elsewhere by the lights going round', () => {
const lights = { key: {}, rim: {}, ground: [{}, {}], ambient: {}, fill: {} };
const shown = [];
const v = Object.assign(Object.create(Object.getPrototypeOf(viewer)), {
scene: { userData: lights }, orbit: { light: (on) => shown.push(on) }, grounded: false, lit: false,
});
v.orbitLight(true);
assert.deepEqual([v.orbiting, shown.at(-1), lights.ground.map((l) => l.intensity), lights.key.intensity], [true, true, [0, 0], 0]);
v.setGroundLights(true);
assert.deepEqual([v.orbiting, shown.at(-1), lights.ground.every((l) => l.intensity > 0), lights.key.intensity], [false, false, true, 0],
'the same view, now under the lights from above');
// "All" has a white light from everywhere besides, a fifth of a surface's colour, under either kind of lights.
assert.ok(Math.abs(lights.fill.intensity / Math.PI - 0.2) < 1e-9);
v.orbitLight(false);
assert.deepEqual([lights.ground.map((l) => l.intensity), lights.key.intensity > 0, lights.fill.intensity], [[0, 0], true, 0],
'a map view: the studio');
v.orbitLight(true);
assert.ok(lights.fill.intensity > 0);
v.orbitLight(false);
v.orbitLight(true);
v.setGroundLights(false);
assert.deepEqual([v.orbiting, shown.at(-1), lights.ground.map((l) => l.intensity)], [true, true, [0, 0]]);
});