Files
gods-eye-view/src/data/spriteOrder.js
T
Sameh Khamis 38ba8a3b59 perf(shipwrecks): draw the wreck markers from one billboard collection, on the wreck, and rebuild them only on new data
- The ~8,000 markers were one Cesium entity each; they are now one
  BillboardCollection (as FIRMS, vessels and wildlife sightings do), ordered
  at the bottom of the shared sprite stack so moving traffic draws over it.
- eyeOffset.z was the death toll in metres, so a 1,500-death wreck's marker
  sat 1.5 km toward the camera and clipped or drifted when close. It is gone;
  markers are added by rising toll, so a bigger toll still draws on top.
- The page asks every 6 h but the proxy changes data at most daily: the proxy
  now sends a version that changes with either source's data, and the layer
  rebuilds only when it does.
- Adds a controller test: enable/disable, teardown, draw order, unchanged
  snapshots, a late response after turning off, and the period closing an
  open card.
2026-11-10 13:25:03 -08:00

81 lines
2.5 KiB
JavaScript

/** Bottom-to-top order for near-plane-clamped contact sprite collections. */
export const SPRITE_LAYER_ORDER = Object.freeze([
'shipwrecks',
'cctv',
'firms',
'wildlife-sightings',
'bikeshare',
'transit',
'transit-motion',
'directions',
'ais',
'military',
'flights',
]);
/** @type {Map<string, Object>} */
const _collections = new Map();
/**
* Register the current primitive collection for a sprite layer.
* @param {string} layerId - Stable sprite-order layer key.
* @param {Object} collection - Cesium billboard/point primitive collection.
* @returns {void}
*/
export function registerSpriteCollection(layerId, collection) {
if (!layerId || !collection) return;
_collections.set(layerId, collection);
}
/**
* Remove a registered collection (primarily useful to lifecycle tests).
* @param {string} layerId - Stable sprite-order layer key.
* @param {Object} [collection] - Optional identity guard against stale teardown.
* @returns {void}
*/
export function unregisterSpriteCollection(layerId, collection) {
if (collection && _collections.get(layerId) !== collection) return;
_collections.delete(layerId);
}
/**
* Reassert deterministic sprite stacking after any layer enable/init.
* Cesium's stable translucent sort otherwise preserves first-enable primitive
* order. Raising bottom-to-top makes flights the final/top collection.
* @param {Cesium.Viewer|Object} viewer - Active viewer.
* @returns {void}
*/
export function restoreSpriteOrder(viewer) {
if (!viewer || viewer.isDestroyed?.()) return;
const scene = viewer.scene;
const primitives = scene?.primitives;
if (!primitives || scene.isDestroyed?.() || primitives.isDestroyed?.())
return;
for (const layerId of SPRITE_LAYER_ORDER) {
const collection = _collections.get(layerId);
if (!collection || collection.isDestroyed?.()) continue;
if (primitives.contains?.(collection) === false) continue;
primitives.raiseToTop(collection);
}
}
/**
* Explicit layer-enable wiring seam. Production callers use the shared
* restorer by default; tests inject a spy to pin each enable path without
* constructing a WebGL viewer.
* @param {string} layerId - Sprite layer whose enable path is restoring order.
* @param {Cesium.Viewer|Object} viewer - Active viewer.
* @param {(viewer: Object) => void} [restore=restoreSpriteOrder] - Test seam.
* @returns {void}
*/
export function restoreSpriteOrderOnEnable(
layerId,
viewer,
restore = restoreSpriteOrder,
) {
if (!SPRITE_LAYER_ORDER.includes(layerId) || typeof restore !== 'function')
return;
restore(viewer);
}