mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
Aircraft draw from GPU icon sets, so the billboard mirror, the billboard treatment and the aircraft visual-state module had no callers left. 3D models are now records the layer holds under the contact's id (modelRecord.js): show and placement are written through showModel/placeModel and read back from the record; renderer-measured values stay on record.model. The tracked-model click check only matches when a tracked model exists, so an id-only pick is never mistaken for a click on the tracked plane.
486 lines
18 KiB
JavaScript
486 lines
18 KiB
JavaScript
import * as Cesium from 'cesium';
|
||
import {
|
||
lerpAngleDeg,
|
||
COURSE_HOLD_SPEED_MPS,
|
||
arcOffsetEnu,
|
||
courseSlewCapDps,
|
||
limitCourseStep,
|
||
} from '../../data/motionModel.js';
|
||
import { CLASS_SCALE_2D } from '../../data/aircraftClass.js';
|
||
import {
|
||
modelVisualAnchor,
|
||
modelAnchorWorld,
|
||
} from '../../data/modelVisualAnchor.js';
|
||
import {
|
||
RENDER_DELAY_SEC,
|
||
TRACKED_MODEL_MAX_PX,
|
||
TRACKED_MODEL_MIN_PX,
|
||
BILLBOARD_SCALE,
|
||
TRACKED_BILLBOARD_SCALE_BY_DISTANCE,
|
||
DR_CORRECTION_MS,
|
||
COURSE_SLEW_DT_MAX_SEC,
|
||
COURSE_MAX_DPS,
|
||
} from './policy.js';
|
||
import { readEntityRecord } from '../../data/entityRecords.js';
|
||
import {
|
||
aircraftMotionRegime,
|
||
createMotionRegime,
|
||
fixSpeedMps,
|
||
fixTrackDeg,
|
||
} from '../../data/aircraftMotionRegime.js';
|
||
|
||
/** Where a military record carries its reported speed and track. */
|
||
const RECORD_FIELDS = { speed: 'speedMps', track: 'track' };
|
||
|
||
export function createMotion({
|
||
flightState,
|
||
services,
|
||
parts,
|
||
layer,
|
||
resolveAsset,
|
||
}) {
|
||
const { trackedModelScaleForPixelCap } = services.camera;
|
||
const {
|
||
nearFarScalarValueAtDistance,
|
||
clearFocusTarget,
|
||
publishFocusTargetFromCachedPosition,
|
||
} = services.focus;
|
||
/** `_motionRegime`'s reused result. */
|
||
const regime = createMotionRegime();
|
||
|
||
/**
|
||
* Dead-reckon an aircraft's current position using ENU (East-North-Up) frame math.
|
||
*
|
||
* Renders one poll interval behind real time so positions interpolate between
|
||
* two KNOWN fixes whenever possible (see RENDER_DELAY_SEC rationale). When the
|
||
* newest fix is older than the delayed render time (stale position), projects
|
||
* forward using the aircraft's ground speed and track through the freshest
|
||
* source contact plus a bounded grace window.
|
||
*
|
||
* @param {string} icao24 - ICAO hex identifier of the aircraft
|
||
* @param {Cesium.Cartesian3} [result] - Optional out-parameter to write into.
|
||
* @returns {Cesium.Cartesian3|null} Estimated current ECEF position, or null if no history
|
||
*/
|
||
|
||
function _deadReckon(icao24, result) {
|
||
const history = flightState._positionHistory.get(icao24);
|
||
const info = flightState.records.data.get(icao24);
|
||
if (!history || history.length === 0) {
|
||
flightState._drCourseDeg = null;
|
||
flightState._drSpeedMps = null;
|
||
flightState._drCourseHold = false;
|
||
return null;
|
||
}
|
||
|
||
const out = result || new Cesium.Cartesian3();
|
||
// Render one poll interval behind real time so we interpolate between
|
||
// two KNOWN fixes whenever possible (see RENDER_DELAY_SEC rationale).
|
||
const renderTime = Cesium.JulianDate.addSeconds(
|
||
Cesium.JulianDate.now(),
|
||
-RENDER_DELAY_SEC,
|
||
flightState._scratchRenderTime,
|
||
);
|
||
|
||
const regime = _motionRegime(history, info, renderTime);
|
||
if (regime.kind === 'between') {
|
||
const { a, b, span } = regime;
|
||
const t =
|
||
span > 0
|
||
? Cesium.JulianDate.secondsDifference(renderTime, a.time) / span
|
||
: 1.0;
|
||
const trackCourse = lerpAngleDeg(regime.trackFrom, regime.trackTo, t);
|
||
flightState._drCourseDeg =
|
||
regime.chordCourse != null
|
||
? lerpAngleDeg(trackCourse, regime.chordCourse, regime.blend)
|
||
: trackCourse;
|
||
flightState._drSpeedMps = regime.speed;
|
||
flightState._drCourseHold = regime.speed < COURSE_HOLD_SPEED_MPS;
|
||
return Cesium.Cartesian3.lerp(a.position, b.position, t, out);
|
||
}
|
||
const dt = Cesium.Math.clamp(
|
||
Cesium.JulianDate.secondsDifference(renderTime, regime.fix.time),
|
||
regime.minDt,
|
||
regime.maxDt,
|
||
);
|
||
return _extrapolateFix(regime.fix, info, dt, out, regime.turnRateDps);
|
||
}
|
||
|
||
/**
|
||
* Which motion a contact's history gives at `renderTime` — the one choice
|
||
* `_deadReckon` (CPU) and the icon planner (GPU segment) both draw from
|
||
* (`aircraftMotionRegime`). The result is a reused scratch: read it before
|
||
* the next call.
|
||
*/
|
||
function _motionRegime(history, info, renderTime) {
|
||
return aircraftMotionRegime(
|
||
history,
|
||
info,
|
||
renderTime,
|
||
RECORD_FIELDS,
|
||
regime,
|
||
);
|
||
}
|
||
|
||
/**
|
||
* Dead-reckon a fix along its own velocity/track by `dt` seconds, integrating a
|
||
* constant-rate-turn arc when `turnRateDps` is significant (straight line
|
||
* otherwise). Positive `dt` projects FORWARD (after the fix); negative `dt`
|
||
* projects BACKWARD (before the fix) — used for warm-up, estimating where the
|
||
* aircraft was before its first observed fix. ENU frame: east = +X, north = +Y,
|
||
* up = +Z; heading 0 deg = north, 90 deg = east (clockwise from north). Sets
|
||
* `_drCourseDeg` to the arc's instantaneous end course on every path.
|
||
* Arc math adapted from skylight (https://github.com/cpaczek/skylight, MIT).
|
||
*/
|
||
|
||
function _extrapolateFix(fix, info, dt, out, turnRateDps = 0) {
|
||
const speed = fixSpeedMps(fix, info, RECORD_FIELDS);
|
||
const heading = fixTrackDeg(fix, info, RECORD_FIELDS);
|
||
flightState._drSpeedMps = speed;
|
||
flightState._drCourseHold = speed < COURSE_HOLD_SPEED_MPS;
|
||
if (speed === 0 || dt === 0) {
|
||
flightState._drCourseDeg = heading;
|
||
return Cesium.Cartesian3.clone(fix.position, out);
|
||
}
|
||
// Constant-rate-turn arc (straight line when turnRateDps ≈ 0) — a plane in a
|
||
// standard-rate turn is ~90° of arc wrong per 30 s if extrapolated straight.
|
||
arcOffsetEnu(speed, heading, turnRateDps, dt, flightState._scratchArc);
|
||
flightState._drCourseDeg = flightState._scratchArc.endCourseDeg;
|
||
Cesium.Cartesian3.fromElements(
|
||
flightState._scratchArc.east,
|
||
flightState._scratchArc.north,
|
||
0,
|
||
flightState._scratchOffset,
|
||
);
|
||
const enu = Cesium.Transforms.eastNorthUpToFixedFrame(
|
||
fix.position,
|
||
Cesium.Ellipsoid.WGS84,
|
||
flightState._scratchEnu,
|
||
);
|
||
return Cesium.Matrix4.multiplyByPoint(enu, flightState._scratchOffset, out);
|
||
}
|
||
|
||
/**
|
||
* True while the tracked aircraft's delayed display time (now − RENDER_DELAY_SEC)
|
||
* predates its oldest real fix — _deadReckon is extrapolating backward with no real
|
||
* history yet behind the displayed icon, so the trail must draw nothing.
|
||
* @returns {boolean}
|
||
*/
|
||
|
||
function _isTrackWarmingUp() {
|
||
if (!flightState._trackedIcao) return false;
|
||
const history = flightState._positionHistory.get(flightState._trackedIcao);
|
||
if (!history || history.length === 0) return true;
|
||
const renderTime = Cesium.JulianDate.addSeconds(
|
||
Cesium.JulianDate.now(),
|
||
-RENDER_DELAY_SEC,
|
||
flightState._scratchWarmupTime,
|
||
);
|
||
return Cesium.JulianDate.lessThan(renderTime, history[0].time);
|
||
}
|
||
|
||
/** Resolve the selected aircraft's actual rendered square extent this frame. */
|
||
|
||
function _trackedFocusSizePx(icao24, position) {
|
||
const camera = flightState._viewer?.camera;
|
||
const scene = flightState._viewer?.scene;
|
||
if (!camera?.positionWC || !position || !scene) return 28;
|
||
const rangeM = Cesium.Cartesian3.distance(camera.positionWC, position);
|
||
if (parts.rendering._modelOwnsVisual(icao24)) {
|
||
const spec = parts.rendering._modelSpec(
|
||
flightState.records.data.get(icao24)?.klass,
|
||
);
|
||
const scale = trackedModelScaleForPixelCap({
|
||
baseScale: spec.scale,
|
||
nativeRadiusM: spec.nativeRadiusM,
|
||
rangeM,
|
||
viewportHeightPx: scene.canvas.clientHeight,
|
||
fovyRad: camera.frustum.fovy,
|
||
maximumPixelSize: TRACKED_MODEL_MAX_PX,
|
||
});
|
||
const focalLengthPx =
|
||
scene.canvas.clientHeight / (2 * Math.tan(camera.frustum.fovy / 2));
|
||
const projectedDiameterPx =
|
||
(2 * spec.nativeRadiusM * scale * focalLengthPx) / rangeM;
|
||
return Math.max(
|
||
TRACKED_MODEL_MIN_PX,
|
||
Math.min(TRACKED_MODEL_MAX_PX, projectedDiameterPx),
|
||
);
|
||
}
|
||
|
||
const sprite = readEntityRecord(flightState._trackedEntity)?.sprite;
|
||
const width = sprite?.width ?? 28;
|
||
const height = sprite?.height ?? 28;
|
||
const scale =
|
||
sprite?.scale ??
|
||
BILLBOARD_SCALE *
|
||
(CLASS_SCALE_2D[flightState.records.data.get(icao24)?.klass] || 1);
|
||
const scaleByDistance =
|
||
sprite?.scaleByDistance ?? TRACKED_BILLBOARD_SCALE_BY_DISTANCE;
|
||
const distanceScale = nearFarScalarValueAtDistance(scaleByDistance, rangeM);
|
||
return Math.max(width, height) * scale * distanceScale;
|
||
}
|
||
|
||
/**
|
||
* Per-frame-cached, discontinuity-smoothed tracked DISPLAY position. Cached by Cesium
|
||
* frame number so the position / rotation / trail-head callbacks share ONE computation
|
||
* (and one reconciliation-state update) per frame. Returns a stable module holder, or
|
||
* null when the aircraft has no fix.
|
||
* @param {string} icao24
|
||
* @returns {Cesium.Cartesian3|null}
|
||
*/
|
||
|
||
function _trackedDisplayPosition(icao24) {
|
||
const frame = flightState._viewer?.scene?.frameState?.frameNumber ?? -1;
|
||
if (
|
||
frame === flightState._cachedDRFrame &&
|
||
icao24 === flightState._drReconcileIcao
|
||
)
|
||
return flightState._cachedDRPosition;
|
||
|
||
// Does the reconciliation state belong to THIS aircraft? (Capture before overwriting
|
||
// _drReconcileIcao, so a track switch doesn't inherit the old plane's _drPrevRaw.)
|
||
const sameTrack =
|
||
flightState._drReconcileValid && flightState._drReconcileIcao === icao24;
|
||
const raw = _deadReckon(icao24, flightState._scratchDrRaw);
|
||
flightState._cachedDRCourse = flightState._drCourseDeg;
|
||
flightState._cachedDRSpeedMps = flightState._drSpeedMps;
|
||
flightState._cachedDRHold = flightState._drCourseHold;
|
||
flightState._cachedDRFrame = frame;
|
||
flightState._drReconcileIcao = icao24;
|
||
if (!raw) {
|
||
flightState._cachedDRPosition = null;
|
||
flightState._drReconcileValid = false;
|
||
clearFocusTarget('militaryFlights', icao24);
|
||
return null;
|
||
}
|
||
|
||
const nowMs = Date.now();
|
||
const info = flightState.records.data.get(icao24);
|
||
if (sameTrack) {
|
||
const dtSec = Math.max(0.001, (nowMs - flightState._drPrevMs) / 1000);
|
||
const speed = (info && info.speedMps) || 0;
|
||
// Plausible single-frame motion (m): real velocity × frame Δt, ×4 slack + 25 m base.
|
||
const plausible = speed * dtSec * 4 + 25;
|
||
if (Cesium.Cartesian3.distance(raw, flightState._drPrevRaw) > plausible) {
|
||
// Discontinuity — re-anchor so the DISPLAYED position stays continuous, then decay.
|
||
Cesium.Cartesian3.subtract(
|
||
flightState._drPrevDisplay,
|
||
raw,
|
||
flightState._drCorrection,
|
||
);
|
||
flightState._drCorrectionStartMs = nowMs;
|
||
}
|
||
} else {
|
||
Cesium.Cartesian3.fromElements(0, 0, 0, flightState._drCorrection);
|
||
flightState._drCorrectionStartMs = nowMs - DR_CORRECTION_MS; // fully decayed
|
||
}
|
||
|
||
const elapsed = nowMs - flightState._drCorrectionStartMs;
|
||
const factor =
|
||
elapsed >= DR_CORRECTION_MS ? 0 : 1 - elapsed / DR_CORRECTION_MS;
|
||
const display = Cesium.Cartesian3.multiplyByScalar(
|
||
flightState._drCorrection,
|
||
factor,
|
||
flightState._trackedPosHolder,
|
||
);
|
||
Cesium.Cartesian3.add(raw, display, display);
|
||
|
||
Cesium.Cartesian3.clone(raw, flightState._drPrevRaw);
|
||
Cesium.Cartesian3.clone(display, flightState._drPrevDisplay);
|
||
flightState._drPrevMs = nowMs;
|
||
flightState._drReconcileValid = true;
|
||
flightState._cachedDRPosition = display;
|
||
const focusSizePx = _trackedFocusSizePx(
|
||
icao24,
|
||
flightState._cachedDRPosition,
|
||
);
|
||
// Publish the exact frame cache shared by entity + follow camera. A second
|
||
// DR sample here would advance reconciliation in a different frame phase
|
||
// and visibly jitter the focus rectangle against the tracked aircraft.
|
||
publishFocusTargetFromCachedPosition({
|
||
ownerLayer: 'militaryFlights',
|
||
id: icao24,
|
||
scene: flightState._viewer?.scene,
|
||
camera: flightState._viewer?.camera,
|
||
displayPosition: flightState._cachedDRPosition,
|
||
widthPx: focusSizePx,
|
||
heightPx: focusSizePx,
|
||
});
|
||
return display;
|
||
}
|
||
|
||
/** The tracked plane's display position WITHOUT recomputing — the value the follow-camera already
|
||
* settled on this frame. Mirror of flights.js: getDetectableObjects + the readout run in postRender at
|
||
* a later frameNumber, so recomputing _trackedDisplayPosition there jitters the label against the
|
||
* now-stable plane. Null when no valid fix (callers fall back to the icon position). */
|
||
|
||
function _trackedDisplayCached() {
|
||
return flightState._drReconcileValid &&
|
||
flightState._drReconcileIcao === flightState._trackedIcao
|
||
? flightState._cachedDRPosition
|
||
: null;
|
||
}
|
||
|
||
/**
|
||
* The position the tracked aircraft is VISUALLY at this frame — the translation its 3D
|
||
* model is actually rendering with when the model owns the visual, otherwise the cached
|
||
* dead-reckoned position. Mirror of flights.js; see that copy for the full rationale.
|
||
* Reads the modelMatrix the tracked-model update already wrote this frame: no sampling,
|
||
* no `_modelDisplayPosition` from postRender, and the entity record's `displayPosition` keeps its
|
||
* follow-camera anti-jitter contract untouched.
|
||
*/
|
||
|
||
function _trackedVisualCached() {
|
||
if (
|
||
flightState._trackedIcao &&
|
||
parts.rendering._modelOwnsVisual(flightState._trackedIcao)
|
||
) {
|
||
const spec = parts.rendering._modelSpec(
|
||
flightState.records.data.get(flightState._trackedIcao)?.klass,
|
||
);
|
||
return modelVisualAnchor(
|
||
flightState._trackedModel.matrix,
|
||
spec.visualCenterNative,
|
||
Number.isFinite(flightState._trackedModel.model.computedScale)
|
||
? flightState._trackedModel.model.computedScale
|
||
: spec.scale,
|
||
flightState._trackedVisualPos,
|
||
);
|
||
}
|
||
return _trackedDisplayCached();
|
||
}
|
||
|
||
/** The tracked model's rendered bounding radius (m), or 0 when no model of this
|
||
* contact is drawing. Carries Cesium's effective `computedScale`, which may be
|
||
* above `scale` to satisfy minimumPixelSize — the trail head has to be judged
|
||
* against the size the operator SEES, not the nominal one. */
|
||
/** World-space origin of the tracked model, or null when no model of this
|
||
* contact is drawing. This is the centre the rendered bounding sphere is
|
||
* measured from, so the trail clip and the envelope agree on one frame. */
|
||
|
||
function _trackedModelCenterWorld() {
|
||
if (
|
||
!flightState._trackedIcao ||
|
||
!flightState._trackedModel ||
|
||
!parts.rendering._modelOwnsVisual(flightState._trackedIcao)
|
||
)
|
||
return null;
|
||
return Cesium.Matrix4.getTranslation(
|
||
flightState._trackedModel.matrix,
|
||
flightState._scratchTrailClip,
|
||
);
|
||
}
|
||
|
||
function _trackedModelEnvelopeM() {
|
||
if (
|
||
!flightState._trackedIcao ||
|
||
!flightState._trackedModel ||
|
||
!parts.rendering._modelOwnsVisual(flightState._trackedIcao)
|
||
)
|
||
return 0;
|
||
const spec = parts.rendering._modelSpec(
|
||
flightState.records.data.get(flightState._trackedIcao)?.klass,
|
||
);
|
||
const scale = Number.isFinite(flightState._trackedModel.model.computedScale)
|
||
? flightState._trackedModel.model.computedScale
|
||
: spec.scale;
|
||
return spec.nativeRadiusM * scale;
|
||
}
|
||
|
||
/** Trail endpoint for the rendered tracked owner. Brackets/readouts stay on
|
||
* the visual centre; only the trail moves to the model's aft-belly hardpoint. */
|
||
|
||
function _trackedTrailCached() {
|
||
if (
|
||
flightState._trackedIcao &&
|
||
parts.rendering._modelOwnsVisual(flightState._trackedIcao)
|
||
) {
|
||
const spec = parts.rendering._modelSpec(
|
||
flightState.records.data.get(flightState._trackedIcao)?.klass,
|
||
);
|
||
// Through the model's OWN render chain (modelVisualAnchor's hand-rolled
|
||
// half-correction put this offset on the lateral axis — see
|
||
// modelAnchorWorld).
|
||
return modelAnchorWorld(
|
||
flightState._trackedModel,
|
||
spec.trailAnchorNative,
|
||
flightState._trackedTrailPos,
|
||
);
|
||
}
|
||
return _trackedDisplayCached();
|
||
}
|
||
|
||
/** Smoothed world course for the tracked aircraft this frame. Reads the
|
||
* frame-cached course (set by _trackedDisplayPosition — the follow-camera's
|
||
* own computation), NEVER re-runs _deadReckon. Safe to call more than once
|
||
* per frame: the second call sees dt≈0 and the limiter is a no-op.
|
||
*
|
||
* The smoothed value lives in the SHARED per-icao _displayCourse entry (see
|
||
* its declaration): on click the limiter continues from whatever nose the
|
||
* fleet pass was displaying, and on untrack the fleet pass continues from
|
||
* whatever nose this path last wrote — tracked and fleet consumers of the
|
||
* same aircraft can never disagree across the handoff (mirror of flights.js). */
|
||
|
||
function _trackedDisplayCourse() {
|
||
const info = flightState.records.data.get(flightState._trackedIcao);
|
||
const fallback = (info && info.track) || 0;
|
||
const cacheValid =
|
||
flightState._drReconcileValid &&
|
||
flightState._drReconcileIcao === flightState._trackedIcao &&
|
||
flightState._cachedDRCourse != null;
|
||
const raw = cacheValid ? flightState._cachedDRCourse : fallback;
|
||
const nowMs = Date.now();
|
||
const dt = flightState._trackedCourseMs
|
||
? Math.min(
|
||
COURSE_SLEW_DT_MAX_SEC,
|
||
(nowMs - flightState._trackedCourseMs) / 1000,
|
||
)
|
||
: 0;
|
||
flightState._trackedCourseMs = nowMs;
|
||
const prev = flightState._displayCourse.get(flightState._trackedIcao);
|
||
// Hover hold: at near-zero displayed speed both the chord and the reported
|
||
// track are noise — keep the last stable nose direction instead of chasing.
|
||
if (cacheValid && flightState._cachedDRHold && prev != null) return prev;
|
||
const cap = courseSlewCapDps(
|
||
cacheValid
|
||
? flightState._cachedDRSpeedMps
|
||
: ((info && info.speedMps) ?? NaN),
|
||
COURSE_MAX_DPS,
|
||
);
|
||
const course = limitCourseStep(prev, raw, cap, dt);
|
||
flightState._displayCourse.set(flightState._trackedIcao, course);
|
||
return course;
|
||
}
|
||
|
||
/** Reset reconciliation + per-frame cache when tracking stops or switches
|
||
* target. Deliberately does NOT touch _displayCourse: the smoothed course
|
||
* entry belongs to the AIRCRAFT (not to the tracked session) and must
|
||
* survive the handoff back to the fleet pass. */
|
||
|
||
function _resetTrackedDisplay() {
|
||
flightState._drReconcileValid = false;
|
||
flightState._drReconcileIcao = null;
|
||
flightState._cachedDRFrame = -1;
|
||
flightState._cachedDRPosition = null;
|
||
flightState._cachedDRCourse = null;
|
||
flightState._cachedDRSpeedMps = null;
|
||
flightState._cachedDRHold = false;
|
||
flightState._trackedCourseMs = 0;
|
||
}
|
||
return {
|
||
_deadReckon,
|
||
_motionRegime,
|
||
_extrapolateFix,
|
||
_isTrackWarmingUp,
|
||
_trackedFocusSizePx,
|
||
_trackedDisplayPosition,
|
||
_trackedDisplayCached,
|
||
_trackedVisualCached,
|
||
_trackedModelCenterWorld,
|
||
_trackedModelEnvelopeM,
|
||
_trackedTrailCached,
|
||
_trackedDisplayCourse,
|
||
_resetTrackedDisplay,
|
||
};
|
||
}
|