Files
gods-eye-view/src/globe/navara/createNavaraEngine.test.mjs
T
German Khokhlov 90c8e7763a feat(globe): implement the engine on Navara
src/globe/navara builds the GlobeEngine on a Navara 0.2.1 ThreeView. The
engine keeps the camera pose in src/geo values and pushes it to Navara's
setCamera in degrees (Cesium's heading/pitch/roll convention, S4);
whatever Navara moves itself (user input, its flyTo arc) is read back
from the drawn camera each frame. Projection, picks against the
ellipsoid and the view rectangle are geo math on that pose, with
Navara's fov kept on Cesium's 60° across the wider side.

- flights: Navara flyTo in ms with mapped easings, cancelFlight via an
  empty setCamera, user input cancels a flight as in Cesium
- events: moveStart/moveEnd (500 ms still)/changed from the pose
- render: forceUpdate and animation (render on demand, S5)
- surface: S2 CPU ray over 3D-tile meshes, Navara terrain fallback
- maps: OSM/Esri raster, Re:Earth or ion quantized-mesh terrain,
  Google 3D tiles with a browser key and Google's logo
- input: DOM pointer events with ScreenSpaceEventHandler semantics
- the three private Navara members used live in internals.js, pinned
  to 0.2.1 by a test

index.js builds the product view (MSAA 4, native pixel ratio, preserved
drawing buffer, effects=minimal) for a dynamic import. The contract
QA script gains --engine=navara on a dev-only harness page: 46/46
contract and 4/4 Navara checks; Cesium stays 53/53.
2026-10-15 19:03:58 +04:00

206 lines
7.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import test from 'node:test';
import assert from 'node:assert/strict';
import * as geo from '../../geo/index.js';
import { assertGlobeEngine } from '../contract.js';
import { cesiumFovy, orientationVectors } from './cameraMath.js';
import { createNavaraEngine } from './createNavaraEngine.js';
const { cartesian2, cartesian3, math } = geo;
// The parts of a Navara ThreeView the engine uses.
function fakeView() {
const handlers = new Map();
const canvas = new EventTarget();
Object.assign(canvas, {
clientWidth: 1280,
clientHeight: 800,
getBoundingClientRect: () => ({ left: 10, top: 20 }),
parentElement: { id: 'container' },
});
const position = cartesian3.fromDegrees(0, 0, 1e7);
const { direction, up } = orientationVectors(position, 0, -math.PI_OVER_TWO, 0);
// three's camera.matrixWorld: columns right, up, back (−direction), position.
const right = cartesian3.cross(direction, up, cartesian3.create());
const elements = [
right.x, right.y, right.z, 0,
up.x, up.y, up.z, 0,
-direction.x, -direction.y, -direction.z, 0,
position.x, position.y, position.z, 1,
];
const view = {
canvas,
animation: false,
calls: [],
flights: [],
on(name, fn) {
if (!handlers.has(name)) handlers.set(name, new Set());
handlers.get(name).add(fn);
},
off(name, fn) {
handlers.get(name)?.delete(fn);
},
emit(name, ...args) {
for (const fn of [...(handlers.get(name) ?? [])]) fn(...args);
},
listenerCount: () =>
[...handlers.values()].reduce((count, set) => count + set.size, 0),
camera: {
raw: { matrixWorld: { elements }, updateMatrixWorld() {} },
fovs: [],
set fov(value) {
view.camera.fovs.push(value);
},
set options(value) {
view.calls.push(['options', value]);
},
},
setCamera(pose) {
view.calls.push(['setCamera', pose]);
},
flyTo(pose, options) {
return new Promise((resolve) => view.flights.push({ pose, options, resolve }));
},
forceUpdate() {
view.calls.push(['forceUpdate']);
},
pickDepthPosition(x, y) {
view.calls.push(['pickDepthPosition', x, y]);
return x > 100 ? { x: 1, y: 2, z: 3, isVector3: true } : null;
},
sampleTerrainHeight: () => undefined,
addSource: (desc) => ({ id: 's', desc, delete: () => true }),
addLayer: (desc) => ({ id: 'l', desc, delete() {} }),
attribution: { add() {}, remove() {} },
};
return view;
}
const hooks = {
tileRoots: () => [],
isIdle: () => true,
retainTileGeometry() {},
};
const setup = () => {
const view = fakeView();
const engine = createNavaraEngine(view, { hooks });
return { view, engine };
};
test('the engine has every contract capability and reports kind navara', () => {
const { engine, view } = setup();
assertGlobeEngine(engine);
assert.equal(engine.kind, 'navara');
assert.equal(engine.host.canvas, view.canvas);
assert.deepEqual(engine.host.size(), { width: 1280, height: 800 });
});
test("Navara's frame events drive the contract frames in order with timestamps", () => {
const { engine, view } = setup();
const order = [];
for (const name of ['preUpdate', 'preRender', 'postRender'])
engine.frames[name].addEventListener((time) => order.push([name, time]));
view.emit('preUpdate', 1);
view.emit('postUpdate', 1);
view.emit('preRender', 2);
view.emit('postRender', 3);
assert.deepEqual(order, [
['preUpdate', 1],
['preRender', 2],
['postRender', 3],
]);
});
test('render requests are forceUpdate and continuous mode is Navara animation', () => {
const { engine, view } = setup();
engine.render.request();
assert.deepEqual(view.calls.at(-1), ['forceUpdate']);
engine.render.setContinuous(true);
assert.equal(view.animation, true);
assert.equal(engine.render.isContinuous(), true);
engine.render.setContinuous(false);
assert.equal(view.animation, false);
});
test("the camera starts from Navara's drawn camera and pushes poses with setCamera", () => {
const { engine, view } = setup();
near(engine.camera.pose().cartographic.height, 1e7, 1e-3, 'height');
engine.camera.setView({ destination: cartesian3.fromDegrees(20, 10, 1e6) });
const [, pose] = view.calls.findLast(([name]) => name === 'setCamera');
near(pose.lng, 20, 1e-9, 'lng');
near(pose.pitch, -90, 1e-9, 'pitch');
});
test("Navara's fov follows Cesium's vertical fov, also after a resize", () => {
const { view, engine } = setup();
near(view.camera.fovs.at(-1), math.toDegrees(cesiumFovy(1280, 800)), 1e-12, 'fov');
view.canvas.clientWidth = 600;
view.canvas.clientHeight = 900;
view.emit('resize', 600, 900);
near(view.camera.fovs.at(-1), 60, 1e-12, 'portrait fov');
near(engine.camera.pose().fovy, Math.PI / 3, 1e-15, 'pose fovy');
});
test('cancelFlight stops Navara with an empty setCamera; user input cancels a flight', () => {
const { engine, view } = setup();
const outcome = [];
engine.camera.flyTo({
destination: cartesian3.fromDegrees(100, 0, 5e6),
duration: 5,
cancel: () => outcome.push('cancel'),
});
assert.equal(view.flights[0].options.duration, 5000);
engine.camera.cancelFlight();
assert.deepEqual(view.calls.at(-1), ['setCamera', {}]);
engine.camera.flyTo({
destination: cartesian3.fromDegrees(100, 0, 5e6),
duration: 5,
cancel: () => outcome.push('input'),
});
view.canvas.dispatchEvent(new Event('wheel'));
assert.deepEqual(outcome, ['cancel', 'input']);
assert.equal(engine.camera.flightActive(), false);
});
test('pickPosition reads Navara depth at client coordinates; pick finds no tiles', () => {
const { engine, view } = setup();
const hit = engine.picker.pickPosition(cartesian2.create(200, 30));
assert.deepEqual(view.calls.at(-1), ['pickDepthPosition', 210, 50]);
assert.deepEqual([hit.x, hit.y, hit.z], [1, 2, 3]);
assert.equal(hit.isVector3, undefined, 'a three Vector3 leaked');
assert.equal(engine.picker.pickPosition(cartesian2.create(1, 1)), undefined);
assert.equal(engine.picker.pickPositionSupported, true);
assert.equal(engine.picker.pick(cartesian2.create(1, 1)), undefined);
assert.deepEqual(engine.picker.drillPick(cartesian2.create(1, 1), 3), []);
});
test('the projector agrees with the camera', () => {
const { engine } = setup();
const pose = engine.camera.pose();
const ahead = cartesian3.add(
pose.position,
cartesian3.multiplyByScalar(pose.direction, 1000, cartesian3.create()),
cartesian3.create(),
);
const window = engine.projector.worldToWindow(ahead);
near(window.x, 640, 1e-6, 'x');
near(window.y, 400, 1e-6, 'y');
assert.equal(engine.projector.horizonVisible(cartesian3.fromDegrees(0, 0)), true);
});
test('destroy removes its Navara listeners, keeps the view and is idempotent', () => {
const { engine, view } = setup();
engine.frames.postRender.addEventListener(() => {});
engine.input.on('click', () => {});
engine.destroy();
engine.destroy();
assert.equal(view.listenerCount(), 0);
});
function near(actual, expected, tolerance, label) {
assert.ok(
Math.abs(actual - expected) <= tolerance,
`${label}: ${actual} is not within ${tolerance} of ${expected}`,
);
}