mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
Start the application through the contract, not on the Cesium viewer
The application scene takes a renderer plugin and no longer refuses one without a Cesium viewer. It loads the default (Cesium) plugin only when it is given none, so a page on the stub never loads Cesium. - Every layer and tool is handed the application's own scene object (createSceneHost): the container and isDestroyed(), bound to the renderer, the same on Cesium and the stub. - What only Cesium needs at startup is done in its plugin's create: the free-look height limit, and keyboard access to its credits' expand link (creditKeyboard.js moves into the adapter). - What a plugin may hand the application beside the contract goes through provideApplicationHooks, each hook optional: Cesium's map engine, stale command trimming, inline panel rendering, viewerDrawsEntity, the zoom-out guard, and the viewer QA scripts read from window.__godsEyeView. - The map stacks, and the photoreal base the scene loads, switch through the renderer's own map engine (mapEngineFor), else its globe port (globeEngine.js). The Cesium engine takes the scene object and changes the viewer behind it.
This commit is contained in:
1 parent
a72d4c3b1f
commit
6daf63d90c
24 files changed
+825
-241
No files matched your search
+60
-46
@@ -1,17 +1,7 @@
|
||||
import { createApplicationOperations } from './operations.js';
|
||||
// The application's composition root: it picks the Cesium renderer, and
|
||||
// this is the only part of the adapter it imports.
|
||||
import {
|
||||
applicationViewerHooks,
|
||||
cesiumRenderer,
|
||||
cesiumViewerOf,
|
||||
loadApplicationPhotoreal,
|
||||
releaseApplicationPhotoreal,
|
||||
showApplicationPhotoreal,
|
||||
} from '../render/cesium/application.js';
|
||||
import { registerDataCredits } from '../data/dataCredits.js';
|
||||
import { configureCreditKeyboardAccess } from '../creditKeyboard.js';
|
||||
import { MapStackController } from '../mapStackController.js';
|
||||
import { mapEngineFor } from '../maps/engine.js';
|
||||
import { initLogoGaze } from '../logoGaze.js';
|
||||
import { inputFor } from '../input/dispatcher.js';
|
||||
import {
|
||||
@@ -21,19 +11,32 @@ import {
|
||||
governorRequestRender,
|
||||
} from '../renderGovernor.js';
|
||||
import { describeError } from './errors.js';
|
||||
import { FREE_LOOK_MAX_HEIGHT_METERS } from '../globeInView.js';
|
||||
import { worldClock, worldTimeFor } from '../time/worldClock.js';
|
||||
import { followWorldTime } from '../time/viewerClock.js';
|
||||
import { onLocaleChange, t } from '../i18n/index.js';
|
||||
import { t } from '../i18n/index.js';
|
||||
import { applicationHooksOf } from '../render/applicationHooks.js';
|
||||
import { createSceneHost } from '../render/rendererOf.js';
|
||||
|
||||
/**
|
||||
* The application's composition root picks the renderer: the one given, else
|
||||
* the default (Cesium), loaded only then, so a page on the stub never
|
||||
* loads it. This is the only part of an adapter the application imports.
|
||||
*/
|
||||
async function defaultRenderer() {
|
||||
const { cesiumRenderer } = await import('../render/cesium/application.js');
|
||||
return cesiumRenderer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct the application globe using the caller's local configuration.
|
||||
* `renderer` is the `RendererPlugin` that draws it (default: Cesium). The
|
||||
* application still runs on the Cesium viewer until its layers are ported,
|
||||
* so a plugin whose renderer has none is refused.
|
||||
* application runs on a renderer with only the contract's required members:
|
||||
* what a renderer's plugin hands it beside the contract
|
||||
* (`src/render/applicationHooks.js`) it uses where given, and does without
|
||||
* where not.
|
||||
*/
|
||||
export async function createApplicationScene({
|
||||
renderer: rendererPlugin = cesiumRenderer,
|
||||
renderer: givenPlugin,
|
||||
requestServices,
|
||||
googleApiKey,
|
||||
googleTokens = null,
|
||||
@@ -60,23 +63,23 @@ export async function createApplicationScene({
|
||||
});
|
||||
}
|
||||
loaderStatus.textContent = t('Configuring viewer...');
|
||||
// The scene still loads the tiles, so their credentials stay here.
|
||||
const rendererPlugin = givenPlugin ?? (await defaultRenderer());
|
||||
signal.throwIfAborted();
|
||||
const container = document.getElementById('cesiumContainer');
|
||||
const renderer = await rendererPlugin.create({
|
||||
container: document.getElementById('cesiumContainer'),
|
||||
container,
|
||||
googleApiKey: googleApiKey || undefined,
|
||||
googleTokens: googleTokens
|
||||
? (options) => googleTokens.token(options)
|
||||
: undefined,
|
||||
worldDataToken: cesiumToken || undefined,
|
||||
signal,
|
||||
});
|
||||
const viewer = cesiumViewerOf(renderer);
|
||||
if (!viewer) {
|
||||
const refusal = new Error(
|
||||
`The ${rendererPlugin.id} renderer cannot run the application yet`,
|
||||
);
|
||||
try {
|
||||
renderer.destroy();
|
||||
} catch (error) {
|
||||
console.warn('[Init] Refused renderer failed to destroy:', error);
|
||||
}
|
||||
throw refusal;
|
||||
}
|
||||
// The scene object every layer and tool is handed: the application's own,
|
||||
// bound to the renderer.
|
||||
const scene = createSceneHost(renderer, { container });
|
||||
const viewer = scene.host;
|
||||
const hooks = applicationHooksOf(renderer);
|
||||
// The governor keeps its holds on the renderer's frame port. The loop
|
||||
// draws every frame until the application's tools are up (they release
|
||||
// 'boot'), as it did before anything could hold it.
|
||||
@@ -86,11 +89,15 @@ export async function createApplicationScene({
|
||||
try {
|
||||
uninstallRenderGovernor(renderer.frame);
|
||||
} finally {
|
||||
renderer.destroy();
|
||||
try {
|
||||
renderer.destroy();
|
||||
} finally {
|
||||
scene.release();
|
||||
}
|
||||
}
|
||||
});
|
||||
// Before any layer binds a click: every layer and tool shares this one.
|
||||
// Deferred after the viewer, so it is released before the viewer goes.
|
||||
// Deferred after the renderer, so it is released before the renderer goes.
|
||||
const input = inputFor(viewer);
|
||||
defer(() => input.destroy());
|
||||
// Ctrl+wheel and trackpad pinches zoom the globe, not the page.
|
||||
@@ -103,28 +110,29 @@ export async function createApplicationScene({
|
||||
requestRender: () => governorRequestRender('world-time'),
|
||||
}),
|
||||
);
|
||||
// What the adapter hands the application about its viewer; free look is
|
||||
// limited to low heights from the start.
|
||||
const viewerHooks = applicationViewerHooks(viewer);
|
||||
defer(viewerHooks.installFreeLookHeightLimit(FREE_LOOK_MAX_HEIGHT_METERS));
|
||||
registerDataCredits(viewer, credits);
|
||||
configureCreditKeyboardAccess(document);
|
||||
defer(onLocaleChange(() => configureCreditKeyboardAccess(document)));
|
||||
loaderStatus.textContent =
|
||||
googleApiKey || googleTokens || cesiumToken
|
||||
? t('Loading Google 3D Tiles...')
|
||||
: t('Loading the keyless globe...');
|
||||
const photoreal = await loadApplicationPhotoreal({
|
||||
// The map stacks, and the photoreal base among them, switch through the
|
||||
// scene's map engine: the renderer's own, or its globe port.
|
||||
const engine = mapOptions.engine ?? mapEngineFor(viewer);
|
||||
const photoreal = (await engine.loadPhotoreal?.(viewer, {
|
||||
googleApiKey,
|
||||
googleTokens,
|
||||
cesiumToken,
|
||||
});
|
||||
})) ?? { tileset: null, route: 'none', errors: [] };
|
||||
const tileset = photoreal.tileset;
|
||||
// A provider can finish after cancellation; retain ownership of its result.
|
||||
defer(() => releaseApplicationPhotoreal(viewer, tileset));
|
||||
defer(() => {
|
||||
if (!tileset || engine.isDestroyed(tileset)) return;
|
||||
engine.removeTileset(viewer, tileset);
|
||||
engine.release(tileset);
|
||||
});
|
||||
signal.throwIfAborted();
|
||||
if (tileset) {
|
||||
showApplicationPhotoreal(viewer, tileset);
|
||||
engine.addTileset(viewer, tileset);
|
||||
// NOTE: Cesium World Terrain intentionally disabled — conflicts with Google 3D Tiles at high zoom.
|
||||
// Google Photorealistic 3D Tiles provide their own terrain/elevation.
|
||||
renderer.globe.setGlobeVisible(false);
|
||||
@@ -152,6 +160,7 @@ export async function createApplicationScene({
|
||||
const mapStackController = new MapController(viewer, {
|
||||
requestRender: governorRequestRender,
|
||||
...mapOptions,
|
||||
engine,
|
||||
googleTileset: tileset,
|
||||
cesiumToken,
|
||||
initialStack: tileset ? 'photoreal' : 'esri-imagery',
|
||||
@@ -180,9 +189,14 @@ export async function createApplicationScene({
|
||||
mapStackController,
|
||||
operations,
|
||||
input,
|
||||
// Trims stale draw commands, keeps an inline panel drawing, says
|
||||
// whether an entity is the viewer's own, and installs the zoom-out
|
||||
// guard (render/cesium/application.js).
|
||||
...viewerHooks,
|
||||
// What the renderer's plugin hands the application beside the contract,
|
||||
// each optional: trims stale draw commands, keeps an inline panel
|
||||
// drawing, says whether an entity is the renderer's own, installs the
|
||||
// zoom-out guard, and what QA scripts read (render/applicationHooks.js).
|
||||
trimStaleCommands: hooks.trimStaleCommands,
|
||||
keepPanelRendering: hooks.keepPanelRendering,
|
||||
viewerDrawsEntity: hooks.viewerDrawsEntity,
|
||||
installZoomOutGuard: hooks.installZoomOutGuard,
|
||||
qaHandles: hooks.qaHandles,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
import '../testSupport/registerDom.mjs';
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createApplicationScene } from './scene.js';
|
||||
import { createApplicationRequestServices } from '../services/requests.js';
|
||||
import { createStubRendererPlugin } from '../render/stub/index.js';
|
||||
import { rendererOf } from '../render/rendererOf.js';
|
||||
|
||||
/** Start the scene on a stub renderer with `capabilities`. */
|
||||
async function startOnStub(t, { capabilities = [], ...rest } = {}) {
|
||||
document.body.innerHTML = '<div id="cesiumContainer"></div>';
|
||||
const made = [];
|
||||
const deferred = [];
|
||||
t.after(async () => {
|
||||
for (const fn of deferred.reverse()) await fn();
|
||||
});
|
||||
const scene = await createApplicationScene({
|
||||
renderer: createStubRendererPlugin({ capabilities }, (renderer) =>
|
||||
made.push(renderer),
|
||||
),
|
||||
requestServices: createApplicationRequestServices(),
|
||||
loaderStatus: { textContent: '' },
|
||||
signal: new AbortController().signal,
|
||||
defer: (fn) => deferred.push(fn),
|
||||
...rest,
|
||||
});
|
||||
return { scene, renderer: made[0], deferred };
|
||||
}
|
||||
|
||||
test('the scene runs on a renderer with only the required members', async (t) => {
|
||||
const { scene, renderer, deferred } = await startOnStub(t);
|
||||
assert.equal(scene.renderer, renderer);
|
||||
assert.equal(rendererOf(scene.viewer), renderer, 'the scene object is bound');
|
||||
assert.equal(
|
||||
scene.viewer.container,
|
||||
document.getElementById('cesiumContainer'),
|
||||
);
|
||||
assert.equal(scene.viewer.isDestroyed(), false);
|
||||
assert.ok(
|
||||
scene.viewer.container.contains(renderer.canvas),
|
||||
'the renderer fills the container',
|
||||
);
|
||||
// Nothing of the engine on the scene object.
|
||||
assert.deepEqual(Object.keys(scene.viewer).sort(), [
|
||||
'container',
|
||||
'isDestroyed',
|
||||
]);
|
||||
// No hooks: each is left out, not faked.
|
||||
for (const name of [
|
||||
'trimStaleCommands',
|
||||
'keepPanelRendering',
|
||||
'viewerDrawsEntity',
|
||||
'installZoomOutGuard',
|
||||
'qaHandles',
|
||||
])
|
||||
assert.equal(scene[name], undefined, name);
|
||||
// Keyless, the map stack shows Esri on the globe through the globe port.
|
||||
assert.equal(scene.tileset, null);
|
||||
assert.equal(scene.mapStackController.getActiveId(), 'esri-imagery');
|
||||
const layers = renderer.globe.imagery.list();
|
||||
assert.equal(layers.length, 1, 'one base imagery layer');
|
||||
assert.ok(
|
||||
renderer.testing.calls.some(
|
||||
(call) =>
|
||||
call.port === 'globe' &&
|
||||
call.member === 'imagery.add' &&
|
||||
call.args[0]?.url?.includes('World_Imagery'),
|
||||
),
|
||||
'Esri imagery through the port',
|
||||
);
|
||||
assert.equal(scene.mapStackController.getDrapeSurface(), 'globe');
|
||||
|
||||
// Switching stacks goes through the port too.
|
||||
await scene.mapStackController.setStack('osm');
|
||||
assert.equal(scene.mapStackController.getActiveId(), 'osm');
|
||||
assert.equal(renderer.globe.imagery.list().length, 1, 'Esri gave way');
|
||||
|
||||
for (const fn of deferred.splice(0).reverse()) await fn();
|
||||
assert.equal(scene.viewer.isDestroyed(), true);
|
||||
assert.equal(rendererOf(scene.viewer), null, 'and lets go of the renderer');
|
||||
assert.equal(renderer.globe.imagery.list().length, 0, 'imagery released');
|
||||
});
|
||||
|
||||
test('with credentials, the photoreal base loads through the globe port', async (t) => {
|
||||
const { scene, renderer } = await startOnStub(t, {
|
||||
capabilities: ['photoreal', 'globeVisibility'],
|
||||
googleApiKey: 'stub-key',
|
||||
});
|
||||
assert.ok(scene.tileset, 'a photoreal base');
|
||||
assert.equal(scene.mapStackController.getActiveId(), 'photoreal');
|
||||
assert.equal(renderer.globe.isGlobeVisible(), false, 'the globe is hidden');
|
||||
const photoreal = renderer.testing.calls.filter(
|
||||
(call) => call.port === 'globe' && call.member === 'setPhotoreal',
|
||||
);
|
||||
assert.equal(photoreal[0]?.args[0]?.key, 'stub-key');
|
||||
|
||||
// A globe stack hides photoreal through the port, and shows the globe.
|
||||
await scene.mapStackController.setStack('esri-imagery');
|
||||
assert.equal(renderer.globe.isGlobeVisible(), true);
|
||||
assert.equal(
|
||||
renderer.testing.calls
|
||||
.filter((call) => call.port === 'globe' && call.member === 'setPhotoreal')
|
||||
.at(-1).args[0],
|
||||
null,
|
||||
);
|
||||
await scene.mapStackController.setStack('photoreal');
|
||||
assert.equal(renderer.globe.isGlobeVisible(), false);
|
||||
});
|
||||
@@ -224,6 +224,9 @@ export function createApplicationTools({
|
||||
surfaceServices: operations.surface,
|
||||
requestRender: governorRequestRender,
|
||||
entityRecords: { read: readEntityRecord, write: entityRecord },
|
||||
// What the renderer's plugin lets QA scripts read (the Cesium viewer,
|
||||
// for the default renderer), in place of the scene object above.
|
||||
...scene.qaHandles,
|
||||
};
|
||||
const debug = window.__godsEyeView;
|
||||
defer(() => {
|
||||
|
||||
Reference in new issue
Block a user