refactor(localAdsb): Cesium rendering behind a renderer interface

Task 12a. The Local ADS-B logic (index.js, models.js) no longer imports
Cesium. src/layers/localAdsb/render/cesium.js holds what it built, one
for one: the CustomDataSource of marker entities (billboard with
per-frame position/rotation/visibility CallbackProperties, the 2,000 km
DistanceDisplayCondition, the 978 MHz UAT ring as a PointGraphics), the
model PrimitiveCollection (models load, pose and leave through the shared
aircraft renderer's model part, adopting that collection), the selected
aircraft's trail (data/trailRenderer, as before) and live head segment,
and the camera reads. Registered for 'local-adsb' in the renderer
registry, created at init(viewer, {engine}).

- Clicks go through the engine's selection dispatcher (drill 8) instead of
  the layer's ScreenSpaceEventHandler, with the layer's own rule kept as is
  — a new `lookUnder` owner kind: its marker on top, or under picks no
  layer owns and under trail lines, is selected (and re-selected);
  another layer's contact on top, or empty map, releases only its own
  selection; a trail line alone does nothing; no gesture rule. To the
  other owners it is another layer, as its pick-owner registration made
  it before. (The dispatcher's `drillThrough` would have taken the marker
  from under another layer's contact, which the old handler never did.)
- models.js reads the camera through the renderer's view (frustum and
  horizon tests, ground snap owner) and draws through renderer.models
  (loadFleet/add/remove/destroy/setPose: the same MIX-tinted glTF options,
  the same HPR frame with the Flights nose offset). The shared aircraft
  renderer gains a loadModel seam (default Cesium.Model.fromGltfAsync).
- Cesium math → src/geo; the model tint is an Rgba equal to the old
  Cesium.Color; check-import-directions: src/layers/localAdsb/ joins the
  rendered layers.

Tests moved with the code, same meaning:
- index.test: the services fixture creates the layer's Cesium renderer and
  the selection dispatcher; the click harness scripts the picks on a fake
  engine registered for the fake viewer (was a fake ScreenSpaceEventHandler)
  and the app's pick owners (trails, a Flights contact) are registered in
  the pick registry (was an injected picking service).
- cockpitModels.test: the GLB loads are the aircraft renderer's loadModel
  calls (Cesium.Model.fromGltfAsync by default), each asynchronous:false.
- trackingClickGesture.test: the dispatcher's empty click goes to the
  plain owners (filter(plain)).
- renderers.test: local-adsb has an extracted Cesium renderer.
New: the dispatcher's look-under rule beside a plain owner (7 click cases).

W1 replay (flights, military, localAdsb) vs a fresh 7c7422e2 run: counts,
known objects, screen points, tracking poses, requests and every shot's
sha256 identical.
This commit is contained in:
German Khokhlov committed 2026-10-17 05:50:30 +04:00
1 parent 941b308c29
commit 0b8cb855fa
15 files changed
+549 -306

No files matched your search

+39 -58
View File
@@ -1,5 +1,4 @@
import * as Cesium from 'cesium';
import { horizonOccluder } from '../../data/iconOrientation.js';
import * as geo from '../../geo/index.js';
import { selectModelEligible } from '../../data/modelEligibility.js';
import { civilAircraftModelSpec } from '../flights/modelSpec.js';
import {
@@ -7,7 +6,6 @@ import {
MODEL_ALL_ADD_M,
MODEL_ALL_KEEP_M,
MODEL_ALT_CEIL_M,
MODEL_HEADING_OFFSET_DEG,
MODEL_MAX,
MODEL_MAX_ALL,
MODEL_MIN_PX,
@@ -27,24 +25,25 @@ const specKey = (spec) => `${spec.url}@${spec.scale}`;
* magenta, as Flights tints military amber). Each model owns its matrix. A
* billboard stays the visual until its model is loaded AND placed; a grounded
* aircraft's model rides the one-shot ground snap plus the class belly
* offset, and without a resolved ground it stays a billboard.
* offset, and without a resolved ground it stays a billboard. The models
* are drawn by the layer's renderer (its `models`: the shared aircraft
* renderer's model part) and read the camera through its `view`.
* @param {object} options
* @param {object} options.viewer Cesium viewer.
* @param {Cesium.Color} options.color Model tint.
* @param {object} options.renderer The layer's renderer (render/cesium.js,
* render/navara.js).
* @param {object} options.color Model tint (a colour with red/green/blue/alpha).
* @param {(url: string) => string} options.resolveAsset
* @param {object|null} [options.groundSnap] `createGroundSnap()` instance.
* @param {(options: object) => Promise<object>} [options.loadModel]
* @returns {object}
*/
export function createLocalAdsbModels({
viewer,
renderer,
color,
resolveAsset,
groundSnap = null,
loadModel = (options) => Cesium.Model.fromGltfAsync(options),
}) {
const collection = new Cesium.PrimitiveCollection();
viewer.scene.primitives.add(collection);
const draw = renderer.models;
const view = renderer.view;
/** @type {Map<string, object>} id -> admitted model */
const models = new Map();
/** @type {Map<string, number>} id -> generation of an in-flight load */
@@ -53,17 +52,15 @@ export function createLocalAdsbModels({
let destroyed = false;
let lastEligibilityAt = 0;
let eligible = new Set();
const scratchHpr = new Cesium.HeadingPitchRoll();
const scratchSphere = new Cesium.BoundingSphere(Cesium.Cartesian3.ZERO, 1);
const scratchCarto = new Cesium.Cartographic();
const scratchGround = new Cesium.Cartesian3();
const scratchCarto = geo.cartographic.create();
const scratchGround = geo.cartesian3.create();
function release(id) {
pending.delete(id);
const model = models.get(id);
if (!model) return;
models.delete(id);
if (!collection.isDestroyed()) collection.remove(model);
draw.remove(model);
groundSnap?.forget?.(id);
}
@@ -84,14 +81,15 @@ export function createLocalAdsbModels({
pending.set(id, loadGeneration);
let model;
try {
model = await loadModel({
model = await draw.loadFleet({
url: resolveAsset(spec.url),
asynchronous: false,
minimumPixelSize: MODEL_MIN_PX,
scale: spec.scale,
// Bounding radius per scale unit (an engine that keeps
// minimumPixelSize on the CPU reports the drawn scale with it).
radiusM: spec.nativeRadiusM,
color,
colorBlendMode: Cesium.ColorBlendMode.MIX,
colorBlendAmount: spec.blendAmount,
blendAmount: spec.blendAmount,
id,
});
} catch {
@@ -107,7 +105,7 @@ export function createLocalAdsbModels({
specKey(civilAircraftModelSpec(marker.klass)) !== key;
if (pending.get(id) === loadGeneration) pending.delete(id);
if (stale) {
model.destroy?.();
draw.destroy(model);
return;
}
model.id = id;
@@ -115,13 +113,13 @@ export function createLocalAdsbModels({
model._gevBellyM = spec.bellyM;
// Admitted, not yet the visual: placement turns it on.
model.show = false;
collection.add(model);
draw.add(model);
models.set(id, model);
}
function regime(preferences) {
if (!preferences?.models3d) return false;
const height = viewer.camera?.positionCartographic?.height ?? Infinity;
const height = view.camera?.positionCartographic?.height ?? Infinity;
return height < MODEL_ALT_CEIL_M;
}
@@ -130,27 +128,17 @@ export function createLocalAdsbModels({
const cap = all ? MODEL_MAX_ALL : MODEL_MAX;
const addM = all ? MODEL_ALL_ADD_M : MODEL_PROX_ADD_M;
const keepM = all ? MODEL_ALL_KEEP_M : MODEL_PROX_KEEP_M;
const camera = viewer.camera;
const cameraPosition = camera.positionWC;
const cull = camera.frustum.computeCullingVolume(
cameraPosition,
camera.directionWC,
camera.upWC,
);
const cameraPosition = view.camera.positionWC;
const inFrustum = view.frustumTester();
const candidates = [];
for (const [id, marker] of markers) {
if (!marker.position) continue;
const distanceSq = Cesium.Cartesian3.distanceSquared(
const distanceSq = geo.cartesian3.distanceSquared(
cameraPosition,
marker.position,
);
if (distanceSq > keepM * keepM) continue;
Cesium.Cartesian3.clone(marker.position, scratchSphere.center);
candidates.push([
id,
distanceSq,
cull.computeVisibility(scratchSphere) !== Cesium.Intersect.OUTSIDE,
]);
candidates.push([id, distanceSq, inFrustum(marker.position)]);
}
candidates.sort((a, b) => a[1] - b[1]);
eligible = selectModelEligible(candidates, {
@@ -168,20 +156,23 @@ export function createLocalAdsbModels({
const model = models.get(id);
// The vertical sample must hit the tile skin, not this aircraft's own
// billboard or model.
const height = groundSnap?.heightFor?.(viewer, id, marker.position, () =>
[marker.entity, model].filter(Boolean),
const height = groundSnap?.heightFor?.(
view.surfaceOwner,
id,
marker.position,
() => [marker.entity, model].filter(Boolean),
);
if (!Number.isFinite(height)) return null;
const carto = Cesium.Cartographic.fromCartesian(
const carto = geo.cartographic.fromCartesian(
marker.position,
Cesium.Ellipsoid.WGS84,
geo.ellipsoid.WGS84,
scratchCarto,
);
return Cesium.Cartesian3.fromRadians(
return geo.cartesian3.fromRadians(
carto.longitude,
carto.latitude,
height + (model?._gevBellyM || 0),
Cesium.Ellipsoid.WGS84,
geo.ellipsoid.WGS84,
scratchGround,
);
}
@@ -213,7 +204,7 @@ export function createLocalAdsbModels({
lastEligibilityAt = nowMs;
refreshEligibility(markers, preferences);
}
const occluder = horizonOccluder(viewer.camera);
const occluder = view.horizon();
for (const [id, marker] of markers) {
const model = models.get(id);
marker.modelOwnsVisual = false;
@@ -223,18 +214,9 @@ export function createLocalAdsbModels({
model.show = false;
continue;
}
scratchHpr.heading = Cesium.Math.toRadians(
(marker.courseDeg || 0) + MODEL_HEADING_OFFSET_DEG,
);
scratchHpr.pitch = 0;
scratchHpr.roll = 0;
Cesium.Transforms.headingPitchRollToFixedFrame(
position,
scratchHpr,
Cesium.Ellipsoid.WGS84,
undefined,
model.modelMatrix,
);
// The renderer's pose: course + the Flights models' nose offset,
// into the model's own matrix.
draw.setPose(model, position, marker.courseDeg || 0);
if (!model.ready) {
model.show = false;
continue;
@@ -271,12 +253,11 @@ export function createLocalAdsbModels({
return models.size;
},
/** Drop every model; the renderer removes the collection (detach). */
destroy() {
destroyed = true;
releaseAll();
groundSnap?.clear?.();
if (!viewer.isDestroyed?.() && !collection.isDestroyed())
viewer.scene.primitives.remove(collection);
},
};
}