mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
Draw earthquakes through the renderer's draw port
The earthquake discs are now one draw set of ground-clamped filled ellipses per layer, with the same colours, alphas and radii; they had no visible outline before either (Cesium turns outlines off on clamped entity ellipses). The layer and its model no longer import Cesium and leave the allow-list. depthColor now returns a renderer colour [r, g, b, a] rather than a Cesium colour, and depthAccent its CSS. A caller handing the layer a Cesium viewer of their own, with no renderer bound, first loads the new ./render/cesium/unbound export, which provides every unbound factory; the camera factory now loads the draw factory too, so entries that already load it draw as well. A fake draw port for layer tests comes with it.
This commit is contained in:
1 parent
8b2859e501
commit
bb3106991e
16 files changed
+384
-149
No files matched your search
@@ -1,9 +1,11 @@
|
||||
import * as Cesium from 'cesium';
|
||||
import { fromDegrees } from '../../geo/ellipsoid.js';
|
||||
import { drawOf } from '../../render/rendererOf.js';
|
||||
import {
|
||||
EARTHQUAKE_OVERLAY_SOURCE_ID,
|
||||
EARTHQUAKE_OVERLAY_COHORT_LIMIT,
|
||||
EARTHQUAKE_OVERLAY_COLLISION_CAPACITY,
|
||||
depthColor,
|
||||
depthAccent,
|
||||
createEarthquakeOverlayEntry,
|
||||
selectEarthquakeOverlayCohort,
|
||||
mapAnalystRecord,
|
||||
@@ -15,15 +17,29 @@ export { createUsgsEarthquakeSource } from './source.js';
|
||||
// panel shows it translated.
|
||||
const msg = (text) => text;
|
||||
|
||||
/** Own one earthquake display and its refresh lifecycle. */
|
||||
/**
|
||||
* The draw sets the discs are drawn in start with this; each layer has its
|
||||
* own (`earthquakes:1`, …), so several can share a renderer. Picks name it
|
||||
* as `Hit.set`.
|
||||
*/
|
||||
export const EARTHQUAKE_DISC_SET_ID = 'earthquakes';
|
||||
let instances = 0;
|
||||
|
||||
/**
|
||||
* Own one earthquake display and its refresh lifecycle.
|
||||
*
|
||||
* The discs are one renderer draw set (`drawOf(viewer)`), made at `init`.
|
||||
* Given a Cesium viewer with no renderer bound, the
|
||||
* caller loads the package's `./render/cesium/unbound` export first.
|
||||
*/
|
||||
export function createEarthquakesLayer({ source, overlayHost } = {}) {
|
||||
if (typeof source?.getSnapshot !== 'function')
|
||||
throw new TypeError('Earthquakes require a snapshot source');
|
||||
if (!overlayHost) throw new TypeError('Earthquakes require an overlay host');
|
||||
let _viewer = null;
|
||||
let _request = null;
|
||||
let _dataSource = null;
|
||||
// What the discs were drawn from, so analyst reads come from the app, not the entities.
|
||||
let _discs = null;
|
||||
// What the discs were drawn from, so analyst reads come from the app, not the drawing.
|
||||
let _events = [];
|
||||
let _count = 0;
|
||||
let _lastUpdate = null;
|
||||
@@ -39,10 +55,11 @@ export function createEarthquakesLayer({ source, overlayHost } = {}) {
|
||||
|
||||
init(viewer) {
|
||||
if (_viewer) throw new Error('Earthquake layer is already initialized');
|
||||
_discs = drawOf(viewer).shapes({
|
||||
id: `${EARTHQUAKE_DISC_SET_ID}:${++instances}`,
|
||||
show: false,
|
||||
});
|
||||
_viewer = viewer;
|
||||
_dataSource = new Cesium.CustomDataSource('earthquakes');
|
||||
_dataSource.show = false;
|
||||
viewer.dataSources.add(_dataSource);
|
||||
_count = 0;
|
||||
_lastUpdate = null;
|
||||
_lastError = null;
|
||||
@@ -55,7 +72,7 @@ export function createEarthquakesLayer({ source, overlayHost } = {}) {
|
||||
_enabled = true;
|
||||
// No continuous-render hold: the discs are static geometry now, so the
|
||||
// layer has no per-frame animator to keep the render loop alive for.
|
||||
if (_dataSource) _dataSource.show = true;
|
||||
if (_discs) _discs.show = true;
|
||||
overlayHost.setVisible(EARTHQUAKE_OVERLAY_SOURCE_ID, true);
|
||||
},
|
||||
|
||||
@@ -63,13 +80,13 @@ export function createEarthquakesLayer({ source, overlayHost } = {}) {
|
||||
_request?.abort();
|
||||
_request = null;
|
||||
_enabled = false;
|
||||
if (_dataSource) _dataSource.show = false;
|
||||
if (_discs) _discs.show = false;
|
||||
overlayHost.clearSource(EARTHQUAKE_OVERLAY_SOURCE_ID);
|
||||
overlayHost.setVisible(EARTHQUAKE_OVERLAY_SOURCE_ID, false);
|
||||
},
|
||||
|
||||
async update(viewer) {
|
||||
if (!_enabled || !_dataSource) return false;
|
||||
if (!_enabled || !_discs) return false;
|
||||
_request?.abort();
|
||||
const request = new AbortController();
|
||||
_request = request;
|
||||
@@ -78,7 +95,7 @@ export function createEarthquakesLayer({ source, overlayHost } = {}) {
|
||||
if (request.signal.aborted || _request !== request || !_enabled)
|
||||
return false;
|
||||
|
||||
const nextEntities = [];
|
||||
const nextDiscs = [];
|
||||
const nextEvents = [];
|
||||
let count = 0;
|
||||
const overlayEntries = [];
|
||||
@@ -96,43 +113,38 @@ export function createEarthquakesLayer({ source, overlayHost } = {}) {
|
||||
count++;
|
||||
const baseRadius = Math.pow(2, mag) * 1000;
|
||||
const color = depthColor(depthKm || 0);
|
||||
const [r, g, b] = color;
|
||||
const isSignificant = mag >= 5.0;
|
||||
const fillAlpha = isSignificant ? 0.4 : 0.3;
|
||||
const outlineAlpha = isSignificant ? 1.0 : 0.8;
|
||||
|
||||
const position = Cesium.Cartesian3.fromDegrees(lon, lat);
|
||||
const position = fromDegrees(lon, lat);
|
||||
nextEvents.push({ usgsId, mag, place, time, depthKm, lon, lat });
|
||||
nextEntities.push(
|
||||
new Cesium.Entity({
|
||||
id: `earthquake:${stableId}`,
|
||||
position,
|
||||
ellipse: {
|
||||
// Static axes — see the module header. A CallbackProperty here
|
||||
// re-tessellates the clamped ground geometry every frame.
|
||||
semiMajorAxis: baseRadius,
|
||||
semiMinorAxis: baseRadius,
|
||||
material: new Cesium.ColorMaterialProperty(
|
||||
color.withAlpha(fillAlpha),
|
||||
),
|
||||
outline: true,
|
||||
outlineColor: color.withAlpha(outlineAlpha),
|
||||
outlineWidth: isSignificant ? 3 : 2,
|
||||
heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
|
||||
},
|
||||
}),
|
||||
);
|
||||
// A filled disc on the ground, with static axes: re-tessellating
|
||||
// clamped ground geometry every frame is what once cost 30 fps.
|
||||
// No outline: clamped to the ground, Cesium never drew one.
|
||||
nextDiscs.push({
|
||||
id: `earthquake:${stableId}`,
|
||||
geometry: {
|
||||
kind: 'ellipse',
|
||||
center: { lon, lat },
|
||||
semiMajor: baseRadius,
|
||||
semiMinor: baseRadius,
|
||||
},
|
||||
fill: [r, g, b, fillAlpha],
|
||||
height: 'clampGround',
|
||||
clampTo: 'both',
|
||||
});
|
||||
overlayEntries.push(
|
||||
createEarthquakeOverlayEntry({
|
||||
id: String(stableId),
|
||||
position,
|
||||
magnitude: mag,
|
||||
accent: color.toCssColorString(),
|
||||
accent: depthAccent(depthKm || 0),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
_dataSource.entities.removeAll();
|
||||
for (const entity of nextEntities) _dataSource.entities.add(entity);
|
||||
_discs.set(nextDiscs);
|
||||
_events = nextEvents;
|
||||
if (_enabled) {
|
||||
overlayHost.setEntries(
|
||||
@@ -169,10 +181,8 @@ export function createEarthquakesLayer({ source, overlayHost } = {}) {
|
||||
_enabled = false;
|
||||
overlayHost.clearSource(EARTHQUAKE_OVERLAY_SOURCE_ID);
|
||||
overlayHost.setVisible(EARTHQUAKE_OVERLAY_SOURCE_ID, false);
|
||||
if (_dataSource) {
|
||||
viewer.dataSources.remove(_dataSource, true);
|
||||
_dataSource = null;
|
||||
}
|
||||
_discs?.dispose();
|
||||
_discs = null;
|
||||
_events = [];
|
||||
_count = 0;
|
||||
_lastUpdate = null;
|
||||
@@ -188,7 +198,7 @@ export function createEarthquakesLayer({ source, overlayHost } = {}) {
|
||||
* @returns {Array<Object>} See mapAnalystRecord for the record shape.
|
||||
*/
|
||||
getAnalystRecords(maxCount = 2000) {
|
||||
if (!_dataSource || !_enabled || !_events.length) return [];
|
||||
if (!_discs || !_enabled || !_events.length) return [];
|
||||
const limit = Number.isFinite(maxCount)
|
||||
? Math.max(1, Math.floor(maxCount))
|
||||
: 2000;
|
||||
|
||||
@@ -1,27 +1,40 @@
|
||||
import * as Cesium from 'cesium';
|
||||
export const EARTHQUAKE_OVERLAY_SOURCE_ID = 'earthquakes';
|
||||
export const EARTHQUAKE_OVERLAY_COHORT_LIMIT = 96;
|
||||
export const EARTHQUAKE_OVERLAY_COLLISION_CAPACITY = 48;
|
||||
|
||||
// The depth bands' colours: CSS red, orange and yellow, as the renderer's
|
||||
// colour tuples (`[r, g, b, a]`, 0..1) and as CSS for the labels' accent.
|
||||
const SHALLOW = Object.freeze({ rgba: [1, 0, 0, 1], css: 'rgb(255,0,0)' });
|
||||
const INTERMEDIATE = Object.freeze({
|
||||
rgba: [1, 165 / 255, 0, 1],
|
||||
css: 'rgb(255,165,0)',
|
||||
});
|
||||
const DEEP = Object.freeze({ rgba: [1, 1, 0, 1], css: 'rgb(255,255,0)' });
|
||||
|
||||
const depthBand = (depthKm) =>
|
||||
depthKm < 70 ? SHALLOW : depthKm < 300 ? INTERMEDIATE : DEEP;
|
||||
|
||||
/**
|
||||
* Color by depth:
|
||||
* Color by depth, as a renderer colour `[r, g, b, a]` (0..1):
|
||||
* - Shallow (<70km): Red
|
||||
* - Intermediate (70-300km): Orange
|
||||
* - Deep (>300km): Yellow
|
||||
*/
|
||||
export function depthColor(depthKm) {
|
||||
if (depthKm < 70) return Cesium.Color.RED;
|
||||
if (depthKm < 300) return Cesium.Color.ORANGE;
|
||||
return Cesium.Color.YELLOW;
|
||||
return [...depthBand(depthKm).rgba];
|
||||
}
|
||||
|
||||
/** The depth band's colour as CSS, for the magnitude label's accent. */
|
||||
export const depthAccent = (depthKm) => depthBand(depthKm).css;
|
||||
|
||||
/**
|
||||
* Build the source-owned presentation for one ambient magnitude label.
|
||||
* Magnitude formatting deliberately remains here instead of moving into the
|
||||
* shared renderer.
|
||||
* @param {object} input
|
||||
* @param {string} input.id Stable USGS or deterministic fallback id.
|
||||
* @param {Cesium.Cartesian3} input.position Ground anchor shared with the pulse.
|
||||
* @param {{x: number, y: number, z: number}} input.position ECEF ground
|
||||
* anchor, shared with the disc.
|
||||
* @param {number} input.magnitude USGS magnitude.
|
||||
* @param {string} input.accent Source-owned depth-band color.
|
||||
* @returns {object}
|
||||
|
||||
@@ -2,19 +2,11 @@ import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import { createEarthquakesLayer } from './index.js';
|
||||
import { createUsgsEarthquakeSource } from './source.js';
|
||||
import { bindFakeDraw } from '../../testSupport/fakeDrawPort.mjs';
|
||||
function harness(source) {
|
||||
const sources = [];
|
||||
const events = [];
|
||||
const viewer = {
|
||||
dataSources: {
|
||||
add(value) {
|
||||
sources.push(value);
|
||||
},
|
||||
remove(value) {
|
||||
sources.splice(sources.indexOf(value), 1);
|
||||
},
|
||||
},
|
||||
};
|
||||
const viewer = {};
|
||||
const { draw } = bindFakeDraw(viewer);
|
||||
const layer = createEarthquakesLayer({
|
||||
source,
|
||||
overlayHost: {
|
||||
@@ -27,7 +19,7 @@ function harness(source) {
|
||||
});
|
||||
layer.init(viewer);
|
||||
layer.enable(viewer);
|
||||
return { layer, viewer, sources, events };
|
||||
return { layer, viewer, draw, events };
|
||||
}
|
||||
const row = {
|
||||
stableId: 'event-a',
|
||||
@@ -61,17 +53,19 @@ test('late refresh cannot publish after disable, re-enable, or destroy', async (
|
||||
h.layer.destroy(h.viewer);
|
||||
}
|
||||
});
|
||||
test('two displays own separate data sources and destruction', async () => {
|
||||
test('two displays own separate draw sets and destruction', async () => {
|
||||
const a = harness({ getSnapshot: async () => [row] });
|
||||
const b = harness({ getSnapshot: async () => [] });
|
||||
await a.layer.update(a.viewer);
|
||||
await b.layer.update(b.viewer);
|
||||
assert.equal(a.layer.getStats().count, 1);
|
||||
assert.equal(b.layer.getStats().count, 0);
|
||||
assert.equal(a.draw.find('earthquakes:').items.size, 1);
|
||||
a.layer.destroy();
|
||||
assert.equal(a.sources.length, 0);
|
||||
assert.equal(b.sources.length, 1);
|
||||
assert.equal(a.draw.find('earthquakes:'), undefined, 'disposed');
|
||||
assert.equal(b.draw.find('earthquakes:').items.size, 0);
|
||||
b.layer.destroy();
|
||||
assert.equal(b.draw.find('earthquakes:'), undefined);
|
||||
});
|
||||
test('USGS body completion honors cancellation even with an uncooperative transport', async () => {
|
||||
const abort = new AbortController();
|
||||
@@ -117,9 +111,60 @@ test('analyst records come from the events the layer drew, and only while it is
|
||||
},
|
||||
]);
|
||||
assert.equal(h.layer.getAnalystRecords(1).length, 1);
|
||||
const [entity] = h.sources[0].entities.values;
|
||||
assert.equal(entity.properties, undefined, 'the entity carries drawing only');
|
||||
const [disc] = h.draw.find('earthquakes:').items.values();
|
||||
assert.equal(disc.meta, undefined, 'the disc carries drawing only');
|
||||
h.layer.disable(h.viewer);
|
||||
assert.deepEqual(h.layer.getAnalystRecords(), []);
|
||||
h.layer.destroy(h.viewer);
|
||||
});
|
||||
test('two layers share one renderer, each with its own disc set', async () => {
|
||||
const events = [];
|
||||
const overlayHost = {
|
||||
setEntries: (...args) => events.push(args),
|
||||
setVisible() {},
|
||||
clearSource() {},
|
||||
};
|
||||
const viewer = {};
|
||||
const { draw } = bindFakeDraw(viewer);
|
||||
const a = createEarthquakesLayer({
|
||||
source: { getSnapshot: async () => [row] },
|
||||
overlayHost,
|
||||
});
|
||||
const b = createEarthquakesLayer({
|
||||
source: { getSnapshot: async () => [] },
|
||||
overlayHost,
|
||||
});
|
||||
a.init(viewer);
|
||||
b.init(viewer);
|
||||
a.enable(viewer);
|
||||
b.enable(viewer);
|
||||
await a.update(viewer);
|
||||
await b.update(viewer);
|
||||
const live = [...draw.sets.values()].filter((set) => !set.disposed);
|
||||
assert.equal(live.length, 2);
|
||||
assert.deepEqual(
|
||||
live.map((set) => set.items.size),
|
||||
[1, 0],
|
||||
);
|
||||
a.destroy();
|
||||
a.init(viewer);
|
||||
assert.equal(
|
||||
[...draw.sets.values()].filter((set) => !set.disposed).length,
|
||||
2,
|
||||
'destroyed, then started again',
|
||||
);
|
||||
a.destroy();
|
||||
b.destroy();
|
||||
});
|
||||
test('a viewer with nothing to draw with is refused at init, and init can be retried', () => {
|
||||
const layer = createEarthquakesLayer({
|
||||
source: { getSnapshot: async () => [] },
|
||||
overlayHost: { setEntries() {}, setVisible() {}, clearSource() {} },
|
||||
});
|
||||
const viewer = {};
|
||||
assert.throws(() => layer.init(viewer), /no renderer to draw with/);
|
||||
const { unbind } = bindFakeDraw(viewer);
|
||||
layer.init(viewer);
|
||||
layer.destroy();
|
||||
unbind();
|
||||
});
|
||||
Reference in new issue
Block a user