Draw Flights and Military 3D models through draw-port model sets

The two aircraft layers' fleet and tracked models are now draw-port
models sets made each layer lifetime, re-posed and restyled in place,
instead of Cesium models in a collection of their own. Model records are
plain data; picks name the set and the contact, readiness comes from the
set's ready event, the IR boost restyles in place (unlit, white tint,
full blend), and the drawn scale comes from the camera pose and pixel
floor. Fleet writes go out together at the end of each tick, before the
icons they replace, and a failed write hands the contacts back to their
icons. A model that has not loaded is rewritten every few seconds so the
renderer can retry it, which replaces the tracked model's load-failure
budget. Models load off the tile-contended job queue, as before.

aircraftModelLayer.js, modelRecord.js and the flights and military
lifecycle and testing files, plus flights rendering.js, come off the
Cesium allow-list.
This commit is contained in:
Sameh Khamis committed 2026-11-07 17:10:38 -08:00
1 parent eed3b83858
commit abe9f97768
28 files changed
+1310 -1255

No files matched your search

+275
View File
@@ -0,0 +1,275 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { createAircraftModels } from './aircraftModelLayer.js';
import { fakeDrawPort } from '../testSupport/fakeDrawPort.mjs';
import { bindRenderer } from '../render/rendererOf.js';
import { fromDegrees } from '../geo/ellipsoid.js';
const POLICY = {
MODEL_ALT_CEIL_M: 800_000,
MODEL_MAX: 40,
MODEL_MAX_ALL: 350,
COCKPIT_MODEL_MAX: 60,
MODEL_PROX_ADD_M: 150_000,
MODEL_PROX_KEEP_M: 185_000,
MODEL_ALL_ADD_M: 400_000,
MODEL_ALL_KEEP_M: 450_000,
MODEL_MIN_PX: 24,
TRACKED_MODEL_MIN_PX: 48,
TRACKED_MODEL_MAX_PX: 600,
MODEL_HEADING_OFFSET_DEG: 0,
FLEET_DR_INTERVAL_MS: 80,
};
const SPEC = {
url: 'plane.glb',
scale: 1,
bellyM: 3,
nativeRadiusM: 20,
blendAmount: 0.9,
};
/**
* A fleet of airborne contacts at `positions` (icao → ECEF), drawn by a
* models layer over a fake draw port whose fleet set can be told to fail a
* write. The eye sits above them at 30 km.
*/
function fleetFixture(positions) {
const base = fakeDrawPort();
const removes = [];
let failNextUpsert = false;
const draw = {
...base,
models(spec) {
const set = base.models(spec);
return {
...set,
get show() {
return set.show;
},
set show(value) {
set.show = value;
},
id: set.id,
upsert(items) {
if (failNextUpsert && spec.id.includes('-models:')) {
failNextUpsert = false;
throw new Error('simulated write failure');
}
return set.upsert(items);
},
remove(ids) {
removes.push([...ids]);
return set.remove(ids);
},
};
},
};
const eye = fromDegrees(-97.71, 30.21, 30_000);
const viewer = {};
bindRenderer(viewer, {
draw,
camera: {
pose: () => ({
position: eye,
direction: { x: 0, y: 0, z: -1 },
up: { x: 0, y: 1, z: 0 },
right: { x: 1, y: 0, z: 0 },
fovy: Math.PI / 3,
aspectRatio: 1,
height: 30_000,
}),
},
});
const shown = new Map([...positions.keys()].map((id) => [id, true]));
const icons = {
show: true,
setId: 'flights-icons:1',
ids: () => [...positions.keys()],
has: (id) => positions.has(id),
position: (id) => positions.get(id),
course: () => 90,
alpha: () => 1,
focus: () => 1,
setShown: (id, value) => shown.set(id, value),
flush() {},
};
const state = {
_viewer: viewer,
_icons: icons,
_models: new Map(),
_modelSets: null,
_trackedIcao: null,
_trackedModel: null,
_models3dEnabled: true,
_models3dMode: 'proximity',
_cockpitContactMode: false,
_irBoost: false,
_lastFleetTickMs: 0,
_lastModelCapWarnMs: 0,
records: {
data: new Map(
[...positions.keys()].map((id) => [id, { klass: 'airliner' }]),
),
},
};
let now = 1_000_000;
const parts = {
icons: {
_displayMs: () => now,
_syncImageSwitch() {},
_replanExpired() {},
_focusIcons() {},
_occluder: () => null,
_aboveHorizon: () => true,
},
motion: { _displayTimeAt: () => null },
tracking: {
_refreshCockpitNearContacts() {},
_trackedModelRegimeActive: () => false,
},
};
const models = createAircraftModels({
state,
services: {
aircraftPresentation: { isTr3b: () => false },
camera: { trackedModelScaleForPixelCap: ({ baseScale }) => baseScale },
focus: { focusNowMs: () => now, getFocusTarget: () => null },
},
parts,
resolveAsset: (url) => url,
logLabel: 'Test',
setPrefix: 'flights',
policy: POLICY,
modelSpec: () => SPEC,
modelColor: () => ({ red: 1, green: 1, blue: 1, alpha: 1 }),
});
models._createModelSets(viewer);
const fleetId = models._modelSetIds()[0];
return {
models,
state,
draw: base,
fleet: () => base.set(fleetId),
fleetId,
removes,
shown,
failNextUpsert: () => (failNextUpsert = true),
/** One fleet tick, past the tick interval. */
tick() {
now += 1000;
models._fleetTick();
},
};
}
const near = (k) => fromDegrees(-97.71 + k * 0.01, 30.21, 10_000);
const far = (k) => fromDegrees(-90 + k * 0.01, 30.21, 10_000);
test('the fleet tick hands contacts to ready models and back without a gap', () => {
const positions = new Map([
['a1', near(1)],
['a2', near(2)],
]);
const f = fleetFixture(positions);
f.tick();
assert.deepEqual([...f.fleet().items.keys()].sort(), ['a1', 'a2']);
assert.equal(
f.shown.get('a1'),
true,
'icons carry contacts until models load',
);
f.draw.ready(f.fleetId, 'a1');
f.tick();
assert.equal(f.shown.get('a1'), false, 'a ready, placed model takes over');
assert.equal(f.fleet().items.get('a1').show, true);
assert.equal(
f.shown.get('a2'),
true,
'a model still loading leaves its icon',
);
});
test('contacts leaving the model band are released in one write', () => {
const positions = new Map([
['a1', near(1)],
['a2', near(2)],
['a3', near(3)],
]);
const f = fleetFixture(positions);
f.tick();
for (const id of ['a1', 'a2', 'a3']) f.draw.ready(f.fleetId, id);
f.tick();
positions.set('a2', far(2));
positions.set('a3', far(3));
f.removes.length = 0;
f.tick();
assert.deepEqual(
f.removes,
[['a2', 'a3']],
'one remove for every evicted model',
);
assert.deepEqual([...f.fleet().items.keys()], ['a1']);
assert.equal(
f.shown.get('a2'),
true,
'a released model hands back to its icon',
);
assert.equal(f.shown.get('a3'), true);
});
test('a failed fleet write leaves the icons in charge and is sent again', () => {
const positions = new Map([['a1', near(1)]]);
const f = fleetFixture(positions);
f.tick();
f.draw.ready(f.fleetId, 'a1');
const realWarn = console.warn;
console.warn = () => {};
try {
f.failNextUpsert();
f.tick();
} finally {
console.warn = realWarn;
}
assert.equal(f.shown.get('a1'), true, 'the contact keeps its icon');
assert.equal(
f.fleet().items.get('a1').show,
false,
'the set still has the hidden model',
);
assert.equal(f.models._modelOwnsVisual('a1'), false);
f.tick();
assert.equal(
f.fleet().items.get('a1').show,
true,
'the next tick sends it again',
);
assert.equal(f.shown.get('a1'), false);
});
test('a model that has not loaded is written again for the renderer to retry', () => {
const positions = new Map([['a1', near(1)]]);
const f = fleetFixture(positions);
const realNow = Date.now;
let clock = 50_000_000;
Date.now = () => clock;
try {
f.tick();
const writes = f.fleet().writes;
f.tick();
assert.equal(
f.fleet().writes,
writes,
'an unchanged loading model is not rewritten every tick',
);
clock += 5_000;
f.tick();
assert.equal(
f.fleet().writes,
writes + 1,
'but it is rewritten every few seconds',
);
} finally {
Date.now = realNow;
}
});