Move Cockpit onto the camera and frame ports

Cockpit now places its eye with the geo maths and moves the camera
through the camera port on the frame port's preUpdate, so cockpitCamera.js
and cockpitController.js no longer load Cesium and leave the allow-list.
Whether the aircraft still draws comes from a trackedEntityDrawn service,
which the application shell answers from the viewer's entities. The mesh
floor probe accepts the viewer as well as its scene.
This commit is contained in:
Sameh Khamis committed 2026-11-07 16:12:43 -08:00
1 parent b47aa5f3de
commit 2a9a2d5be8
11 files changed
+433 -159

No files matched your search

+298
View File
@@ -3,6 +3,17 @@ import assert from 'node:assert/strict';
import { CockpitViewController } from './cockpitController.js';
import { withEntityRecord } from '../data/entityRecords.js';
import { fakeInput } from '../input/testDoubles.mjs';
import { bindRenderer } from '../render/rendererOf.js';
import {
cockpitAnchorCorrectionStep,
cockpitGroundSafeHeight,
} from '../cockpitMath.js';
import {
COCKPIT_FORWARD_OFFSET_M,
COCKPIT_MIN_GROUND_CLEARANCE_M,
COCKPIT_UP_OFFSET_M,
COCKPIT_VIEW_PITCH_DEG,
} from './cockpitPresentation.js';
function environment() {
const original = { document: globalThis.document, window: globalThis.window };
@@ -29,6 +40,13 @@ function environment() {
trackedEntityChanged: event,
entities: { contains: () => false },
};
// The renderer's frame port: Cockpit moves the camera on its preUpdate.
bindRenderer(viewer, {
frame: {
on: (phase, fn) =>
phase === 'preUpdate' ? event.addEventListener(fn) : () => {},
},
});
return {
viewer,
callbacks,
@@ -259,3 +277,283 @@ test('the weather toggle flips from its own state, however fast it is pressed',
env.restore();
}
});
/**
* The Cockpit eye as cockpitCamera.js computed it with Cesium's own maths,
* for one update: the anchor advanced along the heading and pulled toward
* the target (held on it when stale), lifted to the floor, then the eye
* set forward and up from it and lifted again.
*/
function cesiumCockpitEye(
Cesium,
{ target, anchor, info, heading, dtSec, floorM, liftM },
) {
const { Cartesian3, Matrix4, Transforms, Cartographic, Ellipsoid } = Cesium;
const h = Cesium.Math.toRadians(heading);
const p = Cesium.Math.toRadians(COCKPIT_VIEW_PITCH_DEG);
const speed = Math.max(0, info.velocityMps);
const along = (origin, x, y, z) =>
Cartesian3.normalize(
Matrix4.multiplyByPointAsVector(
Transforms.eastNorthUpToFixedFrame(origin, Ellipsoid.WGS84),
new Cartesian3(x, y, z),
new Cartesian3(),
),
new Cartesian3(),
);
let a = Cartesian3.clone(anchor ?? target);
if (info.stale) a = Cartesian3.clone(target);
else {
const horizontal = along(a, Math.sin(h), Math.cos(h), 0);
a = Cartesian3.add(
a,
Cartesian3.multiplyByScalar(horizontal, speed * dtSec, new Cartesian3()),
new Cartesian3(),
);
const correction = Cartesian3.subtract(target, a, new Cartesian3());
const distance = Cartesian3.magnitude(correction);
const step = cockpitAnchorCorrectionStep(distance, speed, dtSec);
if (step > 0 && distance > 0)
a = Cartesian3.add(
a,
Cartesian3.multiplyByScalar(
correction,
step / distance,
new Cartesian3(),
),
new Cartesian3(),
);
}
const anchorCarto = Cartographic.fromCartesian(a);
let floor = floorM;
const targetCarto = Cartographic.fromCartesian(target);
if (!Number.isFinite(floor) && speed < 90) floor = targetCarto.height - liftM;
if (Number.isFinite(floor)) {
const minimum = cockpitGroundSafeHeight(
anchorCarto.height,
floor,
COCKPIT_MIN_GROUND_CLEARANCE_M - COCKPIT_UP_OFFSET_M,
);
if (minimum !== anchorCarto.height) {
anchorCarto.height = minimum;
a = Ellipsoid.WGS84.cartographicToCartesian(anchorCarto);
}
}
const horizontal = along(a, Math.sin(h), Math.cos(h), 0);
const forward = along(
a,
Math.sin(h) * Math.cos(p),
Math.cos(h) * Math.cos(p),
Math.sin(p),
);
const up = along(
a,
-Math.sin(h) * Math.sin(p),
-Math.cos(h) * Math.sin(p),
Math.cos(p),
);
let eye = Cartesian3.add(
Cartesian3.add(
a,
Cartesian3.multiplyByScalar(
horizontal,
COCKPIT_FORWARD_OFFSET_M,
new Cartesian3(),
),
new Cartesian3(),
),
Cartesian3.multiplyByScalar(
Cartesian3.normalize(a, new Cartesian3()),
COCKPIT_UP_OFFSET_M,
new Cartesian3(),
),
new Cartesian3(),
);
const eyeCarto = Cartographic.fromCartesian(eye);
const safe = cockpitGroundSafeHeight(
eyeCarto.height,
floor,
COCKPIT_MIN_GROUND_CLEARANCE_M,
);
if (safe !== eyeCarto.height) {
eyeCarto.height = safe;
eye = Ellipsoid.WGS84.cartographicToCartesian(eyeCarto);
}
return { eye, forward, up, anchor: a };
}
/** A Cockpit flying `info` over `target`, its camera writes in `views`. */
function cockpitOver({ info, target, services = {} }) {
const views = [];
const viewer = {
trackedEntityChanged: { addEventListener: () => () => {} },
};
bindRenderer(viewer, {
frame: { on: () => () => {}, time: () => 0 },
camera: {
followed: null,
on: () => () => {},
pose: () => ({ lat: 30.2, lon: -97.7 }),
setView: (view) => views.push(view),
},
});
const owner = new CockpitViewController(viewer, {
services: {
flightsLayer: { getTrackedInfo: () => info },
militaryFlightsLayer: {},
cachedGroundFloor: () => null,
trackedEntityDrawn: () => true,
releaseContinuousRender() {},
...services,
},
});
owner.updateHud = () => {};
owner.active = true;
owner.heading = info.track;
owner.trackedEntity = {};
owner.trackedFollow = { key: 'flights:abc123', position: () => target };
return { owner, viewer, views };
}
const near = (a, b, eps, what) =>
assert.ok(
Math.abs(a.x - b.x) <= eps &&
Math.abs(a.y - b.y) <= eps &&
Math.abs(a.z - b.z) <= eps,
`${what}: ${JSON.stringify(a)} vs ${JSON.stringify(b)}`,
);
test('Cockpit places the eye at its aircraft as the Cesium maths did', async () => {
const Cesium = await import('cesium');
const env = environment();
const realNow = performance.now;
try {
const target = Cesium.Cartesian3.fromDegrees(-97.67, 30.19, 1200);
const info = {
icao24: 'abc123',
layerId: 'flights',
track: 72,
velocityMps: 200,
latitude: 30.19,
longitude: -97.67,
onGround: false,
stale: true,
};
const { owner, views } = cockpitOver({ info, target });
// Held on the target while the feed is stale.
owner.update();
let expected = cesiumCockpitEye(Cesium, {
target,
info,
heading: 72,
dtSec: 0,
liftM: 0,
});
near(views[0].destination, expected.eye, 1e-6, 'stale eye');
near(views[0].orientation.direction, expected.forward, 1e-12, 'direction');
near(views[0].orientation.up, expected.up, 1e-12, 'up');
// Live: the anchor coasts 80 ms along the heading and is pulled toward
// where the layer now shows the aircraft.
info.stale = false;
const anchor = Cesium.Cartesian3.clone(expected.anchor);
const moved = Cesium.Cartesian3.fromDegrees(-97.6698, 30.1901, 1210);
owner.trackedFollow.position = () => moved;
let now = owner.lastFrameMs + 80;
performance.now = () => now;
owner.update();
expected = cesiumCockpitEye(Cesium, {
target: moved,
anchor,
info,
heading: 72,
dtSec: 0.08,
liftM: 0,
});
assert.equal(views.length, 2);
near(views[1].destination, expected.eye, 1e-6, 'live eye');
near(views[1].orientation.direction, expected.forward, 1e-12, 'direction');
near(views[1].orientation.up, expected.up, 1e-12, 'up');
// An aircraft its layer no longer draws ends Cockpit.
owner.services.trackedEntityDrawn = () => false;
now += 80;
owner.update();
assert.equal(owner.active, false);
assert.equal(views.length, 2);
owner.dispose();
} finally {
performance.now = realNow;
env.restore();
}
});
test('a grounded Cockpit probes the mesh from the camera and keeps the eye above the floor', async () => {
const Cesium = await import('cesium');
const env = environment();
try {
// Reported below the apron: the floor lifts the anchor and the eye.
const target = Cesium.Cartesian3.fromDegrees(-97.67, 30.19, 120);
const info = {
icao24: 'abc123',
layerId: 'flights',
track: 300,
velocityMps: 0,
latitude: 30.19,
longitude: -97.67,
onGround: true,
stale: true,
};
const probes = [];
const { owner, viewer, views } = cockpitOver({
info,
target,
services: {
cachedGroundFloor: () => 140,
warmGroundFloor() {},
cachedMeshFloor: () => null,
meshFloorPreferred: () => true,
sampleMeshFloorCells: (...args) => probes.push(args),
GROUND_FLOOR_LIFT_M: 0,
},
});
// The surface wait is over: Cockpit flies on the ground floor.
owner.surfaceWaitStartedMs = -1e9;
owner.update();
assert.equal(probes.length, 1);
const [host, points, options] = probes[0];
assert.equal(host, viewer);
assert.deepEqual(points, [{ lat: 30.19, lon: -97.67 }]);
assert.deepEqual(options, {
excludeObjects: [owner.trackedEntity],
viewerLat: 30.2,
viewerLon: -97.7,
});
const expected = cesiumCockpitEye(Cesium, {
target,
info,
heading: 300,
dtSec: 0,
floorM: 140,
liftM: 0,
});
assert.equal(views.length, 1);
// The floor lifts round-trip through geodetic heights: the shared geo helpers' and
// Cesium's agree to well under a millimetre there.
near(views[0].destination, expected.eye, 1e-4, 'eye');
const height = Cesium.Cartographic.fromCartesian(
new Cesium.Cartesian3(
views[0].destination.x,
views[0].destination.y,
views[0].destination.z,
),
).height;
assert.ok(
height >= 140 + COCKPIT_MIN_GROUND_CLEARANCE_M - 1e-6,
`${height}`,
);
owner.dispose();
} finally {
env.restore();
}
});