mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
Contract: camera.position(result?) and camera.height() for per-frame readers, render.request(reason?), maps.releasePhotorealTiles, sources with a caller-owned ready provider, isGlobeEngine. engine.maps now hides every tileset it has shown, lets the last imagery/photoreal call win, and setPhotorealTiles(null) restores the last base imagery and its credit instead of leaving the photoreal credit on a bare globe. Cesium engine: reads the Viewer camera/canvas aliases, detaches bridged events through the remover Cesium returns, raises frame timestamps without allocating, re-aims the horizon occluder only when the camera moved, and renders the scene directly when the governor governs another viewer (or none yet). engineFromViewer returns the registered scene engine of a viewer, else builds and caches one. The render governor stays the Cesium render backend under engine.render and gains isRenderGovernorInstalled.
69 lines
2.3 KiB
JavaScript
69 lines
2.3 KiB
JavaScript
// GlobeProjector on a Cesium scene.
|
||
|
||
import * as Cesium from 'cesium';
|
||
import * as geo from '../../geo/index.js';
|
||
|
||
/**
|
||
* @param {Cesium.Viewer} viewer
|
||
* @param {{ worldToWindowCoordinates?: Function }} [options] Test seam for
|
||
* Cesium.SceneTransforms.worldToWindowCoordinates.
|
||
* @returns {{ projector: import('../contract.js').GlobeProjector, destroy: () => void }}
|
||
*/
|
||
export function createCesiumProjector(
|
||
viewer,
|
||
{
|
||
worldToWindowCoordinates = Cesium.SceneTransforms.worldToWindowCoordinates,
|
||
} = {},
|
||
) {
|
||
const { scene } = viewer;
|
||
// Viewer.camera is scene.camera; the engine reads the public alias.
|
||
const camera = viewer.camera ?? scene.camera;
|
||
const scratchWindow = new Cesium.Cartesian2();
|
||
const scratchMatrix = new Cesium.Matrix4();
|
||
const occluder = new Cesium.EllipsoidalOccluder(Cesium.Ellipsoid.WGS84);
|
||
// The occluder is re-aimed only when the camera has moved: horizonVisible
|
||
// runs per label per frame.
|
||
let occluderX = Number.NaN;
|
||
let occluderY = Number.NaN;
|
||
let occluderZ = Number.NaN;
|
||
|
||
const projector = {
|
||
worldToWindow(position, result) {
|
||
// Cesium returns undefined in front of the near plane or behind the camera.
|
||
const window = worldToWindowCoordinates(scene, position, scratchWindow);
|
||
if (!window) return undefined;
|
||
if (!result) return geo.cartesian2.create(window.x, window.y);
|
||
result.x = window.x;
|
||
result.y = window.y;
|
||
return result;
|
||
},
|
||
// The matrix Cesium's worldToWindowCoordinates uses: frustum projection ×
|
||
// view. x, y and w do not depend on the multi-frustum near/far planes.
|
||
viewProjection(result = geo.matrix4.create()) {
|
||
Cesium.Matrix4.multiply(
|
||
camera.frustum.projectionMatrix,
|
||
camera.viewMatrix,
|
||
scratchMatrix,
|
||
);
|
||
for (let index = 0; index < 16; index++)
|
||
result[index] = scratchMatrix[index];
|
||
return result;
|
||
},
|
||
horizonVisible(position) {
|
||
const { positionWC } = camera;
|
||
if (
|
||
positionWC.x !== occluderX ||
|
||
positionWC.y !== occluderY ||
|
||
positionWC.z !== occluderZ
|
||
) {
|
||
occluder.cameraPosition = positionWC;
|
||
occluderX = positionWC.x;
|
||
occluderY = positionWC.y;
|
||
occluderZ = positionWC.z;
|
||
}
|
||
return occluder.isPointVisible(position);
|
||
},
|
||
};
|
||
return { projector, destroy() {} };
|
||
}
|