Project the annotation whiteboard through the renderer's ports

The screen annotation renderer no longer imports Cesium. Each projection
pass reads the eye from the camera port's pose and projects through the
query port: behind-camera and horizon rejection, then project. It
repaints on the frame port's postRender, and asks for ground heights
through the height gateway, which now sees through draw sets not drawn as
ground. It sizes the board from the renderer's canvas. It leaves the
Cesium allow-list.

The Cesium query port's project reuses a scratch point and window
position, as the renderer's own projection did.

Its tests bind fake camera, query and frame ports over their viewer
doubles. The hybrid renderer's shared fixture binds them too, so its
screen marks are projected and checked to be on screen.
This commit is contained in:
Sameh Khamis committed 2026-11-07 04:13:18 -08:00
1 parent ed8f89db74
commit 8d586e5ff9
5 files changed
+94 -57

No files matched your search

+49 -47
View File
@@ -1,5 +1,4 @@
import { followedOf } from '../data/followed.js';
import * as Cesium from 'cesium';
import { getOverlayPaintRect } from '../overlays/worldOverlay.js';
import {
getActiveTrackedReadoutId,
@@ -9,7 +8,15 @@ import { t } from '../i18n/index.js';
import { nextTick } from 'vue';
import { useScreenAnnotationsStore } from '../ui/stores/screenAnnotations.js';
import { applicationPinia } from '../ui/vueRuntime.js';
import { heightsFor } from '../render/cesium/heights.js';
import { heightsOf } from '../queries/heights.js';
import { fromDegrees, toLonLatH } from '../geo/ellipsoid.js';
import { distance } from '../geo/vector.js';
import {
findCamera,
findFrame,
findQuery,
rendererOf,
} from '../render/rendererOf.js';
/** A label whose detailed outline could not be resolved. */
function outlineUnavailableLabel(label) {
@@ -80,11 +87,13 @@ export function createScreenAnnotationRenderer(
const arrowMarkers = new Map();
let viewBox = null;
const scene = viewer.scene;
const occluder = new Cesium.EllipsoidalOccluder(
Cesium.Ellipsoid.WGS84,
scene.camera.positionWC,
);
// The eye this frame is projected from (the camera port's pose), and the
// query port and canvas it is projected through, read once per pass.
let pose = null;
let passQuery = null;
let passCanvas = null;
// The renderer's canvas; a viewer with no renderer bound, its own.
const canvasOf = () => rendererOf(viewer)?.canvas ?? viewer.canvas;
const records = new Map(); // anno.id -> { key, anno, group, parts }
const leaving = new Map(); // key -> a removed mark's record while it fades out
const byKey = new Map(); // key -> record, live or fading
@@ -93,8 +102,6 @@ export function createScreenAnnotationRenderer(
let published = '';
// Only the renderer whose board the store shows may change or remove it.
const shown = () => store.board?.owner === owner;
const scratch = new Cesium.Cartesian2();
const scratchDir = new Cesium.Cartesian3();
// Per-point ground height cache. Marks must sit on the real surface, not at
// sea level — otherwise in elevated cities (Austin ~150 m) they project
// underground and parallax sinks them at oblique angles. Sampled once the
@@ -123,7 +130,7 @@ export function createScreenAnnotationRenderer(
// Re-project every annotation each rendered frame so marks stay glued to the
// world. postRender runs after the camera/tiles update, so positions are exact.
const onPostRender = () => projectAll();
scene.postRender.addEventListener(onPostRender);
const removePostRender = findFrame(viewer)?.on('postRender', onPostRender);
function color(anno) {
return PALETTE[anno.color] || PALETTE.primary;
@@ -143,12 +150,12 @@ export function createScreenAnnotationRenderer(
return cached.h;
}
try {
const heights = heightsFor(scene);
const heights = heightsOf(viewer);
if (heights.supported().clamp) {
// A miss is tiles not ready.
const c = heights.clampNow(Cesium.Cartesian3.fromDegrees(lon, lat, 0));
const c = heights.clampNow(fromDegrees(lon, lat, 0));
if (c) {
const h = Cesium.Cartographic.fromCartesian(c).height;
const h = toLonLatH(c)?.height;
if (Number.isFinite(h) && h > -430 && h < 9000) {
heightCache.set(key, { h, settled: true, gen: projGen });
trimHeightCache();
@@ -277,7 +284,7 @@ export function createScreenAnnotationRenderer(
}
// Record the mark BEFORE it reaches the board, then unwind on any throw
// from here down (projectAll reaches into Cesium and can fail on a lost
// from here down (projectAll reaches into the renderer and can fail on a lost
// context). Previously a throw past the insert left an ORPHANED mark: the
// engine's rollback only deletes its own map entry, so the next annotate
// of the same geometry stacked a fresh mark on top of the corpse.
@@ -362,26 +369,27 @@ export function createScreenAnnotationRenderer(
// Anchor at the real surface height (cached + validated), so marks sit on
// the ground/building instead of at sea level. The settle-once cache keeps
// it stable (no per-frame jitter that earlier broke nearby projections).
const world = Cesium.Cartesian3.fromDegrees(
lon,
lat,
groundHeight(lon, lat),
);
const world = fromDegrees(lon, lat, groundHeight(lon, lat));
if (!pose) return null;
// Reject points behind the camera (they produce wrapped/extreme coords).
Cesium.Cartesian3.subtract(world, scene.camera.positionWC, scratchDir);
if (Cesium.Cartesian3.dot(scratchDir, scene.camera.directionWC) <= 0)
const eye = pose.position;
const look = pose.direction;
if (
(world.x - eye.x) * look.x +
(world.y - eye.y) * look.y +
(world.z - eye.z) * look.z <=
0
)
return null;
// Reject points beyond the globe horizon.
if (!occluder.isPointVisible(world)) return null;
const win = Cesium.SceneTransforms.worldToWindowCoordinates(
scene,
world,
scratch,
);
const query = passQuery;
if (!query || query.isOccluded(world)) return null;
const win = query.project(world);
if (!win || !Number.isFinite(win.x) || !Number.isFinite(win.y)) return null;
// Reject absurd off-screen projections (anchor not in view).
const w = scene.canvas.clientWidth || scene.canvas.width;
const h = scene.canvas.clientHeight || scene.canvas.height;
const canvas = passCanvas;
const w = canvas.clientWidth || canvas.width;
const h = canvas.clientHeight || canvas.height;
if (
!allowOffscreen &&
(win.x < -w || win.x > 2 * w || win.y < -h || win.y > 2 * h)
@@ -400,7 +408,6 @@ export function createScreenAnnotationRenderer(
const TRACKED_BBOX_MARGIN = 8; // px buffer added around the footprint
const TRACKED_FADE_EASE = 0.22; // per-frame ease toward hidden(0) / visible(1)
const TRACKED_HYSTERESIS_PX = 18; // dead-band so the overlap test can't flip-flop
const _scratchTrackedWin = new Cesium.Cartesian2();
// Evaluate a NearFarScalar (billboard scaleByDistance) at a camera distance.
function nearFarValue(nfs, dist) {
@@ -425,13 +432,7 @@ export function createScreenAnnotationRenderer(
const displayPosition = followed?.displayPosition;
const world =
typeof displayPosition === 'function' ? displayPosition() : null;
const win = world
? Cesium.SceneTransforms.worldToWindowCoordinates(
scene,
world,
_scratchTrackedWin,
)
: null;
const win = world ? (passQuery?.project(world) ?? null) : null;
// Billboard box — centered on the anchor, magnified by scaleByDistance, from
// the size the tracking layer recorded for its sprite.
@@ -441,11 +442,9 @@ export function createScreenAnnotationRenderer(
const baseH = sprite.height ?? 28;
let scale = 1;
const sbd = sprite.scaleByDistance;
if (sbd)
scale = nearFarValue(
sbd,
Cesium.Cartesian3.distance(scene.camera.positionWC, world),
);
// No eye to measure from: the sprite's own size.
if (sbd && pose)
scale = nearFarValue(sbd, distance(pose.position, world));
const hw = (baseW * scale) / 2;
const hh = (baseH * scale) / 2;
const bbLeft = win.x - hw;
@@ -474,14 +473,17 @@ export function createScreenAnnotationRenderer(
return;
}
projGen += 1; // new frame: entries touched below are "hot" and survive trimming
pose = findCamera(viewer)?.pose() ?? null;
passQuery = findQuery(viewer);
passCanvas = canvasOf();
const trackedRect = trackedEntityRect();
occluder.cameraPosition = scene.camera.positionWC;
const h = scene.canvas.clientHeight || scene.canvas.height;
const w = scene.canvas.clientWidth || scene.canvas.width;
const canvas = passCanvas;
const h = canvas.clientHeight || canvas.height;
const w = canvas.clientWidth || canvas.width;
viewBox = `0 0 ${w} ${h}`;
// Shrink point-marker geometry as the camera pulls back (declutter the blob).
const mScale = markScale(
viewer.camera.positionCartographic?.height ?? 1000,
Number.isFinite(pose?.height) ? pose.height : 1000,
);
for (const rec of records.values()) {
@@ -819,7 +821,7 @@ export function createScreenAnnotationRenderer(
function destroy() {
try {
scene.postRender.removeEventListener(onPostRender);
removePostRender?.();
} catch {
/* torn down */
}