Move the launches replay and selection cameras onto the camera port

Replay and selection no longer import Cesium. Their maths uses the geo
helpers; the full-globe framing reads the camera's position and field of
view from the port's pose. Choosing the selected entity, showing only the
selected mission's entities and reading the projected orbit for framing
move into the rendering part, which owns the entities, so selection no
longer touches them. The full-globe framing now checks the canvas size
before it asks for the camera.

The replay camera still steps in the scene's preUpdate, as before; it moves
onto the frame port once unbound viewers have one.

Replay and selection leave the allow-list.
This commit is contained in:
Sameh Khamis committed 2026-11-06 22:57:55 -08:00
1 parent a2503476de
commit e2dea507b9
4 files changed
+99 -104

No files matched your search

+40 -1
View File
@@ -37,6 +37,37 @@ export function createRendering({
if (current !== next) graphic.show = next; if (current !== next) graphic.show = next;
} }
/** Make a launch's site marker the viewer's selected entity, or none. */
function selectLaunchEntity(launchId) {
const viewer = layerState._viewer;
if (!viewer) return;
viewer.selectedEntity = launchId
? layerState._dataSource?.entities.getById(`rocket-launch:${launchId}`)
: undefined;
}
/**
* Show only the selected mission's entities, or every site marker and
* nothing else when none is selected.
*/
function isolateMission(launchId) {
if (!layerState._dataSource) return;
for (const entity of layerState._dataSource.entities.values) {
const relatedId = parts.selection.entityLaunchId(entity);
entity.show = launchId
? relatedId === launchId
: entity.id.startsWith('rocket-launch:');
}
}
/**
* The projected (estimated) orbit drawn as an entity for a launch, ECEF;
* null when its orbit is a live satellite ring or it has none.
*/
function projectedOrbitPath(launchId) {
return layerState._replayTracks.get(launchId)?.projectedOrbitPath ?? null;
}
function updateMissionFrame() { function updateMissionFrame() {
if ( if (
!layerState._enabled || !layerState._enabled ||
@@ -526,6 +557,7 @@ export function createRendering({
source: 'Approximate mission orbit', source: 'Approximate mission orbit',
}, },
}); });
layerState._replayTracks.get(launch.id).projectedOrbitPath = orbitPath;
} }
const orbitLabelBakePosition = cartesian(ascentPath.at(-1)); const orbitLabelBakePosition = cartesian(ascentPath.at(-1));
let orbitLabelPosition = orbitLabelBakePosition; let orbitLabelPosition = orbitLabelBakePosition;
@@ -593,5 +625,12 @@ export function createRendering({
}); });
} }
} }
return { setGraphicVisibility, updateMissionFrame, addLaunchEntity }; return {
setGraphicVisibility,
selectLaunchEntity,
isolateMission,
projectedOrbitPath,
updateMissionFrame,
addLaunchEntity,
};
} }
+17 -21
View File
@@ -1,8 +1,9 @@
import { stopFollowing } from '../../data/followed.js'; import { stopFollowing } from '../../data/followed.js';
import * as Cesium from 'cesium'; import { toCartographicRadians } from '../../geo/ellipsoid.js';
import { clamp, distance, toRadians } from '../../geo/vector.js';
import { t } from '../../i18n/index.js'; import { t } from '../../i18n/index.js';
import { cameraOf } from '../../render/cesium/camera.js'; import { cameraOf, findCamera } from '../../render/rendererOf.js';
import { findCamera } from '../../render/rendererOf.js'; import { lerp } from './geometry.js';
import { import {
REPLAY_ASCENT_FALLBACK_SEC, REPLAY_ASCENT_FALLBACK_SEC,
REPLAY_ASCENT_MIN_SEC, REPLAY_ASCENT_MIN_SEC,
@@ -56,10 +57,10 @@ export function createReplay({ state: layerState, services, parts, source }) {
if (!previous || !next) return next; if (!previous || !next) return next;
const distance = Math.hypot(next.x - previous.x, next.y - previous.y); const distance = Math.hypot(next.x - previous.x, next.y - previous.y);
if (!Number.isFinite(distance) || distance > snapDistance) return next; if (!Number.isFinite(distance) || distance > snapDistance) return next;
const amount = Cesium.Math.clamp(Number(alpha) || 0, 0, 1); const amount = clamp(Number(alpha) || 0, 0, 1);
return { return {
x: Cesium.Math.lerp(previous.x, next.x, amount), x: lerp(previous.x, next.x, amount),
y: Cesium.Math.lerp(previous.y, next.y, amount), y: lerp(previous.y, next.y, amount),
}; };
} }
@@ -69,7 +70,8 @@ export function createReplay({ state: layerState, services, parts, source }) {
* SECO, or separation timing. Sparse records fall back to the reconstructed * SECO, or separation timing. Sparse records fall back to the reconstructed
* path length and a conservative ascent velocity estimate. * path length and a conservative ascent velocity estimate.
* @param {object} launch Normalized launch record. * @param {object} launch Normalized launch record.
* @param {Cesium.Cartesian3[]} ascentPath Reconstructed or supplied ascent. * @param {Array<{x: number, y: number, z: number}>} ascentPath
* Reconstructed or supplied ascent, ECEF.
* @returns {number} Replay duration in seconds. * @returns {number} Replay duration in seconds.
*/ */
@@ -80,14 +82,13 @@ export function createReplay({ state: layerState, services, parts, source }) {
const pathLength = ascentPath const pathLength = ascentPath
.slice(1) .slice(1)
.reduce( .reduce(
(total, point, index) => (total, point, index) => total + distance(ascentPath[index], point),
total + Cesium.Cartesian3.distance(ascentPath[index], point),
0, 0,
); );
realAscentSeconds = Math.max(180, pathLength / 9000); realAscentSeconds = Math.max(180, pathLength / 9000);
} }
if (!realAscentSeconds) realAscentSeconds = REPLAY_ASCENT_FALLBACK_SEC * 50; if (!realAscentSeconds) realAscentSeconds = REPLAY_ASCENT_FALLBACK_SEC * 50;
return Cesium.Math.clamp( return clamp(
REPLAY_ASCENT_FALLBACK_SEC * (realAscentSeconds / 600), REPLAY_ASCENT_FALLBACK_SEC * (realAscentSeconds / 600),
REPLAY_ASCENT_MIN_SEC, REPLAY_ASCENT_MIN_SEC,
REPLAY_ASCENT_MAX_SEC, REPLAY_ASCENT_MAX_SEC,
@@ -103,11 +104,7 @@ export function createReplay({ state: layerState, services, parts, source }) {
function normalizeReplaySpeed(value) { function normalizeReplaySpeed(value) {
const numeric = Number(value); const numeric = Number(value);
if (!Number.isFinite(numeric)) return 1; if (!Number.isFinite(numeric)) return 1;
const clamped = Cesium.Math.clamp( const clamped = clamp(numeric, REPLAY_SPEED_MIN, REPLAY_SPEED_MAX);
numeric,
REPLAY_SPEED_MIN,
REPLAY_SPEED_MAX,
);
return Math.round(clamped / REPLAY_SPEED_STEP) * REPLAY_SPEED_STEP; return Math.round(clamped / REPLAY_SPEED_STEP) * REPLAY_SPEED_STEP;
} }
@@ -393,7 +390,7 @@ export function createReplay({ state: layerState, services, parts, source }) {
track.lastCameraHeading, track.lastCameraHeading,
); );
const orbitBlend = !state.ascending const orbitBlend = !state.ascending
? Cesium.Math.clamp( ? clamp(
(Number(state.phaseProgress) || 0) / (Number(state.phaseProgress) || 0) /
REPLAY_ORBIT_PULLBACK_FRACTION, REPLAY_ORBIT_PULLBACK_FRACTION,
0, 0,
@@ -407,16 +404,15 @@ export function createReplay({ state: layerState, services, parts, source }) {
if (state.ascending) track.orbitCameraWorldFrame = false; if (state.ascending) track.orbitCameraWorldFrame = false;
const nowMs = performance.now(); const nowMs = performance.now();
const frameDurationMs = Number.isFinite(lastCameraUpdateMs) const frameDurationMs = Number.isFinite(lastCameraUpdateMs)
? Cesium.Math.clamp(nowMs - lastCameraUpdateMs, 4, 50) ? clamp(nowMs - lastCameraUpdateMs, 4, 50)
: 1000 / 60; : 1000 / 60;
lastCameraUpdateMs = nowMs; lastCameraUpdateMs = nowMs;
track.lastCameraHeading = parts.camera.smoothReplayCameraHeading( track.lastCameraHeading = parts.camera.smoothReplayCameraHeading(
track.lastCameraHeading, track.lastCameraHeading,
desiredHeading, desiredHeading,
(Cesium.Math.toRadians(2) * frameDurationMs) / (1000 / 60), (toRadians(2) * frameDurationMs) / (1000 / 60),
); );
const cartographic = const cartographic = toCartographicRadians(position);
Cesium.Ellipsoid.WGS84.cartesianToCartographic(position);
const altitude = Math.max(0, cartographic?.height || 0); const altitude = Math.max(0, cartographic?.height || 0);
const cameraView = parts.camera.replayCameraView(state, altitude); const cameraView = parts.camera.replayCameraView(state, altitude);
const defaultOrbitTarget = !state.ascending const defaultOrbitTarget = !state.ascending
@@ -492,7 +488,7 @@ export function createReplay({ state: layerState, services, parts, source }) {
{ {
offset: { offset: {
heading: initialHeading, heading: initialHeading,
pitch: Cesium.Math.toRadians(-20), pitch: toRadians(-20),
range: REPLAY_INITIAL_RANGE_M, range: REPLAY_INITIAL_RANGE_M,
}, },
durationS: 1.4, durationS: 1.4,
+42 -80
View File
@@ -1,7 +1,18 @@
import * as Cesium from 'cesium'; import { WGS84, fromDegrees } from '../../geo/ellipsoid.js';
import {
PI_OVER_TWO,
add,
cross,
distance,
dot,
negate,
normalize,
scale,
toRadians,
vec,
} from '../../geo/vector.js';
import { MISSION_GLOBE_VIEW_RANGE_M, MISSION_FOCUS_RANGE_M } from './policy.js'; import { MISSION_GLOBE_VIEW_RANGE_M, MISSION_FOCUS_RANGE_M } from './policy.js';
import { cameraOf } from '../../render/cesium/camera.js'; import { cameraOf, findCamera } from '../../render/rendererOf.js';
import { findCamera } from '../../render/rendererOf.js';
export function createSelection({ export function createSelection({
state: layerState, state: layerState,
@@ -15,18 +26,18 @@ export function createSelection({
const canvas = viewer?.scene?.canvas; const canvas = viewer?.scene?.canvas;
const height = canvas?.clientHeight || canvas?.height; const height = canvas?.clientHeight || canvas?.height;
const width = canvas?.clientWidth || canvas?.width; const width = canvas?.clientWidth || canvas?.width;
const cartographic = viewer?.camera?.positionCartographic; if (!(width > 0) || !(height > 0)) return;
const fovy = viewer?.camera?.frustum?.fovy; const pose = findCamera(viewer)?.pose();
const fovy = pose?.fovy;
if ( if (
!(width > 0) || !pose ||
!(height > 0) || !Number.isFinite(pose.lon) ||
!cartographic ||
!Number.isFinite(fovy) || !Number.isFinite(fovy) ||
fovy <= 0 || fovy <= 0 ||
fovy >= Math.PI fovy >= Math.PI
) )
return; return;
const earthRadius = Cesium.Ellipsoid.WGS84.maximumRadius; const earthRadius = WGS84.radii.x;
const keyholeRadius = getKeyholeGeometry(width, height).radius; const keyholeRadius = getKeyholeGeometry(width, height).radius;
const targetScreenRadius = keyholeRadius * 0.61; const targetScreenRadius = keyholeRadius * 0.61;
const angularRadius = Math.atan( const angularRadius = Math.atan(
@@ -36,15 +47,11 @@ export function createSelection({
const altitude = Math.max(earthRadius * 1.55, distance - earthRadius); const altitude = Math.max(earthRadius * 1.55, distance - earthRadius);
const camera = cameraOf(viewer); const camera = cameraOf(viewer);
camera.flyTo( camera.flyTo(
Cesium.Cartesian3.fromRadians( { lon: pose.lon, lat: pose.lat, height: altitude },
cartographic.longitude,
cartographic.latitude,
altitude,
),
{ {
orientation: { orientation: {
heading: camera.pose().heading, heading: pose.heading,
pitch: -Cesium.Math.PI_OVER_TWO, pitch: -PI_OVER_TWO,
roll: 0, roll: 0,
}, },
durationS: duration, durationS: duration,
@@ -76,15 +83,10 @@ export function createSelection({
if (launchId) layerState._animationStarts.set(launchId, Date.now()); if (launchId) layerState._animationStarts.set(launchId, Date.now());
if (!launchId && layerState._viewer) { if (!launchId && layerState._viewer) {
stopMissionZoomAnchor(); stopMissionZoomAnchor();
layerState._viewer.selectedEntity = undefined; parts.rendering.selectLaunchEntity(null);
} }
if (!layerState._dataSource) return; if (!layerState._dataSource) return;
for (const entity of layerState._dataSource.entities.values) { parts.rendering.isolateMission(launchId);
const relatedId = entityLaunchId(entity);
entity.show = launchId
? relatedId === launchId
: entity.id.startsWith('rocket-launch:');
}
parts.orbitRendering.syncMissionOrbitPrimitiveVisibility(); parts.orbitRendering.syncMissionOrbitPrimitiveVisibility();
parts.overlays.syncMissionOverlayEntries(); parts.overlays.syncMissionOverlayEntries();
parts.panel.renderMissionPanel(); parts.panel.renderMissionPanel();
@@ -103,7 +105,7 @@ export function createSelection({
if (!layerState._viewer || !launch || layerState._replayCameraLaunchId) if (!layerState._viewer || !launch || layerState._replayCameraLaunchId)
return; return;
stopMissionZoomAnchor(); stopMissionZoomAnchor();
const target = Cesium.Cartesian3.fromDegrees(launch.lon, launch.lat); const target = fromDegrees(launch.lon, launch.lat);
const range = Math.max( const range = Math.max(
2000, 2000,
Number(initialRange) || MISSION_GLOBE_VIEW_RANGE_M, Number(initialRange) || MISSION_GLOBE_VIEW_RANGE_M,
@@ -119,24 +121,14 @@ export function createSelection({
function focusMission(launch) { function focusMission(launch) {
if (!layerState._viewer || !launch) return; if (!layerState._viewer || !launch) return;
stopMissionZoomAnchor(); stopMissionZoomAnchor();
layerState._viewer.selectedEntity = layerState._dataSource.entities.getById( parts.rendering.selectLaunchEntity(launch.id);
`rocket-launch:${launch.id}`, const camera = cameraOf(layerState._viewer);
); const orbitPositions = parts.rendering.projectedOrbitPath(launch.id);
const orbitEntity = layerState._dataSource.entities.getById( const launchPosition = fromDegrees(launch.lon, launch.lat);
`rocket-orbit:${launch.id}`,
);
const orbitPositions = orbitEntity?.polyline?.positions?.getValue(
Cesium.JulianDate.now(),
);
const launchPosition = Cesium.Cartesian3.fromDegrees(
launch.lon,
launch.lat,
);
let range = 18000000; let range = 18000000;
if (orbitPositions?.length > 1) { if (orbitPositions?.length > 1) {
const canvas = layerState._viewer.scene.canvas; const canvas = layerState._viewer.scene.canvas;
const fovy = const fovy = camera.pose().fovy || toRadians(60);
layerState._viewer.camera.frustum?.fovy || Cesium.Math.toRadians(60);
const aspect = Math.max( const aspect = Math.max(
0.1, 0.1,
(canvas.clientWidth || canvas.width) / (canvas.clientWidth || canvas.width) /
@@ -145,8 +137,7 @@ export function createSelection({
const fovx = 2 * Math.atan(Math.tan(fovy * 0.5) * aspect); const fovx = 2 * Math.atan(Math.tan(fovy * 0.5) * aspect);
const paddedHalfAngle = Math.min(fovy, fovx) * 0.5 * 0.62; const paddedHalfAngle = Math.min(fovy, fovx) * 0.5 * 0.62;
const orbitExtent = orbitPositions.reduce( const orbitExtent = orbitPositions.reduce(
(largest, point) => (largest, point) => Math.max(largest, distance(launchPosition, point)),
Math.max(largest, Cesium.Cartesian3.distance(launchPosition, point)),
0, 0,
); );
range = Math.max( range = Math.max(
@@ -154,41 +145,14 @@ export function createSelection({
18000000, 18000000,
); );
} }
const targetNormal = Cesium.Cartesian3.normalize( const targetNormal = normalize(launchPosition);
launchPosition, const destination = add(launchPosition, scale(targetNormal, range));
new Cesium.Cartesian3(), const direction = negate(targetNormal);
); let right = cross(direction, vec(0, 0, 1));
const destination = Cesium.Cartesian3.add( if (dot(right, right) < 1e-8) right = cross(direction, vec(0, 1, 0));
launchPosition, right = normalize(right);
Cesium.Cartesian3.multiplyByScalar( const up = normalize(cross(right, direction));
targetNormal, camera.flyTo(destination, {
range,
new Cesium.Cartesian3(),
),
new Cesium.Cartesian3(),
);
const direction = Cesium.Cartesian3.negate(
targetNormal,
new Cesium.Cartesian3(),
);
let right = Cesium.Cartesian3.cross(
direction,
Cesium.Cartesian3.UNIT_Z,
new Cesium.Cartesian3(),
);
if (Cesium.Cartesian3.magnitudeSquared(right) < 1e-8) {
right = Cesium.Cartesian3.cross(
direction,
Cesium.Cartesian3.UNIT_Y,
right,
);
}
Cesium.Cartesian3.normalize(right, right);
const up = Cesium.Cartesian3.normalize(
Cesium.Cartesian3.cross(right, direction, new Cesium.Cartesian3()),
new Cesium.Cartesian3(),
);
cameraOf(layerState._viewer).flyTo(destination, {
orientation: { direction, up }, orientation: { direction, up },
durationS: 1.1, durationS: 1.1,
onComplete: () => { onComplete: () => {
@@ -202,15 +166,13 @@ export function createSelection({
if (!layerState._viewer || !launch) return; if (!layerState._viewer || !launch) return;
parts.replay.stopMissionReplay(); parts.replay.stopMissionReplay();
stopMissionZoomAnchor(); stopMissionZoomAnchor();
layerState._viewer.selectedEntity = layerState._dataSource.entities.getById( parts.rendering.selectLaunchEntity(launch.id);
`rocket-launch:${launch.id}`,
);
cameraOf(layerState._viewer).flyToBounds( cameraOf(layerState._viewer).flyToBounds(
{ center: { lon: launch.lon, lat: launch.lat }, radius: 0 }, { center: { lon: launch.lon, lat: launch.lat }, radius: 0 },
{ {
offset: { offset: {
heading: 0, heading: 0,
pitch: Cesium.Math.toRadians(-42), pitch: toRadians(-42),
range: MISSION_FOCUS_RANGE_M, range: MISSION_FOCUS_RANGE_M,
}, },
durationS: 1.4, durationS: 1.4,
-2
View File
@@ -79,8 +79,6 @@
"src/layers/launches/orbitRendering.js", "src/layers/launches/orbitRendering.js",
"src/layers/launches/overlays.js", "src/layers/launches/overlays.js",
"src/layers/launches/rendering.js", "src/layers/launches/rendering.js",
"src/layers/launches/replay.js",
"src/layers/launches/selection.js",
"src/layers/localAdsb/index.js", "src/layers/localAdsb/index.js",
"src/layers/localAdsb/models.js", "src/layers/localAdsb/models.js",
"src/layers/localAdsb/motion.js", "src/layers/localAdsb/motion.js",