diff --git a/src/data/bhoteKoshiEmbeddedMedia.js b/src/data/bhoteKoshiEmbeddedMedia.js index fb2d4ca7c..74139d256 100644 --- a/src/data/bhoteKoshiEmbeddedMedia.js +++ b/src/data/bhoteKoshiEmbeddedMedia.js @@ -1,4 +1,10 @@ -import * as Cesium from 'cesium'; +import { + findCamera, + findFrame, + findQuery, + rendererOf, +} from '../render/rendererOf.js'; +import { dot, sub } from '../geo/vector.js'; import { t, msg, onLocaleChange } from '../i18n/index.js'; const YOUTUBE_HOSTS = new Set([ @@ -538,23 +544,22 @@ function ensureProviderConnections(documentRef, provider) { } } +/** + * Where an anchor (ECEF) is in the card root, through the renderer's camera + * and query ports; null behind the eye, off the root, or with no renderer. + */ function anchorOnScreen(viewer, anchor, root) { - if (!viewer?.scene || !anchor || !root) return null; - const camera = viewer.camera; - if (camera?.positionWC && camera?.directionWC) { - const delta = Cesium.Cartesian3.subtract( - anchor, - camera.positionWC, - new Cesium.Cartesian3(), - ); - if (Cesium.Cartesian3.dot(delta, camera.directionWC) <= 0) return null; - } - const pixel = Cesium.SceneTransforms.worldToWindowCoordinates( - viewer.scene, - anchor, - ); + if (!anchor || !root) return null; + const camera = findCamera(viewer); + const query = findQuery(viewer); + if (!camera || !query) return null; + const pose = camera.pose(); + if (dot(sub(anchor, pose.position), pose.direction) <= 0) return null; + const pixel = query.project(anchor); if (!pixel) return null; - const canvasBounds = viewer.canvas?.getBoundingClientRect?.(); + // The renderer's canvas; a viewer with no renderer bound, its own. + const canvas = rendererOf(viewer)?.canvas ?? viewer.canvas; + const canvasBounds = canvas?.getBoundingClientRect?.(); const rootBounds = root.getBoundingClientRect?.(); if (!canvasBounds || !rootBounds) return null; const x = pixel.x + canvasBounds.left - rootBounds.left; @@ -865,7 +870,7 @@ export function createBhoteKoshiEmbeddedMedia({ } }; const removePostRender = - viewer.scene?.postRender?.addEventListener?.(position); + findFrame(viewer)?.on('postRender', () => position()) ?? null; function hide({ immediate = false, preserveWarm = false } = {}) { if (!preserveWarm) clearWarm(); diff --git a/src/data/bhoteKoshiEmbeddedMedia.test.mjs b/src/data/bhoteKoshiEmbeddedMedia.test.mjs index e55fb0d65..3d9bf1a98 100644 --- a/src/data/bhoteKoshiEmbeddedMedia.test.mjs +++ b/src/data/bhoteKoshiEmbeddedMedia.test.mjs @@ -10,6 +10,7 @@ import { publicEmbeddedMediaUrl, resolveEmbeddedMediaSource, } from './bhoteKoshiEmbeddedMedia.js'; +import { bindRenderer } from '../render/rendererOf.js'; test('Pinokio fallback never creates provider DOM or loads SDKs', async () => { const unexpected = () => { throw new Error('Provider resource allocated in Pinokio'); }; @@ -417,3 +418,55 @@ test('a media card stays between the panels when right-to-left swaps their sides assert.ok(left >= 340 + 18, `card at ${left}px lands on the context rail`); assert.ok(left + 392 <= 1040 - 18, `card at ${left}px lands on the scene panel`); }); + +test('a shown card follows its anchor through the renderer ports', () => { + class Element extends EventTarget { + constructor(tag) { + super(); this.tag = tag; this.children = []; this.dataset = {}; this.style = {}; + this.classList = { add() {}, remove() {}, contains() { return false; } }; + } + setAttribute() {} + removeAttribute() {} + querySelector() { return null; } + getBoundingClientRect() { return { left: 0, top: 0, right: 1000, bottom: 800, width: 1000, height: 800 }; } + append(...children) { for (const child of children) { child.remove(); child.parent = this; this.children.push(child); } } + remove() { if (this.parent) this.parent.children = this.parent.children.filter((child) => child !== this); this.parent = null; } + } + const documentRef = { + body: new Element('body'), + createElement: (tag) => new Element(tag), + createElementNS: (_, tag) => new Element(tag), + }; + const listeners = new Set(); + const viewer = { canvas: { getBoundingClientRect: () => ({ left: 500, top: 500 }) } }; + let pose = { position: { x: 0, y: 0, z: 0 }, direction: { x: 1, y: 0, z: 0 } }; + const projected = []; + // The renderer's canvas, not the viewer's, places the card. + const unbind = bindRenderer(viewer, { + canvas: { getBoundingClientRect: () => ({ left: 10, top: 5 }) }, + camera: { pose: () => pose }, + query: { project: (p) => { projected.push(p); return { x: 300, y: 200 }; } }, + frame: { + on(phase, cb) { + const entry = [phase, cb]; + listeners.add(entry); + return () => listeners.delete(entry); + }, + }, + }); + const media = createBhoteKoshiEmbeddedMedia({ viewer, documentRef, globalRef: {} }); + assert.deepEqual([...listeners].map(([phase]) => phase), ['postRender']); + const anchor = { x: 10, y: 0, z: 0 }; + assert.equal(media.show({ observation: { title: 'Clip', media: { sourceUrl: 'https://youtu.be/DbqRexFxv3k' } }, anchor }), true); + const card = documentRef.body.children[0].children.find((node) => node.tag === 'section'); + assert.equal(card.hidden, false); + assert.equal(projected.at(-1), anchor); + assert.equal(card.style.left, `${300 + 10 + 42}px`); + // Behind the eye: hidden on the next drawn frame. + pose = { ...pose, direction: { x: -1, y: 0, z: 0 } }; + for (const [, cb] of listeners) cb(); + assert.equal(card.hidden, true); + media.destroy(); + assert.equal(listeners.size, 0); + unbind(); +}); diff --git a/src/render/cesiumAllowList.json b/src/render/cesiumAllowList.json index 3ca1aec4a..b89b8b17f 100644 --- a/src/render/cesiumAllowList.json +++ b/src/render/cesiumAllowList.json @@ -11,7 +11,6 @@ "src/cyberSonarScene.js", "src/data/aircraftModelLayer.js", "src/data/aircraftMotionRegime.js", - "src/data/bhoteKoshiEmbeddedMedia.js", "src/data/bhoteKoshiEvent.js", "src/data/bhoteKoshiLocator.js", "src/data/cctvGizmo.js",