mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-20 10:12:18 +00:00
Add raster imagery to the globe contract and draw FIRMS heat and Recent Imagery through it
engine.imagery draws raster layers over the globe: Web Mercator tile templates and single images or canvases stretched over a rectangle, with alpha, show, a left/right split against the engine's split position and an order. A layer's source can be replaced in place. - Cesium puts one ImageryLayer per layer on the globe's imagery stack, or on the shown 3D tileset's when the globe is hidden, and moves it when the surface changes; the Recent Imagery request gate moved here. - Navara draws them in one screen pass after tone mapping that samples up to eight layers by geodetic position from mipmapped textures and a tile atlas, under the other drapes and engine.ground. - The fake engine keeps each layer's state. The FIRMS far-zoom heat raster and Recent Imagery now draw through it, so FIRMS' heat seam and both its backends, and Recent Imagery's Cesium and Navara renderers and tile plan, go. Against HEAD, Cesium's pixels are identical in all four compared scenes; Navara's orbital heat changes by a mean of 3.8 from new mipmapping, slightly towards Cesium. The contract suite passes on both engines (Cesium 68/68, Navara 71/71). engine.maps.addOverlay stays a Navara-only extension for Sea Marks and Ski Pistes until imagery has a finer tile path for oblique global views.
This commit is contained in:
1 parent
1559bd57f4
commit
406dd9122e
52 files changed
+4171
-1988
No files matched your search
@@ -4,7 +4,6 @@ import * as geo from '../../geo/index.js';
|
||||
import { createFakeEngine } from '../../globe/testing/fakeEngine.js';
|
||||
import { createFirmsHeatmapLayer } from './index.js';
|
||||
import { createFirmsDisplay } from './render/display.js';
|
||||
import { createFirmsHeatDrape } from './render/navaraHeat.js';
|
||||
|
||||
const settle = async () => {
|
||||
for (let i = 0; i < 5; i++)
|
||||
@@ -64,32 +63,6 @@ function fixture() {
|
||||
roll: 0,
|
||||
});
|
||||
pose(12_000_000);
|
||||
const effects = [];
|
||||
const native = {
|
||||
view: {
|
||||
camera: {},
|
||||
registerEffect() {},
|
||||
addEffect(config) {
|
||||
const handle = {
|
||||
config,
|
||||
visible: config.visible,
|
||||
deleted: false,
|
||||
update(next) {
|
||||
if ('visible' in next) handle.visible = next.visible;
|
||||
},
|
||||
delete() {
|
||||
handle.deleted = true;
|
||||
},
|
||||
};
|
||||
effects.push(handle);
|
||||
return handle;
|
||||
},
|
||||
},
|
||||
EffectDesc: class {},
|
||||
NavaraEffect: class {},
|
||||
};
|
||||
// The heatmap draws through the Navara backend on this view.
|
||||
engine.extensions = { navara: native };
|
||||
const noop = () => {};
|
||||
const layer = createFirmsHeatmapLayer({
|
||||
id: 'local-firms',
|
||||
@@ -117,8 +90,7 @@ function fixture() {
|
||||
},
|
||||
overlayHost: { setEntries: noop, setVisible: noop, clearSource: noop },
|
||||
renderer: {
|
||||
create: ({ engine: target, id }) =>
|
||||
createFirmsDisplay(target, { id, navaraHeat: createFirmsHeatDrape }),
|
||||
create: ({ engine: target, id }) => createFirmsDisplay(target, { id }),
|
||||
},
|
||||
});
|
||||
layer.init(null, { engine });
|
||||
@@ -129,18 +101,15 @@ function fixture() {
|
||||
engine,
|
||||
layer,
|
||||
pose,
|
||||
// The heatmap as drawn: its raster's alpha at the fire, 0 when the
|
||||
// drape is hidden, faded out or unpainted.
|
||||
// The heatmap as drawn: its raster's alpha at the fire times the
|
||||
// layer's, 0 when the imagery layer is hidden, faded out or unpainted.
|
||||
drawnHeat() {
|
||||
const handle = effects.find(
|
||||
(effect) => effect.config.gevFirmsHeat && !effect.deleted,
|
||||
);
|
||||
const owner = handle?.config.gevFirmsHeat.owner;
|
||||
if (!handle?.visible || !owner?.texture || !(owner.opacity > 0)) return 0;
|
||||
const { data, width, height } = owner.texture.image;
|
||||
const heat = engine.fake.imageryLayer('local-firms:heat');
|
||||
if (!heat?.shown || !(heat.alpha > 0)) return 0;
|
||||
const { data, width, height } = heat.source.image;
|
||||
const x = Math.floor(((FIRE.lon + 180) / 360) * width);
|
||||
const y = Math.floor(((90 - FIRE.lat) / 180) * height);
|
||||
return (data[(y * width + x) * 4 + 3] / 255) * owner.opacity;
|
||||
return (data[(y * width + x) * 4 + 3] / 255) * heat.alpha;
|
||||
},
|
||||
cards: () => layer.getStats().cells,
|
||||
// Fire sprites actually drawn (shown instances of the engine set).
|
||||
|
||||
@@ -1,88 +0,0 @@
|
||||
/**
|
||||
* Drapes the Active Fires heat raster over the globe as one full-extent
|
||||
* imagery tile on the active imagery host (globe or 3D-tiles), like the wind
|
||||
* layer's scalar field. Owns the canvas and the imagery layer; nothing else
|
||||
* in the fires layer touches imagery.
|
||||
*
|
||||
* `show` is only for the layer being on or off. The zoom hand-off fades
|
||||
* `alpha` instead: Cesium unmaps a hidden imagery layer from the globe
|
||||
* tiles and re-maps it when shown, so a show toggle at the band edge left
|
||||
* a beat with neither markers nor heat. At alpha 0 the layer stays mapped
|
||||
* and costs no draw.
|
||||
*/
|
||||
|
||||
const defaultCanvas = (width, height) => {
|
||||
const canvas = document.createElement('canvas');
|
||||
canvas.width = width;
|
||||
canvas.height = height;
|
||||
return canvas;
|
||||
};
|
||||
|
||||
export function createHeatImagery({
|
||||
cesium,
|
||||
getHost,
|
||||
createCanvas = defaultCanvas,
|
||||
}) {
|
||||
let raster = null;
|
||||
let layer = null;
|
||||
let collection = null;
|
||||
let show = true;
|
||||
let opacity = 1;
|
||||
|
||||
function detach() {
|
||||
if (layer && collection) collection.remove(layer, true);
|
||||
layer = null;
|
||||
collection = null;
|
||||
}
|
||||
|
||||
function attach() {
|
||||
detach();
|
||||
if (!raster) return;
|
||||
const host = getHost?.() ?? { collection: null, kind: 'none' };
|
||||
if (!host.collection) return;
|
||||
const canvas = createCanvas(raster.width, raster.height);
|
||||
const ctx = canvas.getContext('2d');
|
||||
const image = ctx.createImageData(raster.width, raster.height);
|
||||
image.data.set(raster.rgba);
|
||||
ctx.putImageData(image, 0, 0);
|
||||
const provider = new cesium.SingleTileImageryProvider({
|
||||
url: canvas.toDataURL('image/png'),
|
||||
tileWidth: raster.width,
|
||||
tileHeight: raster.height,
|
||||
rectangle: cesium.Rectangle.MAX_VALUE,
|
||||
});
|
||||
collection = host.collection;
|
||||
layer = collection.addImageryProvider(provider);
|
||||
layer.show = show;
|
||||
layer.alpha = opacity;
|
||||
collection.raiseToTop?.(layer);
|
||||
}
|
||||
|
||||
return {
|
||||
setRaster(next) {
|
||||
raster = next;
|
||||
attach();
|
||||
},
|
||||
/** True once a raster was handed over (painted or waiting for a host). */
|
||||
hasRaster() {
|
||||
return raster !== null;
|
||||
},
|
||||
setShow(value) {
|
||||
show = Boolean(value);
|
||||
if (layer) layer.show = show;
|
||||
},
|
||||
/** Hand-off fade, 0..1; the layer stays mapped at 0. */
|
||||
setOpacity(value) {
|
||||
opacity = Math.min(1, Math.max(0, Number(value) || 0));
|
||||
if (layer) layer.alpha = opacity;
|
||||
},
|
||||
rehome() {
|
||||
const host = getHost?.();
|
||||
if ((host?.collection ?? null) !== collection) attach();
|
||||
},
|
||||
destroy() {
|
||||
detach();
|
||||
raster = null;
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -1,128 +0,0 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createHeatImagery } from './heatImagery.js';
|
||||
import { buildHeatRaster } from '../../data/firmsHeatRaster.js';
|
||||
import { HEAT_RASTER } from './policy.js';
|
||||
|
||||
function harness() {
|
||||
const layers = [];
|
||||
const removed = [];
|
||||
const collection = {
|
||||
addImageryProvider(provider) {
|
||||
const layer = { provider, show: true, alpha: 1 };
|
||||
layers.push(layer);
|
||||
return layer;
|
||||
},
|
||||
remove(layer, destroy) {
|
||||
layers.splice(layers.indexOf(layer), 1);
|
||||
removed.push({ layer, destroy });
|
||||
},
|
||||
raiseToTop() {},
|
||||
};
|
||||
let host = { collection, kind: 'globe' };
|
||||
const cesium = {
|
||||
Rectangle: { MAX_VALUE: { max: true } },
|
||||
SingleTileImageryProvider: class {
|
||||
constructor(options) {
|
||||
this.options = options;
|
||||
}
|
||||
},
|
||||
};
|
||||
const createCanvas = (w, h) => ({
|
||||
width: w,
|
||||
height: h,
|
||||
getContext: () => ({
|
||||
createImageData: (cw, ch) => ({
|
||||
data: new Uint8ClampedArray(cw * ch * 4),
|
||||
}),
|
||||
putImageData() {},
|
||||
}),
|
||||
toDataURL: () => 'data:image/png;base64,AAAA',
|
||||
});
|
||||
const imagery = createHeatImagery({
|
||||
cesium,
|
||||
getHost: () => host,
|
||||
createCanvas,
|
||||
});
|
||||
return { imagery, layers, removed, collection, setHost: (h) => (host = h) };
|
||||
}
|
||||
|
||||
const raster = { width: 4, height: 2, rgba: new Uint8ClampedArray(32) };
|
||||
|
||||
test('setRaster drapes one full-globe tile and replaces it on the next build', () => {
|
||||
const h = harness();
|
||||
h.imagery.setRaster(raster);
|
||||
assert.equal(h.layers.length, 1);
|
||||
assert.deepEqual(h.layers[0].provider.options.rectangle, { max: true });
|
||||
assert.equal(h.layers[0].provider.options.tileWidth, 4);
|
||||
h.imagery.setRaster(raster);
|
||||
assert.equal(h.layers.length, 1, 'never two heat layers');
|
||||
assert.equal(h.removed[0].destroy, true);
|
||||
});
|
||||
|
||||
test('setShow toggles without rebuilding', () => {
|
||||
const h = harness();
|
||||
h.imagery.setRaster(raster);
|
||||
const layer = h.layers[0];
|
||||
h.imagery.setShow(false);
|
||||
assert.equal(layer.show, false);
|
||||
h.imagery.setShow(true);
|
||||
assert.equal(h.layers[0], layer);
|
||||
assert.equal(layer.show, true);
|
||||
});
|
||||
|
||||
test('setOpacity fades the same layer and survives a rebuild', () => {
|
||||
const h = harness();
|
||||
h.imagery.setRaster(raster);
|
||||
const layer = h.layers[0];
|
||||
h.imagery.setOpacity(0);
|
||||
assert.equal(layer.alpha, 0);
|
||||
assert.equal(layer.show, true, 'a faded layer stays mapped');
|
||||
h.imagery.setOpacity(0.25);
|
||||
assert.equal(h.layers[0], layer);
|
||||
assert.equal(layer.alpha, 0.25);
|
||||
h.imagery.setRaster(raster);
|
||||
assert.equal(h.layers[0].alpha, 0.25, 'a rebuilt layer keeps the fade');
|
||||
h.imagery.setOpacity(7);
|
||||
assert.equal(h.layers[0].alpha, 1);
|
||||
});
|
||||
|
||||
test('no imagery host keeps nothing draped; rehome brings it back', () => {
|
||||
const h = harness();
|
||||
h.setHost({ collection: null, kind: 'none' });
|
||||
h.imagery.setRaster(raster);
|
||||
assert.equal(h.layers.length, 0);
|
||||
h.setHost({ collection: h.collection, kind: 'globe' });
|
||||
h.imagery.rehome();
|
||||
assert.equal(h.layers.length, 1);
|
||||
});
|
||||
|
||||
test('destroy removes the layer', () => {
|
||||
const h = harness();
|
||||
h.imagery.setRaster(raster);
|
||||
h.imagery.destroy();
|
||||
assert.equal(h.layers.length, 0);
|
||||
});
|
||||
|
||||
test('the shipped raster paints a lone weak fire as a distinct spot', () => {
|
||||
// A hot cluster elsewhere sets the scale, as Africa does on real data.
|
||||
const fires = [{ lat: 0, lon: 0, frp: 5 }];
|
||||
for (let i = 0; i < 40; i++) {
|
||||
fires.push({ lat: -10 + (i % 5) * 0.1, lon: 25 + i * 0.05, frp: 300 });
|
||||
}
|
||||
const r = buildHeatRaster(fires, HEAT_RASTER);
|
||||
const y = Math.floor((90 / 180) * r.height);
|
||||
const painted = [];
|
||||
for (let x = 0; x < r.width; x++) {
|
||||
const lon = ((x + 0.5) / r.width) * 360 - 180;
|
||||
if (Math.abs(lon) < 5 && r.rgba[(y * r.width + x) * 4 + 3] > 0) {
|
||||
painted.push(x);
|
||||
}
|
||||
}
|
||||
const kmPerPx = 40075 / r.width;
|
||||
assert.ok(painted.length > 0, 'the weak fire still shows');
|
||||
assert.ok(
|
||||
painted.length * kmPerPx <= 100,
|
||||
`spot ${Math.round(painted.length * kmPerPx)} km wide`,
|
||||
);
|
||||
});
|
||||
@@ -23,21 +23,6 @@ export function createLifecycle({
|
||||
}
|
||||
|
||||
const methods = {
|
||||
/**
|
||||
* Shell services: `imageryHost()` resolves where draped imagery goes on
|
||||
* the active map stack (globe or 3D tiles, src/maps/imageryHost.js). The
|
||||
* heatmap moves with it; without a host it drapes on the globe's own
|
||||
* imagery (render/heatField.js).
|
||||
* @param {{imageryHost?: () => {collection: ?Object, kind: string}}|null} services
|
||||
*/
|
||||
attachShellServices(services) {
|
||||
layerState._imageryHost =
|
||||
typeof services?.imageryHost === 'function'
|
||||
? services.imageryHost
|
||||
: null;
|
||||
layerState._heatImagery?.rehome();
|
||||
},
|
||||
|
||||
init(viewer, options) {
|
||||
this.attachRenderer(viewer, options);
|
||||
},
|
||||
@@ -161,7 +146,6 @@ export function createLifecycle({
|
||||
layerState._cellCacheByGrid.clear();
|
||||
layerState.glowSpriteCache.clear();
|
||||
releaseHeat();
|
||||
layerState._imageryHost = null;
|
||||
layerState._count = 0;
|
||||
layerState._totalCount = 0;
|
||||
layerState._rowControlsListener = null;
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
// 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',
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -5,15 +5,17 @@
|
||||
// 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.
|
||||
// engine's (engine.camera, engine.projector);
|
||||
// - the far-zoom heatmap: one engine.imagery layer, the heat raster as RGBA
|
||||
// bytes over the whole globe, faded by its alpha for the hand-off.
|
||||
|
||||
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';
|
||||
|
||||
// Above imagery layers of the default order (a pinned Recent Imagery day).
|
||||
const HEAT_ORDER = 1;
|
||||
|
||||
const rgbaOf = (color) => [
|
||||
color?.red ?? 1,
|
||||
@@ -22,25 +24,89 @@ const rgbaOf = (color) => [
|
||||
color?.alpha ?? 1,
|
||||
];
|
||||
|
||||
/**
|
||||
* The heatmap on engine.imagery: setRaster (data/firmsHeatRaster.js's
|
||||
* equirectangular RGBA over the globe; a new one replaces the image in
|
||||
* place), hasRaster, setShow (the layer on or off), setOpacity (the
|
||||
* hand-off fade, 0..1: a faded layer stays on its surface, so it comes
|
||||
* back without a gap) and destroy. The engine layer is made with the first
|
||||
* raster.
|
||||
*/
|
||||
function createHeatImagery(engine, id, onDestroy) {
|
||||
let layer = null,
|
||||
raster = null,
|
||||
show = true,
|
||||
opacity = 1,
|
||||
destroyed = false;
|
||||
const heat = {
|
||||
setRaster(next) {
|
||||
if (destroyed) return;
|
||||
raster = next ?? null;
|
||||
if (!raster) {
|
||||
layer?.destroy();
|
||||
layer = null;
|
||||
return;
|
||||
}
|
||||
const source = {
|
||||
kind: 'image',
|
||||
image: {
|
||||
width: raster.width,
|
||||
height: raster.height,
|
||||
data: raster.rgba,
|
||||
},
|
||||
};
|
||||
if (layer) layer.setSource(source);
|
||||
else
|
||||
layer = engine.imagery.createLayer({
|
||||
id,
|
||||
source,
|
||||
alpha: opacity,
|
||||
show,
|
||||
order: HEAT_ORDER,
|
||||
});
|
||||
},
|
||||
/** True once a raster was handed over. */
|
||||
hasRaster: () => raster !== null,
|
||||
setShow(value) {
|
||||
if (destroyed) return;
|
||||
show = Boolean(value);
|
||||
layer?.show(show);
|
||||
},
|
||||
setOpacity(value) {
|
||||
if (destroyed) return;
|
||||
opacity = Math.min(1, Math.max(0, Number(value) || 0));
|
||||
layer?.setAlpha(opacity);
|
||||
},
|
||||
destroy() {
|
||||
if (destroyed) return;
|
||||
destroyed = true;
|
||||
layer?.destroy();
|
||||
layer = null;
|
||||
raster = null;
|
||||
onDestroy(heat);
|
||||
},
|
||||
};
|
||||
return heat;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
* @param {{ id?: string, loadImage?: Function, createCanvas?: Function }} [options]
|
||||
* `loadImage` and `createCanvas` are the sprite atlas's test seams.
|
||||
*/
|
||||
export function createFirmsDisplay(
|
||||
engine,
|
||||
{ id = 'local-firms', navaraHeat, loadImage, createCanvas } = {},
|
||||
{ id = 'local-firms', loadImage, createCanvas } = {},
|
||||
) {
|
||||
if (!engine?.movers || !engine?.ground)
|
||||
if (!engine?.movers || !engine?.ground || !engine?.imagery)
|
||||
throw new TypeError('The FIRMS display needs a globe engine');
|
||||
let destroyed = false,
|
||||
visible = false,
|
||||
cellItems = [],
|
||||
cellsQueued = false,
|
||||
sprites = null;
|
||||
sprites = null,
|
||||
heat = null;
|
||||
// The layer's visibility as the context store reads a data source's.
|
||||
const dataSource = { show: false };
|
||||
const clickRemovers = new Set();
|
||||
@@ -144,14 +210,16 @@ export function createFirmsDisplay(
|
||||
/** 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.
|
||||
* The far-zoom heatmap on this globe's imagery (createHeatImagery
|
||||
* above); one at a time, destroyed with the display.
|
||||
*/
|
||||
createHeatImagery(imageryHost) {
|
||||
return destroyed
|
||||
? null
|
||||
: createFirmsHeatField(engine, { imageryHost, navaraHeat });
|
||||
createHeatImagery() {
|
||||
if (destroyed) return null;
|
||||
heat?.destroy();
|
||||
heat = createHeatImagery(engine, `${id}:heat`, (gone) => {
|
||||
if (heat === gone) heat = null;
|
||||
});
|
||||
return heat;
|
||||
},
|
||||
/** The hand-off fade: every marker at `alpha`, shown while `show`. */
|
||||
fadeSprites(alpha, show) {
|
||||
@@ -165,6 +233,7 @@ export function createFirmsDisplay(
|
||||
cellItems = [];
|
||||
for (const release of [...clickRemovers]) release();
|
||||
removeSprites();
|
||||
heat?.destroy();
|
||||
cells.destroy();
|
||||
layerView.destroy();
|
||||
engine.render.request('firms-destroy');
|
||||
|
||||
@@ -509,110 +509,99 @@ test('the fake engine: clearing rows during a decode commits only the replacemen
|
||||
}
|
||||
});
|
||||
|
||||
test('the heatmap: the Cesium engine drapes one imagery layer on the viewer, the host when there is one', () => {
|
||||
const priorDocument = globalThis.document;
|
||||
// A 2D canvas for the Cesium engine's image upload (RGBA bytes go on one).
|
||||
function stubDocument() {
|
||||
const prior = globalThis.document;
|
||||
globalThis.document = {
|
||||
createElement: () => ({
|
||||
width: 0,
|
||||
height: 0,
|
||||
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;
|
||||
}
|
||||
});
|
||||
return () => {
|
||||
if (prior === undefined) delete globalThis.document;
|
||||
else globalThis.document = prior;
|
||||
};
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
});
|
||||
for (const make of HARNESSES) {
|
||||
const name = make().name;
|
||||
test(`${name}: the heatmap is one imagery layer over the globe, faded by its alpha`, () => {
|
||||
const restore = stubDocument();
|
||||
const h = make();
|
||||
// Cesium drapes it on the globe's own imagery stack.
|
||||
const stack = new Cesium.ImageryLayerCollection();
|
||||
if (h.viewer) {
|
||||
h.viewer.imageryLayers = stack;
|
||||
h.viewer.scene.globe.show = true;
|
||||
}
|
||||
try {
|
||||
const r = display(h);
|
||||
const heat = r.createHeatImagery();
|
||||
const listed = () => h.engine.imagery.list();
|
||||
assert.equal(heat.hasRaster(), false);
|
||||
assert.deepEqual(listed(), [], 'no layer before the first raster');
|
||||
heat.setOpacity(0);
|
||||
const raster = { width: 4, height: 2, rgba: new Uint8ClampedArray(32) };
|
||||
heat.setRaster(raster);
|
||||
assert.equal(heat.hasRaster(), true);
|
||||
assert.deepEqual(listed(), [
|
||||
{
|
||||
id: 'firms-fixture:heat',
|
||||
kind: 'image',
|
||||
shown: true,
|
||||
alpha: 0,
|
||||
split: 'none',
|
||||
order: 1,
|
||||
},
|
||||
]);
|
||||
if (h.viewer) {
|
||||
assert.equal(stack.length, 1, 'one imagery layer on the globe');
|
||||
assert.equal(stack.get(0).alpha, 0);
|
||||
assert.equal(stack.get(0).show, true, 'a faded layer stays mapped');
|
||||
} else {
|
||||
const drawn = h.engine.fake.imageryLayer('firms-fixture:heat');
|
||||
assert.equal(drawn.source.image.data, raster.rgba);
|
||||
assert.deepEqual(drawn.source.rectangle, {
|
||||
west: -180,
|
||||
south: -90,
|
||||
east: 180,
|
||||
north: 90,
|
||||
});
|
||||
}
|
||||
heat.setOpacity(7);
|
||||
heat.setShow(false);
|
||||
assert.deepEqual(
|
||||
[listed()[0].alpha, listed()[0].shown],
|
||||
[1, false],
|
||||
'the opacity clamps; show hides',
|
||||
);
|
||||
heat.setRaster({ ...raster, rgba: new Uint8ClampedArray(32) });
|
||||
assert.equal(listed().length, 1, 'a new raster replaces the image');
|
||||
assert.deepEqual([listed()[0].alpha, listed()[0].shown], [1, false]);
|
||||
if (h.viewer) {
|
||||
assert.equal(stack.length, 1);
|
||||
assert.equal(stack.get(0).show, false);
|
||||
}
|
||||
heat.destroy();
|
||||
heat.destroy();
|
||||
assert.deepEqual(listed(), []);
|
||||
if (h.viewer) assert.equal(stack.length, 0);
|
||||
// The display takes its heatmap with it.
|
||||
r.createHeatImagery().setRaster(raster);
|
||||
assert.equal(listed().length, 1);
|
||||
r.destroy();
|
||||
assert.deepEqual(listed(), []);
|
||||
assert.equal(r.createHeatImagery(), null);
|
||||
} finally {
|
||||
h.destroy();
|
||||
restore();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
// 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,184 +0,0 @@
|
||||
// The Active Fires far-zoom heatmap on Navara: the same equirectangular heat
|
||||
// raster the Cesium globe drapes as imagery (firmsHeatRaster.js), drawn over
|
||||
// visible terrain and 3D tiles by one non-pickable classification pass. Only
|
||||
// surface pixels take it, so the fire markers above stay untouched. The
|
||||
// adapter has heatImagery.js's interface, so the layer drives both alike.
|
||||
import {
|
||||
DataTexture,
|
||||
LinearFilter,
|
||||
Matrix4,
|
||||
RGBAFormat,
|
||||
RepeatWrapping,
|
||||
ClampToEdgeWrapping,
|
||||
Uniform,
|
||||
UnsignedByteType,
|
||||
} from 'three';
|
||||
import {
|
||||
groundSurfaceUniforms,
|
||||
updateGroundSurface,
|
||||
} from '../../shared/render/navaraGroundSurface.js';
|
||||
import { defineGroundEffect } from '../../shared/render/navaraGroundEffect.js';
|
||||
|
||||
const KEY = 'gevFirmsHeat';
|
||||
|
||||
// Row 0 of the raster is the north edge and column 0 is 180°W; the texture
|
||||
// keeps that order (no flip), so v = 0 is north.
|
||||
const fragmentShader = /* glsl */ `
|
||||
uniform sampler2D tAllDepth;
|
||||
uniform sampler2D tSurfaceDepth;
|
||||
uniform sampler2D tHeat;
|
||||
uniform mat4 uInverseProjection;
|
||||
uniform mat4 uInverseView;
|
||||
uniform float uLogDepthFC;
|
||||
uniform float uOpacity;
|
||||
// WGS84 first eccentricity squared: geodetic latitude of a near-surface point.
|
||||
const float E2=0.0066943799901413165;
|
||||
vec3 toDisplay(vec3 c) {
|
||||
return mix(12.92*c,1.055*pow(max(c,vec3(0.0)),vec3(1.0/2.4))-0.055,step(vec3(0.0031308),c));
|
||||
}
|
||||
vec3 toLinear(vec3 c) {
|
||||
return mix(c/12.92,pow(max((c+0.055)/1.055,vec3(0.0)),vec3(2.4)),step(vec3(0.04045),c));
|
||||
}
|
||||
void mainImage(const in vec4 inputColor,const in vec2 uv,out vec4 outputColor) {
|
||||
outputColor=inputColor;
|
||||
float depth=unpackRGBAToDepth(texture2D(tAllDepth,uv));
|
||||
if(depth>=1.0) return;
|
||||
float surface=texture2D(tSurfaceDepth,uv).r;
|
||||
if(depth<surface-2e-7) return;
|
||||
float distance=exp2(2.0*depth/uLogDepthFC)-1.0;
|
||||
vec4 ray=uInverseProjection*vec4(uv*2.0-1.0,-1.0,1.0);
|
||||
vec3 point=ray.xyz*(distance/-ray.z);
|
||||
vec3 world=(uInverseView*vec4(point,1.0)).xyz;
|
||||
float rho=length(world.xy);
|
||||
float longitude=atan(world.y,world.x);
|
||||
float latitude=atan(world.z,rho*(1.0-E2));
|
||||
// PI comes from three.js's shader chunks.
|
||||
vec2 st=vec2(longitude/(2.0*PI)+0.5,0.5-latitude/PI);
|
||||
vec4 heat=texture2D(tHeat,st);
|
||||
float alpha=heat.a*uOpacity;
|
||||
if(alpha<=0.0) return;
|
||||
vec3 color=mix(toDisplay(inputColor.rgb),heat.rgb,alpha);
|
||||
outputColor=vec4(toLinear(color),inputColor.a);
|
||||
}
|
||||
`;
|
||||
|
||||
const heatEffect = defineGroundEffect({
|
||||
key: KEY,
|
||||
name: 'GevFirmsHeat',
|
||||
fragmentShader,
|
||||
// Under the heat cells' and markers' place in the chain: after the other
|
||||
// surface drapes, before sharpening, anti-aliasing and overlays.
|
||||
insertAfter: [
|
||||
'gevEarthquakeGround',
|
||||
'gevObservedWeather',
|
||||
'gevNepalEvent',
|
||||
'gevRecentImagery',
|
||||
'toneMapping',
|
||||
],
|
||||
insertBefore: [
|
||||
'gevFirmsGround',
|
||||
'gevPreSharpenOverlay',
|
||||
'gevSharpen',
|
||||
'smaa',
|
||||
'fxaa',
|
||||
'gevOverlay',
|
||||
'final',
|
||||
],
|
||||
uniforms: () => [
|
||||
...groundSurfaceUniforms(),
|
||||
['tHeat', new Uniform(null)],
|
||||
['uOpacity', new Uniform(1)],
|
||||
['uInverseView', new Uniform(new Matrix4())],
|
||||
],
|
||||
update() {
|
||||
const owner = this.owner,
|
||||
camera = this.view.camera.raw;
|
||||
if (!this.effect || !owner.texture || !camera) return;
|
||||
updateGroundSurface(this.effect, camera, this.ctx, this.find('mrt'));
|
||||
const u = this.effect.uniforms;
|
||||
u.get('tHeat').value = owner.texture;
|
||||
u.get('uOpacity').value = owner.opacity;
|
||||
u.get('uInverseView').value.copy(camera.matrixWorld);
|
||||
},
|
||||
});
|
||||
|
||||
/**
|
||||
* The raster as a texture: linear between pixels, wrapping across 180°. Its
|
||||
* sRGB bytes are read as they are and blended in display space, as the heat
|
||||
* cells' colors are.
|
||||
*/
|
||||
function heatTexture(raster) {
|
||||
const texture = new DataTexture(
|
||||
raster.rgba,
|
||||
raster.width,
|
||||
raster.height,
|
||||
RGBAFormat,
|
||||
UnsignedByteType,
|
||||
);
|
||||
texture.minFilter = LinearFilter;
|
||||
texture.magFilter = LinearFilter;
|
||||
texture.wrapS = RepeatWrapping;
|
||||
texture.wrapT = ClampToEdgeWrapping;
|
||||
texture.generateMipmaps = false;
|
||||
texture.needsUpdate = true;
|
||||
return texture;
|
||||
}
|
||||
|
||||
/**
|
||||
* The heatmap drape on one Navara view, with heatImagery.js's interface:
|
||||
* setRaster, hasRaster, setShow, setOpacity (the hand-off fade), rehome and
|
||||
* destroy. Drawn only while shown, painted and above zero opacity.
|
||||
*/
|
||||
export function createFirmsHeatDrape(
|
||||
engine,
|
||||
{ view, EffectDesc, NavaraEffect },
|
||||
) {
|
||||
heatEffect.register(view, { EffectDesc, NavaraEffect });
|
||||
const owner = { texture: null, opacity: 1 };
|
||||
let raster = null,
|
||||
show = true,
|
||||
handle = null,
|
||||
destroyed = false;
|
||||
function sync() {
|
||||
if (destroyed) return;
|
||||
const visible = Boolean(owner.texture && show && owner.opacity > 0);
|
||||
if (!handle && visible)
|
||||
handle = view.addEffect({ [KEY]: { owner }, visible: true });
|
||||
else handle?.update({ visible });
|
||||
engine.render.request('firms-heat');
|
||||
}
|
||||
return {
|
||||
setRaster(next) {
|
||||
if (destroyed) return;
|
||||
raster = next;
|
||||
owner.texture?.dispose();
|
||||
owner.texture = next ? heatTexture(next) : null;
|
||||
sync();
|
||||
},
|
||||
/** True once a raster was handed over. */
|
||||
hasRaster() {
|
||||
return raster !== null;
|
||||
},
|
||||
setShow(value) {
|
||||
show = Boolean(value);
|
||||
sync();
|
||||
},
|
||||
/** Hand-off fade, 0..1. */
|
||||
setOpacity(value) {
|
||||
owner.opacity = Math.min(1, Math.max(0, Number(value) || 0));
|
||||
sync();
|
||||
},
|
||||
/** Navara drapes on its own surface; there is no imagery host to follow. */
|
||||
rehome() {},
|
||||
destroy() {
|
||||
if (destroyed) return;
|
||||
destroyed = true;
|
||||
handle?.delete();
|
||||
handle = null;
|
||||
owner.texture?.dispose();
|
||||
owner.texture = null;
|
||||
raster = null;
|
||||
engine.render.request('firms-heat-destroy');
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -1,80 +0,0 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createFirmsHeatDrape } from './navaraHeat.js';
|
||||
|
||||
function harness() {
|
||||
const effects = [];
|
||||
const requests = [];
|
||||
const view = {
|
||||
registered: [],
|
||||
registerEffect(key) {
|
||||
view.registered.push(key);
|
||||
},
|
||||
addEffect(config) {
|
||||
const handle = {
|
||||
config,
|
||||
visible: config.visible,
|
||||
deleted: false,
|
||||
update(next) {
|
||||
handle.visible = next.visible;
|
||||
},
|
||||
delete() {
|
||||
handle.deleted = true;
|
||||
},
|
||||
};
|
||||
effects.push(handle);
|
||||
return handle;
|
||||
},
|
||||
};
|
||||
const engine = { render: { request: (reason) => requests.push(reason) } };
|
||||
const drape = createFirmsHeatDrape(engine, {
|
||||
view,
|
||||
EffectDesc: class {},
|
||||
NavaraEffect: class {},
|
||||
});
|
||||
return { drape, view, effects, requests };
|
||||
}
|
||||
|
||||
const raster = (alpha) => ({
|
||||
width: 4,
|
||||
height: 2,
|
||||
rgba: new Uint8ClampedArray(4 * 2 * 4).fill(alpha),
|
||||
});
|
||||
|
||||
test('the heatmap drape draws only a painted, shown raster above zero opacity', () => {
|
||||
const h = harness();
|
||||
assert.deepEqual(h.view.registered, ['gevFirmsHeat']);
|
||||
assert.equal(h.drape.hasRaster(), false);
|
||||
assert.equal(h.effects.length, 0, 'nothing to draw before a raster');
|
||||
h.drape.setRaster(raster(200));
|
||||
assert.equal(h.drape.hasRaster(), true);
|
||||
assert.equal(h.effects.length, 1);
|
||||
const [effect] = h.effects;
|
||||
const owner = effect.config.gevFirmsHeat.owner;
|
||||
assert.equal(owner.texture.image.width, 4);
|
||||
assert.equal(owner.texture.image.height, 2);
|
||||
assert.equal(effect.visible, true);
|
||||
h.drape.setOpacity(0);
|
||||
assert.equal(effect.visible, false, 'faded out under the markers');
|
||||
h.drape.setOpacity(0.5);
|
||||
assert.equal(effect.visible, true);
|
||||
assert.equal(owner.opacity, 0.5);
|
||||
h.drape.setShow(false);
|
||||
assert.equal(effect.visible, false, 'the layer is off');
|
||||
h.drape.setShow(true);
|
||||
// A new raster replaces the texture in the same pass.
|
||||
const first = owner.texture;
|
||||
let disposed = false;
|
||||
first.dispose = () => (disposed = true);
|
||||
h.drape.setRaster(raster(100));
|
||||
assert.equal(disposed, true);
|
||||
assert.notEqual(owner.texture, first);
|
||||
assert.equal(h.effects.length, 1);
|
||||
assert.ok(h.requests.length > 0, 'every change asks for a frame');
|
||||
h.drape.destroy();
|
||||
assert.equal(effect.deleted, true);
|
||||
assert.equal(owner.texture, null);
|
||||
assert.equal(h.drape.hasRaster(), false);
|
||||
h.drape.setRaster(raster(50));
|
||||
assert.equal(h.effects.length, 1, 'a destroyed drape draws nothing again');
|
||||
});
|
||||
@@ -168,13 +168,11 @@ export function createRendering({
|
||||
return clipped;
|
||||
}
|
||||
|
||||
/** The renderer's heat imagery adapter, created on first use. */
|
||||
/** The renderer's heat imagery layer, created on first use. */
|
||||
|
||||
function heatImagery() {
|
||||
layerState._heatImagery ??=
|
||||
layerState._renderer?.createHeatImagery?.(
|
||||
() => layerState._imageryHost?.() ?? null,
|
||||
) ?? null;
|
||||
layerState._renderer?.createHeatImagery?.() ?? null;
|
||||
return layerState._heatImagery;
|
||||
}
|
||||
|
||||
@@ -210,15 +208,14 @@ export function createRendering({
|
||||
}
|
||||
|
||||
/**
|
||||
* Keep the heatmap draped on the current map stack and current with the
|
||||
* shown set in every band; the hand-off fade decides how much of it shows.
|
||||
* Keep the heatmap current with the shown set in every band (the engine
|
||||
* keeps it on the map's surface); the hand-off fade decides how much of
|
||||
* it shows.
|
||||
* @param {boolean} now The camera is in a far band.
|
||||
*/
|
||||
|
||||
function prepareHeat(now) {
|
||||
const imagery = heatImagery();
|
||||
// The map stack may have changed since the last render.
|
||||
imagery.rehome();
|
||||
ensureHeatRaster(now);
|
||||
imagery.setShow(layerState._enabled);
|
||||
}
|
||||
|
||||
@@ -148,14 +148,10 @@ export function createFirmsState({ services, config }) {
|
||||
|
||||
layerState._handoffHeat = -1;
|
||||
|
||||
/** Imagery adapter (heatImagery.js), created on the first far render. */
|
||||
/** The heatmap's imagery layer (render/display.js), made on the first far render. */
|
||||
|
||||
layerState._heatImagery = null;
|
||||
|
||||
/** Shell-provided imagery host resolver; falls back to viewer.imageryLayers. */
|
||||
|
||||
layerState._imageryHost = null;
|
||||
|
||||
/** Camera idle snapshot so the throttled LOD check is ~free when parked. */
|
||||
|
||||
layerState._camSnapValid = false;
|
||||
|
||||
Reference in new issue
Block a user