diff --git a/src/layers/launches/rendering.js b/src/layers/launches/rendering.js index 556e40f56..2aa1dc627 100644 --- a/src/layers/launches/rendering.js +++ b/src/layers/launches/rendering.js @@ -37,6 +37,37 @@ export function createRendering({ 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() { if ( !layerState._enabled || @@ -526,6 +557,7 @@ export function createRendering({ source: 'Approximate mission orbit', }, }); + layerState._replayTracks.get(launch.id).projectedOrbitPath = orbitPath; } const orbitLabelBakePosition = cartesian(ascentPath.at(-1)); let orbitLabelPosition = orbitLabelBakePosition; @@ -593,5 +625,12 @@ export function createRendering({ }); } } - return { setGraphicVisibility, updateMissionFrame, addLaunchEntity }; + return { + setGraphicVisibility, + selectLaunchEntity, + isolateMission, + projectedOrbitPath, + updateMissionFrame, + addLaunchEntity, + }; } diff --git a/src/layers/launches/replay.js b/src/layers/launches/replay.js index f3324f2ca..57805edd7 100644 --- a/src/layers/launches/replay.js +++ b/src/layers/launches/replay.js @@ -1,8 +1,9 @@ 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 { cameraOf } from '../../render/cesium/camera.js'; -import { findCamera } from '../../render/rendererOf.js'; +import { cameraOf, findCamera } from '../../render/rendererOf.js'; +import { lerp } from './geometry.js'; import { REPLAY_ASCENT_FALLBACK_SEC, REPLAY_ASCENT_MIN_SEC, @@ -56,10 +57,10 @@ export function createReplay({ state: layerState, services, parts, source }) { if (!previous || !next) return next; const distance = Math.hypot(next.x - previous.x, next.y - previous.y); 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 { - x: Cesium.Math.lerp(previous.x, next.x, amount), - y: Cesium.Math.lerp(previous.y, next.y, amount), + x: lerp(previous.x, next.x, 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 * path length and a conservative ascent velocity estimate. * @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. */ @@ -80,14 +82,13 @@ export function createReplay({ state: layerState, services, parts, source }) { const pathLength = ascentPath .slice(1) .reduce( - (total, point, index) => - total + Cesium.Cartesian3.distance(ascentPath[index], point), + (total, point, index) => total + distance(ascentPath[index], point), 0, ); realAscentSeconds = Math.max(180, pathLength / 9000); } if (!realAscentSeconds) realAscentSeconds = REPLAY_ASCENT_FALLBACK_SEC * 50; - return Cesium.Math.clamp( + return clamp( REPLAY_ASCENT_FALLBACK_SEC * (realAscentSeconds / 600), REPLAY_ASCENT_MIN_SEC, REPLAY_ASCENT_MAX_SEC, @@ -103,11 +104,7 @@ export function createReplay({ state: layerState, services, parts, source }) { function normalizeReplaySpeed(value) { const numeric = Number(value); if (!Number.isFinite(numeric)) return 1; - const clamped = Cesium.Math.clamp( - numeric, - REPLAY_SPEED_MIN, - REPLAY_SPEED_MAX, - ); + const clamped = clamp(numeric, REPLAY_SPEED_MIN, REPLAY_SPEED_MAX); return Math.round(clamped / REPLAY_SPEED_STEP) * REPLAY_SPEED_STEP; } @@ -393,7 +390,7 @@ export function createReplay({ state: layerState, services, parts, source }) { track.lastCameraHeading, ); const orbitBlend = !state.ascending - ? Cesium.Math.clamp( + ? clamp( (Number(state.phaseProgress) || 0) / REPLAY_ORBIT_PULLBACK_FRACTION, 0, @@ -407,16 +404,15 @@ export function createReplay({ state: layerState, services, parts, source }) { if (state.ascending) track.orbitCameraWorldFrame = false; const nowMs = performance.now(); const frameDurationMs = Number.isFinite(lastCameraUpdateMs) - ? Cesium.Math.clamp(nowMs - lastCameraUpdateMs, 4, 50) + ? clamp(nowMs - lastCameraUpdateMs, 4, 50) : 1000 / 60; lastCameraUpdateMs = nowMs; track.lastCameraHeading = parts.camera.smoothReplayCameraHeading( track.lastCameraHeading, desiredHeading, - (Cesium.Math.toRadians(2) * frameDurationMs) / (1000 / 60), + (toRadians(2) * frameDurationMs) / (1000 / 60), ); - const cartographic = - Cesium.Ellipsoid.WGS84.cartesianToCartographic(position); + const cartographic = toCartographicRadians(position); const altitude = Math.max(0, cartographic?.height || 0); const cameraView = parts.camera.replayCameraView(state, altitude); const defaultOrbitTarget = !state.ascending @@ -492,7 +488,7 @@ export function createReplay({ state: layerState, services, parts, source }) { { offset: { heading: initialHeading, - pitch: Cesium.Math.toRadians(-20), + pitch: toRadians(-20), range: REPLAY_INITIAL_RANGE_M, }, durationS: 1.4, diff --git a/src/layers/launches/selection.js b/src/layers/launches/selection.js index 1e40c10bb..c3cef876b 100644 --- a/src/layers/launches/selection.js +++ b/src/layers/launches/selection.js @@ -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 { cameraOf } from '../../render/cesium/camera.js'; -import { findCamera } from '../../render/rendererOf.js'; +import { cameraOf, findCamera } from '../../render/rendererOf.js'; export function createSelection({ state: layerState, @@ -15,18 +26,18 @@ export function createSelection({ const canvas = viewer?.scene?.canvas; const height = canvas?.clientHeight || canvas?.height; const width = canvas?.clientWidth || canvas?.width; - const cartographic = viewer?.camera?.positionCartographic; - const fovy = viewer?.camera?.frustum?.fovy; + if (!(width > 0) || !(height > 0)) return; + const pose = findCamera(viewer)?.pose(); + const fovy = pose?.fovy; if ( - !(width > 0) || - !(height > 0) || - !cartographic || + !pose || + !Number.isFinite(pose.lon) || !Number.isFinite(fovy) || fovy <= 0 || fovy >= Math.PI ) return; - const earthRadius = Cesium.Ellipsoid.WGS84.maximumRadius; + const earthRadius = WGS84.radii.x; const keyholeRadius = getKeyholeGeometry(width, height).radius; const targetScreenRadius = keyholeRadius * 0.61; const angularRadius = Math.atan( @@ -36,15 +47,11 @@ export function createSelection({ const altitude = Math.max(earthRadius * 1.55, distance - earthRadius); const camera = cameraOf(viewer); camera.flyTo( - Cesium.Cartesian3.fromRadians( - cartographic.longitude, - cartographic.latitude, - altitude, - ), + { lon: pose.lon, lat: pose.lat, height: altitude }, { orientation: { - heading: camera.pose().heading, - pitch: -Cesium.Math.PI_OVER_TWO, + heading: pose.heading, + pitch: -PI_OVER_TWO, roll: 0, }, durationS: duration, @@ -76,15 +83,10 @@ export function createSelection({ if (launchId) layerState._animationStarts.set(launchId, Date.now()); if (!launchId && layerState._viewer) { stopMissionZoomAnchor(); - layerState._viewer.selectedEntity = undefined; + parts.rendering.selectLaunchEntity(null); } if (!layerState._dataSource) return; - for (const entity of layerState._dataSource.entities.values) { - const relatedId = entityLaunchId(entity); - entity.show = launchId - ? relatedId === launchId - : entity.id.startsWith('rocket-launch:'); - } + parts.rendering.isolateMission(launchId); parts.orbitRendering.syncMissionOrbitPrimitiveVisibility(); parts.overlays.syncMissionOverlayEntries(); parts.panel.renderMissionPanel(); @@ -103,7 +105,7 @@ export function createSelection({ if (!layerState._viewer || !launch || layerState._replayCameraLaunchId) return; stopMissionZoomAnchor(); - const target = Cesium.Cartesian3.fromDegrees(launch.lon, launch.lat); + const target = fromDegrees(launch.lon, launch.lat); const range = Math.max( 2000, Number(initialRange) || MISSION_GLOBE_VIEW_RANGE_M, @@ -119,24 +121,14 @@ export function createSelection({ function focusMission(launch) { if (!layerState._viewer || !launch) return; stopMissionZoomAnchor(); - layerState._viewer.selectedEntity = layerState._dataSource.entities.getById( - `rocket-launch:${launch.id}`, - ); - const orbitEntity = layerState._dataSource.entities.getById( - `rocket-orbit:${launch.id}`, - ); - const orbitPositions = orbitEntity?.polyline?.positions?.getValue( - Cesium.JulianDate.now(), - ); - const launchPosition = Cesium.Cartesian3.fromDegrees( - launch.lon, - launch.lat, - ); + parts.rendering.selectLaunchEntity(launch.id); + const camera = cameraOf(layerState._viewer); + const orbitPositions = parts.rendering.projectedOrbitPath(launch.id); + const launchPosition = fromDegrees(launch.lon, launch.lat); let range = 18000000; if (orbitPositions?.length > 1) { const canvas = layerState._viewer.scene.canvas; - const fovy = - layerState._viewer.camera.frustum?.fovy || Cesium.Math.toRadians(60); + const fovy = camera.pose().fovy || toRadians(60); const aspect = Math.max( 0.1, (canvas.clientWidth || canvas.width) / @@ -145,8 +137,7 @@ export function createSelection({ const fovx = 2 * Math.atan(Math.tan(fovy * 0.5) * aspect); const paddedHalfAngle = Math.min(fovy, fovx) * 0.5 * 0.62; const orbitExtent = orbitPositions.reduce( - (largest, point) => - Math.max(largest, Cesium.Cartesian3.distance(launchPosition, point)), + (largest, point) => Math.max(largest, distance(launchPosition, point)), 0, ); range = Math.max( @@ -154,41 +145,14 @@ export function createSelection({ 18000000, ); } - const targetNormal = Cesium.Cartesian3.normalize( - launchPosition, - new Cesium.Cartesian3(), - ); - const destination = Cesium.Cartesian3.add( - launchPosition, - Cesium.Cartesian3.multiplyByScalar( - targetNormal, - 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, { + const targetNormal = normalize(launchPosition); + const destination = add(launchPosition, scale(targetNormal, range)); + const direction = negate(targetNormal); + let right = cross(direction, vec(0, 0, 1)); + if (dot(right, right) < 1e-8) right = cross(direction, vec(0, 1, 0)); + right = normalize(right); + const up = normalize(cross(right, direction)); + camera.flyTo(destination, { orientation: { direction, up }, durationS: 1.1, onComplete: () => { @@ -202,15 +166,13 @@ export function createSelection({ if (!layerState._viewer || !launch) return; parts.replay.stopMissionReplay(); stopMissionZoomAnchor(); - layerState._viewer.selectedEntity = layerState._dataSource.entities.getById( - `rocket-launch:${launch.id}`, - ); + parts.rendering.selectLaunchEntity(launch.id); cameraOf(layerState._viewer).flyToBounds( { center: { lon: launch.lon, lat: launch.lat }, radius: 0 }, { offset: { heading: 0, - pitch: Cesium.Math.toRadians(-42), + pitch: toRadians(-42), range: MISSION_FOCUS_RANGE_M, }, durationS: 1.4, diff --git a/src/render/cesiumAllowList.json b/src/render/cesiumAllowList.json index 223147218..e466120d6 100644 --- a/src/render/cesiumAllowList.json +++ b/src/render/cesiumAllowList.json @@ -79,8 +79,6 @@ "src/layers/launches/orbitRendering.js", "src/layers/launches/overlays.js", "src/layers/launches/rendering.js", - "src/layers/launches/replay.js", - "src/layers/launches/selection.js", "src/layers/localAdsb/index.js", "src/layers/localAdsb/models.js", "src/layers/localAdsb/motion.js",