mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-21 02:32:18 +00:00
fix(cctv): reuse renderer visibility scratch state
This commit is contained in:
1 parent
c0752784d2
commit
b98b9d390f
6 files changed
+137
-93
No files matched your search
+13
-53
@@ -1,8 +1,4 @@
|
||||
import { findCamera } from '../../render/rendererOf.js';
|
||||
import { offsetEllipsoid } from '../../geo/ellipsoid.js';
|
||||
import { horizonFrom } from '../../geo/horizon.js';
|
||||
import { frustumPlanes, sphereOutside } from '../../geo/frustum.js';
|
||||
import { sphereFromPoints } from '../../geo/sphere.js';
|
||||
import {
|
||||
FRAME_SIGNATURE_W,
|
||||
FRAME_SIGNATURE_H,
|
||||
@@ -46,9 +42,7 @@ export function fitToProjection(sourceW, sourceH) {
|
||||
};
|
||||
}
|
||||
|
||||
// Keep the occluding surface below common negative geoid undulations.
|
||||
const HORIZON_MARGIN_M = 200;
|
||||
const _horizonEllipsoid = offsetEllipsoid(-HORIZON_MARGIN_M);
|
||||
const _scratchPoints = [];
|
||||
|
||||
export function createFrames({ state: layerState, services, parts, source }) {
|
||||
/**
|
||||
@@ -260,53 +254,19 @@ export function createFrames({ state: layerState, services, parts, source }) {
|
||||
const viewer = layerState._viewer;
|
||||
if (!viewer || viewer.isDestroyed?.()) return true;
|
||||
const camera = findCamera(viewer);
|
||||
if (camera?.isWorld3D?.() !== true) return true;
|
||||
const pose = camera.pose?.();
|
||||
const projection = camera.matrices?.()?.projection;
|
||||
const finiteVector = (v) => v && [v.x, v.y, v.z].every(Number.isFinite);
|
||||
// A centred perspective is the only shape frustumPlanes can judge exactly.
|
||||
if (
|
||||
!finiteVector(pose?.position) ||
|
||||
!finiteVector(pose.direction) ||
|
||||
!finiteVector(pose.up) ||
|
||||
!(pose.fovy > 0 && pose.fovy < Math.PI) ||
|
||||
!(pose.aspectRatio > 0) ||
|
||||
!(pose.near > 0 && pose.far > pose.near) ||
|
||||
!projection ||
|
||||
projection[8] !== 0 ||
|
||||
projection[9] !== 0 ||
|
||||
projection[15] !== 0
|
||||
)
|
||||
return true;
|
||||
if (typeof camera?.visiblePoints !== 'function') return true;
|
||||
const placed = record?.frustumPositions;
|
||||
const points = [
|
||||
record?.position,
|
||||
placed?.mount,
|
||||
placed?.capCenter,
|
||||
placed?.tl,
|
||||
placed?.tr,
|
||||
placed?.br,
|
||||
placed?.bl,
|
||||
].filter(Boolean);
|
||||
if (
|
||||
!points.length ||
|
||||
points.some(
|
||||
(p) =>
|
||||
!Number.isFinite(p.x) ||
|
||||
!Number.isFinite(p.y) ||
|
||||
!Number.isFinite(p.z),
|
||||
)
|
||||
)
|
||||
return true;
|
||||
const o = _horizonEllipsoid.oneOverRadii;
|
||||
const eye = pose.position;
|
||||
// Below the shrunken ellipsoid the occluder can hide nearby visible points.
|
||||
const outside = Math.hypot(eye.x * o.x, eye.y * o.y, eye.z * o.z) >= 1;
|
||||
const horizon = outside ? horizonFrom(eye, _horizonEllipsoid) : null;
|
||||
if (horizon && !points.some((point) => horizon.isPointVisible(point)))
|
||||
return false;
|
||||
const sphere = sphereFromPoints(points);
|
||||
return !sphereOutside(frustumPlanes(pose), sphere.center, sphere.radius);
|
||||
const points = _scratchPoints;
|
||||
points.length = 0;
|
||||
if (record?.position) points.push(record.position);
|
||||
if (placed?.mount) points.push(placed.mount);
|
||||
if (placed?.capCenter) points.push(placed.capCenter);
|
||||
if (placed?.tl) points.push(placed.tl);
|
||||
if (placed?.tr) points.push(placed.tr);
|
||||
if (placed?.br) points.push(placed.br);
|
||||
if (placed?.bl) points.push(placed.bl);
|
||||
// Unknown modes or unsupported cameras must not suppress pictures.
|
||||
return camera.visiblePoints(points, 200) !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,6 +5,7 @@ import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import * as Cesium from 'cesium';
|
||||
import { bindRenderer } from '../../render/rendererOf.js';
|
||||
import { createPointsVisibility } from '../../render/cesium/camera.js';
|
||||
import { createFrames, fitToProjection } from './frames.js';
|
||||
import {
|
||||
FRAME_SIGNATURE_W,
|
||||
@@ -194,17 +195,7 @@ function viewerAt(position, direction, up) {
|
||||
};
|
||||
bindRenderer(viewer, {
|
||||
camera: {
|
||||
isWorld3D: () => viewer.scene.mode === Cesium.SceneMode.SCENE3D,
|
||||
pose: () => ({
|
||||
position: viewer.camera.positionWC,
|
||||
direction: viewer.camera.directionWC,
|
||||
up: viewer.camera.upWC,
|
||||
fovy: viewer.camera.frustum.fovy,
|
||||
aspectRatio: viewer.camera.frustum.aspectRatio,
|
||||
near: viewer.camera.frustum.near,
|
||||
far: viewer.camera.frustum.far,
|
||||
}),
|
||||
matrices: () => ({ projection: viewer.camera.frustum.projectionMatrix }),
|
||||
visiblePoints: createPointsVisibility(viewer.scene, viewer.camera),
|
||||
},
|
||||
});
|
||||
return viewer;
|
||||
@@ -377,20 +368,9 @@ test('the renderer scene host culls the far side without raw viewer access', ()
|
||||
const native = lookingDown();
|
||||
const viewer = { isDestroyed: () => false };
|
||||
const camera = native.camera;
|
||||
camera.directionWC = Cesium.Cartesian3.UNIT_X;
|
||||
const unbind = bindRenderer(viewer, {
|
||||
camera: {
|
||||
isWorld3D: () => true,
|
||||
matrices: () => ({ projection: camera.frustum.projectionMatrix }),
|
||||
pose: () => ({
|
||||
position: camera.positionWC,
|
||||
direction: Cesium.Cartesian3.UNIT_X,
|
||||
up: camera.upWC,
|
||||
fovy: camera.frustum.fovy,
|
||||
aspectRatio: camera.frustum.aspectRatio,
|
||||
near: camera.frustum.near,
|
||||
far: camera.frustum.far,
|
||||
}),
|
||||
},
|
||||
camera: { visiblePoints: createPointsVisibility(native.scene, camera) },
|
||||
});
|
||||
try {
|
||||
assert.equal(framesFor(viewer).projectionInView(planeRecord()), false);
|
||||
@@ -399,6 +379,28 @@ test('the renderer scene host culls the far side without raw viewer access', ()
|
||||
}
|
||||
});
|
||||
|
||||
test('visibility checks reuse the points array across ticks', () => {
|
||||
const seen = [];
|
||||
const host = {};
|
||||
const unbind = bindRenderer(host, {
|
||||
camera: {
|
||||
visiblePoints: (points) => {
|
||||
seen.push(points);
|
||||
return true;
|
||||
},
|
||||
},
|
||||
});
|
||||
try {
|
||||
const frames = framesFor(host);
|
||||
frames.projectionInView(planeRecord());
|
||||
frames.projectionInView(planeRecord());
|
||||
assert.equal(seen.length, 2);
|
||||
assert.strictEqual(seen[0], seen[1]);
|
||||
} finally {
|
||||
unbind();
|
||||
}
|
||||
});
|
||||
|
||||
test('deep viewers do not lose nearby planes to the horizon test', () => {
|
||||
const mount = point(0, 0, -350);
|
||||
const position = point(0.00135, 0, -300);
|
||||
@@ -417,13 +419,13 @@ test('deep viewers do not lose nearby planes to the horizon test', () => {
|
||||
);
|
||||
});
|
||||
|
||||
test('flat, unknown, and off-centre projections fail open', () => {
|
||||
test('flat and unknown projections fail open', () => {
|
||||
const native = lookingDown();
|
||||
native.camera.directionWC = Cesium.Cartesian3.UNIT_X;
|
||||
native.scene.mode = Cesium.SceneMode.COLUMBUS_VIEW;
|
||||
assert.equal(framesFor(native).projectionInView(planeRecord()), true);
|
||||
native.scene.mode = Cesium.SceneMode.SCENE3D;
|
||||
native.camera.frustum.xOffset = 0.1;
|
||||
native.camera.directionWC = undefined;
|
||||
assert.equal(framesFor(native).projectionInView(planeRecord()), true);
|
||||
const host = {};
|
||||
const unbind = bindRenderer(host, { camera: { pose: () => ({}) } });
|
||||
|
||||
@@ -86,6 +86,89 @@ function sameMatrix(array, matrix) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Conservative globe/frustum visibility; null means unknown. */
|
||||
export function createPointsVisibility(scene, camera) {
|
||||
const visibilitySphere = new Cesium.BoundingSphere();
|
||||
let visibilityMargin = null;
|
||||
let visibilityEllipsoid = null;
|
||||
let visibilityOccluder = null;
|
||||
return function visiblePoints(points, marginM) {
|
||||
if (
|
||||
scene.mode !== Cesium.SceneMode.SCENE3D ||
|
||||
!points?.length ||
|
||||
!Number.isFinite(marginM) ||
|
||||
marginM < 0 ||
|
||||
typeof camera.frustum?.computeCullingVolume !== 'function'
|
||||
)
|
||||
return null;
|
||||
const radii = Cesium.Ellipsoid.WGS84.radii;
|
||||
if (marginM >= Math.min(radii.x, radii.y, radii.z)) return null;
|
||||
const eye = camera.positionWC;
|
||||
if (
|
||||
!eye ||
|
||||
!Number.isFinite(eye.x) ||
|
||||
!Number.isFinite(eye.y) ||
|
||||
!Number.isFinite(eye.z)
|
||||
)
|
||||
return null;
|
||||
const direction = camera.directionWC;
|
||||
const up = camera.upWC;
|
||||
if (
|
||||
!direction ||
|
||||
!up ||
|
||||
!Number.isFinite(direction.x) ||
|
||||
!Number.isFinite(direction.y) ||
|
||||
!Number.isFinite(direction.z) ||
|
||||
!Number.isFinite(up.x) ||
|
||||
!Number.isFinite(up.y) ||
|
||||
!Number.isFinite(up.z)
|
||||
)
|
||||
return null;
|
||||
for (const p of points) {
|
||||
if (
|
||||
!p ||
|
||||
!Number.isFinite(p.x) ||
|
||||
!Number.isFinite(p.y) ||
|
||||
!Number.isFinite(p.z)
|
||||
)
|
||||
return null;
|
||||
}
|
||||
if (visibilityMargin !== marginM) {
|
||||
visibilityMargin = marginM;
|
||||
visibilityEllipsoid = new Cesium.Ellipsoid(
|
||||
radii.x - marginM,
|
||||
radii.y - marginM,
|
||||
radii.z - marginM,
|
||||
);
|
||||
visibilityOccluder = new Cesium.EllipsoidalOccluder(visibilityEllipsoid);
|
||||
}
|
||||
const inverse = visibilityEllipsoid.oneOverRadii;
|
||||
const outside =
|
||||
Math.hypot(eye.x * inverse.x, eye.y * inverse.y, eye.z * inverse.z) >= 1;
|
||||
if (outside) {
|
||||
visibilityOccluder.cameraPosition = eye;
|
||||
let visible = false;
|
||||
for (const p of points) {
|
||||
if (visibilityOccluder.isPointVisible(p)) {
|
||||
visible = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!visible) return false;
|
||||
}
|
||||
const culling = camera.frustum.computeCullingVolume(
|
||||
eye,
|
||||
camera.directionWC,
|
||||
camera.upWC,
|
||||
);
|
||||
return (
|
||||
culling.computeVisibility(
|
||||
Cesium.BoundingSphere.fromPoints(points, visibilitySphere),
|
||||
) !== Cesium.Intersect.OUTSIDE
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {{scene: object, camera: object, report: (error: unknown) => void,
|
||||
* widget?: object, capabilities?: ReadonlySet<string>,
|
||||
@@ -350,9 +433,7 @@ export function createCesiumCamera({
|
||||
}
|
||||
|
||||
const cameraPort = {
|
||||
// The ECEF camera pose describes the globe only in 3D. Consumers that
|
||||
// cannot prove this must avoid globe-only visibility culling.
|
||||
isWorld3D: () => scene.mode === Cesium.SceneMode.SCENE3D,
|
||||
visiblePoints: createPointsVisibility(scene, camera),
|
||||
on: (event, cb, options) => cameraEvents.on(event, cb, options),
|
||||
onCommand: (cb) => commandHooks.on(cb),
|
||||
setView(spec) {
|
||||
|
||||
@@ -337,21 +337,23 @@ test('the camera port gives the clock its own tick back as it goes', () => {
|
||||
}
|
||||
});
|
||||
|
||||
test('camera reports globe mode and fails closed for other modes', () => {
|
||||
test('camera visibility is unknown outside 3D and resolves on the globe', () => {
|
||||
const { scene, camera } = headlessCamera();
|
||||
scene.postUpdate = new Cesium.Event();
|
||||
scene.requestRender = () => {};
|
||||
const { port } = createCesiumCamera({ scene, camera, report: () => {} });
|
||||
for (const [mode, expected] of [
|
||||
[Cesium.SceneMode.SCENE3D, true],
|
||||
[Cesium.SceneMode.SCENE2D, false],
|
||||
[Cesium.SceneMode.COLUMBUS_VIEW, false],
|
||||
[Cesium.SceneMode.MORPHING, false],
|
||||
[undefined, false],
|
||||
const points = [Cesium.Cartesian3.fromDegrees(0, 0, 10)];
|
||||
for (const mode of [
|
||||
Cesium.SceneMode.SCENE2D,
|
||||
Cesium.SceneMode.COLUMBUS_VIEW,
|
||||
Cesium.SceneMode.MORPHING,
|
||||
undefined,
|
||||
]) {
|
||||
scene.mode = mode;
|
||||
assert.equal(port.isWorld3D(), expected);
|
||||
assert.equal(port.visiblePoints(points, 200), null);
|
||||
}
|
||||
scene.mode = Cesium.SceneMode.SCENE3D;
|
||||
assert.equal(typeof port.visiblePoints(points, 200), 'boolean');
|
||||
});
|
||||
|
||||
test('setFov holds the vertical angle across resizes, and waits for a first one', () => {
|
||||
|
||||
Vendored
+2
-2
@@ -234,8 +234,8 @@ export type CameraEvent =
|
||||
'changed' | 'moveStart' | 'moveEnd' | 'followChanged' | 'flightEnd';
|
||||
|
||||
export interface CameraPort {
|
||||
/** True only when its pose is an ECEF view of the 3D globe. Missing means unknown. */
|
||||
isWorld3D?(): boolean;
|
||||
/** Conservative visibility of an ECEF point set, with a horizon margin in metres; null means unknown. */
|
||||
visiblePoints?(points: Ecef[], horizonMarginM: number): boolean | null;
|
||||
pose(): CameraPose;
|
||||
/**
|
||||
* Place the eye. A position with a `{direction, up}` orientation is placed
|
||||
|
||||
Reference in new issue
Block a user