diff --git a/src/layers/launches/overlays.js b/src/layers/launches/overlays.js index 4b246e702..9bd77ea47 100644 --- a/src/layers/launches/overlays.js +++ b/src/layers/launches/overlays.js @@ -1,4 +1,6 @@ -import * as Cesium from 'cesium'; +import { WGS84 } from '../../geo/ellipsoid.js'; +import { dot, magnitude, normalize, toDegrees } from '../../geo/vector.js'; +import { findQuery } from '../../render/rendererOf.js'; import { t, msg } from '../../i18n/index.js'; import { createCyberSonarSampler, @@ -12,13 +14,16 @@ import { ROCKET_MISSION_AMBIENT_OVERLAY_SOURCE_OPTIONS, } from './policy.js'; +/** The ellipsoid's largest radius, metres. */ +const MAX_RADIUS = Math.max(WGS84.radii.x, WGS84.radii.y, WGS84.radii.z); + export function createOverlays({ state: layerState, services, parts, source }) { /** * Resolve whether a surface mission anchor is safely on the camera-facing * side of Earth. The small positive limb margin prevents labels anchored just - * beyond the horizon from leaking through the hidden Cesium globe. - * @param {Cesium.Cartesian3} cameraPosition Camera world position. - * @param {Cesium.Cartesian3} markerPosition Mission anchor world position. + * beyond the horizon from leaking through the hidden globe. + * @param {{x: number, y: number, z: number}} cameraPosition Camera world position. + * @param {{x: number, y: number, z: number}} markerPosition Mission anchor world position. * @param {number} [limbMargin] Additional normalized horizon clearance. * @returns {boolean} Whether the marker belongs on the visible hemisphere. */ @@ -29,33 +34,21 @@ export function createOverlays({ state: layerState, services, parts, source }) { limbMargin = 0.012, ) { if (!cameraPosition || !markerPosition) return false; - const cameraMagnitude = Cesium.Cartesian3.magnitude(cameraPosition); - if ( - !Number.isFinite(cameraMagnitude) || - cameraMagnitude <= Cesium.Ellipsoid.WGS84.maximumRadius - ) { + const cameraMagnitude = magnitude(cameraPosition); + if (!Number.isFinite(cameraMagnitude) || cameraMagnitude <= MAX_RADIUS) { return false; } - const cameraDirection = Cesium.Cartesian3.normalize( - cameraPosition, - new Cesium.Cartesian3(), - ); - const markerDirection = Cesium.Cartesian3.normalize( - markerPosition, - new Cesium.Cartesian3(), - ); - const limbThreshold = - Cesium.Ellipsoid.WGS84.maximumRadius / cameraMagnitude; + const limbThreshold = MAX_RADIUS / cameraMagnitude; return ( - Cesium.Cartesian3.dot(cameraDirection, markerDirection) > + dot(normalize(cameraPosition), normalize(markerPosition)) > limbThreshold + limbMargin ); } /** * Resolve launch-anchor visibility for overview and selected-mission views. - * @param {Cesium.Cartesian3} cameraPosition Camera world position. - * @param {Cesium.Cartesian3} markerPosition Mission anchor world position. + * @param {{x: number, y: number, z: number}} cameraPosition Camera world position. + * @param {{x: number, y: number, z: number}} markerPosition Mission anchor world position. * @param {string} markerId Mission represented by this anchor. * @param {string|null} selectedLaunchId Explicitly selected mission. * @returns {boolean} Whether the launch anchor should render. @@ -104,7 +97,7 @@ export function createOverlays({ state: layerState, services, parts, source }) { * compete in the bounded ambient-label domain; the selected mission moves to * the protected selected lane and gains the former launch-site detail line. * @param {object} launch Normalized Launch Library mission. - * @param {Cesium.Cartesian3|function():Cesium.Cartesian3} position Existing display position. + * @param {object|function():object} position Existing display position. * @param {boolean} [selected=false] Whether the mission owns the selected view. * @returns {object} */ @@ -164,7 +157,7 @@ export function createOverlays({ state: layerState, services, parts, source }) { * caller; newline semantics become explicit host detail rows. * @param {object} input * @param {string} input.id Stable mission-element identity. - * @param {Cesium.Cartesian3|function():Cesium.Cartesian3} input.position Existing cached position. + * @param {object|function():object} input.position Existing cached position. * @param {string} input.text Former native-label text, including newlines. * @param {string} input.accent Source-owned label color. * @param {number} [input.priority=0] Protected placement order. @@ -490,10 +483,8 @@ export function createOverlays({ state: layerState, services, parts, source }) { hideReplayVehicleOverlay(); return null; } - const windowPosition = Cesium.SceneTransforms.worldToWindowCoordinates( - layerState._viewer.scene, - position, - ); + const query = findQuery(layerState._viewer); + const windowPosition = query?.project(position) ?? null; if (!windowPosition) { hideReplayVehicleOverlay(); return null; @@ -547,10 +538,7 @@ export function createOverlays({ state: layerState, services, parts, source }) { useForward ? forwardProgress : backwardProgress, ); const tangentWindowPosition = tangentPosition - ? Cesium.SceneTransforms.worldToWindowCoordinates( - layerState._viewer.scene, - tangentPosition, - ) + ? query.project(tangentPosition) : null; if (tangentWindowPosition) { const screenDelta = Math.hypot( @@ -577,7 +565,7 @@ export function createOverlays({ state: layerState, services, parts, source }) { } } } - frame.rotation = `${Cesium.Math.toDegrees(vehicleRotation)}deg`; + frame.rotation = `${toDegrees(vehicleRotation)}deg`; const mission = shortMissionLabel(launch.name, 22).toUpperCase(); const siteName = compactLaunchSiteName(launch.launchSite); diff --git a/src/layers/launches/replayOverlay.test.mjs b/src/layers/launches/replayOverlay.test.mjs index 76d85c0e1..21fede06c 100644 --- a/src/layers/launches/replayOverlay.test.mjs +++ b/src/layers/launches/replayOverlay.test.mjs @@ -119,3 +119,84 @@ test('the scene chrome renders the overlay in the globe container', async (t) => await nextTick(); assert.equal(node(), null); }); + +test('the replay overlay sits where the query port projects the vehicle, facing along its path', async () => { + const { bindRenderer } = await import('../../render/rendererOf.js'); + const viewer = { scene: { canvas: { clientWidth: 800, clientHeight: 600 } } }; + const projected = []; + let behindEye = false; + // The path runs along x; the port puts x metres at x pixels, y = 300. + const unbind = bindRenderer(viewer, { + query: { + project(position) { + projected.push(position); + return behindEye ? null : { x: position.x, y: 300 }; + }, + }, + }); + const path = [ + { x: 0, y: 0, z: 0 }, + { x: 400, y: 0, z: 0 }, + ]; + const state = { + _viewer: viewer, + _replayVehicleOverlay: null, + _replayVehicleOverlayListeners: new Set(), + _selectedLaunchId: 'l1', + _replayCameraLaunchId: 'l1', + _replayPaused: false, + _launches: [{ id: 'l1', name: 'Test', trajectory: [{}, {}] }], + _replayTracks: new Map([ + [ + 'l1', + { + ascentPath: path, + getReplayState: () => ({ + ascending: true, + countdownActive: false, + phaseProgress: 0.5, + elapsedSinceStart: 10, + eventTime: 0, + }), + }, + ], + ]), + }; + const parts = createOverlays({ + state, + services: {}, + parts: { + paths: { + samplePath: (points, t) => ({ x: points[1].x * t, y: 0, z: 0 }), + }, + replay: { + replayVehicleScreenRotation: (from, to) => + Math.atan2(to.x - from.x, -(to.y - from.y)), + }, + policyHelpers: { formatMissionEventTime: () => 'T+00:10' }, + }, + source: {}, + }); + try { + parts.createReplayVehicleOverlay(); + const occluder = { isPointVisible: () => true }; + assert.equal(parts.updateReplayVehicleOverlay(occluder), 'ascent'); + const view = state._replayVehicleOverlay; + assert.equal(view.visible, true); + assert.equal( + view.transform, + 'translate3d(200px, 300px, 0) translate(-50%, -50%)', + ); + assert.equal(view.rotation, '90deg', 'the path heads right on screen'); + assert.equal(projected.length, 2, 'vehicle and tangent point'); + behindEye = true; + assert.equal(parts.updateReplayVehicleOverlay(occluder), null); + assert.equal( + state._replayVehicleOverlay.visible, + false, + 'a vehicle behind the eye hides the overlay', + ); + } finally { + unbind(); + } +}); diff --git a/src/render/cesiumAllowList.json b/src/render/cesiumAllowList.json index 2ec2f172e..eeb9a448c 100644 --- a/src/render/cesiumAllowList.json +++ b/src/render/cesiumAllowList.json @@ -60,7 +60,6 @@ "src/layers/installations/rendering.js", "src/layers/launches/launchPad.js", "src/layers/launches/orbitRendering.js", - "src/layers/launches/overlays.js", "src/layers/launches/rendering.js", "src/layers/localAdsb/index.js", "src/layers/localAdsb/models.js",