Draw the satellites layer through the renderer ports

The satellite dots, the tracked satellite's dot and the orbit rings now go
through the draw port instead of a raw point collection, an entity and
primitives, so the layer's rendering no longer drives Cesium and leaves the
Cesium allow-list.

- Dots are one points set, patched in place and sent once per frame.
- The tracked dot is its own set, drawn at the sample the follow camera
  framed (or after the frame's own sample when no camera read it).
- Rings are geodesic lines with a behind-the-globe colour, turned about the
  pole by the sidereal angle in the layer each second, since the port has no
  inertial frame. The orbit frame matrix is a plain 16-number array, and
  the launches layer now receives plain ECEF orbit positions.
- Clicks match the draw hit against the layer's own sets.
- The focus pass projects every dot in one batched call.
- Depth-fail lines in the Cesium draw port honour pickable: false.
This commit is contained in:
Sameh Khamis committed 2026-11-08 13:06:52 -08:00
1 parent 1bff078097
commit d513034f3d
17 files changed
+845 -415

No files matched your search

+246 -62
View File
@@ -13,6 +13,9 @@ import {
TRACKED_ORBIT_COLOR,
} from './policy.js';
import { createRendering } from './rendering.js';
import { bindFakeDraw } from '../../testSupport/fakeDrawPort.mjs';
import { gstime } from 'satellite.js';
import * as focusDeemphasis from '../../data/focusDeemphasis.js';
const rgba = (color) => [color.red, color.green, color.blue, color.alpha];
@@ -44,9 +47,9 @@ test('point styles keep the colours the dots were drawn in', () => {
test('positions read back from the drawn dots match the Cesium geodetic conversion', () => {
const position = Cesium.Cartesian3.fromDegrees(-97.7, 30.2, 420_000);
const point = { position, show: true };
const point = { pos: position, show: true };
_setSatelliteLabelLifecycleStateForTest({
viewer: { scene: { frameState: { frameNumber: 1 } } },
viewer: {},
satrec: null,
point,
overlayHost: { setEntries() {}, setVisible() {}, clearSource() {} },
@@ -68,7 +71,7 @@ test('positions read back from the drawn dots match the Cesium geodetic conversi
}
});
test('only the rendering part drives Cesium', () => {
test('no part of the layer drives Cesium', () => {
const offList = [
'catalog',
'controls',
@@ -114,6 +117,13 @@ test('only the rendering part drives Cesium', () => {
for (const pattern of forbidden)
assert.doesNotMatch(source, pattern, `${name}.js: ${pattern}`);
}
// The rendering part owns the handles, and draws through the draw port.
const rendering = readFileSync(
new URL('./rendering.js', import.meta.url),
'utf8',
);
for (const pattern of forbidden.slice(0, 7))
assert.doesNotMatch(rendering, pattern, `rendering.js: ${pattern}`);
// The patterns themselves still catch what they are for.
const caught = [
'layerState._trackedEntity.position.setValue(p)',
@@ -161,64 +171,238 @@ test('only the rendering part drives Cesium', () => {
);
});
test('the rendering part draws the dots and rings in Cesium colours', () => {
const added = [];
const state = {
_points: new Map(),
_orbitPaths: new Map(),
_catalog: new Map([[25544, { satrec: {} }]]),
_pointCollection: { add: (options) => ({ ...options }) },
_viewer: { scene: { primitives: { add: (p) => added.push(p) } } },
};
const path = [
{ x: 7e6, y: 0, z: 0 },
{ x: 0, y: 7e6, z: 0 },
{ x: -7e6, y: 0, z: 0 },
];
const rendering = createRendering({
state,
services: { focus: {} },
parts: { orbits: { computeOrbitPath: () => path } },
});
rendering._addPoint(25544, { x: 1, y: 2, z: 3 }, POINT_STYLES.iss);
const point = state._points.get(25544);
assert.ok(point.position instanceof Cesium.Cartesian3);
assert.deepEqual(
[point.position.x, point.position.y, point.position.z],
[1, 2, 3],
);
assert.ok(point.color instanceof Cesium.Color);
assert.deepEqual(
rgba(point.color),
rgba(Cesium.Color.fromCssColorString('#ff4444')),
);
assert.deepEqual(
rgba(point.outlineColor),
rgba(Cesium.Color.WHITE.withAlpha(0.3)),
);
assert.equal(point.pixelSize, 12);
assert.equal(point.outlineWidth, 2);
assert.equal(point.id, 25544);
assert.ok(
Cesium.NearFarScalar.equals(
point.scaleByDistance,
new Cesium.NearFarScalar(1e6, 1.5, 2e7, 0.6),
),
);
test('the rendering part draws the dots, the tracked dot and the rings through the draw port', () => {
const viewer = {};
const { draw, unbind } = bindFakeDraw(viewer);
try {
const state = {
_points: new Map(),
_orbitPaths: new Map(),
_catalog: new Map([[25544, { satrec: {} }]]),
_viewer: viewer,
_trackedNorad: null,
_enabled: false,
};
const path = [
{ x: 7e6, y: 0, z: 0 },
{ x: 0, y: 7e6, z: 0 },
{ x: -7e6, y: 0, z: 0 },
];
const rendering = createRendering({
state,
services: {
focus: {
focusNowMs: (ms) => ms,
getFocusTarget: () => null,
focusPassIsNeeded: () => false,
},
},
parts: {
orbits: { computeOrbitPath: () => path },
tracking: { _getTrackedFramePosition: () => sampleFrame() },
},
});
// The frame's own sample, taken after the tick's first part.
let sampleFrame = () => null;
rendering._attach(viewer);
const points = draw.find('satellites:').spec.id;
assert.match(points, /^satellites:\d+:points$/);
rendering._addPoint(25544, { x: 1, y: 2, z: 3 }, POINT_STYLES.iss);
const point = state._points.get(25544);
assert.deepEqual(point.pos, { x: 1, y: 2, z: 3 });
assert.equal(draw.set(points).items.size, 0, 'sent with the frame');
rendering._preRenderTick({ frame: 1 });
const drawn = draw.set(points).items.get('25544');
assert.equal(drawn, point);
assert.deepEqual(
[...drawn.color],
rgba(Cesium.Color.fromCssColorString('#ff4444')),
);
assert.deepEqual(
[...drawn.outlineColor],
rgba(Cesium.Color.WHITE.withAlpha(0.3)),
);
assert.equal(drawn.pixelSize, 12);
assert.equal(drawn.outlineWidth, 2);
assert.deepEqual(drawn.meta, { noradId: 25544 });
assert.deepEqual(
{ ...drawn.scaleByDistance },
{ near: 1e6, nearValue: 1.5, far: 2e7, farValue: 0.6 },
);
// Moves go out in one upsert a frame.
const writes = draw.set(points).writes;
rendering._movePoint(25544, { x: 4, y: 5, z: 6 });
rendering._movePoint(25544, { x: 7, y: 8, z: 9 });
rendering._preRenderTick({ frame: 2 });
assert.equal(draw.set(points).writes, writes + 1);
assert.deepEqual(draw.set(points).items.get('25544').pos, {
x: 7,
y: 8,
z: 9,
});
// A pick on a dot names its satellite; one on another set does not.
assert.equal(rendering._hitPointId({ set: points, id: '25544' }), 25544);
assert.equal(rendering._hitPointId({ set: 'other:1', id: '25544' }), null);
rendering._showOrbitPath(25544, TRACKED_ORBIT_COLOR);
assert.equal(added.length, 1);
const instance = added[0].geometryInstances;
assert.deepEqual(
[...instance.attributes.color.value],
[255, 255, 0, Cesium.Color.floatToByte(0.6)],
);
assert.ok(
instance.geometry._positions.every((p) => p instanceof Cesium.Cartesian3),
);
assert.ok(
rendering
._rendererPositions(path)
.every((p, i) => p instanceof Cesium.Cartesian3 && p.x === path[i].x),
);
rendering._showOrbitPath(25544, TRACKED_ORBIT_COLOR);
const rings = draw.sets.get(points.replace(':points', ':rings'));
assert.equal(rings.spec.pickable, false, 'ring clicks fall through');
const ring = rings.items.get('25544');
assert.deepEqual(ring.style, { kind: 'solid', color: [1, 1, 0, 0.6] });
assert.deepEqual(ring.depthFailColor, [1, 1, 0, 0.35]);
assert.equal(ring.widthPx, 2.5, 'the ISS ring is wider');
assert.equal(ring.mode, 'geodesic');
assert.deepEqual([...ring.positions.slice(0, 3)], [7e6, 0, 0]);
// Turned with the Earth: by minus the GMST elapsed since its bake.
const path0 = state._orbitPaths.get(25544);
const later = new Date(Date.now() + 600_000);
rendering._updateOrbitPathRotations(later);
const angle = -(gstime(later) - path0.gmstAtBake);
const turned = rings.items.get('25544').positions;
assert.ok(Math.abs(turned[0] - 7e6 * Math.cos(angle)) < 1e-3);
assert.ok(Math.abs(turned[1] - 7e6 * Math.sin(angle)) < 1e-3);
rendering._hideOrbitPath(25544);
assert.equal(rings.items.size, 0);
// The tracked dot draws at the tracker's position, from the next frame.
state._trackedNorad = 25544;
state._enabled = true;
state._params = { showPoints: false, showOrbits: false };
let at = { x: 10, y: 20, z: 30 };
const carrier = rendering._addTrackedEntity({ position: () => at });
assert.deepEqual(carrier, { noradId: 25544 });
const tracked = draw.set(points.replace(':points', ':tracked'));
assert.equal(tracked.items.size, 0);
rendering._preRenderTick({ frame: 3 });
assert.deepEqual(tracked.items.get('25544').pos, at);
assert.equal(tracked.items.get('25544').pixelSize, 14);
assert.equal(tracked.items.get('25544').disableDepthTest, Infinity);
at = { x: 11, y: 21, z: 31 };
rendering._preRenderTick({ frame: 4 });
assert.deepEqual(tracked.items.get('25544').pos, at);
// When the follow camera has already sampled this frame's key, the dot
// draws at that sample, before the frame's own sample moves it on.
const framed = { x: 12, y: 22, z: 32 };
at = framed;
state._trackedFrameGeo = {};
state._trackedFrameNumber = 4;
sampleFrame = () => {
at = { x: 13, y: 23, z: 33 };
return at;
};
rendering._preRenderTick({ frame: 5 });
assert.deepEqual(tracked.items.get('25544').pos, framed);
// With no sample under the frame's key, it draws after the frame's own.
state._trackedFrameNumber = 3;
at = framed;
rendering._preRenderTick({ frame: 6 });
assert.deepEqual(tracked.items.get('25544').pos, { x: 13, y: 23, z: 33 });
assert.equal(
rendering._hitIsTracked({ set: tracked.spec.id, id: '25544' }),
true,
);
rendering._removeTrackedEntity(carrier);
assert.equal(tracked.items.size, 0);
rendering._detach(viewer);
assert.ok([...draw.sets.values()].every((set) => set.disposed));
} finally {
unbind();
}
});
test('the orbit frame matrix turns ECEF about Z by minus the GMST elapsed', () => {
const rendering = createRendering({
state: {},
services: { focus: {} },
parts: {},
});
const bake = new Date('2026-07-20T10:00:00Z');
const now = new Date('2026-07-20T10:10:00Z');
const m = rendering.orbitFrameModelMatrix(gstime(bake), now);
const angle = -(gstime(now) - gstime(bake));
assert.equal(m.length, 16);
assert.ok(Math.abs(m[0] - Math.cos(angle)) < 1e-12);
assert.ok(Math.abs(m[1] - Math.sin(angle)) < 1e-12);
assert.ok(Math.abs(m[4] + Math.sin(angle)) < 1e-12);
assert.equal(m[10], 1);
assert.equal(m[15], 1);
// Written in place into a matrix it is handed.
const into = new Cesium.Matrix4();
assert.equal(rendering.orbitFrameModelMatrix(gstime(bake), now, into), into);
assert.ok(Math.abs(into[0] - Math.cos(angle)) < 1e-12);
});
test('the focus pass projects every dot in one call and dims the dot under the focus', () => {
const viewer = {};
const calls = [];
const { unbind } = bindFakeDraw(viewer, {
camera: { pose: () => ({ position: { x: 0, y: 0, z: 0 } }) },
query: {
// Screen x, y are the point's own x, y.
projectMany(source, out) {
calls.push(source.length / 3);
for (let i = 0, j = 0; i + 2 < source.length; i += 3, j += 2) {
out[j] = source[i];
out[j + 1] = source[i + 1];
}
},
project: () => {
throw new Error('the pass projects in one batch');
},
},
});
try {
const target = {
screenRect: { left: 90, right: 110, top: 90, bottom: 110 },
cameraDistance: 1,
};
const state = {
_points: new Map(),
_orbitPaths: new Map(),
_catalog: new Map(),
_viewer: viewer,
_trackedNorad: null,
_enabled: true,
_params: { showPoints: true, showOrbits: false },
_activeFocusCount: 0,
_lastFocusUpdate: -Infinity,
};
const rendering = createRendering({
state,
services: {
focus: {
...focusDeemphasis,
focusNowMs: (ms) => ms,
getFocusTarget: () => target,
focusPassIsNeeded: () => true,
},
},
parts: {
controls: { _pointStyleFor: () => ({ color: [1, 1, 1, 1] }) },
tracking: { _getTrackedFramePosition: () => null },
},
});
rendering._attach(viewer);
const style = { ...POINT_STYLES.iss, color: [1, 1, 1, 1] };
rendering._addPoint(1, { x: 100, y: 100, z: 0 }, style);
rendering._addPoint(2, { x: 100, y: 100, z: 0 }, style);
rendering._addPoint(3, { x: 500, y: 500, z: 0 }, style);
rendering._addPoint(4, { x: 100, y: 100, z: 0 }, style);
state._points.get(2).show = false;
state._trackedNorad = 4;
for (let now = 0; now <= 5000; now += 100) rendering._updatePointFocus(now);
assert.ok(calls.length > 0);
assert.ok(
calls.every((n) => n === 4),
'one call over every dot',
);
const alpha = (id) => state._points.get(id).color[3];
assert.ok(alpha(1) < 1, 'the dot under the focus dims');
assert.equal(alpha(2), 1, 'a hidden dot is not dimmed');
assert.equal(alpha(3), 1, 'a dot away from the focus is not dimmed');
assert.equal(alpha(4), 1, 'the tracked dot is not dimmed');
rendering._detach(viewer);
} finally {
unbind();
}
});