fix(vessels, transit, rail): fleet models keep their cards and detection, stand on the floor, clip ships at the waterline and follow NVG/FLIR

- a vehicle drawn as a model keeps its selected card and stays in the detection overlay
- transit and rail models drop the 1.5 m floor lift their icons carry
- vessel models are clipped below the waterline
- models switch to an unlit white look under NVG/FLIR and back
- unknown transit modes keep their icon
- aircraft model specs are cached per set; no key-array copies per frame
This commit is contained in:
Sameh Khamis committed 2026-11-13 07:16:25 -08:00
1 parent 058ed0ffeb
commit a44c42eb94
11 files changed
+342 -89

No files matched your search

+59 -12
View File
@@ -46,6 +46,11 @@ export function createFleetModel(
const tintShaders = new Map();
/** Models under the NVG/FLIR styles render unlit, as aircraft models do. */
const IR_UNLIT_SHADER = new Cesium.CustomShader({
lightingModel: Cesium.LightingModel.UNLIT,
});
/**
* The custom shader that paints a route colour where a transit model's
* `_TINT` vertex attribute marks it: drawn colour = vertex colour ×
@@ -131,23 +136,34 @@ const scratchScale = new Cesium.Cartesian3();
* `modelOwnsVisual` back from each item and hides that icon.
*
* Each frame the layer passes its vehicles as items:
* `{position, headingDeg, spec, tint, pickId, show}`, `position` the point
* the model's origin stands on (the waterline, the road or rail top; the
* spec's `elevationM` is added here), `tint` a Cesium.Color for a tinted
* model's route colour (or null), `show` false to keep it hidden.
* `{position, headingDeg, spec, tint, pickId, show, groundLiftM}`,
* `position` the point the model's origin stands on (the waterline, the road
* or rail top; the spec's `elevationM` is added here), `groundLiftM` any lift
* the layer's icon carries above that point (taken off again), `tint` a
* Cesium.Color for a tinted model's route colour (or null), `show` false to
* keep it hidden.
*
* Under the luminance-mapped NVG/FLIR styles (`irBoost`) models render
* unlit flat white, as aircraft models do. With `waterline`, each model is
* clipped at its origin's horizontal plane and only the part above it is
* drawn: a ship's hull below the waterline would otherwise show through a
* plain globe, which Cesium does not depth-test models against.
* @param {object} options
* @param {object} options.viewer Cesium viewer.
* @param {() => object|null} [options.display] `{models3d, models3dMode}`.
* @param {() => object|null} [options.display] `{models3d, models3dMode,
* irBoost}`.
* @param {object} options.policy `{altitudeCeilingM, minimumPixelSize,
* cap, capAll, addM, addAllM, keepM, keepAllM, intervalMs}`.
* @param {(url: string) => string} [options.resolveAsset]
* @param {(spec: object, options: object) => Promise<object>} [options.createModel]
* @param {boolean} [options.waterline] Clip each model at its waterline.
* @returns {object}
*/
export function createFleetModelLayer({
viewer,
display = () => null,
policy,
waterline = false,
resolveAsset = (url) => url,
createModel = (spec, options) =>
createFleetModel(spec, options, { resolveAsset }),
@@ -163,6 +179,8 @@ export function createFleetModelLayer({
let lastEligibilityAt = -Infinity;
let eligible = new Set();
let settling = 0;
/** Whether the models drawn now were built for the NVG/FLIR styles. */
let irBoost = false;
const scratchHpr = new Cesium.HeadingPitchRoll();
const scratchSphere = new Cesium.BoundingSphere(Cesium.Cartesian3.ZERO, 1);
const scratchCarto = new Cesium.Cartographic();
@@ -177,7 +195,7 @@ export function createFleetModelLayer({
}
function releaseAll() {
for (const id of [...models.keys()]) release(id);
for (const id of models.keys()) release(id);
pending.clear();
generation += 1;
}
@@ -193,11 +211,23 @@ export function createFleetModelLayer({
}
function shaderFor(item) {
if (irBoost) return IR_UNLIT_SHADER;
return item.spec.tinted && item.tint
? fleetTintShader(item.tint)
: undefined;
}
/** The clipping that keeps a model's part above its waterline. */
function waterlineClipping() {
return new Cesium.ClippingPlaneCollection({
// The planes sit in the model matrix's frame: its origin is the
// waterline and its Z axis the local vertical, so the plane z = 0 with
// its normal up keeps the hull above the water.
planes: [new Cesium.ClippingPlane(Cesium.Cartesian3.UNIT_Z, 0)],
edgeWidth: 0,
});
}
async function ensure(id, item, cap) {
const key = item.spec.key;
const current = models.get(id);
@@ -213,6 +243,14 @@ export function createFleetModelLayer({
minimumPixelSize: policy.minimumPixelSize,
scale: item.spec.scale,
customShader: shaderFor(item),
...(irBoost
? {
color: Cesium.Color.WHITE,
colorBlendMode: Cesium.ColorBlendMode.MIX,
colorBlendAmount: 1,
}
: {}),
...(waterline ? { clippingPlanes: waterlineClipping() } : {}),
id: item.pickId ?? id,
});
} catch {
@@ -232,7 +270,8 @@ export function createFleetModelLayer({
}
model.id = item.pickId ?? id;
model._gevSpecKey = key;
model._gevTint = item.spec.tinted ? item.tint : null;
model._gevTint = item.spec.tinted && !irBoost ? item.tint : null;
model._gevIrBoost = irBoost;
// Admitted, not yet the visual: placement turns it on.
model.show = false;
collection.add(model);
@@ -272,14 +311,14 @@ export function createFleetModelLayer({
addDistSq: addM * addM,
isModeled: (id) => models.has(id),
});
for (const id of [...models.keys(), ...pending.keys()])
if (!eligible.has(id)) release(id);
for (const id of models.keys()) if (!eligible.has(id)) release(id);
for (const id of pending.keys()) if (!eligible.has(id)) release(id);
for (const id of eligible) void ensure(id, items.get(id), cap);
}
/** Where the model's origin stands: the item's point raised by the spec's elevation. */
function placement(item) {
const lift = item.spec.elevationM || 0;
const lift = (item.spec.elevationM || 0) - (item.groundLiftM || 0);
if (!lift) return item.position;
const carto = Cesium.Cartographic.fromCartesian(
item.position,
@@ -320,6 +359,14 @@ export function createFleetModelLayer({
for (const item of items.values()) item.modelOwnsVisual = false;
return false;
}
// A model's look under NVG/FLIR is set when it is built (a custom
// shader on a ready model does not take), so a style change rebuilds.
const boost = prefs.irBoost === true;
if (boost !== irBoost) {
releaseAll();
irBoost = boost;
lastEligibilityAt = -Infinity;
}
if (nowMs - lastEligibilityAt >= policy.intervalMs) {
lastEligibilityAt = nowMs;
refreshEligibility(items, prefs);
@@ -345,7 +392,7 @@ export function createFleetModelLayer({
lastEligibilityAt = -Infinity;
continue;
}
const tint = item.spec.tinted ? item.tint : null;
const tint = item.spec.tinted && !irBoost ? item.tint : null;
if (tint && !Cesium.Color.equals(tint, model._gevTint)) {
model.customShader = fleetTintShader(tint);
model._gevTint = tint;
@@ -366,7 +413,7 @@ export function createFleetModelLayer({
item.modelOwnsVisual = true;
drawn = true;
}
for (const id of [...models.keys()]) if (!items.has(id)) release(id);
for (const id of models.keys()) if (!items.has(id)) release(id);
return drawn;
},
+127
View File
@@ -358,3 +358,130 @@ test('Vessels, Transit and Rail draw fleet models under the shared 3D toggle', (
'Local ADS-B, Vessels, Transit and Rail follow the Flights 3D toggle',
);
});
/** A layer whose models are all ready as soon as they are created. */
function readyLayer({ display, waterline = false } = {}) {
const viewer = fakeViewer();
const created = [];
const layer = createFleetModelLayer({
viewer,
display: () => display,
policy: POLICY,
waterline,
createModel: async (spec, options) => {
const model = fakeModel(options);
model.ready = true;
created.push(model);
return model;
},
});
return { layer, created };
}
test('a model stands on the floor its icon is lifted above', async () => {
const { layer, created } = readyLayer({ display: { models3d: true } });
const tram = transitModelSpec(set, { mode: 'tram' });
const item = itemAt(10, tram, { pickId: 't', groundLiftM: 1.5 });
const items = new Map([['t', item]]);
layer.frame(items, 0);
await flush();
layer.frame(items, 100);
const origin = Cesium.Matrix4.getTranslation(
created[0].modelMatrix,
new Cesium.Cartesian3(),
);
const drop =
Cesium.Cartographic.fromCartesian(item.position).height -
Cesium.Cartographic.fromCartesian(origin).height;
assert.ok(Math.abs(drop - 1.5) < 1e-3, `dropped ${drop} m`);
layer.destroy();
});
test('under NVG/FLIR models are rebuilt unlit white, and rebuilt again on exit', async () => {
const display = { models3d: true, irBoost: false };
const { layer, created } = readyLayer({ display });
const tram = transitModelSpec(set, { mode: 'tram' });
const items = new Map([
['t', itemAt(10, tram, { pickId: 't', tint: Cesium.Color.RED })],
]);
layer.frame(items, 0);
await flush();
assert.equal(created[0].customShader, fleetTintShader(Cesium.Color.RED));
display.irBoost = true;
layer.frame(items, 100);
assert.ok(created[0].destroyed, 'the lit model is released');
await flush();
const boosted = created[1];
assert.equal(boosted.customShader.lightingModel, Cesium.LightingModel.UNLIT);
assert.ok(Cesium.Color.equals(boosted.color, Cesium.Color.WHITE));
assert.equal(boosted.colorBlendAmount, 1);
display.irBoost = false;
layer.frame(items, 200);
await flush();
assert.ok(boosted.destroyed);
assert.equal(created[2].customShader, fleetTintShader(Cesium.Color.RED));
layer.destroy();
});
test('a waterline layer clips each model at its origin, keeping the part above', async () => {
const { layer, created } = readyLayer({
display: { models3d: true },
waterline: true,
});
const ship = vesselModelSpec(set, { type: '70', length: 200, beam: 32 });
layer.frame(new Map([['s', itemAt(10, ship, { pickId: 's' })]]), 0);
await flush();
const planes = created[0].clippingPlanes;
assert.ok(planes instanceof Cesium.ClippingPlaneCollection);
assert.equal(planes.length, 1);
const plane = planes.get(0);
assert.equal(plane.distance, 0);
assert.ok(Cesium.Cartesian3.equals(plane.normal, Cesium.Cartesian3.UNIT_Z));
assert.equal(planes.unionClippingRegions, false);
// Cesium keeps a point where normal · p + distance >= 0: above the waterline.
const above = Cesium.Cartesian3.dot(
plane.normal,
new Cesium.Cartesian3(0, 0, 3),
);
const below = Cesium.Cartesian3.dot(
plane.normal,
new Cesium.Cartesian3(0, 0, -3),
);
assert.ok(above + plane.distance > 0 && below + plane.distance < 0);
layer.destroy();
const plain = readyLayer({ display: { models3d: true } });
plain.layer.frame(
new Map([['t', itemAt(10, transitModelSpec(set, { mode: 'tram' }))]]),
0,
);
await flush();
assert.equal(plain.created[0].clippingPlanes, undefined, 'others unclipped');
plain.layer.destroy();
});
test('a vehicle drawn as its model keeps its card and its detection entry', () => {
const read = (file) =>
fs.readFileSync(new URL(`../../${file}`, import.meta.url), 'utf8');
assert.match(
read('src/layers/transit/selection.js'),
/marker\?\.show !== false \|\| entry\.modelOwnsVisual === true/,
);
assert.match(
read('src/layers/rail/selection.js'),
/entry\.marker\?\.show !== false \|\| entry\.modelOwnsVisual === true/,
);
assert.match(
read('src/layers/transit/queries.js'),
/marker\.show === false && entry\.modelOwnsVisual !== true/,
);
assert.match(
read('src/layers/vessels/queries.js'),
/!visual\.billboard\.show &&\s*!visual\.modelOwnsVisual/,
);
assert.match(
read('src/layers/transit/rendering.js'),
/entry\.mode === 'unknown'/,
'a vehicle of unknown mode keeps its icon',
);
});
+8
View File
@@ -288,3 +288,11 @@ test('civilian and military globe-view aircraft retain the established 3.0 near
);
}
});
test('a layer asking every tick gets one cached spec per type and category', async () => {
const { aircraftLayerModelSpec } = await import('../layers/flights/modelSpec.js');
const a = aircraftLayerModelSpec({ typeCode: 'A320' }, set);
assert.equal(aircraftLayerModelSpec({ typeCode: 'A320' }, set), a);
assert.notEqual(aircraftLayerModelSpec({ typeCode: 'B744' }, set), a);
assert.ok(Object.isFrozen(a));
});