mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-19 01:32:19 +00:00
Drape recent imagery through the renderer's globe port
The two recent imagery slots now add their GIBS layers through the globe port and share their six-request budget through its admission hook. The port moves the layers between the globe and the base tileset, which the renderer now marks as a base when it is the host. The renderer stays the layer's Cesium side: it still loads the camera factory, and now the globe factory too. The weather and wind entries load the globe factory as well, and the factory check counts callers that take findGlobe as a default.
This commit is contained in:
1 parent
76880f6eae
commit
b1733fa41a
9 files changed
+243
-250
No files matched your search
@@ -1,26 +1,26 @@
|
||||
/**
|
||||
* Draped-imagery renderer for the Recent Imagery layer: at most two GIBS
|
||||
* imagery layers (slot `a`, left of a swipe; slot `b`, right) on whichever
|
||||
* host the map stack offers. Against the basemap slot `a` splits left and
|
||||
* slot `b` stays empty. Every layer is removed from the collection that owns it, so a
|
||||
* host switch or a replaced image never strands one. Tile requests pass a
|
||||
* small semaphore of our own; Cesium's global request limits stay untouched.
|
||||
* imagery layers (slot `a`, left of a swipe; slot `b`, right) through the
|
||||
* renderer's globe port, draped on whichever surface the map stack draws.
|
||||
* Against the basemap slot `a` splits left and slot `b` stays empty. Tile
|
||||
* requests pass a small semaphore of our own, shared by both slots, through
|
||||
* each layer's admission hook; the renderer's own request limits stay
|
||||
* untouched.
|
||||
*/
|
||||
import * as Cesium from 'cesium';
|
||||
import { withTileRequestHook } from '../../render/cesium/tileRequests.js';
|
||||
// The layer is Cesium-free and flies through `cameraOf`; its Cesium renderer
|
||||
// brings the factory a viewer with no renderer bound gets a camera from.
|
||||
// The layer flies through `cameraOf` and drapes through `findGlobe`; this
|
||||
// renderer is its Cesium side, so it brings the factories a viewer with no
|
||||
// renderer bound gets those ports from, and marks a tileset host as a base
|
||||
// the globe port drapes on.
|
||||
import '../../render/cesium/camera.js';
|
||||
import { markBaseTileset } from '../../render/cesium/globe.js';
|
||||
import { PRODUCTS, gibsTemplate } from './model.js';
|
||||
|
||||
const SLOT_IDS = ['a', 'b'];
|
||||
const SPLIT_ENUM = { left: 'LEFT', right: 'RIGHT', none: 'NONE' };
|
||||
const NO_HOST = Object.freeze({ collection: null, kind: 'none' });
|
||||
const SPLITS = new Set(['left', 'right', 'none']);
|
||||
const NO_HOST = Object.freeze({ collection: null, kind: 'none', globe: null });
|
||||
|
||||
/** Render reason for a deferred or settled tile request. */
|
||||
/** Render reason for a settled tile request. */
|
||||
export const TILE_RENDER_REASON = 'recent-imagery-tiles';
|
||||
/** Delay before the one frame that retries tiles Cesium itself deferred. */
|
||||
export const TILE_RETRY_DELAY_MS = 250;
|
||||
|
||||
function clampAlpha(value) {
|
||||
const alpha = Number(value);
|
||||
@@ -31,129 +31,90 @@ const boxKey = (box) =>
|
||||
[box.west, box.south, box.east, box.north].map(Number).join(',');
|
||||
|
||||
/**
|
||||
* Gate a provider's tile requests through the semaphore. Returning
|
||||
* `undefined` tells Cesium to retry on a later frame, but under
|
||||
* `requestRenderMode` there is no later frame unless something asks — and a
|
||||
* deferral must never ask for one itself: the retried tile would defer again
|
||||
* and ask again, keeping the scene rendering every frame. So a request that
|
||||
* finds our semaphore full asks for nothing (the request holding the slot
|
||||
* asks for one frame when it settles), and a request the provider itself
|
||||
* deferred goes through the one coalesced delayed retry.
|
||||
* Admit a tile request while the semaphore has room. A request that finds
|
||||
* it full is deferred and asks for no frame itself: the request holding the
|
||||
* slot asks for one as it settles. One the renderer itself defers frees its
|
||||
* slot at once, and the renderer asks again on a later frame.
|
||||
*/
|
||||
function admitTileRequests(provider, admission, requestRender, retry) {
|
||||
return withTileRequestHook(provider, {
|
||||
requestImage: (_tile, issue) => {
|
||||
if (admission.inFlight >= admission.limit) return undefined;
|
||||
admission.inFlight += 1;
|
||||
const release = () => {
|
||||
admission.inFlight -= 1;
|
||||
};
|
||||
let promise;
|
||||
try {
|
||||
promise = issue();
|
||||
} catch (error) {
|
||||
release();
|
||||
throw error;
|
||||
}
|
||||
if (!promise) {
|
||||
release();
|
||||
retry.schedule();
|
||||
return undefined;
|
||||
}
|
||||
return Promise.resolve(promise).finally(() => {
|
||||
release();
|
||||
requestRender(TILE_RENDER_REASON);
|
||||
});
|
||||
},
|
||||
});
|
||||
function admitTileRequests(admission, requestRender) {
|
||||
return (_tile, issue) => {
|
||||
if (admission.inFlight >= admission.limit) return undefined;
|
||||
admission.inFlight += 1;
|
||||
const release = () => {
|
||||
admission.inFlight -= 1;
|
||||
};
|
||||
let promise;
|
||||
try {
|
||||
promise = issue();
|
||||
} catch (error) {
|
||||
release();
|
||||
throw error;
|
||||
}
|
||||
if (!promise) {
|
||||
release();
|
||||
return undefined;
|
||||
}
|
||||
return Promise.resolve(promise).finally(() => {
|
||||
release();
|
||||
requestRender(TILE_RENDER_REASON);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the renderer.
|
||||
* @param {{ cesium?: object, maxTileRequests?: number, requestRender?: (reason: string) => void, setTimeoutImpl?: Function, clearTimeoutImpl?: Function }} [options]
|
||||
* Create the renderer. The host it is bound to (`rebind`) carries the
|
||||
* viewer's globe port.
|
||||
* @param {{ tileTemplate?: Function, credit?: string, maxTileRequests?: number, requestRender?: (reason: string) => void }} [options]
|
||||
*/
|
||||
export function createRecentImageryRenderer({
|
||||
cesium = Cesium,
|
||||
tileTemplate = gibsTemplate,
|
||||
credit = 'NASA GIBS',
|
||||
maxTileRequests = 6,
|
||||
requestRender = () => {},
|
||||
setTimeoutImpl = (fn, ms) => globalThis.setTimeout(fn, ms),
|
||||
clearTimeoutImpl = (id) => globalThis.clearTimeout(id),
|
||||
} = {}) {
|
||||
let host = NO_HOST;
|
||||
let destroyed = false;
|
||||
const admission = { limit: maxTileRequests, inFlight: 0 };
|
||||
/** @type {Record<'a'|'b', null | { layer: object, collection: object, candidate: object, box: object, alpha: number, split: string }>} */
|
||||
/** @type {Record<'a'|'b', null | { layer: object, globe: object, candidate: object, box: object, alpha: number, split: string }>} */
|
||||
const owned = { a: null, b: null };
|
||||
const admit = admitTileRequests(admission, requestRender);
|
||||
|
||||
// One timer for both slots: any number of upstream deferrals while it is
|
||||
// armed share the same single retry frame.
|
||||
let retryTimer = null;
|
||||
const retry = {
|
||||
schedule() {
|
||||
if (retryTimer !== null) return;
|
||||
retryTimer = setTimeoutImpl(() => {
|
||||
retryTimer = null;
|
||||
if (!destroyed) requestRender(TILE_RENDER_REASON);
|
||||
}, TILE_RETRY_DELAY_MS);
|
||||
},
|
||||
cancel() {
|
||||
if (retryTimer !== null) clearTimeoutImpl(retryTimer);
|
||||
retryTimer = null;
|
||||
},
|
||||
};
|
||||
|
||||
function createProvider(candidate, box) {
|
||||
const spec = PRODUCTS[candidate.product];
|
||||
const provider = new cesium.UrlTemplateImageryProvider({
|
||||
function specFor(candidate, box) {
|
||||
const product = PRODUCTS[candidate.product];
|
||||
return {
|
||||
kind: 'xyz',
|
||||
url: tileTemplate(candidate.product, candidate.day),
|
||||
subdomains: ['a', 'b', 'c'],
|
||||
minimumLevel: 0,
|
||||
maximumLevel: spec.maxLevel,
|
||||
tilingScheme: new cesium.WebMercatorTilingScheme(),
|
||||
rectangle: cesium.Rectangle.fromDegrees(
|
||||
box.west,
|
||||
box.south,
|
||||
box.east,
|
||||
box.north,
|
||||
),
|
||||
credit,
|
||||
hasAlphaChannel: spec.format === 'png',
|
||||
});
|
||||
return admitTileRequests(provider, admission, requestRender, retry);
|
||||
tiling: 'webMercator',
|
||||
minLevel: 0,
|
||||
maxLevel: product.maxLevel,
|
||||
rectangle: {
|
||||
west: box.west,
|
||||
south: box.south,
|
||||
east: box.east,
|
||||
north: box.north,
|
||||
},
|
||||
credit: { text: credit },
|
||||
admit,
|
||||
};
|
||||
}
|
||||
|
||||
function removeOwned(slotId) {
|
||||
const record = owned[slotId];
|
||||
if (!record) return false;
|
||||
owned[slotId] = null;
|
||||
try {
|
||||
record.collection.remove(record.layer, true);
|
||||
} catch {
|
||||
/* the collection is already gone */
|
||||
}
|
||||
record.layer.remove();
|
||||
return true;
|
||||
}
|
||||
|
||||
function applyLook(record) {
|
||||
record.layer.alpha = record.alpha;
|
||||
record.layer.splitDirection =
|
||||
cesium.SplitDirection?.[SPLIT_ENUM[record.split] || 'NONE'] ?? 0;
|
||||
record.layer.split = SPLITS.has(record.split) ? record.split : 'none';
|
||||
}
|
||||
|
||||
function mount(slotId, { candidate, box, alpha, split }) {
|
||||
const layer = host.collection.addImageryProvider(
|
||||
createProvider(candidate, box),
|
||||
);
|
||||
owned[slotId] = {
|
||||
layer,
|
||||
collection: host.collection,
|
||||
candidate,
|
||||
box,
|
||||
alpha,
|
||||
split,
|
||||
};
|
||||
const layer = host.globe.imagery.add(specFor(candidate, box));
|
||||
owned[slotId] = { layer, globe: host.globe, candidate, box, alpha, split };
|
||||
applyLook(owned[slotId]);
|
||||
}
|
||||
|
||||
@@ -174,7 +135,7 @@ export function createRecentImageryRenderer({
|
||||
{ alpha = 1, splitDirection = 'none' } = {},
|
||||
) {
|
||||
if (destroyed || !candidate?.key || !box) return false;
|
||||
if (!host.collection) {
|
||||
if (!host.globe) {
|
||||
removeOwned(slotId);
|
||||
return false;
|
||||
}
|
||||
@@ -182,7 +143,7 @@ export function createRecentImageryRenderer({
|
||||
const look = { alpha: clampAlpha(alpha), split: splitDirection };
|
||||
if (
|
||||
current &&
|
||||
current.collection === host.collection &&
|
||||
current.globe === host.globe &&
|
||||
current.candidate.key === candidate.key &&
|
||||
boxKey(current.box) === boxKey(box)
|
||||
) {
|
||||
@@ -218,19 +179,25 @@ export function createRecentImageryRenderer({
|
||||
},
|
||||
|
||||
/**
|
||||
* Move every owned layer to a new host. Layers are rebuilt there, never
|
||||
* re-parented, so the old collection is left as it was found.
|
||||
* @param {{ collection: object | null, kind: string }} nextHost
|
||||
* Follow the map stack's host. The globe port moves the owned layers
|
||||
* between the globe and the 3D tiles itself; without a host they go,
|
||||
* and a new globe port gets them rebuilt.
|
||||
* @param {{ collection: object | null, kind: string, globe?: object | null, tileset?: object | null }} nextHost
|
||||
*/
|
||||
rebind(nextHost) {
|
||||
host = nextHost || NO_HOST;
|
||||
let changed = false;
|
||||
const previous = host;
|
||||
host =
|
||||
nextHost && nextHost.kind !== 'none' && nextHost.globe
|
||||
? nextHost
|
||||
: NO_HOST;
|
||||
if (host.kind === 'tileset') markBaseTileset(host.tileset);
|
||||
let changed = host.kind !== previous.kind && Boolean(owned.a || owned.b);
|
||||
for (const slotId of SLOT_IDS) {
|
||||
const record = owned[slotId];
|
||||
if (!record || record.collection === host.collection) continue;
|
||||
if (!record || record.globe === host.globe) continue;
|
||||
removeOwned(slotId);
|
||||
changed = true;
|
||||
if (host.collection && !destroyed) mount(slotId, record);
|
||||
if (host.globe && !destroyed) mount(slotId, record);
|
||||
}
|
||||
if (changed) requestRender('recent-imagery-rebind');
|
||||
},
|
||||
@@ -261,7 +228,6 @@ export function createRecentImageryRenderer({
|
||||
destroy() {
|
||||
if (destroyed) return;
|
||||
destroyed = true;
|
||||
retry.cancel();
|
||||
let changed = false;
|
||||
for (const slotId of SLOT_IDS) changed = removeOwned(slotId) || changed;
|
||||
host = NO_HOST;
|
||||
|
||||
Reference in new issue
Block a user