feat(cockpit): ride a tracked satellite in an orbital view

With a satellite tracked and no aircraft tracked, Cockpit can ride the
satellite. Entry needs only the Satellites layer; debris can be tracked but
not entered, and voice stays aircraft-only. Double-clicking the tracked
satellite or the entry buttons (reading ORBIT) enter it.

The camera sits on the satellite's per-frame SGP4 position and looks
straight down, turned to the ground track (north-up for a satellite with
none). The optical zoom steps 1x to 8x with a focus pull, the arrows tilt
the view up to 45 degrees off nadir, and Home or 0 returns to nadir. Off
nadir the tilt reads its total angle first and the swath readout becomes an
approximation marked with ≈. While in orbit the map leases flat Esri
imagery, the cloud pass stays off and the view opens in the optical style.

The orbital HUD names the satellite with its owner's flag (from the
flag-icons package, loaded per country) or an organisation badge, its
operator, class, NORAD and COSPAR ids and TLE epoch with the element lines
on request, altitude, orbital speed and the sub-satellite point. CURRENTLY
OVER names the country and its first-level division, or the sea, and from
8x the most prominent populated place nearby, looked up offline from the
bundled Natural Earth packs (populated places are a new pack). The
satellites layer hides the ridden satellite's own marks, dims the other
orbit rings and ignores scene clicks and Escape while held. Exit restores
the camera's field of view and the satellite's follow camera.

C no longer enters or leaves Cockpit, bridge or orbit: it is left to the
app-wide CCTV shortcut, and Escape or the exit button leave first person.
This commit is contained in:
Denis Groshev committed 2026-11-09 00:24:22 +01:00
1 parent a648c9b665
commit 048293fc2a
67 files changed
+6642 -172

No files matched your search

+318
View File
@@ -0,0 +1,318 @@
import * as Cesium from 'cesium';
import { satelliteClassText } from '../../data/satelliteClass.js';
import {
classifySatellite,
cosparIdFromTleLine1,
satelliteClassEnterable,
satelliteOperator,
satelliteOwner,
tleEpochMs,
} from './classes.js';
import { ISS_NORAD } from './policy.js';
/** The layer id a satellite subject carries through Cockpit events and state. */
export const SATELLITE_COCKPIT_LAYER_ID = 'satellites';
/**
* While the Cockpit rides a satellite, every other orbit ring (the ISS ring
* included) draws at this fraction of its normal alpha, so the rings stay
* legible without competing with the nadir picture.
*/
export const COCKPIT_ORBIT_DIM_FACTOR = 0.3;
/**
* Write an orbit ring's alpha as a multiple of the alpha it was built with.
* The ring's per-instance attributes exist only once Cesium has updated the
* primitive; until then nothing is written and the next tick retries.
* @param {{primitive: object, instanceId: string, baseColor: Cesium.Color,
* baseDepthFailColor: Cesium.Color, alphaFactor: number}} path
* @param {number} factor 1 restores the built colours exactly.
* @returns {boolean} True when the ring now carries `factor`.
*/
export function applyOrbitPathAlpha(path, factor) {
if (!path?.primitive || !path.baseColor) return false;
if ((path.alphaFactor ?? 1) === factor) return true;
let attributes;
try {
attributes = path.primitive.getGeometryInstanceAttributes?.(
path.instanceId,
);
} catch {
return false;
}
if (!attributes) return false;
const scaled = (color) => color.withAlpha(color.alpha * factor);
attributes.color = Cesium.ColorGeometryInstanceAttribute.toValue(
factor === 1 ? path.baseColor : scaled(path.baseColor),
);
attributes.depthFailColor = Cesium.ColorGeometryInstanceAttribute.toValue(
factor === 1 ? path.baseDepthFailColor : scaled(path.baseDepthFailColor),
);
path.alphaFactor = factor;
return true;
}
/**
* Who and what a catalog entry is: class, COSPAR id, TLE, owner, operator.
* Fixed for the entry's life, so it is worked out once and kept on it.
* @param {number} noradId
* @param {object} sat Catalog entry.
*/
function satelliteIdentity(noradId, sat) {
if (sat.cockpitIdentity) return sat.cockpitIdentity;
const satelliteClass =
sat.satelliteClass ||
classifySatellite({ noradId, name: sat.name, group: sat.group });
const epochMs = tleEpochMs(sat.satrec);
const identity = Object.freeze({
satelliteClass,
cosparId: sat.satcat?.cosparId || cosparIdFromTleLine1(sat.line1),
tle:
sat.line1 && sat.line2 && epochMs !== null
? Object.freeze({ line1: sat.line1, line2: sat.line2, epochMs })
: null,
owner: freezeOwner(
satelliteOwner({ ucs: sat.ucs, satcat: sat.satcat, satelliteClass }),
),
operator: satelliteOperator({ ucs: sat.ucs, satelliteClass }),
});
sat.cockpitIdentity = identity;
return identity;
}
function freezeOwner(owner) {
return owner
? Object.freeze({
countryCode: owner.countryCode ?? null,
label: owner.label ?? null,
})
: null;
}
/**
* Normalize the tracked satellite and its per-frame fix into Cockpit subject
* info, in the shape every Cockpit subject kind shares.
* @param {number} noradId NORAD catalog number.
* @param {{name?: string, group?: string}|null} sat Catalog entry.
* @param {{latitude: number, longitude: number, altitude: number,
* speedMps?: number|null, groundTrackDeg?: number|null}|null} fix Per-frame
* SGP4 sample (orbits.js propagatePosition).
* @returns {object|null} Subject info, or null without a usable fix.
*/
export function satelliteCockpitInfo(noradId, sat, fix) {
if (!sat || !fix) return null;
const { latitude, longitude, altitude } = fix;
if (![latitude, longitude, altitude].every(Number.isFinite)) return null;
const isIss = noradId === ISS_NORAD;
const identity = satelliteIdentity(noradId, sat);
return {
kind: 'satellite',
layerId: SATELLITE_COCKPIT_LAYER_ID,
id: String(noradId),
noradId,
name: sat.name?.trim() || `SAT-${noradId}`,
// Localized at read time, as the vessel layer's type label is.
classLabel: satelliteClassText(identity.satelliteClass, sat.group, {
isIss,
}),
latitude,
longitude,
altitudeM: altitude,
// SGP4's TEME velocity: the inertial orbital speed, not ground speed.
velocityMps: Number.isFinite(fix.speedMps) ? fix.speedMps : null,
// Degrees clockwise from north; null for a geostationary satellite.
groundTrackDeg: Number.isFinite(fix.groundTrackDeg)
? fix.groundTrackDeg
: null,
stale: false,
// src/layers/satellites/classes.js: 'stations'|'imaging'|'telecom'|'starlink'|
// 'probes'|'debris'.
satelliteClass: identity.satelliteClass,
// Whether its class may be ridden in the orbital view (debris may not);
// the Cockpit's entry gate reads it (orbitEntryAllowed).
enterable: satelliteClassEnterable(identity.satelliteClass),
// International designator, e.g. '1998-067A'.
cosparId: identity.cosparId,
tle: identity.tle,
// { countryCode: ISO 3166-1 alpha-2 or null (organisations, joint
// owners), label: English name } or null.
owner: identity.owner,
operator: identity.operator,
};
}
/**
* The tracked satellite as a Cockpit orbital-view subject, and the hold that
* hides its own marks while the Cockpit camera rides it.
*
* While held, the tracked dot is hidden (it would sit on the lens), as are
* the point primitives of docked companions (they share the position within
* 2 km) and the subject's orbit ring: a nadir camera on the ring sees its far
* side, dimmed by the ring's depth-fail appearance, drawn straight across the
* Earth's disk. The detection overlay skips the subject too. The tracked
* card needs nothing here: its overlay source already hides in Cockpit mode.
* Every other orbit ring is dimmed to COCKPIT_ORBIT_DIM_FACTOR of its alpha
* while a subject is set; other satellites' points and labels stay as they
* are. Scene clicks and the layer's Escape are ignored while held, so the
* picture cannot untrack its own subject. Release restores exactly what the
* hold hid or dimmed.
*/
export function createCockpitSubject({ state: layerState, parts }) {
/** The subject is held only while it is the tracked satellite. */
function _cockpitHeld() {
return (
layerState._cockpitSubjectNorad !== null &&
layerState._cockpitSubjectNorad === layerState._trackedNorad
);
}
function _setPointShown(noradId, show) {
const point = layerState._points.get(noradId);
// Released points go back to their class's visibility.
if (point) point.show = show && parts.controls._classVisible(noradId);
}
/**
* Apply or release the hold. Idempotent, and cheap enough for the preRender
* tick, which re-applies it while held so catalog rebuilds, enable and the
* orbit toggle cannot re-show what the hold hides.
* @returns {void}
*/
function _syncCockpitSubjectVisuals() {
const held = _cockpitHeld();
const tracked = layerState._trackedNorad;
const dot = layerState._trackedEntity?.point;
if (held) {
if (dot) dot.show = false;
layerState._cockpitHiddenDot = true;
} else if (layerState._cockpitHiddenDot) {
if (dot) dot.show = true;
layerState._cockpitHiddenDot = false;
}
const hiddenPoints = layerState._cockpitHiddenPoints;
const companions = held ? layerState._dockedCompanions : new Set();
for (const noradId of [...hiddenPoints]) {
if (companions.has(noradId)) continue;
hiddenPoints.delete(noradId);
// The tracked primitive stays hidden: its entity draws the dot.
if (noradId !== tracked) _setPointShown(noradId, true);
}
for (const noradId of companions) {
if (noradId === tracked || hiddenPoints.has(noradId)) continue;
hiddenPoints.add(noradId);
_setPointShown(noradId, false);
}
const hiddenPath = layerState._cockpitHiddenPath;
if (hiddenPath !== null && (!held || hiddenPath !== tracked)) {
const path = layerState._orbitPaths.get(hiddenPath);
if (path)
path.primitive.show = parts.controls.satelliteVisualsVisible(
layerState._enabled,
layerState._params.showOrbits,
);
layerState._cockpitHiddenPath = null;
}
if (held) {
const path = layerState._orbitPaths.get(tracked);
if (path) {
path.primitive.show = false;
layerState._cockpitHiddenPath = tracked;
}
}
// Every other ring is dimmed for the whole session and restored on
// release; rebuilt rings arrive at full alpha and are dimmed again here.
const subject = layerState._cockpitSubjectNorad;
for (const [noradId, path] of layerState._orbitPaths) {
const dim = subject !== null && noradId !== subject;
applyOrbitPathAlpha(path, dim ? COCKPIT_ORBIT_DIM_FACTOR : 1);
}
}
const methods = {
/**
* The tracked satellite as Cockpit subject info, from the same per-frame
* SGP4 sample the dot and the follow camera use.
* @returns {{kind: 'satellite', layerId: 'satellites', id: string,
* noradId: number, name: string, classLabel: string,
* latitude: number, longitude: number, altitudeM: number,
* velocityMps: number|null, stale: false,
* satelliteClass: 'stations'|'imaging'|'telecom'|'starlink'|'probes'|'debris',
* cosparId: string|null,
* tle: {line1: string, line2: string, epochMs: number}|null,
* owner: {countryCode: string|null, label: string|null}|null,
* operator: string|null}|null} Null while the layer is off or nothing
* is tracked.
*/
getCockpitSubject() {
if (!layerState._enabled || layerState._trackedNorad === null)
return null;
const noradId = layerState._trackedNorad;
return satelliteCockpitInfo(
noradId,
layerState._catalog.get(noradId),
parts.tracking._getTrackedFramePosition(),
);
},
/**
* The tracked satellite's Earth-fixed position this frame: the per-frame
* cache the dot and follow camera read, never a separate propagation.
* @param {Cesium.Cartesian3} [result] Receives the position.
* @returns {Cesium.Cartesian3|null} Null while the layer is off, nothing
* is tracked or no sample has propagated.
*/
getCockpitPosition(result) {
if (!layerState._enabled || layerState._trackedNorad === null)
return null;
if (!parts.tracking._getTrackedFramePosition()) return null;
return Cesium.Cartesian3.clone(layerState._trackedFrameCartesian, result);
},
/**
* Mark the satellite whose position the Cockpit camera occupies, or
* release it. The hold applies while that satellite is tracked.
* @param {string|number|null} noradId Subject NORAD id, or null to release.
* @returns {boolean} Always true.
*/
setCockpitSubject(noradId) {
const next =
noradId === null || noradId === undefined
? null
: parts.tracking._normalizeTrackedNorad(noradId);
if (layerState._cockpitSubjectNorad === next) return true;
layerState._cockpitSubjectNorad = next;
_syncCockpitSubjectVisuals();
return true;
},
/**
* Put the follow camera back on the tracked satellite after the Cockpit
* released it, without re-tracking (no new selection or context events).
* @param {string|number} noradId The satellite the Cockpit rode.
* @returns {boolean} True when that satellite is still tracked and the
* follow camera was restored.
*/
refocusTrackedById(noradId) {
const id = parts.tracking._normalizeTrackedNorad(noradId);
const viewer = layerState._viewer;
const entity = layerState._trackedEntity;
if (
id === null ||
id !== layerState._trackedNorad ||
!viewer ||
!entity ||
!viewer.entities?.contains?.(entity)
)
return false;
viewer.camera?.cancelFlight?.();
viewer.trackedEntity = entity;
return true;
},
};
return { _cockpitHeld, _syncCockpitSubjectVisuals, methods };
}