Files
gods-eye-view/src/globe/cesium/projector.js
T
German Khokhlov 58a196374f feat(globe): engine members and the viewer bridge for the shell
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.
2026-10-15 18:18:19 +04:00

69 lines
2.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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() {} };
}