mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-17 00:32:21 +00:00
The Directions A and B letters and a data pack's media label were the two Cesium text labels left, kept by an allow-list in noCesiumLabels.test.mjs. Both now publish entries to the shared world-overlay host, and the allow-list is gone, so any Cesium label in src is a test failure. Directions publishes the letters to the directions-markers source; the markers keep only their points. A clicked endpoint's letter hangs over the surface that was clicked. One placed without a click stays hidden until its ground-floor cell resolves, then requests a render, so a still camera shows it; a cell that settles with no height falls back to the ellipsoid. Each position is computed once. A pack resource handle gains label(entry). The application passes its shared overlay host to SceneDirector through dataPacks.overlayHost; without one a media pack fails to load rather than drawing a native label. Each labelled resource takes a source from a pool and returns it on dispose, as the host never removes a source, so two presentations of one pack never clear each other's labels and repeat loads add no sources. A label written after dispose is ignored. The media label is protected, so it always draws as before. Live (Denver, keyless globe): a clicked A and a programmatic B both anchor on their dots over 1.58 km ground. Documented in docs/CURRENT-STATE.md and docs/DIRECTOR-DATA-PACKS.md.
263 lines
8.3 KiB
JavaScript
263 lines
8.3 KiB
JavaScript
import * as Cesium from 'cesium';
|
|
import { renderPackGeometry } from './geometry.js';
|
|
import { useDataPacksStore } from '../../ui/stores/dataPacks.js';
|
|
import { applicationPinia } from '../../ui/vueRuntime.js';
|
|
|
|
const xyz = (p) => Cesium.Cartesian3.fromDegrees(...p);
|
|
|
|
const MEDIA_MARKER_CSS = '#ffff00';
|
|
|
|
// Key every panel and card on the page, so a new one never reuses an old
|
|
// one's DOM.
|
|
let panelKeys = 0,
|
|
cardKeys = 0;
|
|
|
|
// Label overlay sources, one per pack resource while it shows text, so two
|
|
// presentations of one pack never share or clear each other's labels. The host
|
|
// keeps a source once made, so a disposed resource returns its id for reuse
|
|
// and the count stays at the most labelled resources ever shown at once.
|
|
let labelSources = 0;
|
|
const freeLabelSources = [];
|
|
|
|
/**
|
|
* Create rendering adapters; data acquisition and authored placement stay
|
|
* independent. Each pack's attribution card is presented to the data packs
|
|
* store, which its view renders inside the viewer's container.
|
|
*/
|
|
export function createPackPresentations(
|
|
viewer,
|
|
targets = new Map(),
|
|
{ store = useDataPacksStore(applicationPinia()), overlayHost = null } = {},
|
|
) {
|
|
const owner = {};
|
|
let key,
|
|
cards = [];
|
|
function present() {
|
|
store.publish(
|
|
owner,
|
|
cards.length ? { key, container: viewer.container, cards } : null,
|
|
);
|
|
}
|
|
function addCard(pack) {
|
|
if (!cards.length) key = ++panelKeys;
|
|
const card = {
|
|
key: ++cardKeys,
|
|
id: pack.id,
|
|
title: `${pack.id} · ${pack.attribution.text} · ${pack.attribution.license}`,
|
|
url: pack.attribution.url || null,
|
|
media: null,
|
|
};
|
|
cards = [...cards, card];
|
|
present();
|
|
return card.key;
|
|
}
|
|
function updateCard(key, patch) {
|
|
cards = cards.map((card) =>
|
|
card.key === key ? { ...card, ...patch } : card,
|
|
);
|
|
present();
|
|
}
|
|
function removeCard(key) {
|
|
cards = cards.filter((card) => card.key !== key);
|
|
present();
|
|
}
|
|
function resource(pack) {
|
|
const entities = [],
|
|
primitives = [],
|
|
materials = [],
|
|
urls = [],
|
|
labels = [];
|
|
const featureKeys = [];
|
|
let labelSource = null;
|
|
let card,
|
|
destroyed = false;
|
|
return {
|
|
feature(id, pickedId) {
|
|
const key = JSON.stringify([id.packId, id.featureId]);
|
|
targets.set(key, pickedId);
|
|
featureKeys.push(key);
|
|
},
|
|
add(spec) {
|
|
const entity = viewer.entities.add(spec);
|
|
entities.push(entity);
|
|
viewer.scene.requestRender();
|
|
return entity;
|
|
},
|
|
/**
|
|
* World text for this pack, as a label-overlay entry in the pack's own
|
|
* source. The application passes the shared host; there is no text
|
|
* without one, so a pack that needs it fails to load instead.
|
|
*/
|
|
label(entry) {
|
|
if (!overlayHost) throw new Error('Pack labels need an overlay host');
|
|
// A late adapter writing after disposal must not take a source again.
|
|
if (destroyed) return;
|
|
labelSource ||=
|
|
freeLabelSources.pop() ?? `data-pack-labels:${++labelSources}`;
|
|
labels.push(entry);
|
|
overlayHost.setEntries(labelSource, labels.slice());
|
|
viewer.scene.requestRender();
|
|
},
|
|
primitive(value) {
|
|
primitives.push(viewer.scene.primitives.add(value));
|
|
if (value.appearance?.material)
|
|
materials.push(value.appearance.material);
|
|
viewer.scene.requestRender();
|
|
},
|
|
card() {
|
|
card ||= addCard(pack);
|
|
},
|
|
/** @param {{kind: 'video'|'audio', src: string}} media */
|
|
media(media) {
|
|
card ||= addCard(pack);
|
|
updateCard(card, { media });
|
|
},
|
|
url(blob) {
|
|
const url = URL.createObjectURL(blob);
|
|
urls.push(url);
|
|
return url;
|
|
},
|
|
dispose() {
|
|
if (destroyed) return;
|
|
destroyed = true;
|
|
for (const key of featureKeys.splice(0)) targets.delete(key);
|
|
// The view stops a removed card's player as it goes.
|
|
if (card) removeCard(card);
|
|
for (const entity of entities.splice(0)) viewer.entities.remove(entity);
|
|
if (labelSource) {
|
|
labels.length = 0;
|
|
overlayHost.clearSource(labelSource);
|
|
freeLabelSources.push(labelSource);
|
|
labelSource = null;
|
|
}
|
|
for (const primitive of primitives.splice(0))
|
|
viewer.scene.primitives.remove(primitive);
|
|
for (const material of materials.splice(0)) {
|
|
if (!material.isDestroyed()) material.destroy();
|
|
}
|
|
for (const url of urls.splice(0)) URL.revokeObjectURL(url);
|
|
viewer.scene.requestRender();
|
|
},
|
|
};
|
|
}
|
|
function guarded(render) {
|
|
return async (context) => {
|
|
const handle = resource(context.pack);
|
|
const abort = () => handle.dispose();
|
|
context.signal.addEventListener('abort', abort, { once: true });
|
|
try {
|
|
context.signal.throwIfAborted();
|
|
await render(context, handle);
|
|
context.signal.throwIfAborted();
|
|
handle.card();
|
|
return handle;
|
|
} catch (error) {
|
|
handle.dispose();
|
|
throw error;
|
|
} finally {
|
|
context.signal.removeEventListener('abort', abort);
|
|
}
|
|
};
|
|
}
|
|
return {
|
|
geojson: guarded(renderPackGeometry),
|
|
image: guarded(async ({ asset, pack, signal }, handle) => {
|
|
if (asset.mimeType !== 'image/png') throw new Error('Expected PNG image');
|
|
const bytes = asset.bytes;
|
|
if (
|
|
bytes.length < 24 ||
|
|
![137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82].every(
|
|
(v, i) => bytes[i] === v,
|
|
)
|
|
)
|
|
throw new Error('Invalid PNG header');
|
|
const header = new DataView(
|
|
bytes.buffer,
|
|
bytes.byteOffset,
|
|
bytes.byteLength,
|
|
);
|
|
const width = header.getUint32(16),
|
|
height = header.getUint32(20);
|
|
if (!width || !height || width > 4096 || height > 4096)
|
|
throw new Error('Image dimensions exceed limit');
|
|
const image = new Image();
|
|
image.src = handle.url(new Blob([asset.bytes], { type: asset.mimeType }));
|
|
const abort = () => {
|
|
image.src = '';
|
|
};
|
|
signal.addEventListener('abort', abort, { once: true });
|
|
try {
|
|
await image.decode();
|
|
} finally {
|
|
signal.removeEventListener('abort', abort);
|
|
}
|
|
signal.throwIfAborted();
|
|
if (image.naturalWidth * image.naturalHeight > 16 * 1024 * 1024)
|
|
throw new Error('Image dimensions exceed limit');
|
|
handle.primitive(
|
|
new Cesium.Primitive({
|
|
asynchronous: false,
|
|
geometryInstances: new Cesium.GeometryInstance({
|
|
id: { packId: pack.id },
|
|
geometry: new Cesium.RectangleGeometry({
|
|
rectangle: Cesium.Rectangle.fromDegrees(...pack.placement.bounds),
|
|
height: pack.placement.height,
|
|
vertexFormat:
|
|
Cesium.MaterialAppearance.MaterialSupport.TEXTURED.vertexFormat,
|
|
}),
|
|
}),
|
|
appearance: new Cesium.MaterialAppearance({
|
|
material: Cesium.Material.fromType('Image', { image }),
|
|
materialSupport: Cesium.MaterialAppearance.MaterialSupport.TEXTURED,
|
|
flat: true,
|
|
faceForward: true,
|
|
translucent: true,
|
|
}),
|
|
}),
|
|
);
|
|
}),
|
|
media: guarded(({ asset, pack, anchors }, handle) => {
|
|
if (
|
|
![
|
|
'video/mp4',
|
|
'video/webm',
|
|
'audio/mpeg',
|
|
'audio/ogg',
|
|
'audio/wav',
|
|
'audio/webm',
|
|
].includes(asset.mimeType)
|
|
)
|
|
throw new Error('Unsupported media');
|
|
const anchor = anchors.find(
|
|
(item) => item.id === pack.placement.anchorId,
|
|
);
|
|
if (!anchor) throw new Error('Missing media anchor');
|
|
const position = xyz([anchor.lon, anchor.lat, anchor.alt]);
|
|
handle.add({
|
|
name: pack.id,
|
|
position,
|
|
point: {
|
|
pixelSize: 16,
|
|
color: Cesium.Color.fromCssColorString(MEDIA_MARKER_CSS),
|
|
},
|
|
});
|
|
handle.label({
|
|
id: 'media',
|
|
position,
|
|
variant: 'label',
|
|
title: pack.id,
|
|
accent: MEDIA_MARKER_CSS,
|
|
protected: true,
|
|
interactive: false,
|
|
gapPx: 15,
|
|
verticalOnly: true,
|
|
placement: 'above',
|
|
});
|
|
handle.media({
|
|
kind: asset.mimeType.startsWith('video/') ? 'video' : 'audio',
|
|
src: handle.url(new Blob([asset.bytes], { type: asset.mimeType })),
|
|
});
|
|
}),
|
|
};
|
|
}
|