mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
Draw scene data packs through the draw port and take their geometry and presentation off the Cesium list
Pack points, lines, polygons, image overlays and media markers are draw sets now, one per pack resource and kind. Feature picks match the click's draw hit by set and item id, and item ids are namespaced so code that reads a bare pick id never takes a pack feature for a live contact. An image pack draws its decoded pixels from a canvas: the Cesium draw port now fills unclamped shapes with images, as the contract's fills allow, and frees their materials with the set. The ./scenes export joins the entries whose caller loads the unbound renderer factories.
This commit is contained in:
1 parent
dbf1647572
commit
71ff682c7f
9 files changed
+526
-175
No files matched your search
@@ -1269,23 +1269,64 @@ export function createCesiumDraw({
|
||||
const host = new Cesium.PrimitiveCollection();
|
||||
const outlines = new Cesium.PolylineCollection();
|
||||
const clamped = [];
|
||||
// Image fills' materials hold their textures, which primitives leave
|
||||
// alone: the host destroys them as it goes.
|
||||
const materials = [];
|
||||
const ownMaterial = (material) => {
|
||||
if (!materials.length) {
|
||||
const destroyHost = host.destroy.bind(host);
|
||||
host.destroy = () => {
|
||||
for (const owned of materials.splice(0))
|
||||
if (!owned.isDestroyed()) owned.destroy();
|
||||
return destroyHost();
|
||||
};
|
||||
}
|
||||
materials.push(material);
|
||||
return material;
|
||||
};
|
||||
for (const item of record.items.values()) {
|
||||
if (item.show === false) continue;
|
||||
if (item.height === 'clampGround') {
|
||||
clamped.push(item);
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
item.fill &&
|
||||
typeof item.fill === 'object' &&
|
||||
!Array.isArray(item.fill)
|
||||
)
|
||||
const imageFill =
|
||||
item.fill && typeof item.fill === 'object' && !Array.isArray(item.fill)
|
||||
? item.fill
|
||||
: null;
|
||||
if (imageFill && !imageFill.image)
|
||||
throw new Error('the Cesium renderer fills shapes with colours only');
|
||||
const shape = shapeGeometry(item);
|
||||
const id = pickId(record, item);
|
||||
const solid =
|
||||
item.extrudeHeight !== undefined || item.geometry.kind === 'box';
|
||||
if (item.fill && !item.outlineOnly) {
|
||||
if (imageFill && !item.outlineOnly) {
|
||||
const material = ownMaterial(
|
||||
Cesium.Material.fromType('Image', {
|
||||
image: toImage(imageFill.image),
|
||||
repeat: new Cesium.Cartesian2(...(imageFill.repeat ?? [1, 1])),
|
||||
}),
|
||||
);
|
||||
host.add(
|
||||
new Cesium.Primitive({
|
||||
geometryInstances: new Cesium.GeometryInstance({
|
||||
geometry: shape.fill,
|
||||
modelMatrix: shape.modelMatrix,
|
||||
id,
|
||||
}),
|
||||
appearance: new Cesium.MaterialAppearance({
|
||||
material,
|
||||
materialSupport:
|
||||
Cesium.MaterialAppearance.MaterialSupport.TEXTURED,
|
||||
translucent: true,
|
||||
closed: solid,
|
||||
flat: !solid,
|
||||
faceForward: !solid,
|
||||
}),
|
||||
asynchronous: false,
|
||||
}),
|
||||
);
|
||||
} else if (item.fill && !item.outlineOnly) {
|
||||
const color = toColor(item.fill);
|
||||
host.add(
|
||||
new Cesium.Primitive({
|
||||
|
||||
@@ -2033,3 +2033,183 @@ test('an aircraft layer draws its contacts as one GPU icon set moving on the del
|
||||
unbind();
|
||||
}
|
||||
});
|
||||
test('an unclamped shape filled with an image draws a textured primitive and frees its material with its host', () =>
|
||||
withImageClasses(() => {
|
||||
const scene = fakeScene();
|
||||
const draw = createCesiumDraw({
|
||||
scene,
|
||||
report: (error) => assert.fail(error),
|
||||
}).port;
|
||||
const set = draw.shapes({ id: 'images' });
|
||||
set.set([
|
||||
{
|
||||
id: 'image',
|
||||
geometry: {
|
||||
kind: 'rectangle',
|
||||
rect: { west: 2, south: 1, east: 2.1, north: 1.1 },
|
||||
height: 200,
|
||||
},
|
||||
fill: { image: 'picture.png', repeat: [2, 3] },
|
||||
},
|
||||
]);
|
||||
const host = scene.added[0];
|
||||
const primitive = host.get(0);
|
||||
assert.ok(primitive instanceof Cesium.Primitive);
|
||||
const { appearance, geometryInstances } = primitive;
|
||||
assert.ok(appearance instanceof Cesium.MaterialAppearance);
|
||||
assert.equal(appearance.material.type, 'Image');
|
||||
assert.equal(appearance.material.uniforms.image, 'picture.png');
|
||||
assert.deepEqual(
|
||||
appearance.material.uniforms.repeat,
|
||||
new Cesium.Cartesian2(2, 3),
|
||||
);
|
||||
assert.deepEqual(
|
||||
[appearance.flat, appearance.faceForward, appearance.translucent],
|
||||
[true, true, true],
|
||||
);
|
||||
assert.ok(geometryInstances.geometry instanceof Cesium.RectangleGeometry);
|
||||
assert.equal(geometryInstances.id.id, 'image');
|
||||
assert.equal(
|
||||
drawHitOf({ primitive, id: geometryInstances.id }).set,
|
||||
'images',
|
||||
);
|
||||
const material = appearance.material;
|
||||
// As Cesium's collection does, a removed host is destroyed; a
|
||||
// rebuild's old host goes with its material.
|
||||
scene.primitives.remove = (primitive) => {
|
||||
const at = scene.added.indexOf(primitive);
|
||||
if (at >= 0) scene.added.splice(at, 1);
|
||||
primitive.destroy();
|
||||
return at >= 0;
|
||||
};
|
||||
set.upsert([
|
||||
{
|
||||
id: 'image',
|
||||
geometry: {
|
||||
kind: 'rectangle',
|
||||
rect: { west: 2, south: 1, east: 2.2, north: 1.1 },
|
||||
},
|
||||
fill: { image: 'picture.png' },
|
||||
},
|
||||
]);
|
||||
assert.equal(material.isDestroyed(), true);
|
||||
const next = scene.added[0].get(0).appearance.material;
|
||||
assert.equal(next.isDestroyed(), false);
|
||||
set.dispose();
|
||||
assert.equal(scene.added.length, 0);
|
||||
assert.equal(next.isDestroyed(), true);
|
||||
assert.throws(
|
||||
() =>
|
||||
draw.shapes({ id: 'patterns' }).set([
|
||||
{
|
||||
id: 'p',
|
||||
geometry: {
|
||||
kind: 'rectangle',
|
||||
rect: { west: 0, south: 0, east: 1, north: 1 },
|
||||
},
|
||||
fill: {
|
||||
preset: 'radialRim',
|
||||
color: '#fff',
|
||||
rimColor: '#000',
|
||||
rimWidth: 1,
|
||||
},
|
||||
},
|
||||
]),
|
||||
/colours only/,
|
||||
);
|
||||
}));
|
||||
|
||||
test('scene data packs draw their geometry and media marker through the Cesium draw port on a viewer of its own', async (t) => {
|
||||
const names = [
|
||||
'HTMLCanvasElement',
|
||||
'HTMLImageElement',
|
||||
'ImageBitmap',
|
||||
'OffscreenCanvas',
|
||||
];
|
||||
const real = names.map((name) => globalThis[name]);
|
||||
for (const name of names) globalThis[name] ??= class {};
|
||||
t.after(() => names.forEach((name, i) => (globalThis[name] = real[i])));
|
||||
const { createPackPresentations } =
|
||||
await import('../../scenes/dataPacks/presentation.js');
|
||||
const scene = fakeScene();
|
||||
const viewer = { scene, container: {} };
|
||||
const targets = new Map();
|
||||
const presentations = createPackPresentations(viewer, targets, {
|
||||
store: { publish() {} },
|
||||
overlayHost: { setEntries() {}, clearSource() {} },
|
||||
});
|
||||
const bytes = new TextEncoder().encode(
|
||||
JSON.stringify({
|
||||
type: 'FeatureCollection',
|
||||
features: [
|
||||
{
|
||||
type: 'Feature',
|
||||
id: 'point',
|
||||
geometry: { type: 'Point', coordinates: [2, 1, 10] },
|
||||
},
|
||||
{
|
||||
type: 'Feature',
|
||||
id: 'line',
|
||||
geometry: {
|
||||
type: 'LineString',
|
||||
coordinates: [
|
||||
[2, 1, 5],
|
||||
[2.1, 1.1, 5],
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
type: 'Feature',
|
||||
id: 'area',
|
||||
geometry: {
|
||||
type: 'Polygon',
|
||||
coordinates: [
|
||||
[
|
||||
[2, 1, 50],
|
||||
[2.1, 1, 50],
|
||||
[2.1, 1.1, 50],
|
||||
[2, 1, 50],
|
||||
],
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
}),
|
||||
);
|
||||
const pack = { id: 'data', attribution: { text: 'Example', license: 'CC0' } };
|
||||
const geometry = await presentations.geojson({
|
||||
pack,
|
||||
asset: { bytes, mimeType: 'application/geo+json' },
|
||||
signal: new AbortController().signal,
|
||||
});
|
||||
const [points, lines, shapes] = scene.added;
|
||||
assert.ok(points instanceof Cesium.PointPrimitiveCollection);
|
||||
assert.equal(points.get(0).pixelSize, 12);
|
||||
assert.equal(points.get(0).outlineWidth, 2);
|
||||
assert.ok(Cesium.Color.CYAN.equals(points.get(0).color));
|
||||
assert.ok(lines instanceof Cesium.PolylineCollection);
|
||||
assert.equal(lines.get(0).width, 4);
|
||||
const fill = shapes.get(0);
|
||||
assert.ok(fill.geometryInstances.geometry instanceof Cesium.PolygonGeometry);
|
||||
assert.equal(fill.appearance.translucent, true);
|
||||
assert.equal(fill.appearance.flat, true);
|
||||
// A click names the feature's set and item, which the targets hold.
|
||||
const hit = drawHitOf({ primitive: lines.get(0), id: lines.get(0).id });
|
||||
assert.deepEqual(targets.get(JSON.stringify(['data', 'line'])), {
|
||||
set: hit.set,
|
||||
id: hit.id,
|
||||
});
|
||||
const media = await presentations.media({
|
||||
pack: { ...pack, id: 'sound', placement: { anchorId: 'here' } },
|
||||
anchors: [{ id: 'here', lon: 2, lat: 1, alt: 5 }],
|
||||
asset: { bytes: new Uint8Array(4), mimeType: 'audio/mpeg' },
|
||||
signal: new AbortController().signal,
|
||||
});
|
||||
const marker = scene.added[3].get(0);
|
||||
assert.equal(marker.pixelSize, 16);
|
||||
assert.ok(Cesium.Color.fromCssColorString('#ffff00').equals(marker.color));
|
||||
geometry.dispose();
|
||||
media.dispose();
|
||||
assert.equal(scene.added.length, 0);
|
||||
assert.equal(targets.size, 0);
|
||||
});
|
||||
@@ -79,8 +79,6 @@
|
||||
"src/maps/imagery.js",
|
||||
"src/maps/terrain.js",
|
||||
"src/sceneCommandRelease.js",
|
||||
"src/scenes/dataPacks/geometry.js",
|
||||
"src/scenes/dataPacks/presentation.js",
|
||||
"src/services/groundSnap.js",
|
||||
"src/services/meshFloorSampler.js",
|
||||
"src/sky/starrySky.js",
|
||||
|
||||
@@ -84,6 +84,9 @@ const DRAW_FROM_CALLER = new Set([
|
||||
'./layers/street-level/kartaview',
|
||||
'./layers/street-level/panoramax',
|
||||
'./layers/air-quality',
|
||||
// Its data packs draw through `drawOf`; its flights ask `cameraOf`, which
|
||||
// throws until then too.
|
||||
'./scenes',
|
||||
// Asks `findQuery` instead, and answers null rather than throw until the
|
||||
// caller has loaded the factory.
|
||||
'./queries/heights',
|
||||
@@ -96,10 +99,6 @@ const DRAW_FROM_CALLER = new Set([
|
||||
* `./render/cesium/unbound`.
|
||||
*/
|
||||
const CAMERA_FROM_CALLER = new Set([
|
||||
// Not Cesium-free yet: its data packs still draw through Cesium in their
|
||||
// own listed modules. Its flights ask `cameraOf`, which throws until the
|
||||
// caller has loaded the export.
|
||||
'./scenes',
|
||||
'./ui/navigation',
|
||||
'./voice/commands',
|
||||
'./voice/controller',
|
||||
|
||||
@@ -1,77 +1,72 @@
|
||||
import * as Cesium from 'cesium';
|
||||
import { decodePackGeoJSON } from '../../director/packs/geojson.js';
|
||||
const xyz = (p) => Cesium.Cartesian3.fromDegrees(...p);
|
||||
import { fromDegrees } from '../../geo/ellipsoid.js';
|
||||
|
||||
/** Render a bounded geometry pack in owned batches; no asynchronous entity geometry or update loop. */
|
||||
const CYAN = [0, 1, 1, 1];
|
||||
const BLACK = [0, 0, 0, 1];
|
||||
const lonLatHeight = ([lon, lat, height]) => ({ lon, lat, height });
|
||||
|
||||
/**
|
||||
* A feature's item id. Code that reads a pick's bare item id (the pick
|
||||
* registry, vessel and flight lookups) must never take a pack feature for a
|
||||
* live contact, so the id is namespaced, never the feature's own.
|
||||
*/
|
||||
export const packItemId = (packId, featureId) =>
|
||||
`data-pack:${JSON.stringify([packId, featureId])}`;
|
||||
|
||||
/**
|
||||
* Render a bounded geometry pack as one draw set per kind (points, lines,
|
||||
* polygons); no asynchronous geometry or update loop. Each feature's pick
|
||||
* target is its set and item id.
|
||||
*/
|
||||
export function renderPackGeometry({ asset, pack }, handle) {
|
||||
if (!['application/json', 'application/geo+json'].includes(asset.mimeType))
|
||||
throw new Error('Expected GeoJSON');
|
||||
const features = decodePackGeoJSON(asset.bytes);
|
||||
const polygons = [];
|
||||
let lines;
|
||||
const items = { points: [], lines: [], shapes: [] };
|
||||
const kinds = new Map();
|
||||
for (const feature of features) {
|
||||
const id = { packId: pack.id, featureId: feature.id };
|
||||
const id = packItemId(pack.id, feature.id);
|
||||
if (feature.type === 'Point') {
|
||||
const entity = handle.add({
|
||||
name: `${pack.id} / ${feature.id}`,
|
||||
position: xyz(feature.coordinates),
|
||||
point: {
|
||||
pixelSize: 12,
|
||||
color: Cesium.Color.CYAN,
|
||||
outlineColor: Cesium.Color.BLACK,
|
||||
outlineWidth: 2,
|
||||
},
|
||||
});
|
||||
handle.feature?.(id, entity);
|
||||
} else if (feature.type === 'LineString') {
|
||||
handle.feature?.(id, id);
|
||||
if (!lines) {
|
||||
lines = new Cesium.PolylineCollection();
|
||||
handle.primitive(lines);
|
||||
}
|
||||
lines.add({
|
||||
kinds.set(feature.id, 'points');
|
||||
items.points.push({
|
||||
id,
|
||||
positions: feature.coordinates.map(xyz),
|
||||
width: 4,
|
||||
material: Cesium.Material.fromType('Color', {
|
||||
color: Cesium.Color.CYAN,
|
||||
}),
|
||||
pos: lonLatHeight(feature.coordinates),
|
||||
pixelSize: 12,
|
||||
color: CYAN,
|
||||
outlineColor: BLACK,
|
||||
outlineWidth: 2,
|
||||
});
|
||||
} else if (feature.type === 'LineString') {
|
||||
kinds.set(feature.id, 'lines');
|
||||
items.lines.push({
|
||||
id,
|
||||
positions: feature.coordinates.map((p) => fromDegrees(...p)),
|
||||
widthPx: 4,
|
||||
mode: '3d',
|
||||
style: { kind: 'solid', color: CYAN },
|
||||
});
|
||||
} else {
|
||||
handle.feature?.(id, id);
|
||||
polygons.push(
|
||||
new Cesium.GeometryInstance({
|
||||
id,
|
||||
geometry: new Cesium.PolygonGeometry({
|
||||
polygonHierarchy: new Cesium.PolygonHierarchy(
|
||||
feature.coordinates[0].map(xyz),
|
||||
feature.coordinates
|
||||
.slice(1)
|
||||
.map((ring) => new Cesium.PolygonHierarchy(ring.map(xyz))),
|
||||
),
|
||||
perPositionHeight: true,
|
||||
vertexFormat: Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
|
||||
arcType: Cesium.ArcType.GEODESIC,
|
||||
}),
|
||||
attributes: {
|
||||
color: Cesium.ColorGeometryInstanceAttribute.fromColor(
|
||||
Cesium.Color.CYAN.withAlpha(0.35),
|
||||
),
|
||||
},
|
||||
}),
|
||||
);
|
||||
kinds.set(feature.id, 'shapes');
|
||||
items.shapes.push({
|
||||
id,
|
||||
geometry: {
|
||||
kind: 'polygon',
|
||||
rings: feature.coordinates.map((ring) => ring.map(lonLatHeight)),
|
||||
perPositionHeight: true,
|
||||
},
|
||||
fill: [0, 1, 1, 0.35],
|
||||
});
|
||||
}
|
||||
}
|
||||
if (polygons.length)
|
||||
handle.primitive(
|
||||
new Cesium.Primitive({
|
||||
asynchronous: false,
|
||||
geometryInstances: polygons,
|
||||
appearance: new Cesium.PerInstanceColorAppearance({
|
||||
translucent: true,
|
||||
flat: true,
|
||||
closed: false,
|
||||
}),
|
||||
}),
|
||||
const sets = {};
|
||||
for (const kind of ['points', 'lines', 'shapes'])
|
||||
if (items[kind].length) sets[kind] = handle.draw(kind, items[kind]);
|
||||
for (const feature of features)
|
||||
handle.feature?.(
|
||||
{ packId: pack.id, featureId: feature.id },
|
||||
{
|
||||
set: sets[kinds.get(feature.id)],
|
||||
id: packItemId(pack.id, feature.id),
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -1,10 +1,9 @@
|
||||
import * as Cesium from 'cesium';
|
||||
import { renderPackGeometry } from './geometry.js';
|
||||
import { packItemId, renderPackGeometry } from './geometry.js';
|
||||
import { fromDegrees } from '../../geo/ellipsoid.js';
|
||||
import { drawOf, findFrame } from '../../render/rendererOf.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
|
||||
@@ -12,6 +11,9 @@ const MEDIA_MARKER_CSS = '#ffff00';
|
||||
let panelKeys = 0,
|
||||
cardKeys = 0;
|
||||
|
||||
// Draw sets, one per pack resource and kind, unique among the renderer's.
|
||||
let drawSets = 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
|
||||
@@ -21,8 +23,10 @@ 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.
|
||||
* independent. Packs draw through the viewer's draw port (`drawOf`); a
|
||||
* feature's pick target is its draw set and item id. 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,
|
||||
@@ -30,6 +34,7 @@ export function createPackPresentations(
|
||||
{ store = useDataPacksStore(applicationPinia()), overlayHost = null } = {},
|
||||
) {
|
||||
const owner = {};
|
||||
const requestRender = () => findFrame(viewer)?.requestRender('data-pack');
|
||||
let key,
|
||||
cards = [];
|
||||
function present() {
|
||||
@@ -62,9 +67,7 @@ export function createPackPresentations(
|
||||
present();
|
||||
}
|
||||
function resource(pack) {
|
||||
const entities = [],
|
||||
primitives = [],
|
||||
materials = [],
|
||||
const sets = [],
|
||||
urls = [],
|
||||
labels = [];
|
||||
const featureKeys = [];
|
||||
@@ -77,11 +80,17 @@ export function createPackPresentations(
|
||||
targets.set(key, pickedId);
|
||||
featureKeys.push(key);
|
||||
},
|
||||
add(spec) {
|
||||
const entity = viewer.entities.add(spec);
|
||||
entities.push(entity);
|
||||
viewer.scene.requestRender();
|
||||
return entity;
|
||||
/**
|
||||
* Draw `items` as a new set of `kind` (`points`, `lines`, `shapes`)
|
||||
* owned by this resource.
|
||||
* @returns {string} The set's id.
|
||||
*/
|
||||
draw(kind, items) {
|
||||
const set = drawOf(viewer)[kind]({ id: `data-pack:${++drawSets}` });
|
||||
sets.push(set);
|
||||
set.set(items);
|
||||
requestRender();
|
||||
return set.id;
|
||||
},
|
||||
/**
|
||||
* World text for this pack, as a label-overlay entry in the pack's own
|
||||
@@ -96,13 +105,7 @@ export function createPackPresentations(
|
||||
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();
|
||||
requestRender();
|
||||
},
|
||||
card() {
|
||||
card ||= addCard(pack);
|
||||
@@ -123,20 +126,15 @@ export function createPackPresentations(
|
||||
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);
|
||||
for (const set of sets.splice(0)) set.dispose();
|
||||
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();
|
||||
requestRender();
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -194,27 +192,24 @@ export function createPackPresentations(
|
||||
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,
|
||||
}),
|
||||
}),
|
||||
);
|
||||
// The draw port takes images as URLs, SVG or canvases; a canvas
|
||||
// keeps the decoded pixels, so nothing loads again.
|
||||
const canvas = document.createElement('canvas');
|
||||
canvas.width = image.naturalWidth;
|
||||
canvas.height = image.naturalHeight;
|
||||
canvas.getContext('2d').drawImage(image, 0, 0);
|
||||
const [west, south, east, north] = pack.placement.bounds;
|
||||
handle.draw('shapes', [
|
||||
{
|
||||
id: packItemId(pack.id, 'image'),
|
||||
geometry: {
|
||||
kind: 'rectangle',
|
||||
rect: { west, south, east, north },
|
||||
height: pack.placement.height,
|
||||
},
|
||||
fill: { image: { canvas, key: image.src } },
|
||||
},
|
||||
]);
|
||||
}),
|
||||
media: guarded(({ asset, pack, anchors }, handle) => {
|
||||
if (
|
||||
@@ -232,15 +227,15 @@ export function createPackPresentations(
|
||||
(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: {
|
||||
const position = fromDegrees(anchor.lon, anchor.lat, anchor.alt);
|
||||
handle.draw('points', [
|
||||
{
|
||||
id: packItemId(pack.id, 'media'),
|
||||
pos: position,
|
||||
pixelSize: 16,
|
||||
color: Cesium.Color.fromCssColorString(MEDIA_MARKER_CSS),
|
||||
color: MEDIA_MARKER_CSS,
|
||||
},
|
||||
});
|
||||
]);
|
||||
handle.label({
|
||||
id: 'media',
|
||||
position,
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createPackPresentations } from './presentation.js';
|
||||
import { packItemId } from './geometry.js';
|
||||
import { bindFakeDraw } from '../../testSupport/fakeDrawPort.mjs';
|
||||
|
||||
test('abort releases an in-flight image URL before the decoder settles or the viewer is destroyed', async () => {
|
||||
let settle;
|
||||
@@ -19,14 +21,8 @@ test('abort releases an in-flight image URL before the decoder settles or the vi
|
||||
revoked.push(url);
|
||||
priorRevoke(url);
|
||||
};
|
||||
const viewer = {
|
||||
scene: { requestRender() {} },
|
||||
entities: {
|
||||
remove() {
|
||||
assert.fail('No entity should be acquired');
|
||||
},
|
||||
},
|
||||
};
|
||||
const viewer = {};
|
||||
const { draw, unbind } = bindFakeDraw(viewer);
|
||||
const signalOwner = new AbortController();
|
||||
const bytes = new Uint8Array(24);
|
||||
bytes.set([137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82]);
|
||||
@@ -49,7 +45,9 @@ test('abort releases an in-flight image URL before the decoder settles or the vi
|
||||
settle();
|
||||
await rejected;
|
||||
assert.equal(revoked.length, 1, 'late decode cannot dispose twice');
|
||||
assert.equal(draw.sets.size, 0, 'nothing is drawn');
|
||||
} finally {
|
||||
unbind();
|
||||
settle();
|
||||
globalThis.Image = priorImage;
|
||||
URL.revokeObjectURL = priorRevoke;
|
||||
@@ -59,15 +57,8 @@ test('abort releases an in-flight image URL before the decoder settles or the vi
|
||||
test('a media pack names its marker through the label overlay, cleared on dispose', async () => {
|
||||
const published = [];
|
||||
const cleared = [];
|
||||
const added = [];
|
||||
const viewer = {
|
||||
container: {},
|
||||
scene: { requestRender() {} },
|
||||
entities: {
|
||||
add: (spec) => (added.push(spec), spec),
|
||||
remove() {},
|
||||
},
|
||||
};
|
||||
const viewer = { container: {} };
|
||||
const { draw, unbind } = bindFakeDraw(viewer);
|
||||
const overlayHost = {
|
||||
setEntries: (source, entries) => published.push([source, entries]),
|
||||
clearSource: (source) => cleared.push(source),
|
||||
@@ -84,27 +75,30 @@ test('a media pack names its marker through the label overlay, cleared on dispos
|
||||
asset: { bytes: new Uint8Array(4), mimeType: 'audio/mpeg' },
|
||||
signal: new AbortController().signal,
|
||||
});
|
||||
assert.equal(added.length, 1);
|
||||
assert.equal(added[0].label, undefined, 'no Cesium label on the marker');
|
||||
const marker = draw.find('data-pack:');
|
||||
assert.equal(marker.kind, 'points');
|
||||
const point = marker.items.get(packItemId('harbour-audio', 'media'));
|
||||
assert.deepEqual(
|
||||
{ pixelSize: point.pixelSize, color: point.color },
|
||||
{ pixelSize: 16, color: '#ffff00' },
|
||||
);
|
||||
const [source, entries] = published.at(-1);
|
||||
assert.match(source, /^data-pack-labels:\d+$/);
|
||||
assert.deepEqual(
|
||||
entries.map(({ title, variant }) => ({ title, variant })),
|
||||
[{ title: 'harbour-audio', variant: 'label' }],
|
||||
);
|
||||
assert.equal(entries[0].position, added[0].position);
|
||||
assert.equal(entries[0].position, point.pos);
|
||||
assert.equal(entries[0].protected, true, 'it always draws, as before');
|
||||
handle.dispose();
|
||||
assert.deepEqual(cleared, [source]);
|
||||
assert.equal(marker.disposed, true);
|
||||
unbind();
|
||||
});
|
||||
|
||||
test('without an overlay host a media pack fails to load instead of losing its name', async () => {
|
||||
const removed = [];
|
||||
const viewer = {
|
||||
container: {},
|
||||
scene: { requestRender() {} },
|
||||
entities: { add: (spec) => spec, remove: (entity) => removed.push(entity) },
|
||||
};
|
||||
const viewer = { container: {} };
|
||||
const { draw, unbind } = bindFakeDraw(viewer);
|
||||
await assert.rejects(
|
||||
createPackPresentations(viewer).media({
|
||||
pack: {
|
||||
@@ -118,7 +112,10 @@ test('without an overlay host a media pack fails to load instead of losing its n
|
||||
}),
|
||||
/overlay host/,
|
||||
);
|
||||
assert.equal(removed.length, 1, 'the marker it placed is removed again');
|
||||
const sets = [...draw.sets.values()];
|
||||
assert.equal(sets.length, 1);
|
||||
assert.equal(sets[0].disposed, true, 'the marker it placed is removed again');
|
||||
unbind();
|
||||
});
|
||||
|
||||
test('two presentations of one media pack keep their labels apart, and reuse sources', async () => {
|
||||
@@ -130,11 +127,8 @@ test('two presentations of one media pack keep their labels apart, and reuse sou
|
||||
};
|
||||
const shown = () =>
|
||||
[...sources.values()].filter((entries) => entries.length).length;
|
||||
const viewer = {
|
||||
container: {},
|
||||
scene: { requestRender() {} },
|
||||
entities: { add: (spec) => spec, remove() {} },
|
||||
};
|
||||
const viewer = { container: {} };
|
||||
const { unbind } = bindFakeDraw(viewer);
|
||||
const show = () =>
|
||||
createPackPresentations(viewer, new Map(), { overlayHost }).media({
|
||||
pack: {
|
||||
@@ -161,4 +155,150 @@ test('two presentations of one media pack keep their labels apart, and reuse sou
|
||||
late.label({ id: 'late', position: { x: 1, y: 0, z: 0 }, title: 'late' });
|
||||
late.dispose();
|
||||
assert.equal(shown(), 0, 'a label after dispose is ignored');
|
||||
unbind();
|
||||
});
|
||||
|
||||
test('a geometry pack draws its points, lines and polygons as sets, and its features pick by set and item', async () => {
|
||||
const viewer = { container: {} };
|
||||
const { draw, unbind } = bindFakeDraw(viewer);
|
||||
const targets = new Map();
|
||||
const ring = [
|
||||
[2, 1, 50],
|
||||
[2.1, 1, 50],
|
||||
[2.1, 1.1, 50],
|
||||
[2, 1, 50],
|
||||
];
|
||||
const bytes = new TextEncoder().encode(
|
||||
JSON.stringify({
|
||||
type: 'FeatureCollection',
|
||||
features: [
|
||||
{
|
||||
type: 'Feature',
|
||||
id: 'point',
|
||||
geometry: { type: 'Point', coordinates: [2, 1, 10] },
|
||||
},
|
||||
{
|
||||
type: 'Feature',
|
||||
id: 'line',
|
||||
geometry: {
|
||||
type: 'LineString',
|
||||
coordinates: [
|
||||
[2, 1],
|
||||
[2.1, 1.1],
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
type: 'Feature',
|
||||
id: 'area',
|
||||
geometry: { type: 'Polygon', coordinates: [ring] },
|
||||
},
|
||||
],
|
||||
}),
|
||||
);
|
||||
const handle = await createPackPresentations(viewer, targets, {
|
||||
overlayHost: { setEntries() {}, clearSource() {} },
|
||||
}).geojson({
|
||||
pack: { id: 'data', attribution: { text: 'Example', license: 'CC0' } },
|
||||
asset: { bytes, mimeType: 'application/geo+json' },
|
||||
signal: new AbortController().signal,
|
||||
});
|
||||
const byKind = Object.fromEntries(
|
||||
[...draw.sets.values()].map((record) => [record.kind, record]),
|
||||
);
|
||||
assert.deepEqual(Object.keys(byKind).sort(), ['lines', 'points', 'shapes']);
|
||||
const point = byKind.points.items.get(packItemId('data', 'point'));
|
||||
assert.deepEqual(point.pos, { lon: 2, lat: 1, height: 10 });
|
||||
assert.deepEqual(
|
||||
[point.pixelSize, point.outlineWidth, point.color, point.outlineColor],
|
||||
[12, 2, [0, 1, 1, 1], [0, 0, 0, 1]],
|
||||
);
|
||||
const line = byKind.lines.items.get(packItemId('data', 'line'));
|
||||
assert.equal(line.mode, '3d');
|
||||
assert.equal(line.widthPx, 4);
|
||||
assert.deepEqual(line.style, { kind: 'solid', color: [0, 1, 1, 1] });
|
||||
assert.equal(line.positions.length, 2);
|
||||
const area = byKind.shapes.items.get(packItemId('data', 'area'));
|
||||
assert.equal(area.geometry.perPositionHeight, true);
|
||||
assert.deepEqual(area.geometry.rings[0][1], { lon: 2.1, lat: 1, height: 50 });
|
||||
assert.deepEqual(area.fill, [0, 1, 1, 0.35]);
|
||||
assert.deepEqual(targets.get(JSON.stringify(['data', 'line'])), {
|
||||
set: byKind.lines.spec.id,
|
||||
id: packItemId('data', 'line'),
|
||||
});
|
||||
assert.equal(
|
||||
packItemId('data', 'line'),
|
||||
'data-pack:["data","line"]',
|
||||
'never a bare feature id a contact lookup could claim',
|
||||
);
|
||||
assert.equal(targets.size, 3);
|
||||
handle.dispose();
|
||||
assert.equal(targets.size, 0);
|
||||
assert.ok([...draw.sets.values()].every((record) => record.disposed));
|
||||
unbind();
|
||||
});
|
||||
|
||||
test('an image pack draws its decoded pixels as a rectangle at its height', async () => {
|
||||
const priorImage = globalThis.Image;
|
||||
const priorDocument = globalThis.document;
|
||||
const drawn = [];
|
||||
globalThis.Image = class {
|
||||
naturalWidth = 8;
|
||||
naturalHeight = 4;
|
||||
decode() {
|
||||
return Promise.resolve();
|
||||
}
|
||||
};
|
||||
globalThis.document = {
|
||||
createElement: (tag) => {
|
||||
assert.equal(tag, 'canvas');
|
||||
const canvas = {
|
||||
getContext: () => ({
|
||||
drawImage: (image, x, y) =>
|
||||
drawn.push([image, x, y, canvas.width, canvas.height]),
|
||||
}),
|
||||
};
|
||||
return canvas;
|
||||
},
|
||||
};
|
||||
const viewer = { container: {} };
|
||||
const { draw, unbind } = bindFakeDraw(viewer);
|
||||
const bytes = new Uint8Array(24);
|
||||
bytes.set([137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82]);
|
||||
const header = new DataView(bytes.buffer);
|
||||
header.setUint32(16, 8);
|
||||
header.setUint32(20, 4);
|
||||
try {
|
||||
const handle = await createPackPresentations(viewer, new Map(), {
|
||||
store: { publish() {} },
|
||||
}).image({
|
||||
pack: {
|
||||
id: 'overlay',
|
||||
attribution: { text: 'Example', license: 'CC0' },
|
||||
placement: { bounds: [-98, 30, -97, 31], height: 200 },
|
||||
},
|
||||
asset: { bytes, mimeType: 'image/png' },
|
||||
signal: new AbortController().signal,
|
||||
});
|
||||
const record = draw.find('data-pack:');
|
||||
assert.equal(record.kind, 'shapes');
|
||||
const item = record.items.get(packItemId('overlay', 'image'));
|
||||
assert.deepEqual(item.geometry, {
|
||||
kind: 'rectangle',
|
||||
rect: { west: -98, south: 30, east: -97, north: 31 },
|
||||
height: 200,
|
||||
});
|
||||
const { canvas, key } = item.fill.image;
|
||||
assert.deepEqual([canvas.width, canvas.height], [8, 4]);
|
||||
assert.equal(drawn.length, 1);
|
||||
assert.deepEqual(drawn[0].slice(1), [0, 0, 8, 4]);
|
||||
assert.match(key, /^blob:/);
|
||||
handle.dispose();
|
||||
assert.equal(record.disposed, true);
|
||||
} finally {
|
||||
unbind();
|
||||
globalThis.Image = priorImage;
|
||||
if (priorDocument === undefined) delete globalThis.document;
|
||||
else globalThis.document = priorDocument;
|
||||
}
|
||||
});
|
||||
@@ -9,6 +9,7 @@ import { createPinia } from 'pinia';
|
||||
import { useDataPacksStore } from '../../ui/stores/dataPacks.js';
|
||||
import DataPacks from '../../ui/views/panels/DataPacks.vue';
|
||||
import { createPackPresentations } from './presentation.js';
|
||||
import { bindFakeDraw } from '../../testSupport/fakeDrawPort.mjs';
|
||||
|
||||
const overlayHost = { setEntries() {}, clearSource() {} };
|
||||
|
||||
@@ -25,17 +26,17 @@ function fixture(t) {
|
||||
const pinia = createPinia();
|
||||
const app = createApp(DataPacks).use(pinia);
|
||||
app.mount(document.body.appendChild(document.createElement('div')));
|
||||
const viewer = {
|
||||
container,
|
||||
scene: { requestRender() {} },
|
||||
entities: { add: (spec) => spec, remove() {} },
|
||||
};
|
||||
const viewer = { container };
|
||||
const { unbind } = bindFakeDraw(viewer);
|
||||
const store = useDataPacksStore(pinia);
|
||||
const presentations = createPackPresentations(viewer, new Map(), {
|
||||
store,
|
||||
overlayHost,
|
||||
});
|
||||
t.after(() => app.unmount());
|
||||
t.after(() => {
|
||||
app.unmount();
|
||||
unbind();
|
||||
});
|
||||
const settle = async () => {
|
||||
for (let flush = 0; flush < 4; flush += 1) await nextTick();
|
||||
};
|
||||
|
||||
@@ -119,13 +119,15 @@ export function createSceneInteractions(
|
||||
session.activate(items);
|
||||
unsubscribeClick = inputFor(viewer).onClick((event) => {
|
||||
if (panel?.owner !== owner || !shown() || !available()) return;
|
||||
const picked = event.pick?.id;
|
||||
const matches = items.filter(
|
||||
(item) =>
|
||||
targets.get(
|
||||
JSON.stringify([item.target.packId, item.target.featureId]),
|
||||
) === picked,
|
||||
);
|
||||
// A pack feature is its draw set's item (see dataPacks/presentation.js).
|
||||
const hit = event.drawHit;
|
||||
if (!hit) return;
|
||||
const matches = items.filter((item) => {
|
||||
const target = targets.get(
|
||||
JSON.stringify([item.target.packId, item.target.featureId]),
|
||||
);
|
||||
return target?.set === hit.set && target.id === hit.id;
|
||||
});
|
||||
if (!matches.length) return;
|
||||
update({
|
||||
outlined: matches.map((item) => item.id),
|
||||
|
||||
Reference in new issue
Block a user