diff --git a/src/annotations/hybridAnnotationRenderer.test.mjs b/src/annotations/hybridAnnotationRenderer.test.mjs index 721a5dcd5..8996c1618 100644 --- a/src/annotations/hybridAnnotationRenderer.test.mjs +++ b/src/annotations/hybridAnnotationRenderer.test.mjs @@ -5,7 +5,9 @@ import { createPinia } from 'pinia'; import { useScreenAnnotationsStore } from '../ui/stores/screenAnnotations.js'; import { createHybridAnnotationRenderer } from './hybridAnnotationRenderer.js'; import { showOsmCredit, hideOsmCredit } from '../data/dataCredits.js'; -import { bindFakeCesiumCredits } from '../testSupport/fakeCreditsPort.mjs'; +import { bindFakeCesiumCredits, fakeCesiumCredits } from '../testSupport/fakeCreditsPort.mjs'; +import { bindRenderer } from '../render/rendererOf.js'; +import { bindFakeCesiumScene } from '../testSupport/fakeCesiumScene.mjs'; /** A hybrid renderer whose screen board is presented to its own store. */ function hybridWithBoard(viewer) { @@ -90,6 +92,17 @@ function installBrowserGlobals(t) { }); } +/** + * `viewer` with the fake scene's camera, query, frame and draw ports and the + * fake credits bound over it as one renderer. + */ +function withScenePorts(viewer) { + const { unbind, ...ports } = bindFakeCesiumScene(viewer); + unbind(); + bindRenderer(viewer, { ...ports, credits: fakeCesiumCredits(viewer) }); + return viewer; +} + /** A viewer whose scene/camera is just enough for both sub-renderers. */ function fakeViewer(lon, lat) { const credits = new Set(); @@ -111,7 +124,7 @@ function fakeViewer(lon, lat) { return { dataSources, frames, - viewer: bindFakeCesiumCredits({ + viewer: withScenePorts({ credits, creditDisplay: { addStaticCredit: (credit) => credits.add(credit), @@ -119,6 +132,7 @@ function fakeViewer(lon, lat) { }, scene, camera, + canvas: scene.canvas, trackedEntity: null, dataSources: { add(dataSource) { @@ -207,6 +221,7 @@ test('hybrid outline upgrade preserves the screen group and adds world geometry' const viewer = { scene, camera, + canvas: scene.canvas, trackedEntity: null, dataSources: { add(dataSource) { @@ -220,6 +235,8 @@ test('hybrid outline upgrade preserves the screen group and adds world geometry' }, }, }; + // The screen board projects through the renderer's camera and query. + bindFakeCesiumScene(viewer); const { store, renderer } = hybridWithBoard(viewer); const anno = { id: 'anno-hybrid-fb3', @@ -783,6 +800,7 @@ test('a deadline passed during caption conversion leaves a drawn pending point', const live = annotationGroups(store).filter((mark) => !mark.className?.includes('gev-out')); assert.equal(live.length, 1); assert.ok(childrenWithClass(live[0], 'gev-anno-ring').length > 0, 'the pending point still draws'); + assert.notEqual(live[0].display, 'none', 'and is on screen'); renderer.remove(anno); renderer.destroy(); }); diff --git a/src/annotations/screenAnnotationRenderer.js b/src/annotations/screenAnnotationRenderer.js index 634f17539..168a86051 100644 --- a/src/annotations/screenAnnotationRenderer.js +++ b/src/annotations/screenAnnotationRenderer.js @@ -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 */ } diff --git a/src/annotations/screenAnnotationRenderer.test.mjs b/src/annotations/screenAnnotationRenderer.test.mjs index ce1fb4058..f885ca577 100644 --- a/src/annotations/screenAnnotationRenderer.test.mjs +++ b/src/annotations/screenAnnotationRenderer.test.mjs @@ -6,7 +6,19 @@ import { createApp, nextTick } from 'vue'; import { createPinia } from 'pinia'; import { useScreenAnnotationsStore } from '../ui/stores/screenAnnotations.js'; import ScreenAnnotations from '../ui/views/panels/ScreenAnnotations.vue'; -import { createScreenAnnotationRenderer } from './screenAnnotationRenderer.js'; +import { createScreenAnnotationRenderer as createRenderer } from './screenAnnotationRenderer.js'; +import { bindFakeCesiumScene } from '../testSupport/fakeCesiumScene.mjs'; +import { rendererOf } from '../render/rendererOf.js'; + +/** + * The renderer over a viewer double with the camera, query and frame ports + * bound over it (once per double), and the scene's canvas as the viewer's. + */ +function createScreenAnnotationRenderer(viewer, options) { + if (!rendererOf(viewer)) bindFakeCesiumScene(viewer); + viewer.canvas ??= viewer.scene?.canvas; + return createRenderer(viewer, options); +} // happy-dom does not lay out SVG; every element measures as a 40 x 16 box. for (const type of [globalThis.SVGElement, globalThis.SVGGraphicsElement]) { @@ -53,7 +65,7 @@ function sceneFor(camera, extra = {}) { return { camera, canvas: { clientWidth: 800, clientHeight: 600, width: 800, height: 600 }, - clampToHeightSupported: false, + sampleHeightSupported: false, postRender: { addEventListener() {}, removeEventListener() {} }, ...extra, }; @@ -173,14 +185,13 @@ test('outline upgrade preserves the existing SVG group identity', async (t) => { const sampledAnchors = []; const scene = sceneFor(camera, { canvas: { clientWidth: 1280, clientHeight: 720, width: 1280, height: 720 }, - clampToHeightSupported: true, - clampToHeight(world) { - const cartographic = Cesium.Cartographic.fromCartesian(world); + sampleHeightSupported: true, + sampleHeight(cartographic) { sampledAnchors.push([ Number(Cesium.Math.toDegrees(cartographic.longitude).toFixed(4)), Number(Cesium.Math.toDegrees(cartographic.latitude).toFixed(4)), ]); - return world; + return 0; }, }); const renderer = createScreenAnnotationRenderer( diff --git a/src/render/cesium/query.js b/src/render/cesium/query.js index 306bf3fd7..8bbb23510 100644 --- a/src/render/cesium/query.js +++ b/src/render/cesium/query.js @@ -197,6 +197,9 @@ export function createCesiumQuery({ let occluderEllipsoid = null; const occluderEye = new Cesium.Cartesian3(NaN, NaN, NaN); const occludedPoint = new Cesium.Cartesian3(); + /** `project`'s scratch point and window position. */ + const projectedPoint = new Cesium.Cartesian3(); + const projectedWindow = new Cesium.Cartesian2(); const query = { pick(x, y, window = {}) { @@ -233,9 +236,13 @@ export function createCesiumQuery({ return { origin: ecef(ray.origin), direction: ecef(ray.direction) }; }, project(position) { + projectedPoint.x = position.x; + projectedPoint.y = position.y; + projectedPoint.z = position.z; const s = Cesium.SceneTransforms.worldToWindowCoordinates( scene, - toCartesian(position), + projectedPoint, + projectedWindow, ); return s ? { x: s.x, y: s.y } : null; }, diff --git a/src/render/cesiumAllowList.json b/src/render/cesiumAllowList.json index cd94cd023..1bd58dc7a 100644 --- a/src/render/cesiumAllowList.json +++ b/src/render/cesiumAllowList.json @@ -3,7 +3,6 @@ "src/annotations/annotationEngine.js", "src/annotations/drawTool.js", "src/annotations/resolver.js", - "src/annotations/screenAnnotationRenderer.js", "src/annotations/worldAnnotationRenderer.js", "src/app/embed.js", "src/app/scene.js",