Draw movers and lines on Cesium and require them of every engine

Layers are to draw through the globe contract on both engines, so Cesium
gets back the movers and lines removed as unused:

- engine.movers draws sprite sets as a BillboardCollection (atlas cells
  cropped once per set), point sets as a PointPrimitiveCollection and
  model sets as one Model per instance. depthTest, disableWithinM, order,
  scaleByDistance, colorBlend, unlit and minimumPixelSize follow the
  contract; a model's matrix is written in place, so instances no longer
  share one scratch matrix.
- engine.lines draws each line as a polyline Primitive with width in CSS
  px, geodesic arcs, dashes, the tactical and arrow materials,
  depthFailAlpha and modelMatrix; a rebuild keeps its show, matrix,
  colour and place in order.
- The picker returns the pickId a layer wrote last, skips sets that are
  not pickable and instances behind the horizon, and falls through to
  what lies under them. Surface samples can exclude mover sets again.

movers and lines move from the optional capabilities into the required
contract shape, and the contract suite always runs their checks.
This commit is contained in:
German Khokhlov committed 2026-11-07 01:24:59 +04:00
1 parent 59a233db51
commit 2213be6566
19 files changed
+1758 -75

No files matched your search

@@ -7,7 +7,9 @@ import {
getRenderGovernorDiagnostics,
installRenderGovernor,
} from '../../renderGovernor.js';
import { createContractSuite } from '../testing/contractSuite.mjs';
import { createCesiumEngine } from './createCesiumEngine.js';
import { createCameraViewer } from './testing/cameraViewer.mjs';
import {
createFakeEventHandlers,
createFakeViewer,
@@ -154,3 +156,133 @@ test('the engine clock is the app clock given, latched on the viewer clock tick'
engine.destroy();
assert.equal(viewer.clock.onTick.numberOfListeners, 0);
});
// The Cesium collections under the engine's root collections.
function collectionsUnder(viewer) {
return viewer.primitiveList.flatMap((root) =>
Array.from({ length: root.length }, (_, index) => root.get(index)),
);
}
test('movers and lines draw on the scene, answer picks with their pickId and go with the engine', async () => {
const geo = await import('../../geo/index.js');
const viewer = createFakeViewer();
const reasons = [];
const engine = createCesiumEngine(viewer, {
requestRender: (reason) => reasons.push(reason),
});
const set = engine.movers.createSet({
id: 'horizon-points',
pickable: true,
kind: 'point',
capacity: 2,
point: { sizePx: 8 },
});
// The fake camera is at x = 7e6 (over 0°, 0°): 0°E is in front, 180° behind.
set.set(0, { position: geo.cartesian3.fromDegrees(0, 1, 0), pickId: 'a' });
set.set(1, { position: geo.cartesian3.fromDegrees(180, 1, 0), pickId: 'b' });
set.setCount(2);
set.commit();
const line = engine.lines.createLine({
id: 'orbit',
widthPx: 2,
color: [1, 1, 1, 1],
pickId: 'orbit-1',
});
assert.deepEqual(engine.movers.list(), [
{ id: 'horizon-points', kind: 'point', count: 2, capacity: 2 },
]);
assert.deepEqual(engine.lines.list(), [
{ id: 'orbit', points: 0, shown: true },
]);
assert.ok(reasons.includes('movers'));
assert.equal(viewer.primitiveList.length, 2, 'one root each');
const [points] = collectionsUnder(viewer).filter(
(item) => item instanceof Cesium.PointPrimitiveCollection,
);
const front = points.get(0);
const back = points.get(1);
const lineSlot = { gevLine: 'orbit' };
viewer.scene.drillPick = () => [
{ primitive: back, id: 'b' },
{ primitive: front, id: 'stale' },
{ primitive: lineSlot, id: 'orbit-1' },
];
assert.deepEqual(
engine.picker
.drillPick({ x: 1, y: 1 })
.map(({ id, owner, instance }) => [id, owner, instance]),
[
['a', 'horizon-points', 0],
['orbit-1', 'orbit', undefined],
],
'the instance behind the horizon is left out',
);
viewer.scene.pick = () => ({ primitive: front, id: 'stale' });
assert.equal(engine.picker.pick({ x: 1, y: 1 }).id, 'a');
// A mover set in a surface exclusion is hidden for the sample only.
let shownDuring;
viewer.scene.sampleHeight = (position, excluded) => {
shownDuring = [points.show, excluded];
return 5;
};
const place = geo.cartographic.fromDegrees(0, 1, 0);
assert.equal(engine.surface.sampleHeight(place, [set]), 5);
assert.deepEqual(shownDuring, [false, []]);
assert.equal(points.show, true);
engine.destroy();
assert.equal(points.isDestroyed(), true);
assert.deepEqual(viewer.primitiveList, []);
assert.deepEqual(engine.movers.list(), []);
assert.deepEqual(engine.lines.list(), []);
assert.equal(set.isDestroyed(), true);
assert.throws(() => line.show(false), /destroyed/);
});
test('the contract suite movers and lines checks pass on the Cesium engine (no pixels or pointer)', async (t) => {
// Cesium's Material sorts uniform values with instanceof checks against
// these DOM classes; Node has none of them.
for (const name of [
'HTMLCanvasElement',
'HTMLImageElement',
'HTMLVideoElement',
'ImageBitmap',
'OffscreenCanvas',
])
globalThis[name] ??= class {};
const handlers = createFakeEventHandlers();
const { scene, engine, frame } = createCameraViewer({
engineOptions: {
createEventHandler: handlers.create,
// The atlas crop needs a DOM, the glTF load the network.
moverFactories: {
cropCells: async (atlas, keys) =>
new Map(keys.map((key) => [key, { width: 16, height: 16 }])),
loadModel: async () => ({
ready: true,
modelMatrix: new Cesium.Matrix4(),
isDestroyed: () => false,
destroy() {},
}),
},
},
});
// The terrain under the surface check's sample.
scene.globe.getHeight = () => 12;
const checks = createContractSuite(engine, {
waitFrames: async (count) => {
for (let index = 0; index < count; index++) frame();
},
}).filter(({ name }) =>
/^(shape|movers|lines|colours|input: picks|surface\.sampleHeightDetailed)\b/.test(
name,
),
);
assert.equal(checks.length, 8);
try {
for (const { name, run } of checks) await t.test(name, run);
} finally {
engine.destroy();
}
assert.equal(scene.primitives.length, 0, 'no collection left behind');
});