Read the HUD and share-link camera through the renderer's camera port

The first camera-family slice. The camera port's events move into a
shared module (render/camera/events.js) that the stub and the Cesium
port both use: listener lists, delivery with errors reported, and the
contract's per-listener `changed` measure.

The Cesium port gains:
- camera.on: `changed` measured per listener on every tick, where Cesium
  measures its single threshold (the counts match Cesium's own at each
  threshold); moveStart and moveEnd relayed from Cesium's camera;
- camera.viewRectangle, in degrees (now in CESIUM_CHECKS);
- lon and lat in pose, and NaN where Cesium has no cartographic position.

Code that still holds the viewer reaches the renderer through
rendererOf(viewer); the application renderer binds it on create and
unbinds on destroy.

The HUD reads its telemetry from the camera port and no longer imports
Cesium (off the allow-list). The share link writes the hash from the
pose. Its restore path stays on the viewer's camera for a later slice.

Behaviour changes:
- The share link listens to `changed` at its own 0.5 threshold, not at
  whatever global threshold layers had set, and also to moveEnd, so the
  hash always catches up with where the camera comes to rest.
- The HUD's and share link's camera callbacks run from the port's single
  relay, ahead of other moveStart/moveEnd listeners, and a throw in one
  is reported to the renderer's error listeners instead of escaping from
  Cesium's render.
This commit is contained in:
Sameh Khamis committed 2026-11-05 19:48:58 -08:00
1 parent 910185bfe0
commit b5900b4178
28 files changed
+688 -243

No files matched your search

+20 -17
View File
@@ -1,5 +1,6 @@
import * as Cesium from 'cesium';
import { fromDegrees, toDegrees, toRadians } from './geo/index.js';
import { rendererOf } from './render/rendererOf.js';
import { BLOOM_INTENSITY_DEFAULT, BLOOM_SCALE_VERSION } from './bloom.js';
import {
migrateDetectionState,
@@ -69,9 +70,12 @@ export class ShareLinkManager {
isNavigationCurrent,
cancelOwnedNavigation,
initialShareCode = null,
camera = rendererOf(viewer)?.camera,
} = {},
) {
if (!camera) throw new Error('ShareLinkManager needs a camera port');
this.viewer = viewer;
this._camera = camera;
// A host may hand over the share code to open instead of the address.
this._initialShareCode =
typeof initialShareCode === 'string' ? initialShareCode : null;
@@ -133,12 +137,13 @@ export class ShareLinkManager {
? cancelOwnedNavigation
: null;
// Listen for camera changes
this._removeCameraChanged = this.viewer.camera.changed.addEventListener(
() => {
this._scheduleUpdate();
},
);
// The hash follows the camera as it moves, and catches up with where
// it comes to rest.
const schedule = () => this._scheduleUpdate();
this._cameraOffs = [
camera.on('changed', schedule),
camera.on('moveEnd', schedule),
];
}
/**
@@ -645,18 +650,17 @@ export class ShareLinkManager {
/** Build a deterministic snapshot without mutating history. */
_buildHashParams() {
if (this._destroyed) return null;
const camera = this.viewer.camera;
const carto = camera.positionCartographic;
if (!carto) return null;
const pose = this._camera.pose();
if (!Number.isFinite(pose.lat) || !Number.isFinite(pose.lon)) return null;
const params = new URLSearchParams();
params.set('v', '2');
params.set('lat', toDegrees(carto.latitude).toFixed(4));
params.set('lon', toDegrees(carto.longitude).toFixed(4));
params.set('alt', Math.round(carto.height).toString());
params.set('heading', Math.round(toDegrees(camera.heading)).toString());
params.set('pitch', Math.round(toDegrees(camera.pitch)).toString());
params.set('roll', Math.round(toDegrees(camera.roll)).toString());
params.set('lat', pose.lat.toFixed(4));
params.set('lon', pose.lon.toFixed(4));
params.set('alt', Math.round(pose.height).toString());
params.set('heading', Math.round(toDegrees(pose.heading)).toString());
params.set('pitch', Math.round(toDegrees(pose.pitch)).toString());
params.set('roll', Math.round(toDegrees(pose.roll)).toString());
params.set('style', STYLE_TO_URL[this._currentStyle] || 'normal');
params.set('bloom', this._bloomEnabled ? '1' : '0');
params.set('sharpen', this._sharpenEnabled ? '1' : '0');
@@ -711,8 +715,7 @@ export class ShareLinkManager {
this._destroyed = true;
clearTimeout(this._debounceTimer);
this._debounceTimer = null;
this._removeCameraChanged?.();
this._removeCameraChanged = null;
for (const off of this._cameraOffs.splice(0)) off();
this._unwatchNavigation?.();
this._layerStateProvider = null;
this._panelStateProvider = null;