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

+60 -61
View File
@@ -13,6 +13,7 @@ import { cancelCameraArrival } from './data/cameraArrival.js';
import { guardCameraAboveGround } from './cameraGroundGuard.js';
import { CITY_POIS } from './cityPois.js';
import { heightsFor } from './queries/heights.js';
import { cameraOf } from './render/rendererOf.js';
/** Newest landmark arrival; an older post-arrival ground guard yields to it. */
let arrivalGeneration = 0;
@@ -37,30 +38,23 @@ export const GLOBE_VIEW = Object.freeze({
* @returns {{latitude: number, longitude: number, heightM: number}}
*/
export function flyToGlobeView(viewer, options = {}) {
const carto = viewer.camera.positionCartographic;
const longitude = Cesium.Math.toDegrees(carto.longitude);
const latitude = Cesium.Math.toDegrees(carto.latitude);
viewer.camera.cancelFlight();
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(
longitude,
latitude,
GLOBE_VIEW.heightM,
),
orientation: {
heading: 0,
pitch: Cesium.Math.toRadians(GLOBE_VIEW.pitchDeg),
roll: 0,
const camera = cameraOf(viewer);
const { lon: longitude, lat: latitude } = camera.pose();
camera.cancelFlight();
camera.flyTo(
{ lon: longitude, lat: latitude, height: GLOBE_VIEW.heightM },
{
orientation: {
heading: 0,
pitch: Cesium.Math.toRadians(GLOBE_VIEW.pitchDeg),
roll: 0,
},
durationS: finitePositive(options.duration) || GLOBE_VIEW.durationS,
endFrame: 'world',
onComplete: options.onComplete,
onCancel: options.onCancel,
},
duration: finitePositive(options.duration) || GLOBE_VIEW.durationS,
endTransform: Cesium.Matrix4.IDENTITY,
// Cesium's Camera.flyTo reads `complete`/`cancel`. `onComplete`/`onCancel`
// are this module's OWN option names and are silently ignored by Cesium —
// spelling them through to flyTo meant the reset never resolved on the
// flight's own events and every caller fell back to its watchdog timeout.
complete: options.onComplete,
cancel: options.onCancel,
});
);
return { latitude, longitude, heightM: GLOBE_VIEW.heightM };
}
@@ -147,11 +141,11 @@ export function flyToLandmark(viewer, lat, lon, options = {}) {
)
: range;
const hpr = new Cesium.HeadingPitchRange(
Cesium.Math.toRadians(heading),
Cesium.Math.toRadians(pitch),
framingRange,
);
const hpr = {
heading: Cesium.Math.toRadians(heading),
pitch: Cesium.Math.toRadians(pitch),
range: framingRange,
};
if (typeof onStart === 'function') {
try {
@@ -162,14 +156,15 @@ export function flyToLandmark(viewer, lat, lon, options = {}) {
}
// Fly to target, then lock with lookAt for guaranteed centering
viewer.camera.flyToBoundingSphere(
new Cesium.BoundingSphere(targetPosition, boundingRadius),
const camera = cameraOf(viewer);
camera.flyToBounds(
{ center: targetPosition, radius: boundingRadius },
{
offset: hpr,
duration,
complete: () => {
viewer.camera.lookAt(targetPosition, hpr);
viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
durationS: duration,
onComplete: () => {
camera.lookAt(targetPosition, hpr);
camera.release();
// Framing used a predicted ground height. Once tiles stream in, measure
// the rendered surface and lift the eye if it is buried or pressed
// against it; a newer arrival or a manual gesture ends the check.
@@ -190,7 +185,7 @@ export function flyToLandmark(viewer, lat, lon, options = {}) {
}
}
},
cancel: () => {
onCancel: () => {
if (typeof onCancel === 'function') {
try {
onCancel();
@@ -788,15 +783,17 @@ function flyToViewportBounds(viewer, viewport, options = {}) {
const paddedLonSpan = metrics.lonSpanDeg + longitudePadding * 2;
const south = Math.max(-89.9, southwest.lat - latitudePadding);
const north = Math.min(89.9, northeast.lat + latitudePadding);
const rectangle =
const rect =
paddedLonSpan >= 360
? Cesium.Rectangle.fromDegrees(-180, south, 180, north)
: Cesium.Rectangle.fromDegrees(
wrapLongitude(southwest.lng - longitudePadding),
? { west: -180, south, east: 180, north }
: {
west: wrapLongitude(southwest.lng - longitudePadding),
south,
wrapLongitude(southwest.lng + metrics.lonSpanDeg + longitudePadding),
east: wrapLongitude(
southwest.lng + metrics.lonSpanDeg + longitudePadding,
),
north,
);
};
if (typeof beforeFly === 'function' && beforeFly() === false)
return CANCELLED_SEARCH;
if (typeof onStart === 'function') {
@@ -806,29 +803,31 @@ function flyToViewportBounds(viewer, viewport, options = {}) {
/* no-op */
}
}
viewer.camera.flyTo({
destination: rectangle,
duration,
endTransform: Cesium.Matrix4.IDENTITY,
complete: () => {
if (typeof onComplete === 'function') {
try {
onComplete();
} catch {
/* no-op */
cameraOf(viewer).flyTo(
{ rect },
{
durationS: duration,
endFrame: 'world',
onComplete: () => {
if (typeof onComplete === 'function') {
try {
onComplete();
} catch {
/* no-op */
}
}
}
},
cancel: () => {
if (typeof onCancel === 'function') {
try {
onCancel();
} catch {
/* no-op */
},
onCancel: () => {
if (typeof onCancel === 'function') {
try {
onCancel();
} catch {
/* no-op */
}
}
}
},
},
});
);
// Same short-way-round rule for the reported centre: averaging raw longitudes
// puts a dateline-crossing box's centre on the opposite side of the planet.
return {