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',
|
||||
|
||||
Reference in new issue
Block a user