Draw the screen-space label overlay through the renderer's ports

The world overlay no longer imports Cesium. It reads the eye from the
camera port's pose, builds its view-projection from the camera port's
matrices, asks the query port whether a point is behind the globe, paints
on the frame port's postRender and asks it for renders, and invalidates
on the camera port's moveEnd. It still reads the viewer's container and
canvas, which are DOM. It leaves the Cesium allow-list.

The Cesium query port now keeps one horizon occluder per eye and a
scratch point, so the overlay's per-label horizon test allocates nothing,
as it did when the overlay held the occluder itself. The overlay tests
bind camera, frame and query ports over their viewer doubles, the
allocation probe using the Cesium port's own horizon test.
This commit is contained in:
Sameh Khamis committed 2026-11-07 04:13:18 -08:00
1 parent e34e923ddd
commit dd4f19967d
10 files changed
+235 -72

No files matched your search

+12 -1
View File
@@ -6,7 +6,8 @@
*
* - camera: `pose()` from the double's camera (`viewer.camera`, else
* `viewer.scene.camera`): `positionWC`, `directionWC` and
* `positionCartographic`; `viewRectangle()` from its
* `positionCartographic`; `matrices()` from its `viewMatrix` and
* `frustum.projectionMatrix`; `viewRectangle()` from its
* `computeViewRectangle()`, in degrees; events from its own `moveEnd`,
* `moveStart` and `changed`.
* - query: `sampleHeightNow` through the scene's height service;
@@ -155,6 +156,16 @@ export function bindFakeCesiumScene(viewer, { draw = fakeDrawPort() } = {}) {
frameIsIdentity: true,
};
},
/** The double camera's `viewMatrix` and `frustum.projectionMatrix`. */
matrices() {
const c = cam();
return {
view: Float64Array.from(Cesium.Matrix4.toArray(c.viewMatrix)),
projection: Float64Array.from(
Cesium.Matrix4.toArray(c.frustum.projectionMatrix),
),
};
},
viewRectangle() {
const rect = cam()?.computeViewRectangle?.();
if (!rect) return null;
+73
View File
@@ -0,0 +1,73 @@
/**
* Camera, frame and query ports over a Cesium-shaped viewer double, for the
* screen-space overlay's tests: what the overlay asks of a renderer, read
* live from the double (`viewer.camera.positionWC`, `positionCartographic`,
* `viewMatrix`, `frustum.projectionMatrix`, `moveEnd`; `viewer.scene`'s
* `postRender` and `requestRender`), so a test moves the camera by editing
* the double. The horizon test is the Cesium query port's own; the rest
* allocates nothing per call once warm, so the overlay's allocation probe
* measures the overlay and the occlusion test it runs in the app.
*/
import * as Cesium from 'cesium';
import { createCesiumQuery } from '../render/cesium/query.js';
import { bindRenderer } from '../render/rendererOf.js';
import { fakeSceneFrame } from './fakeCesiumScene.mjs';
/**
* Bind the ports to `viewer`.
* @param {object} viewer
* @returns {{camera: object, frame: object, query: object, unbind: () => void}}
*/
export function bindOverlayPorts(viewer) {
const pose = {
position: { x: 0, y: 0, z: 0 },
height: Number.NaN,
};
const matrices = {
view: new Float64Array(16),
projection: new Float64Array(16),
};
const camera = {
pose() {
const c = viewer.camera;
pose.position.x = c.positionWC.x;
pose.position.y = c.positionWC.y;
pose.position.z = c.positionWC.z;
pose.height = c.positionCartographic?.height ?? Number.NaN;
return pose;
},
matrices() {
Cesium.Matrix4.toArray(viewer.camera.viewMatrix, matrices.view);
Cesium.Matrix4.toArray(
viewer.camera.frustum.projectionMatrix,
matrices.projection,
);
return matrices;
},
on(event, cb) {
const source = viewer.camera?.[event];
const off = source?.addEventListener?.(cb);
return typeof off === 'function'
? off
: () => source?.removeEventListener?.(cb);
},
};
const frame = fakeSceneFrame(viewer);
// The Cesium query port's own horizon test, so the allocation probe
// measures what the app runs.
const cesiumQuery = createCesiumQuery({
scene: viewer.scene ?? {},
camera: {
get positionWC() {
return viewer.camera.positionWC;
},
},
draw: { sets: new Map() },
capabilities: new Set(),
});
const query = {
isOccluded: (position) => cesiumQuery.isOccluded(position),
};
const unbind = bindRenderer(viewer, { camera, frame, query });
return { camera, frame, query, unbind };
}