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

+21 -9
View File
@@ -37,17 +37,29 @@ export const UNLOADED_AIRCRAFT_MODEL_SPEC = Object.freeze({
*/
export function aircraftLayerModelSpec(aircraft, set = fleetModelSet()) {
if (!set) return UNLOADED_AIRCRAFT_MODEL_SPEC;
const spec = aircraftModelSpec(set, {
typeCode: aircraft?.typeCode ?? aircraft?.type ?? null,
category: aircraft?.category ?? null,
});
return Object.freeze({
...spec,
bellyM: spec.clearanceM,
blendAmount: MODEL_COLOR_BLEND_AMOUNT,
});
// Layers ask on every tick; each type and category is resolved once per set.
let cache = specCaches.get(set);
if (!cache) specCaches.set(set, (cache = new Map()));
const key = aircraftModelSpecCacheKey(aircraft);
let layerSpec = cache.get(key);
if (!layerSpec) {
const spec = aircraftModelSpec(set, {
typeCode: aircraft?.typeCode ?? aircraft?.type ?? null,
category: aircraft?.category ?? null,
});
layerSpec = Object.freeze({
...spec,
bellyM: spec.clearanceM,
blendAmount: MODEL_COLOR_BLEND_AMOUNT,
});
cache.set(key, layerSpec);
}
return layerSpec;
}
/** @type {WeakMap<object, Map<string, object>>} set -> cache key -> spec */
const specCaches = new WeakMap();
/**
* Cache key of an aircraft's model spec: what selects its model.
* @param {object|null|undefined} aircraft
+16 -5
View File
@@ -696,7 +696,9 @@ export function createRendering({ state, services, parts }) {
// aircraft layers' icon-to-model rules (services.fleet). The fleet set
// loads once, up front, when the layer starts.
let fleetModels = null;
/** Items drawn last frame, and the map the next frame's are gathered into. */
let modelItems = new Map();
let spareItems = new Map();
let modelCandidatesAtMs = -Infinity;
/** Load the fleet set and create the layer's models (on enable). */
@@ -724,7 +726,13 @@ export function createRendering({ state, services, parts }) {
function modelItem(entry, set) {
let item = modelItems.get(entry.key);
if (!item || item.entry !== entry)
item = { entry, pickId: entry.key, modelOwnsVisual: false };
item = {
entry,
pickId: entry.key,
modelOwnsVisual: false,
// Markers are drawn this far above the track; a model stands on it.
groundLiftM: GROUND_FLOOR_LIFT_M,
};
const specKey = `${entry.category}|${entry.railMode}`;
if (item.specKey !== specKey) {
item.specKey = specKey;
@@ -744,15 +752,16 @@ export function createRendering({ state, services, parts }) {
if (!fleetModels) return;
if (!fleetModels.active() || state._trains.size === 0) {
if (modelItems.size) {
fleetModels.frame(new Map(), performance.now());
for (const item of modelItems.values()) handBack(item.entry);
modelItems = new Map();
modelItems.clear();
fleetModels.frame(modelItems, performance.now());
}
return;
}
const set = fleetModelSet();
const now = performance.now();
const next = new Map();
const next = spareItems;
next.clear();
if (now - modelCandidatesAtMs >= MODEL_CANDIDATES_MS) {
modelCandidatesAtMs = now;
const camera = state._viewer?.camera;
@@ -781,6 +790,7 @@ export function createRendering({ state, services, parts }) {
}
for (const [key, item] of modelItems)
if (next.get(key) !== item) handBack(item.entry);
spareItems = modelItems;
modelItems = next;
fleetModels.frame(modelItems, now);
for (const item of modelItems.values()) {
@@ -796,7 +806,8 @@ export function createRendering({ state, services, parts }) {
/** Drop every model (layer disabled); `destroy` also removes the collection. */
function stopModels(destroy = false) {
for (const item of modelItems.values()) item.entry.modelOwnsVisual = false;
modelItems = new Map();
modelItems.clear();
spareItems.clear();
modelCandidatesAtMs = -Infinity;
if (destroy) {
fleetModels?.destroy();
+7 -2
View File
@@ -19,10 +19,15 @@ import { t } from '../../i18n/index.js';
export function createSelection({ state, services, parts }) {
const { governorRequestRender } = services.render;
/** The card's anchor: the marker's current position, read on every paint. */
/**
* The card's anchor: the marker's current position, read on every paint,
* whether the train is drawn as its marker or as its 3D model.
*/
function anchorFor(entry) {
return () => {
const p = entry.marker?.show !== false ? entry.marker?.position : null;
const drawn =
entry.marker?.show !== false || entry.modelOwnsVisual === true;
const p = drawn ? entry.marker?.position : null;
return p && Number.isFinite(p.x) && Number.isFinite(p.y) ? p : null;
};
}
+3 -1
View File
@@ -129,7 +129,9 @@ export function createQueries({ state, parts }) {
const visible = [];
for (const entry of state._visible) {
const marker = entry.marker;
if (!marker?.position || marker.show === false) continue;
// A vehicle drawn as its 3D model is present though its marker is hidden.
if (!marker?.position) continue;
if (marker.show === false && entry.modelOwnsVisual !== true) continue;
visible.push(entry);
}
let chosen = visible;
+20 -6
View File
@@ -468,7 +468,11 @@ export function createRendering({ state, services, parts }) {
// icon-to-model rules (services.fleet). The fleet set loads once, up front,
// when the layer starts.
let fleetModels = null;
/** Items drawn last frame, and the map the next frame's are gathered into. */
let modelItems = new Map();
let spareItems = new Map();
/** The lift icons are drawn with above the floor; a model stands on the floor. */
const groundLiftM = services.ground?.GROUND_FLOOR_LIFT_M || 0;
/** Load the fleet set and create the layer's models (on enable). */
function startModels() {
@@ -500,19 +504,27 @@ export function createRendering({ state, services, parts }) {
if (!fleetModels) return;
if (!fleetModels.active() || state._vehicles.size === 0) {
if (modelItems.size) {
fleetModels.frame(new Map(), performance.now());
for (const item of modelItems.values()) handBack(item.entry);
modelItems = new Map();
modelItems.clear();
fleetModels.frame(modelItems, performance.now());
}
return;
}
const set = fleetModelSet();
const next = new Map();
const next = spareItems;
next.clear();
for (const entry of state._visible) {
if (!entry.marker || !entry.hasRendered) continue;
// A vehicle of no known mode keeps its icon: no model says what it is.
if (!entry.marker || !entry.hasRendered || entry.mode === 'unknown')
continue;
let item = modelItems.get(entry.key);
if (!item || item.entry !== entry) {
item = { entry, pickId: entry.key, modelOwnsVisual: false };
item = {
entry,
pickId: entry.key,
modelOwnsVisual: false,
groundLiftM,
};
}
if (item.mode !== entry.mode) {
item.mode = entry.mode;
@@ -527,6 +539,7 @@ export function createRendering({ state, services, parts }) {
}
for (const [key, item] of modelItems)
if (next.get(key) !== item) handBack(item.entry);
spareItems = modelItems;
modelItems = next;
fleetModels.frame(modelItems, performance.now());
for (const item of modelItems.values()) {
@@ -542,7 +555,8 @@ export function createRendering({ state, services, parts }) {
/** Drop every model (layer disabled); `destroy` also removes the collection. */
function stopModels(destroy = false) {
for (const item of modelItems.values()) item.entry.modelOwnsVisual = false;
modelItems = new Map();
modelItems.clear();
spareItems.clear();
if (destroy) {
fleetModels?.destroy();
fleetModels = null;
+4 -2
View File
@@ -24,14 +24,16 @@ export function createSelection({ state, services, parts }) {
* (the tracked-aircraft readout uses the same), so the anchor moves with the
* sprite at frame rate, through poll arrivals, settling, a late floor and a
* reset alike, with no per-frame work here. Null when there is nothing to
* anchor to: a hidden or removed marker draws no card.
* anchor to: a hidden or removed marker draws no card, unless the vehicle's
* 3D model is drawing it in the marker's place.
* @param {object} entry
* @returns {() => Cesium.Cartesian3|null}
*/
function anchorFor(entry) {
return () => {
const marker = entry.marker;
const p = marker?.show !== false ? marker?.position : null;
const drawn = marker?.show !== false || entry.modelOwnsVisual === true;
const p = drawn ? marker?.position : null;
return p &&
Number.isFinite(p.x) &&
Number.isFinite(p.y) &&
+7 -1
View File
@@ -664,7 +664,13 @@ export function createQueries({
if ((idx - start) % stride !== 0) continue;
const record = records[idx];
const visual = components.rendering.getVisual(record);
if (visual.billboard && !visual.billboard.show) continue;
// A vessel drawn as its model is present though its chevron is hidden.
if (
visual.billboard &&
!visual.billboard.show &&
!visual.modelOwnsVisual
)
continue;
const position = visual.billboard?.position || visual.position;
if (!position) continue;
result.push({
+70 -51
View File
@@ -224,8 +224,11 @@ export function createRendering({
// on the aircraft layers' icon-to-model rules (services.fleet). The fleet
// set loads once, up front, when the layer starts.
let fleetModels = null;
/** Items drawn last frame, and the map the next frame's are gathered into. */
let modelItems = new Map();
let spareItems = new Map();
let modelCandidatesAtMs = -Infinity;
const modelOccluder = new Cesium.EllipsoidalOccluder(Cesium.Ellipsoid.WGS84);
function ensureModels(viewer) {
const fleet = services.fleet;
@@ -238,96 +241,109 @@ export function createRendering({
display: fleet.display,
policy: fleet.policy,
resolveAsset: fleet.resolveAsset,
// A hull below the waterline would show through a plain globe.
waterline: true,
});
}
/**
* Whether a vessel's chevron is drawn: on the camera's side of the Earth,
* not the bridge camera's own vessel, and not drawn by its model. The
* visibility pass and the model handoff both decide by this.
*/
function billboardShown(record, occluder) {
const visual = getVisual(record);
return (
isVisible(visual.surfacePosition, occluder) &&
record.id !== state.cockpitSubjectId &&
!visual.modelOwnsVisual
);
}
/** The model item for `record`, its spec rebuilt only when its type or size changes. */
function modelItem(record, set) {
let item = modelItems.get(record.id);
if (!item || item.record !== record)
item = { record, pickId: record, tint: null, modelOwnsVisual: false };
const specKey = `${record.type}|${record.length}|${record.beam}|${record.name}`;
if (!item) {
item = { pickId: record, tint: null, modelOwnsVisual: false };
modelItems.set(record.id, item);
}
if (item.specKey !== specKey) {
item.specKey = specKey;
item.spec = vesselModelSpec(set, record);
}
const visual = getVisual(record);
item.record = record;
item.position = visual.waterline;
item.position = getVisual(record).waterline;
item.headingDeg = vesselCourseDeg(record);
item.show = record.id !== state.cockpitSubjectId;
return item;
}
/** Gather the vessels within the models' keep radius of the camera. */
function gatherModelItems(set, prefs) {
/** Gather the vessels within the models' keep radius of the camera into `into`. */
function gatherModelItems(set, into) {
const camera = state.viewer?.camera;
if (!camera?.positionWC) return;
const policy = services.fleet.policy;
const keepM =
prefs?.models3dMode === 'all' ? policy.keepAllM : policy.keepM;
const next = new Map();
if (camera?.positionWC) {
const limitSq = keepM * keepM * 1.44;
for (const record of state.records.all) {
const visual = getVisual(record);
if (!visual.waterline) continue;
if (
Cesium.Cartesian3.distanceSquared(
camera.positionWC,
visual.waterline,
) > limitSq
)
continue;
next.set(record.id, modelItem(record, set));
}
services.fleet.display?.()?.models3dMode === 'all'
? policy.keepAllM
: policy.keepM;
const limitSq = keepM * keepM * 1.44;
for (const record of state.records.all) {
const waterline = getVisual(record).waterline;
if (!waterline) continue;
if (
Cesium.Cartesian3.distanceSquared(camera.positionWC, waterline) >
limitSq
)
continue;
into.set(record.id, modelItem(record, set));
}
for (const [id, item] of modelItems)
if (!next.has(id)) handBackVisual(item);
modelItems = next;
}
/** Give a vessel's icon back when its model no longer draws it. */
function handBackVisual(item) {
if (!item.ownedBillboard) return;
item.ownedBillboard = false;
const billboard = getVisual(item.record).billboard;
if (billboard)
billboard.show =
isVisible(getVisual(item.record).surfacePosition, makeOccluder()) &&
item.record.id !== state.cockpitSubjectId;
/** Give a vessel's chevron back when its model no longer draws it. */
function handBackVisual(record, occluder) {
const visual = getVisual(record);
if (!visual.modelOwnsVisual) return;
visual.modelOwnsVisual = false;
if (visual.billboard)
visual.billboard.show = billboardShown(record, occluder);
}
function updateModels() {
if (!fleetModels) return;
const camera = state.viewer?.camera;
const occluder = camera?.positionWC ? modelOccluder : null;
if (occluder) occluder.cameraPosition = camera.positionWC;
if (!fleetModels.active()) {
if (modelItems.size) {
modelItems.clear();
fleetModels.frame(modelItems, performance.now());
for (const item of modelItems.values()) handBackVisual(item);
modelItems = new Map();
for (const record of state.records.all)
handBackVisual(record, occluder);
}
return;
}
const set = fleetModelSet();
const now = performance.now();
const next = spareItems;
next.clear();
if (now - modelCandidatesAtMs >= MODEL_CANDIDATES_MS) {
modelCandidatesAtMs = now;
gatherModelItems(set, services.fleet.display?.());
gatherModelItems(set, next);
} else {
for (const [id, item] of modelItems) {
for (const [id, item] of modelItems)
if (state.records.byId.get(id) === item.record)
modelItem(item.record, set);
else modelItems.delete(id);
}
next.set(id, modelItem(item.record, set));
}
for (const [id, item] of modelItems)
if (next.get(id) !== item) handBackVisual(item.record, occluder);
spareItems = modelItems;
modelItems = next;
fleetModels.frame(modelItems, now);
for (const item of modelItems.values()) {
const billboard = getVisual(item.record).billboard;
const visual = getVisual(item.record);
if (item.modelOwnsVisual) {
item.ownedBillboard = true;
if (billboard) billboard.show = false;
} else handBackVisual(item);
visual.modelOwnsVisual = true;
if (visual.billboard) visual.billboard.show = false;
} else handBackVisual(item.record, occluder);
}
if (fleetModels.settling) governorRequestRender('ais-vessels-models');
}
@@ -336,6 +352,7 @@ export function createRendering({
fleetModels?.destroy();
fleetModels = null;
modelItems = new Map();
spareItems = new Map();
modelCandidatesAtMs = -Infinity;
}
@@ -443,10 +460,11 @@ export function createRendering({
const visible = isVisible(visual.surfacePosition, occluder);
if (visual.billboard) {
// The bridge camera sits on this sprite; showing it would fill the view.
// The same rule as billboardShown, its horizon test already made.
visual.billboard.show =
visible &&
record.id !== state.cockpitSubjectId &&
!modelItems.get(record.id)?.ownedBillboard;
!visual.modelOwnsVisual;
if (visible && doRotations && scene) {
const rot = screenProjectedRotation(
scene,
@@ -724,8 +742,9 @@ export function createRendering({
fleetModels?.setVisible(show);
if (!show && fleetModels) {
fleetModels.clear();
for (const item of modelItems.values()) item.ownedBillboard = false;
modelItems = new Map();
for (const item of modelItems.values())
getVisual(item.record).modelOwnsVisual = false;
modelItems.clear();
}
vesselState._vesselOverlayHost.setVisible(VESSEL_OVERLAY_SOURCE_ID, show);
}