mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-20 10:12:18 +00:00
Draw nine layers' fills through engine.ground, one display each
Earthquakes, air quality, GNSS Integrity, fire perimeters, Prohibited & Restricted Airspace, Active Fires, S2 GhostMaps, cyclone advisories and submarine cables each have one render/display.js on the globe contract; their render/cesium* and render/navara* files go. Their discs, cells, polygons and corridors are engine.ground sets, so on Navara they come from the one ground screen pass with exact colours instead of nine hand-written classification passes and lit SDK vector layers. Cesium's look stays the reference: - Earthquake and air-quality discs have no outline (Cesium drew none on clamped ellipses); earthquake discs can now be picked on Navara too. - Perimeters, drone zones and GhostMaps polygons outline their outer ring only, as Cesium did. - GNSS cells stay out of the pick pass; cyclone cones, tracks and centres are culled by a port of Cesium's horizon occluder. - Submarine cables are one ground corridor set, as Cesium batched them; landing points, tips and stems are movers and lines. - FIRMS glows are a sprite set and its cells a ground set. The far-zoom heat raster, which no primitive draws yet, sits behind one seam, render/heatField.js, with a Cesium and a Navara backend. The shared Navara ground helpers that only these layers used go (navaraGroundCells, the cell and ground geometry, the zone renderers), as does the ground prototype script. Coupling: layer renderer lines fall from 1636/6953 to 1011/4832 (Cesium/Navara), Navara ground passes from 7 to 5, Cesium-importing files from 65 to 53, engine.extensions reads from 26 to 19.
This commit is contained in:
1 parent
974a0b6361
commit
54fe0aea40
124 files changed
+8407
-9576
No files matched your search
@@ -1,147 +0,0 @@
|
||||
import * as Cesium from 'cesium';
|
||||
import {
|
||||
worldToWindowCoordinates,
|
||||
horizonOccluder,
|
||||
} from '../../../data/render/cesiumProjection.js';
|
||||
import { createHeatImagery } from '../heatImagery.js';
|
||||
|
||||
// Cesium ScreenSpaceEventType.LEFT_CLICK; available before the SDK loads.
|
||||
export const FIRMS_LEFT_CLICK = 2;
|
||||
export const projectFirmsWindow = worldToWindowCoordinates;
|
||||
export const firmsHorizonOccluder = horizonOccluder;
|
||||
|
||||
/** Own the native ground rectangles, translucent sprites and their scene. */
|
||||
export function createFirmsCesiumRenderer(
|
||||
engine,
|
||||
{ viewer, id, services, screenSpaceEventHandlerFactory } = {},
|
||||
) {
|
||||
if (!viewer?.dataSources)
|
||||
throw new TypeError('FIRMS requires a native viewer');
|
||||
const dataSource = new Cesium.CustomDataSource(id);
|
||||
dataSource.show = false;
|
||||
viewer.dataSources.add(dataSource);
|
||||
let sprites = null,
|
||||
visible = false,
|
||||
destroyed = false;
|
||||
const fadeColor = new Cesium.Color();
|
||||
const spriteServices = services.sprites;
|
||||
function removeSprites() {
|
||||
if (!sprites) return;
|
||||
spriteServices.unregisterSpriteCollection?.('firms', sprites);
|
||||
if (!viewer.scene?.isDestroyed?.()) {
|
||||
try {
|
||||
viewer.scene?.primitives?.remove(sprites);
|
||||
} catch {
|
||||
/* scene already removed it */
|
||||
}
|
||||
}
|
||||
sprites = null;
|
||||
}
|
||||
return {
|
||||
kind: 'cesium',
|
||||
view: viewer,
|
||||
dataSource,
|
||||
clickType: FIRMS_LEFT_CLICK,
|
||||
project(position, result) {
|
||||
return projectFirmsWindow(viewer.scene, position, result);
|
||||
},
|
||||
viewRectangle(result) {
|
||||
return viewer.camera.computeViewRectangle(
|
||||
viewer.scene?.globe?.ellipsoid,
|
||||
result,
|
||||
);
|
||||
},
|
||||
horizon() {
|
||||
const camera = viewer.camera?.positionWC
|
||||
? viewer.camera
|
||||
: viewer.scene?.camera;
|
||||
return camera?.positionWC ? horizonOccluder(camera) : null;
|
||||
},
|
||||
createInputHandler() {
|
||||
return screenSpaceEventHandlerFactory
|
||||
? screenSpaceEventHandlerFactory(viewer)
|
||||
: new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
|
||||
},
|
||||
pick(position) {
|
||||
return viewer.scene.pick(position);
|
||||
},
|
||||
setVisible(show) {
|
||||
if (destroyed) return;
|
||||
visible = show !== false;
|
||||
dataSource.show = visible;
|
||||
if (sprites) sprites.show = visible;
|
||||
},
|
||||
clearCells() {
|
||||
if (!destroyed) dataSource.entities.removeAll();
|
||||
},
|
||||
addCell({ west, south, east, north, color, position, properties }) {
|
||||
if (destroyed) return null;
|
||||
return dataSource.entities.add({
|
||||
rectangle: {
|
||||
coordinates: Cesium.Rectangle.fromDegrees(west, south, east, north),
|
||||
material: new Cesium.ColorMaterialProperty(
|
||||
new Cesium.Color(color.red, color.green, color.blue, color.alpha),
|
||||
),
|
||||
heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
|
||||
},
|
||||
position,
|
||||
properties,
|
||||
});
|
||||
},
|
||||
ensureSprites() {
|
||||
if (destroyed) return null;
|
||||
if (!sprites) {
|
||||
sprites = new Cesium.BillboardCollection({
|
||||
scene: viewer.scene,
|
||||
blendOption: Cesium.BlendOption.TRANSLUCENT,
|
||||
});
|
||||
sprites.show = visible;
|
||||
viewer.scene.primitives.add(sprites);
|
||||
spriteServices.registerSpriteCollection('firms', sprites);
|
||||
spriteServices.restoreSpriteOrder(viewer);
|
||||
}
|
||||
return sprites;
|
||||
},
|
||||
addSprite(description) {
|
||||
if (destroyed) return null;
|
||||
return sprites.add({
|
||||
...description,
|
||||
verticalOrigin: Cesium.VerticalOrigin.CENTER,
|
||||
horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
|
||||
disableDepthTestDistance: Number.POSITIVE_INFINITY,
|
||||
});
|
||||
},
|
||||
removeSprites,
|
||||
/**
|
||||
* The far-zoom heatmap draped on this globe: on the active map stack's
|
||||
* imagery host (`imageryHost()`), else on the viewer's imagery layers.
|
||||
*/
|
||||
createHeatImagery(imageryHost) {
|
||||
return createHeatImagery({
|
||||
cesium: Cesium,
|
||||
getHost: () =>
|
||||
imageryHost?.() ?? {
|
||||
collection: viewer.imageryLayers ?? null,
|
||||
kind: 'globe',
|
||||
},
|
||||
});
|
||||
},
|
||||
/** The hand-off fade: every marker at `alpha`, shown while `show`. */
|
||||
fadeSprites(alpha, show) {
|
||||
if (destroyed || !sprites) return;
|
||||
sprites.show = show;
|
||||
const color = Cesium.Color.WHITE.withAlpha(alpha, fadeColor);
|
||||
for (let i = 0; i < sprites.length; i += 1) sprites.get(i).color = color;
|
||||
},
|
||||
onEnable() {
|
||||
if (!destroyed)
|
||||
spriteServices.restoreSpriteOrderOnEnable('firms', viewer);
|
||||
},
|
||||
destroy() {
|
||||
if (destroyed) return;
|
||||
destroyed = true;
|
||||
removeSprites();
|
||||
viewer.dataSources.remove(dataSource, true);
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -1,137 +0,0 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import * as Cesium from 'cesium';
|
||||
import * as geo from '../../../geo/index.js';
|
||||
import { constantRgba } from '../../shared/color.js';
|
||||
import { createFirmsCesiumRenderer } from './cesium.js';
|
||||
|
||||
function harness() {
|
||||
const sources = [],
|
||||
primitives = new Cesium.PrimitiveCollection(),
|
||||
registrations = [],
|
||||
unregistered = [],
|
||||
restored = [];
|
||||
const viewer = {
|
||||
scene: {
|
||||
primitives,
|
||||
frameState: { mode: Cesium.SceneMode.SCENE3D },
|
||||
isDestroyed: () => false,
|
||||
},
|
||||
dataSources: {
|
||||
add(source) {
|
||||
sources.push(source);
|
||||
},
|
||||
remove(source, destroy) {
|
||||
const i = sources.indexOf(source);
|
||||
assert.ok(i >= 0);
|
||||
sources.splice(i, 1);
|
||||
assert.equal(destroy, true);
|
||||
},
|
||||
},
|
||||
};
|
||||
const services = {
|
||||
sprites: {
|
||||
registerSpriteCollection: (...args) => registrations.push(args),
|
||||
unregisterSpriteCollection: (...args) => unregistered.push(args),
|
||||
restoreSpriteOrder: (value) => restored.push(value),
|
||||
restoreSpriteOrderOnEnable: (id, value) => {
|
||||
assert.equal(id, 'firms');
|
||||
restored.push(value);
|
||||
},
|
||||
},
|
||||
};
|
||||
const renderer = createFirmsCesiumRenderer(null, {
|
||||
viewer,
|
||||
id: 'firms-fixture',
|
||||
services,
|
||||
});
|
||||
return {
|
||||
viewer,
|
||||
renderer,
|
||||
sources,
|
||||
primitives,
|
||||
registrations,
|
||||
unregistered,
|
||||
restored,
|
||||
};
|
||||
}
|
||||
|
||||
test('native FIRMS renderer retains exact ground rectangle, color, metadata and translucent sprite policy', () => {
|
||||
const h = harness(),
|
||||
r = h.renderer,
|
||||
epoch = Cesium.JulianDate.fromDate(new Date(0));
|
||||
assert.equal(r.dataSource.show, false);
|
||||
r.setVisible(true);
|
||||
const entity = r.addCell({
|
||||
west: 179,
|
||||
south: 40,
|
||||
east: 180,
|
||||
north: 42,
|
||||
color: constantRgba(1, 165 / 255, 0, 0.66),
|
||||
position: geo.cartesian3.fromDegrees(179.5, 41),
|
||||
properties: { count: 7, intensity: 12.5, maxFrp: 999, night: 3 },
|
||||
});
|
||||
const rectangle = entity.rectangle.coordinates.getValue(epoch),
|
||||
color = entity.rectangle.material.color.getValue(epoch);
|
||||
assert.deepEqual(
|
||||
[rectangle.west, rectangle.south, rectangle.east, rectangle.north],
|
||||
[179, 40, 180, 42].map(Cesium.Math.toRadians),
|
||||
);
|
||||
assert.deepEqual(
|
||||
[color.red, color.green, color.blue, color.alpha],
|
||||
[1, 165 / 255, 0, 0.66],
|
||||
);
|
||||
assert.equal(
|
||||
entity.rectangle.heightReference.getValue(epoch),
|
||||
Cesium.HeightReference.CLAMP_TO_GROUND,
|
||||
);
|
||||
for (const [key, value] of Object.entries({
|
||||
count: 7,
|
||||
intensity: 12.5,
|
||||
maxFrp: 999,
|
||||
night: 3,
|
||||
}))
|
||||
assert.equal(entity.properties[key].getValue(epoch), value);
|
||||
const sprites = r.ensureSprites();
|
||||
assert.equal(sprites, r.ensureSprites());
|
||||
const sprite = r.addSprite({
|
||||
id: 'firms-7',
|
||||
position: geo.cartesian3.fromDegrees(179.5, 41, 105),
|
||||
image: 'fixture.png',
|
||||
});
|
||||
assert.equal(sprite.id, 'firms-7');
|
||||
assert.equal(sprite.disableDepthTestDistance, Infinity);
|
||||
assert.equal(sprite.verticalOrigin, Cesium.VerticalOrigin.CENTER);
|
||||
assert.equal(sprite.horizontalOrigin, Cesium.HorizontalOrigin.CENTER);
|
||||
assert.equal(sprites.blendOption, Cesium.BlendOption.TRANSLUCENT);
|
||||
assert.equal(h.registrations.length, 1);
|
||||
r.setVisible(false);
|
||||
assert.equal(r.dataSource.show, false);
|
||||
assert.equal(sprites.show, false);
|
||||
r.destroy();
|
||||
});
|
||||
|
||||
test('native FIRMS destroys and unregisters only its original scene resources, once', () => {
|
||||
const h = harness(),
|
||||
r = h.renderer,
|
||||
sprites = r.ensureSprites();
|
||||
const foreign = {
|
||||
dataSources: {
|
||||
remove() {
|
||||
assert.fail('foreign scene must not own FIRMS teardown');
|
||||
},
|
||||
},
|
||||
};
|
||||
r.onEnable();
|
||||
assert.ok(h.restored.every((viewer) => viewer === h.viewer));
|
||||
r.destroy(foreign);
|
||||
r.destroy();
|
||||
assert.equal(h.sources.length, 0);
|
||||
assert.equal(h.primitives.length, 0);
|
||||
assert.equal(sprites.isDestroyed(), true);
|
||||
assert.deepEqual(h.unregistered, [['firms', sprites]]);
|
||||
assert.equal(r.ensureSprites(), null);
|
||||
r.onEnable();
|
||||
assert.equal(h.primitives.length, 0);
|
||||
assert.equal(h.registrations.length, 1);
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
// The heatmap's Cesium backend (render/heatField.js): one full-globe imagery
|
||||
// tile on the active map stack's imagery host, else on the viewer's imagery
|
||||
// layers (heatImagery.js).
|
||||
import * as Cesium from 'cesium';
|
||||
import { createHeatImagery } from '../heatImagery.js';
|
||||
|
||||
/**
|
||||
* @param {{ viewer: object }} cesium The engine's Cesium extension.
|
||||
* @param {() => ({ collection: object, kind: string } | null)} [imageryHost]
|
||||
*/
|
||||
export function createFirmsCesiumHeat({ viewer }, imageryHost) {
|
||||
return createHeatImagery({
|
||||
cesium: Cesium,
|
||||
getHost: () =>
|
||||
imageryHost?.() ?? {
|
||||
collection: viewer.imageryLayers ?? null,
|
||||
kind: 'globe',
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
// The Active Fires drawing on the globe contract, the same on every engine:
|
||||
// - the detection glows: one sprite mover set over an atlas of the layer's
|
||||
// glow PNGs (render/sprites.js);
|
||||
// - the aggregated cells: one ground cell set, filled in each cell's colour,
|
||||
// without an outline, not pickable;
|
||||
// - clicks: the engine's selection dispatcher, with the pick under the click;
|
||||
// - the camera, projection, view rectangle and horizon the layer reads: the
|
||||
// engine's (engine.camera, engine.projector).
|
||||
// The far-zoom heatmap is a draped raster, which the contract has no
|
||||
// primitive for; it sits behind render/heatField.js.
|
||||
|
||||
import { createEngineLayerView } from '../../shared/render/engineView.js';
|
||||
import { selectionDispatcherFor } from '../../../data/selectionDispatcher.js';
|
||||
import { LOD_LEVELS } from '../policy.js';
|
||||
import { createFirmsSpriteBank } from './sprites.js';
|
||||
import { createFirmsHeatField } from './heatField.js';
|
||||
|
||||
const rgbaOf = (color) => [
|
||||
color?.red ?? 1,
|
||||
color?.green ?? 1,
|
||||
color?.blue ?? 1,
|
||||
color?.alpha ?? 1,
|
||||
];
|
||||
|
||||
/**
|
||||
* The renderer the fire layer draws through (lifecycle.js, rendering.js).
|
||||
* @param {import('../../../globe/contract.js').GlobeEngine} engine
|
||||
* @param {{ id?: string, navaraHeat?: Function, loadImage?: Function,
|
||||
* createCanvas?: Function }} [options] `navaraHeat` is the heatmap's Navara
|
||||
* backend (render/navaraHeat.js), which the Navara mode passes in;
|
||||
* `loadImage` and `createCanvas` are the sprite atlas's test seams.
|
||||
*/
|
||||
export function createFirmsDisplay(
|
||||
engine,
|
||||
{ id = 'local-firms', navaraHeat, loadImage, createCanvas } = {},
|
||||
) {
|
||||
if (!engine?.movers || !engine?.ground)
|
||||
throw new TypeError('The FIRMS display needs a globe engine');
|
||||
let destroyed = false,
|
||||
visible = false,
|
||||
cellItems = [],
|
||||
cellsQueued = false,
|
||||
sprites = null;
|
||||
// The layer's visibility as the context store reads a data source's.
|
||||
const dataSource = { show: false };
|
||||
const clickRemovers = new Set();
|
||||
const cells = engine.ground.createSet({ id: `${id}:cells`, kind: 'cell' });
|
||||
cells.show(false);
|
||||
const layerView = createEngineLayerView(engine, {
|
||||
layerId: id,
|
||||
isDestroyed: () => destroyed,
|
||||
});
|
||||
// The camera and scene events the layer's watchers listen to.
|
||||
layerView.view.camera.moveEnd = engine.camera.moveEnd;
|
||||
layerView.view.scene.preRender = engine.frames.preRender;
|
||||
// Cells arrive one addCell at a time: one commit per batch.
|
||||
const scheduleCells = () => {
|
||||
if (cellsQueued || destroyed) return;
|
||||
cellsQueued = true;
|
||||
queueMicrotask(() => {
|
||||
cellsQueued = false;
|
||||
if (destroyed) return;
|
||||
cells.replace(cellItems);
|
||||
cells.commit();
|
||||
});
|
||||
};
|
||||
const removeSprites = () => {
|
||||
sprites?.destroy();
|
||||
sprites = null;
|
||||
};
|
||||
return {
|
||||
view: layerView.view,
|
||||
dataSource,
|
||||
project: (position, result) =>
|
||||
engine.projector.worldToWindow(position, result),
|
||||
viewRectangle(result) {
|
||||
const rect = engine.camera.viewRectangle();
|
||||
return rect ? Object.assign(result ?? {}, rect) : null;
|
||||
},
|
||||
horizon: () => ({
|
||||
isPointVisible: (position) => engine.projector.horizonVisible(position),
|
||||
}),
|
||||
/**
|
||||
* Calls `handler({ position, picked })` for every click while the layer
|
||||
* is shown, with the pick under it; returns the remover.
|
||||
*/
|
||||
onClick(handler) {
|
||||
if (destroyed) return () => {};
|
||||
const remove = selectionDispatcherFor(engine).register(id, {
|
||||
owns: (pickId) => sprites?.owns(pickId) === true,
|
||||
onClick: (event) => {
|
||||
if (!destroyed && visible) handler(event);
|
||||
},
|
||||
});
|
||||
const release = () => {
|
||||
if (!clickRemovers.delete(release)) return;
|
||||
remove();
|
||||
};
|
||||
clickRemovers.add(release);
|
||||
return release;
|
||||
},
|
||||
setVisible(show) {
|
||||
if (destroyed) return;
|
||||
const next = show !== false;
|
||||
if (visible === next) return;
|
||||
visible = next;
|
||||
dataSource.show = next;
|
||||
sprites?.setVisible(next);
|
||||
cells.show(next);
|
||||
},
|
||||
clearCells() {
|
||||
if (destroyed) return;
|
||||
cellItems = [];
|
||||
scheduleCells();
|
||||
},
|
||||
addCell({ west, south, east, north, color }) {
|
||||
if (destroyed) return null;
|
||||
const item = { west, south, east, north, fill: rgbaOf(color) };
|
||||
cellItems.push(item);
|
||||
scheduleCells();
|
||||
return item;
|
||||
},
|
||||
/** Ground cells drawn by the last commit. */
|
||||
cellCount: () => (destroyed ? 0 : cells.count()),
|
||||
ensureSprites() {
|
||||
if (destroyed) return null;
|
||||
if (!sprites) {
|
||||
sprites = createFirmsSpriteBank(engine, {
|
||||
id: `firms:${id}`,
|
||||
capacity: Math.max(
|
||||
...LOD_LEVELS.map((lod) => lod.maxDetections ?? 0),
|
||||
),
|
||||
...(loadImage ? { loadImage } : {}),
|
||||
...(createCanvas ? { createCanvas } : {}),
|
||||
});
|
||||
sprites.setVisible(visible);
|
||||
}
|
||||
return sprites.collection;
|
||||
},
|
||||
addSprite(description) {
|
||||
return destroyed || !sprites ? null : sprites.add(description);
|
||||
},
|
||||
removeSprites,
|
||||
/** The mover set drawing the glows, for tests and QA (null before any). */
|
||||
spriteSet: () => sprites?.set ?? null,
|
||||
/**
|
||||
* The far-zoom heatmap draped on this globe (render/heatField.js): on
|
||||
* Cesium on the active map stack's imagery host (`imageryHost()`), else
|
||||
* on the viewer's imagery layers.
|
||||
*/
|
||||
createHeatImagery(imageryHost) {
|
||||
return destroyed
|
||||
? null
|
||||
: createFirmsHeatField(engine, { imageryHost, navaraHeat });
|
||||
},
|
||||
/** The hand-off fade: every marker at `alpha`, shown while `show`. */
|
||||
fadeSprites(alpha, show) {
|
||||
if (!destroyed) sprites?.setFade(alpha, show);
|
||||
},
|
||||
destroy() {
|
||||
if (destroyed) return;
|
||||
destroyed = true;
|
||||
visible = false;
|
||||
dataSource.show = false;
|
||||
cellItems = [];
|
||||
for (const release of [...clickRemovers]) release();
|
||||
removeSprites();
|
||||
cells.destroy();
|
||||
layerView.destroy();
|
||||
engine.render.request('firms-destroy');
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,618 @@
|
||||
// The FIRMS display on the fake engine and on the Cesium engine over a stub
|
||||
// scene: the same calls, the same drawing, read back from each engine's own
|
||||
// store (the fake's mover and ground sets, Cesium's billboards and ground
|
||||
// primitives).
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import * as Cesium from 'cesium';
|
||||
import * as geo from '../../../geo/index.js';
|
||||
import { createFakeEngine } from '../../../globe/testing/fakeEngine.js';
|
||||
import { createFakeEventHandlers } from '../../../globe/cesium/testing/fakeViewer.mjs';
|
||||
import { createCesiumMoversEngine } from '../../../testSupport/cesiumMoversEngine.mjs';
|
||||
import { SPRITE_LAYER_ORDER } from '../../../data/spriteOrder.js';
|
||||
import { constantRgba } from '../../shared/color.js';
|
||||
import { createFirmsDisplay } from './display.js';
|
||||
import { FIRMS_SPRITE_ORDER } from './sprites.js';
|
||||
|
||||
const settle = async () => {
|
||||
for (let i = 0; i < 4; i++) await new Promise((r) => setImmediate(r));
|
||||
};
|
||||
const FIRE = { lon: -98.2, lat: 30.5 };
|
||||
const at = (lon, lat, height = 0) =>
|
||||
geo.cartesian3.fromDegrees(lon, lat, height);
|
||||
// Glow PNGs as their size: `glow-<side>`.
|
||||
const loadImage = async (url) => {
|
||||
const side = Number(url.split('-')[1]);
|
||||
return { width: side, height: side };
|
||||
};
|
||||
const createCanvas = () => ({
|
||||
width: 0,
|
||||
height: 0,
|
||||
getContext: () => ({ drawImage() {} }),
|
||||
});
|
||||
|
||||
// What each ground set was given, by id.
|
||||
function watchGround(engine) {
|
||||
const items = new Map();
|
||||
const createSet = engine.ground.createSet.bind(engine.ground);
|
||||
engine.ground.createSet = (options) => {
|
||||
const set = createSet(options);
|
||||
const { replace } = set;
|
||||
set.replace = (next) => {
|
||||
replace(next);
|
||||
items.set(options.id, next);
|
||||
};
|
||||
return set;
|
||||
};
|
||||
return {
|
||||
items: (id) => items.get(id) ?? [],
|
||||
listed: (id) => engine.ground.list().find((entry) => entry.id === id),
|
||||
};
|
||||
}
|
||||
|
||||
function fakeHarness() {
|
||||
const engine = createFakeEngine();
|
||||
engine.fake.setPose({
|
||||
longitude: geo.math.toRadians(FIRE.lon),
|
||||
latitude: geo.math.toRadians(FIRE.lat),
|
||||
height: 2_000_000,
|
||||
});
|
||||
const ground = watchGround(engine);
|
||||
return {
|
||||
name: 'the fake engine',
|
||||
engine,
|
||||
ground,
|
||||
/** The sprite set as drawn: its options and shown instances. */
|
||||
sprites(id) {
|
||||
const set = engine.fake.moverSet(id);
|
||||
if (!set) return null;
|
||||
const { store, options } = set;
|
||||
return {
|
||||
options,
|
||||
count: store.count,
|
||||
instances: Array.from({ length: store.count }, (_, i) => ({
|
||||
show: store.shows[i] === 1,
|
||||
scale: store.scales[i],
|
||||
alpha: store.colors[i * 4 + 3],
|
||||
pickId: store.pickIds[i],
|
||||
})),
|
||||
};
|
||||
},
|
||||
click(picked) {
|
||||
engine.fake.script('pick', picked);
|
||||
engine.fake.script('drillPick', picked ? [picked] : []);
|
||||
engine.fake.input('click', { position: { x: 10, y: 20 } });
|
||||
engine.fake.unscript('pick');
|
||||
engine.fake.unscript('drillPick');
|
||||
},
|
||||
pickOf: (setId, instance, pickId) => ({
|
||||
id: pickId,
|
||||
owner: setId,
|
||||
instance,
|
||||
}),
|
||||
destroy: () => engine.destroy(),
|
||||
};
|
||||
}
|
||||
|
||||
function cesiumHarness() {
|
||||
const handlers = createFakeEventHandlers();
|
||||
const { engine, viewer, primitive } = createCesiumMoversEngine({
|
||||
engineOptions: {
|
||||
createEventHandler: handlers.create,
|
||||
scheduleFrame: (run) => run(),
|
||||
},
|
||||
});
|
||||
viewer.scene.groundPrimitives = new Cesium.PrimitiveCollection();
|
||||
engine.camera.setView({ destination: at(FIRE.lon, FIRE.lat, 2_000_000) });
|
||||
let native;
|
||||
viewer.scene.pick = () => native;
|
||||
viewer.scene.drillPick = () => (native ? [native] : []);
|
||||
const ground = watchGround(engine);
|
||||
return {
|
||||
name: 'the Cesium engine',
|
||||
engine,
|
||||
viewer,
|
||||
ground,
|
||||
sprites(id) {
|
||||
const collection = primitive(id);
|
||||
if (!collection) return null;
|
||||
const listed = engine.movers.list().find((entry) => entry.id === id);
|
||||
const count = listed?.count ?? 0;
|
||||
return {
|
||||
collection,
|
||||
count,
|
||||
instances: Array.from({ length: count }, (_, i) => {
|
||||
const billboard = collection.get(i);
|
||||
return {
|
||||
show: billboard.show,
|
||||
scale: billboard.scale,
|
||||
alpha: billboard.color.alpha,
|
||||
pickId: billboard.id,
|
||||
billboard,
|
||||
};
|
||||
}),
|
||||
};
|
||||
},
|
||||
/** The Cesium ground primitives of a set. */
|
||||
groundPrimitives(id) {
|
||||
const root = viewer.scene.groundPrimitives.get(0);
|
||||
for (let i = 0; i < (root?.length ?? 0); i++) {
|
||||
const slot = root.get(i);
|
||||
const values = Array.from({ length: slot.length }, (_, j) =>
|
||||
slot.get(j),
|
||||
);
|
||||
if (values.some((value) => value.gevGroundSet === id))
|
||||
return { slot, values };
|
||||
}
|
||||
return null;
|
||||
},
|
||||
click(picked) {
|
||||
native = picked;
|
||||
for (const handler of handlers.handlers)
|
||||
handler.fire(Cesium.ScreenSpaceEventType.LEFT_CLICK, {
|
||||
position: new Cesium.Cartesian2(10, 20),
|
||||
});
|
||||
native = undefined;
|
||||
},
|
||||
// A Cesium pick of the instance's billboard.
|
||||
pickOf(setId, instance) {
|
||||
const billboard = primitive(setId).get(instance);
|
||||
return { primitive: billboard, id: billboard.id };
|
||||
},
|
||||
destroy: () => engine.destroy(),
|
||||
};
|
||||
}
|
||||
|
||||
const HARNESSES = [fakeHarness, cesiumHarness];
|
||||
const display = (h, options = {}) =>
|
||||
createFirmsDisplay(h.engine, {
|
||||
id: 'firms-fixture',
|
||||
loadImage,
|
||||
createCanvas,
|
||||
...options,
|
||||
});
|
||||
|
||||
for (const make of HARNESSES) {
|
||||
const name = make().name;
|
||||
|
||||
test(`${name}: cells are one ground cell set in each cell's colour, committed once per batch`, async () => {
|
||||
const h = make();
|
||||
try {
|
||||
const r = display(h);
|
||||
const id = 'firms-fixture:cells';
|
||||
assert.equal(h.ground.listed(id).shown, false, 'hidden until shown');
|
||||
r.setVisible(true);
|
||||
assert.equal(h.ground.listed(id).shown, true);
|
||||
r.clearCells();
|
||||
r.addCell({
|
||||
west: 179,
|
||||
south: 40,
|
||||
east: 180,
|
||||
north: 42,
|
||||
color: constantRgba(1, 165 / 255, 0, 0.66),
|
||||
position: at(179.5, 41),
|
||||
properties: { count: 7 },
|
||||
});
|
||||
r.addCell({
|
||||
west: 0,
|
||||
south: 0,
|
||||
east: 1,
|
||||
north: 1,
|
||||
color: constantRgba(1, 0, 0, 0.3),
|
||||
});
|
||||
assert.equal(r.cellCount(), 0, 'drawn on the next microtask');
|
||||
await settle();
|
||||
assert.equal(r.cellCount(), 2);
|
||||
assert.deepEqual(h.ground.items(id)[0], {
|
||||
west: 179,
|
||||
south: 40,
|
||||
east: 180,
|
||||
north: 42,
|
||||
fill: [1, 165 / 255, 0, 0.66],
|
||||
});
|
||||
if (h.groundPrimitives) {
|
||||
const { values } = h.groundPrimitives(id);
|
||||
assert.equal(values.length, 1, 'fills only: no outline');
|
||||
const [fills] = values;
|
||||
assert.equal(fills.classificationType, Cesium.ClassificationType.BOTH);
|
||||
assert.equal(fills.gevPickable, false, 'cells stay out of picks');
|
||||
const rectangle = fills.geometryInstances[0].geometry._rectangle;
|
||||
assert.ok(
|
||||
Math.abs(rectangle.west - Cesium.Math.toRadians(179)) < 1e-12,
|
||||
);
|
||||
assert.ok(
|
||||
Math.abs(rectangle.north - Cesium.Math.toRadians(42)) < 1e-12,
|
||||
);
|
||||
assert.deepEqual(
|
||||
Array.from(fills.geometryInstances[0].attributes.color.value),
|
||||
[255, 165, 0, Math.round(0.66 * 255)],
|
||||
);
|
||||
}
|
||||
r.setVisible(false);
|
||||
assert.equal(h.ground.listed(id).shown, false);
|
||||
assert.equal(r.dataSource.show, false);
|
||||
r.clearCells();
|
||||
await settle();
|
||||
assert.equal(r.cellCount(), 0);
|
||||
r.destroy();
|
||||
assert.equal(h.ground.listed(id), undefined);
|
||||
} finally {
|
||||
h.destroy();
|
||||
}
|
||||
});
|
||||
|
||||
test(`${name}: glows are one sprite set at their own size, over the scene, in the firms sprite slot`, async () => {
|
||||
const h = make();
|
||||
try {
|
||||
const r = display(h);
|
||||
r.setVisible(true);
|
||||
const collection = r.ensureSprites();
|
||||
assert.equal(collection, r.ensureSprites());
|
||||
const small = r.addSprite({
|
||||
id: 'firms-0',
|
||||
position: at(FIRE.lon, FIRE.lat),
|
||||
image: 'glow-16',
|
||||
});
|
||||
r.addSprite({
|
||||
id: 'firms-1',
|
||||
position: at(FIRE.lon + 1, FIRE.lat),
|
||||
image: 'glow-56',
|
||||
});
|
||||
assert.equal(collection.length, 2);
|
||||
await settle();
|
||||
const drawn = h.sprites('firms:firms-fixture');
|
||||
assert.equal(drawn.count, 2);
|
||||
assert.ok(Math.abs(drawn.instances[0].scale - 16 / 56) < 1e-7);
|
||||
assert.equal(drawn.instances[1].scale, 1);
|
||||
assert.deepEqual(
|
||||
drawn.instances.map((i) => i.pickId),
|
||||
['firms-0', 'firms-1'],
|
||||
);
|
||||
assert.deepEqual(
|
||||
drawn.instances.map((i) => i.alpha),
|
||||
[1, 1],
|
||||
);
|
||||
assert.equal(FIRMS_SPRITE_ORDER, SPRITE_LAYER_ORDER.indexOf('firms'));
|
||||
if (drawn.options) {
|
||||
assert.equal(drawn.options.order, FIRMS_SPRITE_ORDER);
|
||||
assert.equal(drawn.options.depthTest, 'always');
|
||||
assert.equal(drawn.options.pickable, true);
|
||||
assert.equal(drawn.options.sharpen, true);
|
||||
assert.equal(drawn.options.sprite.sizePx, 56);
|
||||
} else {
|
||||
const { billboard } = drawn.instances[0];
|
||||
assert.equal(billboard.disableDepthTestDistance, Infinity);
|
||||
assert.equal(billboard.width, 56);
|
||||
assert.equal(billboard.verticalOrigin, Cesium.VerticalOrigin.CENTER);
|
||||
assert.equal(
|
||||
billboard.horizontalOrigin,
|
||||
Cesium.HorizontalOrigin.CENTER,
|
||||
);
|
||||
}
|
||||
// The horizon cull flips one marker: only that instance is rewritten.
|
||||
small.show = false;
|
||||
await settle();
|
||||
assert.deepEqual(
|
||||
h.sprites('firms:firms-fixture').instances.map((i) => i.show),
|
||||
[false, true],
|
||||
);
|
||||
r.setVisible(false);
|
||||
await settle();
|
||||
assert.equal(
|
||||
h.sprites('firms:firms-fixture').count,
|
||||
0,
|
||||
'hidden: none drawn',
|
||||
);
|
||||
r.setVisible(true);
|
||||
await settle();
|
||||
assert.deepEqual(
|
||||
h.sprites('firms:firms-fixture').instances.map((i) => i.show),
|
||||
[false, true],
|
||||
);
|
||||
collection.removeAll();
|
||||
await settle();
|
||||
assert.equal(h.sprites('firms:firms-fixture').count, 0);
|
||||
r.removeSprites();
|
||||
assert.equal(h.sprites('firms:firms-fixture'), null);
|
||||
assert.equal(r.addSprite({ id: 'late', image: 'glow-16' }), null);
|
||||
r.destroy();
|
||||
} finally {
|
||||
h.destroy();
|
||||
}
|
||||
});
|
||||
|
||||
test(`${name}: the hand-off fades every marker and hides them all once the heatmap is whole`, async () => {
|
||||
const h = make();
|
||||
try {
|
||||
const r = display(h);
|
||||
r.setVisible(true);
|
||||
r.ensureSprites();
|
||||
r.addSprite({
|
||||
id: 'firms-0',
|
||||
position: at(FIRE.lon, FIRE.lat),
|
||||
image: 'glow-56',
|
||||
});
|
||||
r.addSprite({
|
||||
id: 'firms-1',
|
||||
position: at(FIRE.lon, FIRE.lat + 1),
|
||||
image: 'glow-56',
|
||||
});
|
||||
await settle();
|
||||
r.fadeSprites(0.25, true);
|
||||
await settle();
|
||||
assert.deepEqual(
|
||||
h.sprites('firms:firms-fixture').instances.map((i) => i.alpha),
|
||||
[0.25, 0.25],
|
||||
);
|
||||
r.fadeSprites(0, false);
|
||||
await settle();
|
||||
assert.equal(
|
||||
h.sprites('firms:firms-fixture').count,
|
||||
0,
|
||||
'the heatmap alone: no marker',
|
||||
);
|
||||
r.fadeSprites(1, true);
|
||||
await settle();
|
||||
assert.deepEqual(
|
||||
h.sprites('firms:firms-fixture').instances.map((i) => i.alpha),
|
||||
[1, 1],
|
||||
);
|
||||
r.destroy();
|
||||
} finally {
|
||||
h.destroy();
|
||||
}
|
||||
});
|
||||
|
||||
test(`${name}: clicks reach the layer with the pick under them while it shows, and release on destroy`, async () => {
|
||||
const h = make();
|
||||
try {
|
||||
const r = display(h);
|
||||
r.ensureSprites();
|
||||
r.addSprite({
|
||||
id: 'firms-7',
|
||||
position: at(FIRE.lon, FIRE.lat),
|
||||
image: 'glow-56',
|
||||
});
|
||||
r.setVisible(true);
|
||||
await settle();
|
||||
const clicks = [];
|
||||
const remove = r.onClick((event) => clicks.push(event));
|
||||
h.click(h.pickOf('firms:firms-fixture', 0, 'firms-7'));
|
||||
assert.equal(clicks.length, 1);
|
||||
assert.equal(clicks[0].picked.id, 'firms-7');
|
||||
assert.deepEqual(
|
||||
{ x: clicks[0].position.x, y: clicks[0].position.y },
|
||||
{ x: 10, y: 20 },
|
||||
);
|
||||
h.click(undefined);
|
||||
assert.equal(clicks.length, 2, 'a click on nothing is the layer’s too');
|
||||
assert.equal(clicks[1].picked, undefined);
|
||||
r.setVisible(false);
|
||||
h.click(undefined);
|
||||
assert.equal(clicks.length, 2, 'a hidden layer takes no click');
|
||||
r.setVisible(true);
|
||||
remove();
|
||||
remove();
|
||||
h.click(undefined);
|
||||
assert.equal(clicks.length, 2, 'removed');
|
||||
r.onClick((event) => clicks.push(event));
|
||||
r.destroy();
|
||||
h.click(undefined);
|
||||
assert.equal(clicks.length, 2, 'destroy releases every subscription');
|
||||
} finally {
|
||||
h.destroy();
|
||||
}
|
||||
});
|
||||
|
||||
test(`${name}: the camera, projection, view rectangle and horizon are the engine's`, () => {
|
||||
const h = make();
|
||||
try {
|
||||
const r = display(h);
|
||||
const pose = h.engine.camera.pose();
|
||||
assert.ok(
|
||||
Math.abs(
|
||||
r.view.camera.positionCartographic.height - pose.cartographic.height,
|
||||
) < 1e-6,
|
||||
);
|
||||
assert.ok(
|
||||
geo.cartesian3.equalsEpsilon(
|
||||
r.view.camera.positionWC,
|
||||
pose.position,
|
||||
0,
|
||||
1e-6,
|
||||
),
|
||||
);
|
||||
assert.equal(r.view.camera.moveEnd, h.engine.camera.moveEnd);
|
||||
assert.equal(r.view.scene.preRender, h.engine.frames.preRender);
|
||||
const near = at(FIRE.lon, FIRE.lat);
|
||||
assert.deepEqual(
|
||||
r.project(near, {}),
|
||||
h.engine.projector.worldToWindow(near, {}),
|
||||
);
|
||||
assert.deepEqual(r.viewRectangle({}), {
|
||||
...h.engine.camera.viewRectangle(),
|
||||
});
|
||||
const far = at(FIRE.lon + 180, -FIRE.lat);
|
||||
assert.equal(r.horizon().isPointVisible(near), true);
|
||||
assert.equal(r.horizon().isPointVisible(far), false);
|
||||
r.destroy();
|
||||
} finally {
|
||||
h.destroy();
|
||||
}
|
||||
});
|
||||
|
||||
test(`${name}: an image that decodes after hide or destroy draws nothing`, async () => {
|
||||
const h = make();
|
||||
try {
|
||||
let finish;
|
||||
const r = display(h, {
|
||||
loadImage: () => new Promise((resolve) => (finish = resolve)),
|
||||
});
|
||||
r.ensureSprites();
|
||||
r.addSprite({
|
||||
id: 'firms-0',
|
||||
position: at(FIRE.lon, FIRE.lat),
|
||||
image: 'glow-16',
|
||||
});
|
||||
r.setVisible(true);
|
||||
await settle();
|
||||
r.setVisible(false);
|
||||
finish({ width: 16, height: 16 });
|
||||
await settle();
|
||||
assert.equal(h.sprites('firms:firms-fixture'), null);
|
||||
r.setVisible(true);
|
||||
await settle();
|
||||
assert.equal(
|
||||
h.sprites('firms:firms-fixture').count,
|
||||
1,
|
||||
'the decoded image draws once shown',
|
||||
);
|
||||
r.destroy();
|
||||
r.destroy();
|
||||
assert.equal(h.sprites('firms:firms-fixture'), null);
|
||||
assert.equal(h.engine.movers.list().length, 0);
|
||||
assert.equal(h.engine.ground.list().length, 0);
|
||||
assert.equal(r.ensureSprites(), null);
|
||||
assert.equal(r.createHeatImagery(), null);
|
||||
} finally {
|
||||
h.destroy();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
test('the fake engine: clearing rows during a decode commits only the replacement', async () => {
|
||||
const h = fakeHarness();
|
||||
try {
|
||||
const complete = new Map();
|
||||
const r = display(h, {
|
||||
loadImage: (url) => new Promise((resolve) => complete.set(url, resolve)),
|
||||
});
|
||||
r.setVisible(true);
|
||||
const sprites = r.ensureSprites();
|
||||
r.addSprite({ id: 'old', position: at(0, 0), image: 'glow-16' });
|
||||
await settle();
|
||||
sprites.removeAll();
|
||||
r.addSprite({ id: 'new', position: at(0, 1), image: 'glow-56' });
|
||||
await settle();
|
||||
complete.get('glow-16')({ width: 16, height: 16 });
|
||||
await settle();
|
||||
assert.equal(h.sprites('firms:firms-fixture'), null);
|
||||
complete.get('glow-56')({ width: 56, height: 56 });
|
||||
await settle();
|
||||
const drawn = h.engine.fake.moverSet('firms:firms-fixture');
|
||||
assert.equal(drawn.store.count, 1);
|
||||
assert.equal(drawn.store.pickIds[0], 'new');
|
||||
assert.equal(drawn.commits, 1);
|
||||
r.destroy();
|
||||
} finally {
|
||||
h.destroy();
|
||||
}
|
||||
});
|
||||
|
||||
test('the heatmap: the Cesium engine drapes one imagery layer on the viewer, the host when there is one', () => {
|
||||
const priorDocument = globalThis.document;
|
||||
globalThis.document = {
|
||||
createElement: () => ({
|
||||
getContext: () => ({
|
||||
createImageData: (w, hh) => ({
|
||||
data: new Uint8ClampedArray(w * hh * 4),
|
||||
}),
|
||||
putImageData() {},
|
||||
}),
|
||||
toDataURL: () => 'data:image/png;base64,AAAA',
|
||||
}),
|
||||
};
|
||||
const h = cesiumHarness();
|
||||
try {
|
||||
const layers = (added) => ({
|
||||
addImageryProvider(provider) {
|
||||
const layer = { provider, show: true, alpha: 1 };
|
||||
added.push(layer);
|
||||
return layer;
|
||||
},
|
||||
remove: (layer) => added.splice(added.indexOf(layer), 1),
|
||||
raiseToTop() {},
|
||||
});
|
||||
const onViewer = [];
|
||||
h.viewer.imageryLayers = layers(onViewer);
|
||||
const onHost = [];
|
||||
let host = null;
|
||||
const r = display(h);
|
||||
const heat = r.createHeatImagery(() => host);
|
||||
const raster = { width: 2, height: 1, rgba: new Uint8ClampedArray(8) };
|
||||
heat.setRaster(raster);
|
||||
assert.equal(heat.hasRaster(), true);
|
||||
assert.equal(onViewer.length, 1, 'no host: the viewer’s imagery layers');
|
||||
assert.ok(onViewer[0].provider instanceof Cesium.SingleTileImageryProvider);
|
||||
heat.setOpacity(0.5);
|
||||
heat.setShow(false);
|
||||
assert.deepEqual([onViewer[0].alpha, onViewer[0].show], [0.5, false]);
|
||||
host = { kind: 'tiles', collection: layers(onHost) };
|
||||
heat.rehome();
|
||||
assert.deepEqual(
|
||||
[onViewer.length, onHost.length],
|
||||
[0, 1],
|
||||
'it follows the host',
|
||||
);
|
||||
assert.deepEqual([onHost[0].alpha, onHost[0].show], [0.5, false]);
|
||||
heat.destroy();
|
||||
assert.equal(onHost.length, 0);
|
||||
r.destroy();
|
||||
} finally {
|
||||
h.destroy();
|
||||
if (priorDocument === undefined) delete globalThis.document;
|
||||
else globalThis.document = priorDocument;
|
||||
}
|
||||
});
|
||||
|
||||
test('the heatmap: Navara draws through the backend the mode passes in; an engine without one keeps the state', () => {
|
||||
const h = fakeHarness();
|
||||
try {
|
||||
const first = display(h);
|
||||
const plain = first.createHeatImagery();
|
||||
plain.setRaster({ width: 1, height: 1, rgba: new Uint8ClampedArray(4) });
|
||||
plain.setOpacity(2);
|
||||
plain.setShow(false);
|
||||
plain.rehome();
|
||||
assert.equal(plain.hasRaster(), true);
|
||||
assert.deepEqual(
|
||||
{ ...plain.state(), raster: Boolean(plain.state().raster) },
|
||||
{ raster: true, show: false, opacity: 1, drawn: false },
|
||||
);
|
||||
plain.destroy();
|
||||
first.destroy();
|
||||
|
||||
const native = { view: {} };
|
||||
h.engine.extensions = { navara: native };
|
||||
const calls = [];
|
||||
const navaraHeat = (engine, navara) => {
|
||||
calls.push(['create', engine === h.engine, navara === native]);
|
||||
return {
|
||||
setRaster: (raster) => calls.push(['raster', raster.width]),
|
||||
setShow: (show) => calls.push(['show', show]),
|
||||
setOpacity: (opacity) => calls.push(['opacity', opacity]),
|
||||
rehome: () => calls.push(['rehome']),
|
||||
destroy: () => calls.push(['destroy']),
|
||||
};
|
||||
};
|
||||
const second = display(h, { navaraHeat });
|
||||
const heat = second.createHeatImagery();
|
||||
heat.setRaster({ width: 3, height: 1, rgba: new Uint8ClampedArray(12) });
|
||||
heat.setOpacity(0.25);
|
||||
heat.setShow(true);
|
||||
heat.rehome();
|
||||
heat.destroy();
|
||||
heat.setShow(false);
|
||||
assert.deepEqual(calls, [
|
||||
['create', true, true],
|
||||
['raster', 3],
|
||||
['opacity', 0.25],
|
||||
['show', true],
|
||||
['rehome'],
|
||||
['destroy'],
|
||||
]);
|
||||
second.destroy();
|
||||
} finally {
|
||||
h.destroy();
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,68 @@
|
||||
// The Active Fires far-zoom heatmap behind one seam: one equirectangular
|
||||
// RGBA raster over the whole globe (data/firmsHeatRaster.js), draped on the
|
||||
// terrain and 3D tiles and blended between texels. The globe contract has no
|
||||
// primitive for an image draped on the ground yet (engine.ground draws flat
|
||||
// colours per item), so each engine draws it through its own backend:
|
||||
// render/cesiumHeat.js (one imagery layer on the active imagery host) and
|
||||
// render/navaraHeat.js (a surface pass in Navara's view), which the Navara
|
||||
// mode loads and passes in, as Navara modules reach the app only through its
|
||||
// import(). This is the only FIRMS module that reads engine.extensions.
|
||||
//
|
||||
// The field keeps what the layer asked for, so an engine with no backend
|
||||
// (the fake engine) draws nothing and still answers.
|
||||
|
||||
import { createFirmsCesiumHeat } from './cesiumHeat.js';
|
||||
|
||||
/**
|
||||
* The heatmap with heatImagery.js's interface: setRaster, hasRaster,
|
||||
* setShow, setOpacity (the hand-off fade, 0..1), rehome (follow the active
|
||||
* imagery host) and destroy.
|
||||
* @param {import('../../../globe/contract.js').GlobeEngine} engine
|
||||
* @param {{ imageryHost?: () => ({ collection: object, kind: string } | null),
|
||||
* navaraHeat?: (engine: object, navara: object) => object }} [options]
|
||||
* `imageryHost` is the shell's imagery host (Cesium); `navaraHeat` the
|
||||
* Navara backend's factory (render/navaraHeat.js).
|
||||
*/
|
||||
export function createFirmsHeatField(engine, { imageryHost, navaraHeat } = {}) {
|
||||
const cesium = engine.extensions?.cesium;
|
||||
const navara = engine.extensions?.navara;
|
||||
const backend = cesium
|
||||
? createFirmsCesiumHeat(cesium, imageryHost)
|
||||
: navara && navaraHeat
|
||||
? navaraHeat(engine, navara)
|
||||
: null;
|
||||
let raster = null,
|
||||
show = true,
|
||||
opacity = 1,
|
||||
destroyed = false;
|
||||
return {
|
||||
setRaster(next) {
|
||||
if (destroyed) return;
|
||||
raster = next;
|
||||
backend?.setRaster(next);
|
||||
},
|
||||
/** True once a raster was handed over. */
|
||||
hasRaster: () => raster !== null,
|
||||
setShow(value) {
|
||||
if (destroyed) return;
|
||||
show = Boolean(value);
|
||||
backend?.setShow(show);
|
||||
},
|
||||
setOpacity(value) {
|
||||
if (destroyed) return;
|
||||
opacity = Math.min(1, Math.max(0, Number(value) || 0));
|
||||
backend?.setOpacity(opacity);
|
||||
},
|
||||
rehome() {
|
||||
if (!destroyed) backend?.rehome();
|
||||
},
|
||||
/** What the layer asked for: { raster, show, opacity, drawn }. */
|
||||
state: () => ({ raster, show, opacity, drawn: backend !== null }),
|
||||
destroy() {
|
||||
if (destroyed) return;
|
||||
destroyed = true;
|
||||
backend?.destroy();
|
||||
raster = null;
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -1,151 +0,0 @@
|
||||
// FIRMS terrain/tiles classification and actual engine mover glows.
|
||||
import { createEngineLayerView } from '../../shared/render/engineView.js';
|
||||
import { selectionDispatcherFor } from '../../../data/selectionDispatcher.js';
|
||||
import { LOD_LEVELS } from '../policy.js';
|
||||
import { createFirmsSpriteBank } from './navaraSprites.js';
|
||||
import { createGroundCellDisplay } from '../../shared/render/navaraGroundCells.js';
|
||||
import { createFirmsHeatDrape } from './navaraHeat.js';
|
||||
export function createFirmsNavaraRenderer(
|
||||
engine,
|
||||
{ view: nativeView, EffectDesc, NavaraEffect, id = 'local-firms' } = {},
|
||||
) {
|
||||
const display = createGroundCellDisplay(
|
||||
engine,
|
||||
{ view: nativeView, EffectDesc, NavaraEffect },
|
||||
1,
|
||||
);
|
||||
let destroyed = false,
|
||||
visible = false,
|
||||
cells = [],
|
||||
queued = false,
|
||||
sprites = null;
|
||||
const dataSource = { show: false },
|
||||
handlers = new Set();
|
||||
const adapter = createEngineLayerView(engine, {
|
||||
layerId: id,
|
||||
isDestroyed: () => destroyed,
|
||||
});
|
||||
adapter.view.camera.moveEnd = engine.camera.moveEnd;
|
||||
adapter.view.scene.preRender = engine.frames.preRender;
|
||||
const scheduleCells = () => {
|
||||
if (queued || destroyed) return;
|
||||
queued = true;
|
||||
queueMicrotask(() => {
|
||||
queued = false;
|
||||
if (destroyed) return;
|
||||
display.replace(cells);
|
||||
});
|
||||
};
|
||||
const removeSprites = () => {
|
||||
sprites?.destroy();
|
||||
sprites = null;
|
||||
};
|
||||
return {
|
||||
kind: 'navara',
|
||||
view: adapter.view,
|
||||
dataSource,
|
||||
clickType: 'click',
|
||||
project: (position, result) =>
|
||||
engine.projector.worldToWindow(position, result),
|
||||
viewRectangle(result) {
|
||||
const rect = engine.camera.viewRectangle();
|
||||
return rect ? Object.assign(result ?? {}, rect) : null;
|
||||
},
|
||||
horizon: () => ({
|
||||
isPointVisible: (position) => engine.projector.horizonVisible(position),
|
||||
}),
|
||||
createInputHandler() {
|
||||
let remove = null;
|
||||
const handler = {
|
||||
setInputAction(callback) {
|
||||
remove?.();
|
||||
remove = selectionDispatcherFor(engine).register(id, {
|
||||
owns: (pickId) => {
|
||||
const collection = sprites?.collection;
|
||||
if (!collection) return false;
|
||||
for (let i = 0; i < collection.length; i++)
|
||||
if (collection.get(i).id === pickId) return true;
|
||||
return false;
|
||||
},
|
||||
onClick: (event) => {
|
||||
if (!destroyed && visible) callback(event);
|
||||
},
|
||||
});
|
||||
},
|
||||
destroy() {
|
||||
remove?.();
|
||||
remove = null;
|
||||
handlers.delete(handler);
|
||||
},
|
||||
};
|
||||
handlers.add(handler);
|
||||
return handler;
|
||||
},
|
||||
pick: (position, event) => event?.picked,
|
||||
setVisible(show) {
|
||||
if (destroyed) return;
|
||||
const next = show !== false;
|
||||
if (visible === next) return;
|
||||
visible = next;
|
||||
dataSource.show = next;
|
||||
sprites?.setVisible(next);
|
||||
display.setVisible(visible);
|
||||
engine.render.request('firms-visibility');
|
||||
},
|
||||
clearCells() {
|
||||
if (destroyed) return;
|
||||
cells = [];
|
||||
scheduleCells();
|
||||
},
|
||||
addCell(cell) {
|
||||
if (destroyed) return null;
|
||||
cells.push(cell);
|
||||
scheduleCells();
|
||||
return cell;
|
||||
},
|
||||
ensureSprites() {
|
||||
if (destroyed) return null;
|
||||
if (!sprites) {
|
||||
sprites = createFirmsSpriteBank(engine, {
|
||||
id: `firms:${id}`,
|
||||
capacity: Math.max(
|
||||
...LOD_LEVELS.map((lod) => lod.maxDetections ?? 0),
|
||||
),
|
||||
});
|
||||
sprites.setVisible(visible);
|
||||
}
|
||||
return sprites.collection;
|
||||
},
|
||||
addSprite(description) {
|
||||
return destroyed ? null : sprites.add(description);
|
||||
},
|
||||
removeSprites,
|
||||
/** The far-zoom heatmap, draped on this view's surface. */
|
||||
createHeatImagery() {
|
||||
return destroyed
|
||||
? null
|
||||
: createFirmsHeatDrape(engine, {
|
||||
view: nativeView,
|
||||
EffectDesc,
|
||||
NavaraEffect,
|
||||
});
|
||||
},
|
||||
/** The hand-off fade: every marker at `alpha`, shown while `show`. */
|
||||
fadeSprites(alpha, show) {
|
||||
if (!destroyed) sprites?.setFade(alpha, show);
|
||||
},
|
||||
onEnable() {},
|
||||
destroy() {
|
||||
if (destroyed) return;
|
||||
destroyed = true;
|
||||
visible = false;
|
||||
dataSource.show = false;
|
||||
cells = [];
|
||||
for (const handler of [...handlers]) handler.destroy();
|
||||
removeSprites();
|
||||
display.destroy();
|
||||
adapter.destroy();
|
||||
engine.render.request('firms-destroy');
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -1,188 +0,0 @@
|
||||
import { createGroundCellDisplay } from '../../shared/render/navaraGroundCells.js';
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createEngineEvent } from '../../../globe/contract.js';
|
||||
import * as geo from '../../../geo/index.js';
|
||||
import { createFirmsNavaraRenderer } from './navara.js';
|
||||
const settle = () => new Promise((resolve) => setImmediate(resolve));
|
||||
function harness() {
|
||||
const effects = [],
|
||||
registered = [],
|
||||
requests = [],
|
||||
input = new Map();
|
||||
const frames = {
|
||||
preUpdate: createEngineEvent(),
|
||||
preRender: createEngineEvent(),
|
||||
};
|
||||
const position = geo.cartesian3.fromDegrees(0, 0, 4000000);
|
||||
const engine = {
|
||||
frames,
|
||||
host: { size: () => ({ width: 1280, height: 800 }) },
|
||||
movers: {
|
||||
createSet() {
|
||||
throw new Error('cells do not allocate sprite movers');
|
||||
},
|
||||
},
|
||||
render: { request: (reason) => requests.push(reason) },
|
||||
camera: {
|
||||
livePosition: position,
|
||||
moveEnd: createEngineEvent(),
|
||||
viewRectangle: () => ({ west: -1, south: -0.5, east: 1, north: 0.5 }),
|
||||
pose: () => ({
|
||||
position,
|
||||
cartographic: { longitude: 0, latitude: 0, height: 4000000 },
|
||||
direction: { x: -1, y: 0, z: 0 },
|
||||
up: { x: 0, y: 0, z: 1 },
|
||||
right: { x: 0, y: 1, z: 0 },
|
||||
heading: 0,
|
||||
pitch: -Math.PI / 2,
|
||||
roll: 0,
|
||||
fovy: 1,
|
||||
}),
|
||||
},
|
||||
projector: {
|
||||
worldToWindow: (point, result) =>
|
||||
Object.assign(result ?? {}, { x: 10, y: 20 }),
|
||||
horizonVisible: () => true,
|
||||
},
|
||||
input: {
|
||||
on(type, listener) {
|
||||
input.set(type, listener);
|
||||
return () => input.delete(type);
|
||||
},
|
||||
},
|
||||
};
|
||||
const view = {
|
||||
camera: {},
|
||||
registerEffect: (key, desc) => registered.push([key, desc]),
|
||||
addEffect(config) {
|
||||
const handle = {
|
||||
config,
|
||||
updates: [],
|
||||
deleted: 0,
|
||||
update(value) {
|
||||
this.updates.push(value);
|
||||
},
|
||||
delete() {
|
||||
this.deleted++;
|
||||
},
|
||||
};
|
||||
effects.push(handle);
|
||||
return handle;
|
||||
},
|
||||
};
|
||||
const options = { view, EffectDesc: class {}, NavaraEffect: class {} };
|
||||
return {
|
||||
engine,
|
||||
options,
|
||||
effects,
|
||||
registered,
|
||||
requests,
|
||||
input,
|
||||
frames,
|
||||
renderer: createFirmsNavaraRenderer(engine, options),
|
||||
};
|
||||
}
|
||||
const cell = {
|
||||
west: 0,
|
||||
south: 0,
|
||||
east: 1,
|
||||
north: 1,
|
||||
color: { red: 1, green: 0, blue: 0, alpha: 0.3 },
|
||||
};
|
||||
test('ground changes are atomic, lazy and dispose replaced and destroyed GPU textures once', async () => {
|
||||
const h = harness(),
|
||||
r = h.renderer;
|
||||
r.setVisible(true);
|
||||
assert.equal(h.effects.length, 0);
|
||||
r.clearCells();
|
||||
r.addCell(cell);
|
||||
r.addCell({ ...cell, west: 1, east: 2 });
|
||||
assert.equal(h.effects.length, 0);
|
||||
await settle();
|
||||
assert.equal(h.effects.length, 1);
|
||||
const owner = h.effects[0].config.gevFirmsGround.owner;
|
||||
assert.equal(owner.state.count, 2);
|
||||
let disposed = 0;
|
||||
for (const t of [
|
||||
owner.state.packed.texture,
|
||||
owner.state.gridTexture,
|
||||
owner.state.candidates.texture,
|
||||
])
|
||||
t.addEventListener('dispose', () => disposed++);
|
||||
r.clearCells();
|
||||
r.addCell(cell);
|
||||
await settle();
|
||||
assert.equal(owner.state.count, 1);
|
||||
assert.equal(disposed, 3);
|
||||
r.setVisible(false);
|
||||
assert.equal(r.dataSource.show, false);
|
||||
assert.deepEqual(h.effects[0].updates.at(-1), { visible: false });
|
||||
r.clearCells();
|
||||
r.addCell(cell);
|
||||
r.destroy();
|
||||
r.destroy();
|
||||
const before = h.requests.length;
|
||||
await settle();
|
||||
assert.equal(h.requests.length, before);
|
||||
assert.equal(h.effects[0].deleted, 1);
|
||||
assert.equal(h.frames.preUpdate.numberOfListeners, 0);
|
||||
const second = createFirmsNavaraRenderer(h.engine, h.options);
|
||||
second.destroy();
|
||||
assert.equal(h.registered.length, 1);
|
||||
});
|
||||
test('GNSS and FIRMS share one ground pass without hiding or destroying one another', async () => {
|
||||
const h = harness();
|
||||
const gnss = createGroundCellDisplay(h.engine, h.options, 0);
|
||||
const fire = h.renderer;
|
||||
try {
|
||||
fire.setVisible(true);
|
||||
fire.addCell(cell);
|
||||
await settle();
|
||||
gnss.replace([cell, cell]);
|
||||
gnss.setVisible(true);
|
||||
const owner = h.effects[0].config.gevFirmsGround.owner;
|
||||
assert.equal(h.effects.length, 1);
|
||||
assert.equal(owner.state.count, 3);
|
||||
gnss.setVisible(false);
|
||||
assert.equal(owner.state.count, 1);
|
||||
gnss.setVisible(true);
|
||||
fire.destroy();
|
||||
assert.equal(owner.state.count, 2);
|
||||
assert.equal(h.effects[0].deleted, 0);
|
||||
} finally {
|
||||
fire.destroy();
|
||||
gnss.destroy();
|
||||
}
|
||||
assert.equal(h.effects[0].deleted, 1);
|
||||
});
|
||||
|
||||
test('FIRMS selection uses actual engine event picks and releases its dispatcher subscriptions', () => {
|
||||
const h = harness(),
|
||||
r = h.renderer;
|
||||
r.setVisible(true);
|
||||
const handler = r.createInputHandler();
|
||||
let event;
|
||||
handler.setInputAction((value) => (event = value), 'click');
|
||||
assert.equal(h.input.size, 4);
|
||||
const picked = { id: 'sibling-contact' };
|
||||
h.input.get('click')({ position: { x: 10, y: 20 }, picked, drilled: [] });
|
||||
assert.equal(r.pick(event.position, event), picked);
|
||||
assert.equal(r.view.camera.moveEnd, h.engine.camera.moveEnd);
|
||||
assert.equal(r.view.scene.preRender, h.engine.frames.preRender);
|
||||
assert.deepEqual(r.viewRectangle({}), {
|
||||
west: -1,
|
||||
south: -0.5,
|
||||
east: 1,
|
||||
north: 0.5,
|
||||
});
|
||||
assert.deepEqual(r.project({}, {}), { x: 10, y: 20 });
|
||||
event = null;
|
||||
r.setVisible(false);
|
||||
h.input.get('click')({ position: { x: 10, y: 20 }, picked, drilled: [] });
|
||||
assert.equal(event, null);
|
||||
handler.destroy();
|
||||
handler.destroy();
|
||||
assert.equal(h.input.size, 0);
|
||||
r.destroy();
|
||||
});
|
||||
@@ -1,120 +0,0 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import * as Cesium from 'cesium';
|
||||
import * as geo from '../../../geo/index.js';
|
||||
import {
|
||||
rectangleBoundaries,
|
||||
rectangleDistances,
|
||||
rectangleFrame,
|
||||
} from '../../shared/render/navaraCellGeometry.js';
|
||||
const cases = [];
|
||||
for (const lat of [-90, -80, -45, 0, 45, 80, 88])
|
||||
for (const west of [-180, -123, 0, 178])
|
||||
for (const width of [1, 2])
|
||||
cases.push({
|
||||
west,
|
||||
south: lat,
|
||||
east: west + width,
|
||||
north: Math.min(90, lat + width),
|
||||
});
|
||||
test('geodetic cell sides contain public native rectangle vertices and retain exact parallels at every height', () => {
|
||||
let vertices = 0,
|
||||
heights = 0;
|
||||
for (const rect of cases) {
|
||||
const bounds = rectangleBoundaries(rect);
|
||||
const native = Cesium.RectangleGeometry.createGeometry(
|
||||
new Cesium.RectangleGeometry({
|
||||
rectangle: Cesium.Rectangle.fromDegrees(
|
||||
rect.west,
|
||||
rect.south,
|
||||
rect.east,
|
||||
rect.north,
|
||||
),
|
||||
vertexFormat: Cesium.VertexFormat.POSITION_ONLY,
|
||||
}),
|
||||
);
|
||||
const positions = native.attributes.position.values;
|
||||
for (let i = 0; i < positions.length; i += 3) {
|
||||
const distance = rectangleDistances(bounds, {
|
||||
x: positions[i],
|
||||
y: positions[i + 1],
|
||||
z: positions[i + 2],
|
||||
});
|
||||
assert.ok(Math.min(...distance) > -1e-6, JSON.stringify(rect));
|
||||
vertices++;
|
||||
}
|
||||
for (const h of [-100000, 0, 200, 12000])
|
||||
for (const t of [0.1, 0.5, 0.9]) {
|
||||
const lon = rect.west + (rect.east - rect.west) * t,
|
||||
lat = rect.south + (rect.north - rect.south) * t;
|
||||
for (const [x, y] of [
|
||||
[lon, rect.south],
|
||||
[lon, rect.north],
|
||||
[rect.west, lat],
|
||||
[rect.east, lat],
|
||||
]) {
|
||||
const d = rectangleDistances(
|
||||
bounds,
|
||||
geo.cartesian3.fromDegrees(x, y, h),
|
||||
);
|
||||
assert.ok(
|
||||
Math.abs(Math.min(...d)) < 1e-6,
|
||||
`${JSON.stringify(rect)} height ${h}`,
|
||||
);
|
||||
heights++;
|
||||
}
|
||||
assert.ok(
|
||||
Math.min(
|
||||
...rectangleDistances(
|
||||
bounds,
|
||||
geo.cartesian3.fromDegrees(lon, lat, h),
|
||||
),
|
||||
) > 0,
|
||||
);
|
||||
assert.ok(
|
||||
Math.min(
|
||||
...rectangleDistances(
|
||||
bounds,
|
||||
geo.cartesian3.fromDegrees(rect.west - 0.01, lat, h),
|
||||
),
|
||||
) < 0,
|
||||
);
|
||||
assert.ok(
|
||||
Math.min(
|
||||
...rectangleDistances(
|
||||
bounds,
|
||||
geo.cartesian3.fromDegrees(rect.east + 0.01, lat, h),
|
||||
),
|
||||
) < 0,
|
||||
);
|
||||
}
|
||||
}
|
||||
assert.equal(vertices, 562);
|
||||
assert.equal(heights, 2688);
|
||||
});
|
||||
test('candidate volumes enclose geodetic cells through their conservative terrain/roof height window', () => {
|
||||
for (const rect of cases) {
|
||||
const frame = rectangleFrame(rect);
|
||||
for (const lat of [rect.south, (rect.south + rect.north) / 2, rect.north])
|
||||
for (const lon of [rect.west, (rect.west + rect.east) / 2, rect.east])
|
||||
for (const h of [-100000, 0, 12000]) {
|
||||
const p = geo.cartesian3.fromDegrees(lon, lat, h),
|
||||
delta = {
|
||||
x: p.x - frame.center.x,
|
||||
y: p.y - frame.center.y,
|
||||
z: p.z - frame.center.z,
|
||||
};
|
||||
const x = geo.cartesian3.dot(delta, frame.east),
|
||||
y = geo.cartesian3.dot(delta, frame.north),
|
||||
z = geo.cartesian3.dot(delta, frame.up);
|
||||
assert.ok(
|
||||
Math.abs(x) <= frame.radius && Math.abs(y) <= frame.radius,
|
||||
JSON.stringify(rect),
|
||||
);
|
||||
assert.ok(
|
||||
z >= frame.floor - 1e-6 && z <= frame.ceiling + 1e-6,
|
||||
`${JSON.stringify(rect)} ${z}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -1,177 +0,0 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createMoverRegistry } from '../../../globe/movers.js';
|
||||
import { createFirmsSpriteBank } from './navaraSprites.js';
|
||||
const settle = () => new Promise((resolve) => setImmediate(resolve));
|
||||
function harness(loadImage) {
|
||||
let commits = 0,
|
||||
disposals = 0;
|
||||
const draws = [],
|
||||
canvases = [];
|
||||
const registry = createMoverRegistry((options, store) => ({
|
||||
ready: () => true,
|
||||
commit() {
|
||||
commits++;
|
||||
},
|
||||
destroy() {
|
||||
disposals++;
|
||||
},
|
||||
}));
|
||||
const bank = createFirmsSpriteBank(
|
||||
{ movers: registry.movers },
|
||||
{
|
||||
id: 'firms-test',
|
||||
capacity: 3,
|
||||
loadImage,
|
||||
createCanvas() {
|
||||
const canvas = {
|
||||
getContext: () => ({ drawImage: (...args) => draws.push(args) }),
|
||||
};
|
||||
canvases.push(canvas);
|
||||
return canvas;
|
||||
},
|
||||
},
|
||||
);
|
||||
return {
|
||||
bank,
|
||||
registry,
|
||||
draws,
|
||||
canvases,
|
||||
get commits() {
|
||||
return commits;
|
||||
},
|
||||
get disposals() {
|
||||
return disposals;
|
||||
},
|
||||
};
|
||||
}
|
||||
test('actual image dimensions set both the atlas cell and visible/pickable quad, with batched horizon flips', async () => {
|
||||
const h = harness(async (url) => ({
|
||||
width: url === 'small' ? 16 : 56,
|
||||
height: url === 'small' ? 16 : 56,
|
||||
}));
|
||||
const first = h.bank.add({
|
||||
id: 'firms-0',
|
||||
position: { x: 1, y: 2, z: 3 },
|
||||
image: 'small',
|
||||
});
|
||||
const second = h.bank.add({
|
||||
id: 'firms-1',
|
||||
position: { x: 4, y: 5, z: 6 },
|
||||
image: 'large',
|
||||
});
|
||||
h.bank.setVisible(true);
|
||||
await settle();
|
||||
const entry = h.registry.sets.get('firms-test');
|
||||
assert.equal(h.registry.sets.size, 1);
|
||||
assert.equal(entry.options.sharpen, true);
|
||||
assert.equal(entry.options.depthTest, 'always');
|
||||
assert.equal(entry.options.pickable, true);
|
||||
assert.equal(entry.store.count, 2);
|
||||
assert.ok(Math.abs(entry.store.scales[0] - 16 / 56) < 1e-7);
|
||||
assert.equal(entry.store.scales[1], 1);
|
||||
assert.deepEqual(entry.store.pickIds.slice(0, 2), ['firms-0', 'firms-1']);
|
||||
assert.equal(h.draws.length, 2);
|
||||
assert.ok(
|
||||
h.draws.every((args) => args.length === 3),
|
||||
'atlas does not resize source PNGs',
|
||||
);
|
||||
const rect = entry.options.sprite.atlas.cells['0'];
|
||||
assert.ok(Math.abs((rect[2] - rect[0]) * h.canvases[0].width - 16) < 1e-10);
|
||||
assert.ok(
|
||||
rect[0] > 0 && rect[1] > 0,
|
||||
'transparent neighbor padding is retained',
|
||||
);
|
||||
const before = h.commits;
|
||||
first.show = false;
|
||||
second.show = false;
|
||||
await settle();
|
||||
assert.equal(h.commits, before + 1);
|
||||
assert.equal(h.canvases.length, 1);
|
||||
assert.deepEqual([...entry.store.shows.slice(0, 2)], [0, 0]);
|
||||
h.bank.setVisible(false);
|
||||
assert.equal(entry.store.count, 0);
|
||||
const hidden = h.commits;
|
||||
await settle();
|
||||
assert.equal(h.commits, hidden);
|
||||
h.bank.destroy();
|
||||
h.bank.destroy();
|
||||
assert.equal(h.registry.sets.size, 0);
|
||||
assert.equal(h.disposals, 1);
|
||||
});
|
||||
test('uncooperative image completion after disable or destroy cannot create movers or request a frame', async () => {
|
||||
let finish;
|
||||
const h = harness(() => new Promise((resolve) => (finish = resolve)));
|
||||
h.bank.add({ id: 'firms-0', position: { x: 1, y: 2, z: 3 }, image: 'late' });
|
||||
h.bank.setVisible(true);
|
||||
await settle();
|
||||
h.bank.setVisible(false);
|
||||
finish({ width: 16, height: 16 });
|
||||
await settle();
|
||||
assert.equal(h.registry.sets.size, 0);
|
||||
assert.equal(h.commits, 0);
|
||||
h.bank.collection.removeAll();
|
||||
h.bank.add({ id: 'firms-1', position: { x: 1, y: 2, z: 3 }, image: 'later' });
|
||||
h.bank.setVisible(true);
|
||||
await settle();
|
||||
h.bank.destroy();
|
||||
finish({ width: 56, height: 56 });
|
||||
await settle();
|
||||
assert.equal(h.registry.sets.size, 0);
|
||||
assert.equal(h.commits, 0);
|
||||
assert.equal(h.bank.collection.length, 0);
|
||||
});
|
||||
test('clearing rows during decode preserves the new generation and commits only the replacement', async () => {
|
||||
const complete = new Map(),
|
||||
h = harness((url) => new Promise((resolve) => complete.set(url, resolve)));
|
||||
h.bank.setVisible(true);
|
||||
h.bank.add({ id: 'old', position: { x: 1, y: 2, z: 3 }, image: 'old' });
|
||||
await settle();
|
||||
h.bank.collection.removeAll();
|
||||
h.bank.add({ id: 'new', position: { x: 4, y: 5, z: 6 }, image: 'new' });
|
||||
await settle();
|
||||
complete.get('old')({ width: 16, height: 16 });
|
||||
await settle();
|
||||
assert.equal(h.registry.sets.size, 0);
|
||||
complete.get('new')({ width: 56, height: 56 });
|
||||
await settle();
|
||||
const entry = h.registry.sets.get('firms-test');
|
||||
assert.equal(entry.store.count, 1);
|
||||
assert.equal(entry.store.pickIds[0], 'new');
|
||||
assert.equal(h.commits, 1);
|
||||
h.bank.destroy();
|
||||
});
|
||||
|
||||
test('the heatmap hand-off fades every marker and hides them all once it completes', async () => {
|
||||
const h = harness(async () => ({ width: 56, height: 56 }));
|
||||
h.bank.add({ id: 'firms-0', position: { x: 1, y: 2, z: 3 }, image: 'a' });
|
||||
h.bank.add({ id: 'firms-1', position: { x: 4, y: 5, z: 6 }, image: 'a' });
|
||||
h.bank.setVisible(true);
|
||||
await settle();
|
||||
await settle();
|
||||
const entry = h.registry.sets.get('firms-test');
|
||||
assert.equal(entry.store.count, 2);
|
||||
assert.deepEqual(
|
||||
[...entry.store.colors.slice(0, 8)],
|
||||
[1, 1, 1, 1, 1, 1, 1, 1],
|
||||
);
|
||||
h.bank.setFade(0.25, true);
|
||||
await settle();
|
||||
await settle();
|
||||
assert.deepEqual(
|
||||
[...entry.store.colors.slice(0, 8)],
|
||||
[1, 1, 1, 0.25, 1, 1, 1, 0.25],
|
||||
);
|
||||
h.bank.setFade(0, false);
|
||||
await settle();
|
||||
await settle();
|
||||
assert.equal(entry.store.count, 0, 'the heatmap alone: no marker drawn');
|
||||
h.bank.setFade(1, true);
|
||||
await settle();
|
||||
await settle();
|
||||
assert.equal(entry.store.count, 2);
|
||||
assert.deepEqual(
|
||||
[...entry.store.colors.slice(0, 8)],
|
||||
[1, 1, 1, 1, 1, 1, 1, 1],
|
||||
);
|
||||
});
|
||||
@@ -1,10 +1,17 @@
|
||||
// The real FIRMS PNGs in one engine mover atlas.
|
||||
// The FIRMS glow PNGs as one engine.movers sprite set over an atlas of the
|
||||
// decoded images, the same on every engine. Each glow is drawn at its own
|
||||
// pixel size (a scale of its side over BASE_SIZE), centred on its anchor,
|
||||
// over the scene (depth test off), in the sprite order's 'firms' slot.
|
||||
import { packImageAtlas } from '../../shared/render/imageAtlas.js';
|
||||
import { SPRITE_LAYER_ORDER } from '../../../data/spriteOrder.js';
|
||||
|
||||
const WHITE = Object.freeze([1, 1, 1, 1]);
|
||||
const CELL = 60,
|
||||
PAD = 2,
|
||||
BASE_SIZE = 56;
|
||||
/** The glows' draw order among the sprite layers (data/spriteOrder.js). */
|
||||
export const FIRMS_SPRITE_ORDER = SPRITE_LAYER_ORDER.indexOf('firms');
|
||||
|
||||
const imageFromUrl = (url) =>
|
||||
new Promise((resolve, reject) => {
|
||||
const image = new Image();
|
||||
@@ -18,6 +25,19 @@ const imageFromUrl = (url) =>
|
||||
};
|
||||
image.src = url;
|
||||
});
|
||||
|
||||
/**
|
||||
* A bank of glow sprites: `collection` is billboard-collection shaped
|
||||
* ({ length, get(i), removeAll() }) and each row's `show` is writable, as
|
||||
* the layer's horizon cull expects. Writes are drawn on the next microtask,
|
||||
* once the images they name are decoded; a show flip rewrites only that
|
||||
* instance.
|
||||
* @param {import('../../../globe/contract.js').GlobeEngine} engine
|
||||
* @param {{ id: string, capacity: number,
|
||||
* loadImage?: (url: string) => Promise<object>,
|
||||
* createCanvas?: () => object }} options `loadImage` and `createCanvas`
|
||||
* are test seams (Node has neither Image nor canvas).
|
||||
*/
|
||||
export function createFirmsSpriteBank(
|
||||
engine,
|
||||
{
|
||||
@@ -35,8 +55,11 @@ export function createFirmsSpriteBank(
|
||||
destroyed = false,
|
||||
generation = 0,
|
||||
queued = false,
|
||||
// Every row is rewritten on the next flush (else only `showDirty`).
|
||||
full = true,
|
||||
set = null,
|
||||
atlas = null;
|
||||
const showDirty = new Set();
|
||||
const images = new Map();
|
||||
const loaded = new Map();
|
||||
const getImage = (url) => {
|
||||
@@ -57,6 +80,7 @@ export function createFirmsSpriteBank(
|
||||
return task;
|
||||
};
|
||||
const hide = () => {
|
||||
full = true;
|
||||
if (set && set.count() !== 0) {
|
||||
set.setCount(0);
|
||||
set.commit();
|
||||
@@ -73,12 +97,12 @@ export function createFirmsSpriteBank(
|
||||
lookup.set(url, { key, scale: width / BASE_SIZE });
|
||||
return { key, image, width, height };
|
||||
});
|
||||
const atlas = packImageAtlas(entries, {
|
||||
const packed = packImageAtlas(entries, {
|
||||
canvas: createCanvas(),
|
||||
cell: CELL,
|
||||
pad: PAD,
|
||||
});
|
||||
return { ...atlas, lookup };
|
||||
return { ...packed, lookup };
|
||||
}
|
||||
async function flush() {
|
||||
if (destroyed) return;
|
||||
@@ -98,6 +122,7 @@ export function createFirmsSpriteBank(
|
||||
id,
|
||||
kind: 'sprite',
|
||||
capacity,
|
||||
order: FIRMS_SPRITE_ORDER,
|
||||
depthTest: 'always',
|
||||
pickable: true,
|
||||
sharpen: true,
|
||||
@@ -107,19 +132,27 @@ export function createFirmsSpriteBank(
|
||||
},
|
||||
});
|
||||
atlas = next;
|
||||
full = true;
|
||||
}
|
||||
snapshot.forEach((row, index) => {
|
||||
const cell = atlas.lookup.get(row.image);
|
||||
set.set(index, {
|
||||
position: row.position,
|
||||
cell: cell.key,
|
||||
scale: cell.scale,
|
||||
color: fade,
|
||||
show: row.show,
|
||||
pickId: row.id,
|
||||
if (full || set.count() !== snapshot.length) {
|
||||
snapshot.forEach((row, index) => {
|
||||
const cell = atlas.lookup.get(row.image);
|
||||
set.set(index, {
|
||||
position: row.position,
|
||||
cell: cell.key,
|
||||
scale: cell.scale,
|
||||
color: fade,
|
||||
show: row.show,
|
||||
pickId: row.id,
|
||||
});
|
||||
});
|
||||
});
|
||||
set.setCount(snapshot.length);
|
||||
set.setCount(snapshot.length);
|
||||
} else
|
||||
for (const index of showDirty)
|
||||
if (index < snapshot.length)
|
||||
set.set(index, { show: snapshot[index].show });
|
||||
full = false;
|
||||
showDirty.clear();
|
||||
set.commit();
|
||||
}
|
||||
function schedule() {
|
||||
@@ -145,6 +178,7 @@ export function createFirmsSpriteBank(
|
||||
removeAll() {
|
||||
if (destroyed) return;
|
||||
rows = [];
|
||||
full = true;
|
||||
schedule();
|
||||
},
|
||||
};
|
||||
@@ -154,6 +188,7 @@ export function createFirmsSpriteBank(
|
||||
if (destroyed) return null;
|
||||
if (rows.length >= capacity)
|
||||
throw new RangeError('FIRMS sprite capacity exceeded');
|
||||
const index = rows.length;
|
||||
let show = description.show !== false;
|
||||
const row = {
|
||||
...description,
|
||||
@@ -164,13 +199,20 @@ export function createFirmsSpriteBank(
|
||||
const next = Boolean(value);
|
||||
if (next === show || destroyed) return;
|
||||
show = next;
|
||||
showDirty.add(index);
|
||||
schedule();
|
||||
},
|
||||
};
|
||||
rows.push(row);
|
||||
full = true;
|
||||
schedule();
|
||||
return row;
|
||||
},
|
||||
/** Whether a pick id names one of the bank's rows. */
|
||||
owns(pickId) {
|
||||
for (const row of rows) if (row.id === pickId) return true;
|
||||
return false;
|
||||
},
|
||||
/** The hand-off fade: every marker at `alpha`, drawn while `show`. */
|
||||
setFade(alpha, show) {
|
||||
if (destroyed) return;
|
||||
@@ -178,6 +220,7 @@ export function createFirmsSpriteBank(
|
||||
if (next[3] === fade[3] && Boolean(show) === fadeShown) return;
|
||||
fade = next;
|
||||
fadeShown = Boolean(show);
|
||||
full = true;
|
||||
schedule();
|
||||
},
|
||||
setVisible(value) {
|
||||
@@ -186,11 +229,16 @@ export function createFirmsSpriteBank(
|
||||
if (!visible) hide();
|
||||
schedule();
|
||||
},
|
||||
/** The mover set drawing the glows (null before the first flush). */
|
||||
get set() {
|
||||
return set;
|
||||
},
|
||||
destroy() {
|
||||
if (destroyed) return;
|
||||
destroyed = true;
|
||||
generation++;
|
||||
rows = [];
|
||||
showDirty.clear();
|
||||
images.clear();
|
||||
loaded.clear();
|
||||
set?.destroy();
|
||||
Reference in new issue
Block a user