Fly the camera through the renderer's camera port

The Cesium camera port moves into render/cesium/camera.js, with the
listener list and position conversion shared from common.js. It now
covers flights (flyTo, flyToBounds, cancelFlight, flying), lookAt and
release, command hooks, and a flightEnd event for the flights it starts.

A flight's end that Cesium raises inside one of the port's own calls is
delivered once Cesium's call returns, so its callback sees the flight
let go of. A flight Cesium declines to start, mid scene-mode morph,
ends as cancelled instead of never settling.

Flight traces run in Node on the recorder's headless camera with a
virtual clock; the browser harness keeps the instant traces.

The startup fly-in, landmark and viewport flights, share-link restore
and the shell's camera state now go through the port. camera.js and
sharelink.js leave the Cesium allow-list.
This commit is contained in:
Sameh Khamis committed 2026-11-05 20:15:52 -08:00
1 parent b5900b4178
commit 46a53bb49c
20 files changed
+846 -305

No files matched your search

+80 -23
View File
@@ -5,6 +5,7 @@
import { createCameraEvents } from '../render/camera/events.js';
import { toDegrees } from '../geo/index.js';
import { bindRenderer, rendererOf } from '../render/rendererOf.js';
import { createCesiumCamera } from '../render/cesium/camera.js';
const DEFAULT_POSE = Object.freeze({
lon: 0,
@@ -64,38 +65,94 @@ export function fakeCameraPort({ pose = {}, viewRectangle = null } = {}) {
}
/**
* Bind to a fake viewer a camera port that reads its Cesium-shaped fake
* camera live (`positionCartographic`, `heading`, `pitch`, `roll`, and the
* `changed`/`moveStart`/`moveEnd` events it has), for tests whose code
* still moves that camera directly. Returns the viewer.
* Bind to a fake viewer the Cesium renderer's own camera port over its
* Cesium-shaped fake camera, for tests whose code is half on the port: the
* port's commands reach the fake camera as the renderer maps them
* (`flyTo({destination: Cartesian3, ...})`), while poses are read leniently
* from what the fake has (`positionCartographic`, `heading`, `pitch`,
* `roll`) and events come from the fake's own `changed`, `moveStart` and
* `moveEnd`. Binding a viewer twice keeps the first. Returns the viewer.
* @template {{camera: object}} V
* @param {V} viewer
* @returns {V}
*/
export function bindFakeCesiumCamera(viewer) {
if (rendererOf(viewer)) return viewer;
const camera = viewer.camera;
const port = {
pose() {
const c = camera.positionCartographic;
return {
...DEFAULT_POSE,
lon: c ? toDegrees(c.longitude) : NaN,
lat: c ? toDegrees(c.latitude) : NaN,
height: c ? c.height : NaN,
heading: camera.heading ?? 0,
pitch: camera.pitch ?? -Math.PI / 2,
roll: camera.roll ?? 0,
const fake = viewer.camera;
const noEvent = { addEventListener: () => () => {} };
// A fake that does not keep Cesium's current flight gets one kept for it,
// as Cesium does: from a flight's start until it completes or cancels.
let current = null;
const flown = (fly) =>
function (...args) {
const options = args.at(-1) ?? {};
const flight = {};
current = flight;
const settle = (cb) => () => {
if (current === flight) current = null;
cb?.();
};
args[args.length - 1] = {
...options,
complete: settle(options.complete),
cancel: settle(options.cancel),
};
return fly.apply(this, args);
};
const keepsFlight = '_currentFlight' in fake;
const camera = new Proxy(fake, {
get(target, key, receiver) {
if ((key === 'moveStart' || key === 'moveEnd') && !target[key])
return noEvent;
if (!keepsFlight) {
if (key === '_currentFlight') return current ?? undefined;
if ((key === 'flyTo' || key === 'flyToBoundingSphere') && target[key])
return flown(target[key]);
if (key === 'cancelFlight' && target[key])
return function (...args) {
current = null;
return target[key].apply(this, args);
};
}
return Reflect.get(target, key, receiver);
},
viewRectangle: () => null,
on(event, cb) {
const source = camera[event];
const off = source?.addEventListener?.(cb);
if (typeof off === 'function') return off;
return () => source?.removeEventListener?.(cb);
},
});
const scene = {
postUpdate: noEvent,
requestRender: () => viewer.scene?.requestRender?.(),
};
const real = createCesiumCamera({
scene,
camera,
report: (error) => {
throw error;
},
}).port;
const port = Object.create(real, {
pose: {
value() {
const c = fake.positionCartographic;
return {
...DEFAULT_POSE,
lon: c ? toDegrees(c.longitude) : NaN,
lat: c ? toDegrees(c.latitude) : NaN,
height: c ? c.height : NaN,
heading: fake.heading ?? 0,
pitch: fake.pitch ?? -Math.PI / 2,
roll: fake.roll ?? 0,
};
},
},
viewRectangle: { value: () => null },
on: {
value(event, cb) {
const source = fake[event];
const off = source?.addEventListener?.(cb);
if (typeof off === 'function') return off;
return () => source?.removeEventListener?.(cb);
},
},
});
bindRenderer(viewer, { camera: port });
return viewer;
}