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

+33 -33
View File
@@ -3,7 +3,7 @@ import { applicationServices } from './services/application.js';
* @module hud
* @description Intelligence HUD Overlay — NRO/NGA Satellite Aesthetic.
*
* Renders reconnaissance-styled telemetry over the Cesium canvas. The
* Renders reconnaissance-styled telemetry over the map canvas. The
* MGRS/lat-lon coordinates (the point below the camera), altitude and
* off-nadir angle come from the live camera, and the clocks show the current
* UTC time. Sun elevation is an approximate estimate from that position and
@@ -19,7 +19,8 @@ import { applicationServices } from './services/application.js';
* Color theming is driven by the active shader mode via CSS custom properties.
*/
import * as Cesium from 'cesium';
import { toDegrees, toRadians } from './geo/index.js';
import { rendererOf } from './render/rendererOf.js';
import { greatCircleKm } from './geoDistance.js';
import { forward as toMGRS } from 'mgrs';
import { CITY_POIS } from './cityPois.js';
@@ -215,7 +216,7 @@ const NEARBY_POINTS = Object.values(CITY_POIS).flatMap((city) =>
);
/**
* Full-screen intelligence HUD overlay rendered on top of the Cesium canvas.
* Full-screen intelligence HUD overlay rendered on top of the map canvas.
*
* Displays MGRS/lat-lon, altitude and off-nadir readouts from the live
* camera, UTC clocks, an approximate sun elevation, the selected vessel and a
@@ -227,8 +228,10 @@ const NEARBY_POINTS = Object.values(CITY_POIS).flatMap((city) =>
*/
export class IntelHUD {
/**
* @param {Cesium.Viewer} viewer - The Cesium Viewer instance used for
* camera telemetry and coordinate derivation.
* @param {object} viewer - The scene the HUD sits over.
* @param {object} [options]
* @param {import('./render/contract/camera').CameraPort} [options.camera]
* Camera telemetry; default the viewer's renderer's camera.
*/
constructor(
viewer,
@@ -238,8 +241,11 @@ export class IntelHUD {
basemapContext = {},
summaryService = applicationServices.summary,
publish = () => {},
camera = rendererOf(viewer)?.camera,
} = {},
) {
if (!camera) throw new Error('IntelHUD needs a camera port');
this._camera = camera;
this._publish = publish;
this.summaryService = summaryService;
this.summaryPolicy = summaryPolicy;
@@ -278,7 +284,7 @@ export class IntelHUD {
// A) kick a real AI summary once the intro fly-to settles.
this._firstMetricsShown = false;
this._firstSummaryKicked = false;
// ALT readout datum: the camera height Cesium reports is ELLIPSOIDAL, the
// ALT readout datum: the camera height the renderer reports is ELLIPSOIDAL, the
// number a viewer reads is MSL. N comes from the same lazy ~2.7 MB EGM96
// chunk the flight layers use — requested on the first telemetry tick of a
// VISIBLE HUD, never at construction, so a hidden HUD costs nothing — and
@@ -343,8 +349,10 @@ export class IntelHUD {
: { active: false, text: t('AIS: --') },
}),
);
this.viewer.camera.moveStart.addEventListener(this._onCameraMoveStart);
this.viewer.camera.moveEnd.addEventListener(this._onCameraMoveEnd);
this._offCamera = [
camera.on('moveStart', this._onCameraMoveStart),
camera.on('moveEnd', this._onCameraMoveEnd),
];
this._offLocale = onLocaleChange(() => this._relocalize());
this._startTimers();
}
@@ -495,18 +503,17 @@ export class IntelHUD {
/**
* Derive all camera-based telemetry and push values to the DOM.
* Reads the viewer camera's cartographic position and computes MGRS,
* Reads the camera's pose and computes MGRS,
* lat/lon DMS, GSD, NIIRS, sun elevation, off-nadir angle, and
* collection timestamp. Stores results in {@link _latestMetrics}.
*/
_updateCameraData() {
const camera = this.viewer.camera;
const cartographic = camera.positionCartographic;
if (!cartographic) return;
const lonDeg = Cesium.Math.toDegrees(cartographic.longitude);
const latDeg = Cesium.Math.toDegrees(cartographic.latitude);
const altM = cartographic.height;
const pose = this._camera.pose();
// No position yet: the readouts keep their last values.
if (![pose.lon, pose.lat, pose.height].every(Number.isFinite)) return;
const lonDeg = pose.lon;
const latDeg = pose.lat;
const altM = pose.height;
const latDMS = this._toDMS(latDeg, 'lat');
const lonDMS = this._toDMS(lonDeg, 'lon');
let mgrsLabel = '---';
@@ -563,7 +570,7 @@ export class IntelHUD {
// Off-nadir angle (ONA): camera pitch of -90 deg is nadir (straight down),
// so ONA = 90 + pitch gives 0 at nadir and increases toward the horizon.
const pitchDeg = Cesium.Math.toDegrees(camera.pitch);
const pitchDeg = toDegrees(pose.pitch);
const ona = Math.max(0, 90 + pitchDeg);
// i18n-ignore: sensor codes
readouts.ona = `ONA: ${ona.toFixed(1)}°`;
@@ -670,19 +677,19 @@ export class IntelHUD {
const declination =
23.45 *
Math.sin(
Cesium.Math.toRadians(
toRadians(
(360 / 365) * (now.getUTCDate() + 30 * now.getUTCMonth() - 81),
),
);
const latRad = Cesium.Math.toRadians(lat);
const decRad = Cesium.Math.toRadians(declination);
const haRad = Cesium.Math.toRadians(hourAngle);
const latRad = toRadians(lat);
const decRad = toRadians(declination);
const haRad = toRadians(hourAngle);
// Standard formula: sin(el) = sin(lat)*sin(dec) + cos(lat)*cos(dec)*cos(ha)
const sinEl =
Math.sin(latRad) * Math.sin(decRad) +
Math.cos(latRad) * Math.cos(decRad) * Math.cos(haRad);
// Clamp to [-1,1] to guard against floating-point drift before asin
return Cesium.Math.toDegrees(Math.asin(Math.max(-1, Math.min(1, sinEl))));
return toDegrees(Math.asin(Math.max(-1, Math.min(1, sinEl))));
}
/**
@@ -727,22 +734,16 @@ export class IntelHUD {
* dimensions, or null if the view rectangle cannot be computed.
*/
_viewWindowKm(latDeg) {
const rect = this.viewer.camera.computeViewRectangle();
const rect = this._camera.viewRectangle();
if (!rect) return null;
const north = Cesium.Math.toDegrees(rect.north);
const south = Cesium.Math.toDegrees(rect.south);
let east = Cesium.Math.toDegrees(rect.east);
let west = Cesium.Math.toDegrees(rect.west);
const { north, south, east, west } = rect;
let lonSpan = Math.abs(east - west);
// Handle antimeridian wrap: if span exceeds 180 deg, take the shorter arc
if (lonSpan > 180) lonSpan = 360 - lonSpan;
const latSpan = Math.abs(north - south);
// 111 km/deg is the approximate surface distance per degree of latitude;
// longitude distance is scaled by cos(lat) to account for meridian convergence.
const widthKm = Math.max(
0,
lonSpan * 111 * Math.cos(Cesium.Math.toRadians(latDeg)),
);
const widthKm = Math.max(0, lonSpan * 111 * Math.cos(toRadians(latDeg)));
const heightKm = Math.max(0, latSpan * 111);
return { widthKm, heightKm };
}
@@ -1218,8 +1219,7 @@ export class IntelHUD {
clearInterval(this._timestampInterval);
clearInterval(this._summaryInterval);
clearInterval(this._summaryTypingInterval);
this.viewer.camera.moveStart.removeEventListener(this._onCameraMoveStart);
this.viewer.camera.moveEnd.removeEventListener(this._onCameraMoveEnd);
for (const off of this._offCamera.splice(0)) off();
this._dataManagerUnsubscribe?.();
this._vesselUnsubscribe?.();
this._summaryRequest?.abort();