mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
Add indexed world triangles to the required globe contract on Cesium, Navara and the fake engine. Own geometry, textures, visibility, depth tests, pick identities and teardown inside each engine. Reused canvas images request another frame until their new pixels upload. Move obstruction rays and owned mesh, line and mover exclusions into the Cesium surface adapter. Draw CCTV monitor planes and viewshed volumes through shared mesh handles. Draw data-pack points, straight lines, raised polygons, image rectangles and media captions through movers, lines and meshes. Delete the feature renderers for each engine and retain decoded assets across scene rebinds. Register the modules, dependency, format scope and reduced coupling baseline. Cesium is the reference for geometry, colours, image placement, marker sizes, caption offsets, picking and resource ownership. Navara now uses straight pack lines and per-position polygon heights without edge densification. Navara meshes draw in the transparent pass after the effects, with the scene-depth test and the view's exposure undone, as the pack meshes did: in the scene pass the landscape look's exposure washed a red pack image to white. The scene-depth helper moves into the Navara engine, and the stems helper that only its test still used is deleted. The mesh picker takes the globe when Navara's depth read answers with the camera itself, which it does right after another click and which hid a polygon from a quick second click. The 256 px media caption is not pickable: its transparent square took clicks meant for nearby pack features, and no action targets it. Annotation drapes remain on their existing renderers because bounded building classification, animated fills and wire cages need a separate contract extension to preserve Cesium behaviour. Coupling falls from 344 to 210 Cesium layer-renderer lines, 1294 to 794 Navara layer-renderer lines, 9 to 7 external Cesium import files, 1026 to 1005 viewer lines, 9 to 6 extension reads and 42 to 39 engine-name lines. Other counts do not grow. The full unit suite, import directions, package boundaries, formatting and coupling checks pass. No user-visible strings change. In the browser, keyless: the contract suite passes on Navara and on Cesium apart from the camera-events check that also fails intermittently before this change; CCTV v2 (58 checks), the CCTV and director engine switches pass, with the pack image at the same pixel as before on both engines. The mesh contract fixture now sits at 3 km, above the terrain Navara draws there. The director interactions script starts at its view in the link so the app's arrival flight no longer lands after the shot, and waits for the click's asynchronous pick; its last step, the next-shot action, fails the same way before this change.
256 lines
8.6 KiB
JavaScript
256 lines
8.6 KiB
JavaScript
import { releaseCameraVerbs } from '../cameraVerbs.js';
|
|
import { createApplicationOperations } from './operations.js';
|
|
import {
|
|
createApplicationViewer,
|
|
installFreeLookHeightLimit,
|
|
installTrackpadPinchZoom,
|
|
loadCesiumPhotorealTileset,
|
|
} from '../globe/cesium/viewer.js';
|
|
import { registerDataCredits } from '../data/dataCredits.js';
|
|
import { configureCreditKeyboardAccess } from '../creditKeyboard.js';
|
|
import { MapStackController } from '../mapStackController.js';
|
|
import { initLogoGaze } from '../logoGaze.js';
|
|
import {
|
|
uninstallRenderGovernor,
|
|
governorRequestRender,
|
|
} from '../renderGovernor.js';
|
|
import { describeError } from './errors.js';
|
|
import { createSceneEngine } from './globeEngine.js';
|
|
import { onLocaleChange, t } from '../i18n/index.js';
|
|
import { loadCesium } from './loadCesium.js';
|
|
|
|
/**
|
|
* Construct the application globe using the caller's local configuration.
|
|
* `engineKind` 'navara' or 'navara-hd' (the page's engine choice,
|
|
* src/app/engineChoice.js) builds the experimental Navara engine instead of
|
|
* the Cesium viewer: the scene then has no viewer (`viewer: null`).
|
|
*/
|
|
export async function createApplicationScene({
|
|
requestServices,
|
|
googleApiKey,
|
|
signal,
|
|
defer,
|
|
rendererCache,
|
|
layerSession,
|
|
...options
|
|
}) {
|
|
const operations =
|
|
layerSession?.operations ??
|
|
createApplicationOperations({
|
|
requests: requestServices,
|
|
signal: layerSession?.lifetime.signal ?? signal,
|
|
});
|
|
if (layerSession) layerSession.operations = operations;
|
|
// The logo's gaze and the page's Google key belong to the page session
|
|
// when there is one: an engine switch keeps them.
|
|
const pageChrome = layerSession ? (layerSession.chrome ??= {}) : null;
|
|
const own = pageChrome
|
|
? (release) =>
|
|
layerSession.lifetime.signal.addEventListener('abort', release, {
|
|
once: true,
|
|
})
|
|
: defer;
|
|
if (!pageChrome?.sceneChrome) {
|
|
if (pageChrome) pageChrome.sceneChrome = true;
|
|
own(initLogoGaze());
|
|
const previousKey = window.__GOOGLE_MAPS_API_KEY__;
|
|
if (googleApiKey) {
|
|
window.__GOOGLE_MAPS_API_KEY__ = googleApiKey;
|
|
own(() => {
|
|
if (window.__GOOGLE_MAPS_API_KEY__ !== googleApiKey) return;
|
|
if (previousKey === undefined) delete window.__GOOGLE_MAPS_API_KEY__;
|
|
else window.__GOOGLE_MAPS_API_KEY__ = previousKey;
|
|
});
|
|
}
|
|
}
|
|
// A cached renderer is built once, by the first application that asks for
|
|
// its engine, and later applications reuse it warm. Everything this closure
|
|
// captures from that application (its abort signal, the loader status
|
|
// element, Navara's `?look=` read from location.search) is read only while
|
|
// the renderer is being built; nothing in the built scene keeps using it.
|
|
// A changed `?look=` therefore applies to a renderer built after it.
|
|
const build = (resources) =>
|
|
createRenderer({
|
|
...options,
|
|
googleApiKey,
|
|
signal,
|
|
...resources,
|
|
});
|
|
const { viewer, engine, tileset, mapStackController } = rendererCache
|
|
? await rendererCache.acquire(options.engineKind, build, defer)
|
|
: await build({ defer });
|
|
if (layerSession) {
|
|
// DEM cells belong to the session. Mesh samples came from the outgoing
|
|
// scene, including when the incoming canvas was warm in the cache.
|
|
operations.surface.groundFloor.clearMeshFloorCells();
|
|
operations.surface.groundFloor.setMeshFloorPreferred(
|
|
mapStackController.getActiveId() === 'photoreal',
|
|
);
|
|
}
|
|
return {
|
|
viewer,
|
|
engine,
|
|
tileset,
|
|
mapStackController,
|
|
operations,
|
|
};
|
|
}
|
|
|
|
async function createRenderer({
|
|
googleApiKey,
|
|
googleTokens = null,
|
|
cesiumToken,
|
|
credits,
|
|
MapController = MapStackController,
|
|
mapOptions = {},
|
|
loaderStatus,
|
|
engineKind = 'cesium',
|
|
loadNavaraGlobe,
|
|
signal,
|
|
defer,
|
|
container,
|
|
}) {
|
|
// The Navara mode's own code loads only in that mode, with Navara itself,
|
|
// so the Cesium-mode bundle carries none of it.
|
|
if (engineKind === 'navara' || engineKind === 'navara-hd') {
|
|
const scene = await (
|
|
await import('./navaraMode.js')
|
|
).createNavaraScene({
|
|
container,
|
|
cesiumToken,
|
|
credits,
|
|
MapController,
|
|
mapOptions,
|
|
loaderStatus,
|
|
loadNavaraGlobe,
|
|
hd: engineKind === 'navara-hd',
|
|
signal,
|
|
defer,
|
|
});
|
|
// As the Cesium scene below: a parked or released engine keeps no camera
|
|
// verb running on it (a Directions flight is parked before this).
|
|
const { engine } = scene;
|
|
defer(() => releaseCameraVerbs(engine));
|
|
const suspend = scene.suspend;
|
|
scene.suspend = () => {
|
|
releaseCameraVerbs(engine);
|
|
suspend();
|
|
};
|
|
return scene;
|
|
}
|
|
if (engineKind !== 'cesium')
|
|
throw new TypeError(`Unknown globe engine: ${engineKind}`);
|
|
await loadCesium();
|
|
signal.throwIfAborted();
|
|
loaderStatus.textContent = t('Configuring viewer...');
|
|
// Provider attribution stays visible, including clean-view and recording.
|
|
const creditContainer = document.createElement('div');
|
|
creditContainer.id = 'cesium-credits';
|
|
(container ?? document.body).appendChild(creditContainer);
|
|
defer(() => creditContainer.remove());
|
|
const viewer = createApplicationViewer({
|
|
container: container ?? 'cesiumContainer',
|
|
creditContainer,
|
|
});
|
|
defer(() => {
|
|
uninstallRenderGovernor(viewer);
|
|
if (!viewer.isDestroyed()) viewer.destroy();
|
|
});
|
|
// The globe engine over this viewer; Google's startup tileset is named
|
|
// once it loads.
|
|
const tilesetRef = { current: null };
|
|
const engine = createSceneEngine(viewer, { tilesetRef, defer });
|
|
// As the Navara scene above: a released engine keeps no camera verb.
|
|
defer(() => releaseCameraVerbs(engine));
|
|
defer(installTrackpadPinchZoom(viewer));
|
|
defer(installFreeLookHeightLimit(viewer));
|
|
registerDataCredits(engine, 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 loadCesiumPhotorealTileset({
|
|
googleApiKey,
|
|
googleTokens,
|
|
cesiumToken,
|
|
});
|
|
const tileset = photoreal.tileset;
|
|
tilesetRef.current = tileset;
|
|
// A provider can finish after cancellation; retain ownership of its result.
|
|
defer(() => {
|
|
if (tileset && !tileset.isDestroyed()) {
|
|
if (!viewer.scene.primitives.remove(tileset)) tileset.destroy();
|
|
}
|
|
});
|
|
signal.throwIfAborted();
|
|
if (tileset) {
|
|
viewer.scene.primitives.add(tileset);
|
|
// NOTE: Cesium World Terrain intentionally disabled — conflicts with Google 3D Tiles at high zoom.
|
|
// Google Photorealistic 3D Tiles provide their own terrain/elevation.
|
|
viewer.scene.globe.show = false;
|
|
console.info(`[Init] Google 3D Tiles loaded via ${photoreal.route}.`);
|
|
} else {
|
|
if (photoreal.errors.length) {
|
|
const tileError = photoreal.errors.at(-1);
|
|
console.warn(
|
|
'[Init] Google 3D Tiles unavailable, using the keyless globe:',
|
|
tileError,
|
|
);
|
|
const tileErrorDetail = describeError(tileError);
|
|
loaderStatus.textContent = t(
|
|
'Google 3D Tiles unavailable ({detail}). Loading the keyless globe...',
|
|
{
|
|
detail: tileErrorDetail,
|
|
},
|
|
);
|
|
}
|
|
viewer.scene.globe.show = true;
|
|
}
|
|
|
|
loaderStatus.textContent = t('Initializing systems...');
|
|
|
|
const mapStackController = new MapController(viewer, {
|
|
requestRender: governorRequestRender,
|
|
// Map sources reach the scene through the globe engine's maps.
|
|
engine,
|
|
...mapOptions,
|
|
googleTileset: tileset,
|
|
cesiumToken,
|
|
initialStack: tileset ? 'photoreal' : 'esri-imagery',
|
|
// Rebroadcast stack changes as a window CustomEvent so data layers (CCTV
|
|
// per-regime ground resolution) can react without coupling
|
|
// MapStackController to layer modules. Fires on 'switching'/'ready'/
|
|
// 'error'; listeners derive the surface regime from live scene state, so
|
|
// intermediate emissions are harmless.
|
|
onChange: (state) => {
|
|
window.dispatchEvent(
|
|
new CustomEvent('gev:map-stack-changed', { detail: state }),
|
|
);
|
|
},
|
|
onError: (message) => console.warn('[MapStack]', message),
|
|
});
|
|
defer(() => mapStackController.destroy());
|
|
await mapStackController.setStack(tileset ? 'photoreal' : 'esri-imagery', {
|
|
silent: true,
|
|
});
|
|
|
|
signal.throwIfAborted();
|
|
return {
|
|
viewer,
|
|
engine,
|
|
tileset,
|
|
mapStackController,
|
|
suspend() {
|
|
uninstallRenderGovernor(viewer);
|
|
engine.camera.cancelFlight();
|
|
releaseCameraVerbs(engine);
|
|
viewer.useDefaultRenderLoop = false;
|
|
},
|
|
resume() {
|
|
viewer.useDefaultRenderLoop = !document.hidden;
|
|
},
|
|
};
|
|
}
|