Move vessels every frame and ask for frames as they move

Vessels between two fixes, and live vessels a host placement places, were
redrawn at most every 250 ms, and a frame was asked for only once a second.
With nothing else asking for frames the scene drew vessels at about 1 to
2 fps live, and none at all while the world clock played at 1x.

They are now redrawn every frame the scene draws, moved in place without
new allocations. After each pass the layer works out how far the fastest
drawn vessel moved on screen and asks for the next frame for when it will
have moved another half pixel, at least once a second while any drawn
vessel moves. A vessel crossing the view close up therefore glides, and
one far away lets the scene idle. A frame already asked for later is
brought forward when a vessel speeds up.
This commit is contained in:
Sameh Khamis committed 2026-11-14 23:18:57 -08:00
1 parent e4708a815d
commit 57bacd98a8
3 files changed
+169 -46

No files matched your search

+58 -1
View File
@@ -3,6 +3,7 @@ import assert from 'node:assert/strict';
import { createVesselLayer } from './index.js';
import { VISIBILITY_UPDATE_MS } from './policy.js';
import { layerPlacement } from '../../data/placementProvider.js';
import * as Cesium from 'cesium';
// Vessels move only when a poll lands, so the layer must let the scene idle:
// it asks for frames for forced passes and one trailing frame after the camera
@@ -75,7 +76,16 @@ function setup({ placement = null } = {}) {
}
};
const frame = () => layer.testing._runVisibilityPassForTest();
return { layer, camera, requests, holds, timers, advance, frame };
return {
layer,
camera,
requests,
holds,
timers,
advance,
frame,
now: () => clock,
};
}
test('a parked camera schedules no frames after the cadence pass', () => {
@@ -196,3 +206,50 @@ test('a placement that places nothing asks for no frames; one that places a draw
advance(1_000);
assert.deepEqual(requests, ['ais-vessels-placement']);
});
test('a vessel moving visibly asks for its next frame as soon as it moves half a pixel; a slow one once a second', (t) => {
let mono = 0;
t.mock.method(performance, 'now', () => mono);
const originalDocument = globalThis.document;
globalThis.document = { getElementById: () => null };
t.after(() => {
if (originalDocument === undefined) delete globalThis.document;
else globalThis.document = originalDocument;
});
let spot = { lat: 51.93, lon: 4.05 };
const { layer, camera, timers, advance, now } = setup({
placement: { layer: () => ({ place: () => spot }) },
});
// 20 km above the vessel, in a 1,000 px tall view about 57° tall.
camera.positionWC = Cesium.Cartesian3.fromDegrees(4.05, 51.93, 20_000);
camera.frustum = { fovy: 1 };
const record = {
mmsi: '111',
lat: 51.93,
lon: 4.05,
billboard: { show: true, position: null },
};
layer.testing._setVesselStateForTest({
viewer: { camera, scene: {}, canvas: { clientHeight: 1000 } },
records: [record],
});
layer.testing._prepareVesselVisualForTest(record);
// How long after a pass the next frame is asked for, once the vessel moved
// `dLat` in the 16 ms since the pass before.
const waitAfterStep = (dLat) => {
layer.testing._runPlacementPassForTest();
advance(1_000); // whatever frame that asked for comes and goes
assert.equal(timers.size, 0);
mono += 16;
spot = { lat: spot.lat + dLat, lon: spot.lon };
layer.testing._runPlacementPassForTest();
assert.equal(timers.size, 1);
const [timer] = [...timers.values()];
return timer.at - now();
};
// About 11 m in a frame, 20 km away: over half a pixel, so the next frame
// is asked for within the frame.
assert.ok(waitAfterStep(0.0001) <= 16);
// About 1 cm in a frame: half a pixel takes seconds, so once a second.
assert.equal(waitAfterStep(0.0000001), 1000);
});