Draw military flights from the shared GPU icon set

The flights icon planning and presentation move into
`createAircraftIcons` (src/data/aircraftIconLayer.js), and the motion
regime both layers dead-reckon from into src/data/aircraftMotionRegime.js.
Flights keeps only its grounded-floor pass on top.

Military now draws every contact as a row of that icon set, like
flights: the billboards, the cull positions and the rotation-pass
signature go. Tracking hides the row and untracking re-plans it from
the tracked course. Queries read the drawn row position and judge the
horizon as the shader does. Snapshots that arrive before init or after
destroy are ignored, and per-contact lookups find no contact then
instead of throwing.

With about 120 live military contacts, preRender p95 drops from
0.7-0.8 ms to 0.2 ms on the same views. Render time and frame gaps are
unchanged or slightly lower.
This commit is contained in:
Sameh Khamis committed 2026-11-03 20:30:43 -08:00
1 parent fa3a3a3bca
commit b027bde6ad
33 files changed
+1256 -1376

No files matched your search

+37 -361
View File
@@ -1,35 +1,11 @@
import * as Cesium from 'cesium';
import { courseSlewCapDps, norm180 } from '../../data/motionModel.js';
import {
COURSE_HOLD_SPEED_MPS,
courseBetweenCartesians,
courseSlewCapDps,
norm180,
speedRamp,
staleCoastLimitSeconds,
} from '../../data/motionModel.js';
import {
createIconSet,
ICON_CULL_LIFT,
ICON_RECEDES,
ICON_ROTATES,
} from '../../data/iconSet.js';
import {
segmentCourse,
segmentTargetCourse,
writeArcSegment,
writeCourseGlide,
writeHoldSegment,
writeLerpSegment,
} from '../../data/iconSegment.js';
import { IconSetPrimitive } from '../../data/iconSetPrimitive.js';
import { iconAtlas } from '../../data/iconAtlas.js';
import {
aircraftIcon,
FLEET_ICON_PX,
TRACKED_ICON_PX,
} from '../../data/aircraftIcons.js';
import { cockpitContactDotImage } from '../../data/cockpitContactDot.js';
import { horizonOccluder } from '../../data/iconOrientation.js';
import { createAircraftIcons } from '../../data/aircraftIconLayer.js';
import {
COCKPIT_CONTACT_SIZE_PX,
COURSE_MAX_DPS,
@@ -38,184 +14,48 @@ import {
RENDER_DELAY_SEC,
} from './policy.js';
/** Raster size of the cockpit contact pip (cockpitContactDot.js draws 16 px). */
const COCKPIT_DOT_PX = 16;
/** Pinned grounded rows rewrite only past this displayed move (m²) or turn (°). */
const PIN_MOVE_SQ_M = 1;
const PIN_TURN_DEG = 0.1;
/**
* The flights layer's contacts as one icon set: each row's motion segment
* mirrors `_deadReckon`'s regime for the delayed display time, so the GPU
* moves every untracked contact between polls with no per-frame CPU pass.
* The flights layer's contacts as one icon set (`createAircraftIcons`), plus
* the grounded floor: a floor-pinned row holds a CPU position, so it is not
* planned, and settling a grounded contact puts it straight onto the floor.
*/
export function createIcons({ flightState, services, parts }) {
const { focusPassIsNeeded } = services.focus;
const scratchTime = new Cesium.JulianDate();
const scratchPosition = new Cesium.Cartesian3();
const scratchCull = new Cesium.Cartesian3();
const scratchScaleByDistance = new Cesium.NearFarScalar();
let normalScaleByDistance = null;
let cockpitScaleByDistance = null;
/** The delayed instant every untracked contact is drawn at. */
function _displayTime(result = scratchTime) {
return Cesium.JulianDate.addSeconds(
Cesium.JulianDate.now(result),
-RENDER_DELAY_SEC,
result,
);
}
function _displayTau() {
return flightState._icons.seconds(_displayTime());
}
/** Create the set and its primitive for one layer lifetime. */
function _createIcons(viewer) {
flightState._icons = createIconSet({ epoch: _displayTime() });
flightState._iconPrimitive = new IconSetPrimitive({
set: flightState._icons,
renderTime: () => _displayTime(flightState._scratchIconRenderTime),
});
viewer.scene.primitives.add(flightState._iconPrimitive);
return flightState._iconPrimitive;
}
/** Where a contact is drawn right now, or null when it has no row. */
function _iconPosition(icao24, result = new Cesium.Cartesian3()) {
const set = flightState._icons;
const row = set.row(icao24);
return row < 0 ? null : set.position(row, _displayTau(), result);
}
/** The nose direction a contact is drawn with right now. */
function _iconCourse(icao24) {
const set = flightState._icons;
const row = set.row(icao24);
return row < 0 ? null : set.course(row, _displayTau());
}
/** Whether the layer draws this contact's icon (not tracked, not model-owned). */
function _iconShown(icao24) {
const set = flightState._icons;
const row = set.row(icao24);
return row >= 0 && set.shown(row);
}
/** The camera's horizon test, or null when there is no placed camera. */
function _occluder() {
const camera = flightState._viewer?.camera;
return camera?.positionWC ? horizonOccluder(camera) : null;
}
/** Whether a drawn row at `position` clears the camera's horizon, judged
* exactly as the icon shader judges it. No camera, no horizon. */
function _aboveHorizon(row, position, occluder) {
return (
!occluder ||
occluder.isPointVisible(
flightState._icons.cullPoint(row, position, scratchCull),
)
);
}
/**
* Write the segment `_deadReckon` would follow from now on: interpolation
* between the bracketing fixes, the warm-up arc back from the oldest fix, or
* the coast arc past the newest. The displayed nose glides from where it is
* onto the new course at the fleet's slew cap; a hovering contact keeps its
* nose. The row expires when the regime would change.
* @param {string} icao24
* @param {{fromCourseDeg?: number|null}} [options] Nose to glide from when
* the row's own segment is not what was on screen (tracking handoff).
*/
function _planIcon(icao24, { fromCourseDeg } = {}) {
const set = flightState._icons;
const row = set.row(icao24);
if (row < 0) return;
if (flightState._focusEvidenceIds.has(icao24)) return;
if (flightState._floorPinned.has(icao24)) return;
const history = flightState._positionHistory.get(icao24);
if (!history?.length) return;
const info = flightState.records.data.get(icao24);
const renderTime = _displayTime();
const tau = set.seconds(renderTime);
const seg = set.segments;
const shownCourse =
fromCourseDeg ??
(set.expires(row) === -Infinity ? null : set.course(row, tau));
const regime = parts.motion._motionRegime(history, info, renderTime);
let speed;
let expiresAt = Infinity;
if (regime.kind === 'between') {
const { a, b, span, trackFrom, trackTo, chordCourse } = regime;
speed = regime.speed;
const blend = chordCourse == null ? 0 : regime.blend;
const t0 = set.seconds(a.time);
if (span > 0) {
writeLerpSegment(seg, row, {
from: a.position,
to: b.position,
t0,
t1: set.seconds(b.time),
course0Deg:
trackFrom + blend * norm180((chordCourse ?? 0) - trackFrom),
courseSlopeDeg: norm180(trackTo - trackFrom) * (1 - blend),
});
} else {
writeHoldSegment(
seg,
row,
b.position,
trackTo + blend * norm180((chordCourse ?? 0) - trackTo),
);
}
expiresAt = set.seconds(b.time);
} else {
const { fix, heading } = regime;
speed = regime.speed;
const t0 = set.seconds(fix.time);
if (speed === 0) {
writeHoldSegment(seg, row, fix.position, heading);
} else {
writeArcSegment(seg, row, {
fix: fix.position,
t0,
minDt: regime.minDt,
maxDt: regime.maxDt,
speedMps: speed,
trackDeg: heading,
turnRateDps: regime.turnRateDps,
});
}
// A warm-up's arc ends at its fix; a coast's runs to its limit.
if (regime.maxDt === 0) expiresAt = t0;
}
// Below hover speed the nose holds what is on screen (or, for a new row,
// where this segment starts); above it, it glides on at the slew cap.
const hold = speed < COURSE_HOLD_SPEED_MPS;
if (hold || shownCourse != null) {
writeCourseGlide(
seg,
row,
shownCourse ?? segmentCourse(seg, row, tau),
tau,
hold ? 0 : courseSlewCapDps(speed, COURSE_MAX_DPS),
const icons = createAircraftIcons({
state: flightState,
services,
parts,
layerId: 'flights',
renderDelaySec: RENDER_DELAY_SEC,
courseMaxDps: COURSE_MAX_DPS,
cockpitContactSizePx: COCKPIT_CONTACT_SIZE_PX,
holdsPlan: (icao24) =>
flightState._focusEvidenceIds.has(icao24) ||
flightState._floorPinned.has(icao24),
// A pin stops at the last floor pass, which skips a tracked contact.
beforeSettle: (icao24) => flightState._floorPinned.delete(icao24),
// Straight onto the floor under where it is drawn, not its fix's floor
// until the next fleet tick raises it.
afterSettle(icao24) {
if (!flightState._grounded.has(icao24)) return;
const renderTime = icons._displayTime();
_floorIcon(
icao24,
renderTime,
flightState._icons.seconds(renderTime),
Date.now(),
);
}
set.segmentWritten(row, expiresAt);
}
/** Re-plan every row whose regime ended before `tau`. */
function _replanExpired(tau) {
const set = flightState._icons;
const due = [];
for (let row = 0; row < set.count; row++)
if (set.expires(row) <= tau) due.push(set.idAt(row));
for (const icao24 of due) _planIcon(icao24);
}
},
onForget(icao24) {
flightState._displayFloorState.delete(icao24);
flightState._grounded.delete(icao24);
flightState._floorPinned.delete(icao24);
},
});
/**
* Grounded contacts stand on the floor under where they are DRAWN, which the
@@ -272,7 +112,7 @@ export function createIcons({ flightState, services, parts }) {
);
if (!display || display === raw) {
if (flightState._floorPinned.delete(icao24))
_planIcon(icao24, { fromCourseDeg: set.course(row, tau) });
icons._planIcon(icao24, { fromCourseDeg: set.course(row, tau) });
return;
}
const planned = set.expires(row) !== -Infinity;
@@ -303,169 +143,5 @@ export function createIcons({ flightState, services, parts }) {
set.segmentWritten(row);
}
/**
* Advance focus de-emphasis for every drawn row (and every row a fleet model
* draws for, whose alpha reads it), only while a focus target exists or some
* row is still relaxing back to full emphasis.
*/
function _focusIcons(tau, scene, camera, nowMs, target) {
if (!focusPassIsNeeded(target, flightState._iconFocusActive)) return;
const { advanceProjectedSpriteFocus, nearFarScalarValueAtDistance } =
services.focus;
const set = flightState._icons;
let active = 0;
for (let row = 0; row < set.count; row++) {
if (!set.shown(row) && !flightState._models.has(set.idAt(row))) continue;
const position = set.position(row, tau, scratchPosition);
const distanceScale = nearFarScalarValueAtDistance(
set.scaleByDistance(row, scratchScaleByDistance),
Cesium.Cartesian3.distance(camera.positionWC, position),
);
const half = set.scale(row) * distanceScale * 0.5;
const focus = advanceProjectedSpriteFocus(
set.metaAt(row),
position,
scene,
camera,
nowMs,
target,
undefined,
set.width(row) * half,
set.height(row) * half,
);
set.setFocus(row, focus.factor);
if (focus.active || focus.transitioning) active++;
}
flightState._iconFocusActive = active;
}
/** Image, size, scale, tint and freshness for one row under the current
* normal/cockpit contract. Motion and rotation stay with the segment. */
function _applyIconPresentation(icao24) {
const set = flightState._icons;
const row = set.row(icao24);
if (row < 0) return;
const info = flightState.records.data.get(icao24);
const tracked = icao24 === flightState._trackedIcao;
const cockpitDot =
flightState._cockpitContactMode &&
!tracked &&
!flightState._cockpitNearContacts.has(icao24);
if (cockpitDot) {
const dot = iconAtlas.slot(cockpitContactDotImage(), COCKPIT_DOT_PX);
set.setImage(row, dot, dot);
set.setSize(row, COCKPIT_CONTACT_SIZE_PX, COCKPIT_CONTACT_SIZE_PX);
set.setScale(row, 1);
cockpitScaleByDistance ??=
parts.rendering._cockpitBillboardScaleByDistance();
set.setScaleByDistance(row, cockpitScaleByDistance);
set.setColor(row, parts.rendering._cockpitContactColor(icao24, info));
set.setFlag(row, ICON_ROTATES, false);
} else {
set.setImage(
row,
iconAtlas.slot(
aircraftIcon(parts.rendering._iconKind(icao24, info?.klass)),
FLEET_ICON_PX,
),
iconAtlas.slot(
aircraftIcon(
parts.rendering._iconKind(icao24, info?.klass),
TRACKED_ICON_PX,
),
TRACKED_ICON_PX,
),
);
const size = tracked ? 24 : 20;
set.setSize(row, size, size);
set.setScale(
row,
parts.rendering._fleetBillboardScale(icao24, info?.klass),
);
normalScaleByDistance ??=
parts.rendering._normalBillboardScaleByDistance();
set.setScaleByDistance(row, normalScaleByDistance);
set.setColor(row, parts.rendering._fleetBillboardColor(icao24, info));
set.setFlag(row, ICON_ROTATES, true);
}
set.setFlag(row, ICON_RECEDES, true);
set.setFlag(
row,
ICON_CULL_LIFT,
Number.isFinite(info?.renderAltitudeM) && info.renderAltitudeM < 10,
);
set.setAlpha(row, flightState.records.missingPolls.get(icao24) ? 0.45 : 1);
}
/** Add a row for a new contact, hidden while it is the tracked one. */
function _addIcon(icao24) {
const set = flightState._icons;
const row = set.add(icao24, { layer: 'flights', id: icao24 });
_applyIconPresentation(icao24);
_settleIcon(icao24);
set.setShown(row, icao24 !== flightState._trackedIcao);
return row;
}
/**
* Plan a row afresh, dropping any floor pin (a pin stops at the last floor
* pass, which skips a tracked contact), and let a dimmed row relax. A grounded contact goes straight onto
* the floor under where it is drawn, not its fix's floor until the next fleet
* tick raises it.
* @param {string} icao24
* @param {{fromCourseDeg?: number}} [options] The course on screen to glide from.
*/
function _settleIcon(icao24, options) {
const set = flightState._icons;
flightState._floorPinned.delete(icao24);
// A row that sat out the focus pass (tracked) may come back dimmed after
// the pass went idle; count it so the pass runs and relaxes it.
if (set.focus(set.row(icao24)) < 1) flightState._iconFocusActive += 1;
_planIcon(icao24, options);
if (flightState._grounded.has(icao24)) {
const renderTime = _displayTime();
_floorIcon(icao24, renderTime, set.seconds(renderTime), Date.now());
}
}
/** Drop a contact and everything the layer keeps for it. */
function _forgetContact(icao24) {
flightState._icons?.remove(icao24);
parts.rendering._releaseModel(icao24);
flightState.records.forget(icao24);
flightState._positionHistory.delete(icao24);
flightState._displayCourse.delete(icao24);
flightState._groundSnap.forget(icao24);
flightState._displayFloorState.delete(icao24);
flightState._grounded.delete(icao24);
flightState._floorPinned.delete(icao24);
}
/** Icon ids, so ray samples never land on an aircraft instead of the ground. */
function _iconIds() {
const set = flightState._icons;
const ids = new Array(set?.count ?? 0);
for (let row = 0; row < ids.length; row++) ids[row] = set.idAt(row);
return ids;
}
return {
_displayTime,
_displayTau,
_createIcons,
_iconPosition,
_iconCourse,
_iconShown,
_occluder,
_aboveHorizon,
_planIcon,
_settleIcon,
_replanExpired,
_floorGroundedIcons,
_focusIcons,
_applyIconPresentation,
_addIcon,
_forgetContact,
_iconIds,
};
return { ...icons, _floorGroundedIcons };
}