fix(cctv): reuse renderer visibility scratch state

This commit is contained in:
brandonabyrd committed 2026-11-18 17:13:06 -06:00
1 parent c0752784d2
commit b98b9d390f
6 files changed
+137 -93

No files matched your search

+13 -53
View File
@@ -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;
}
/**
+28 -26
View File
@@ -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: () => ({}) } });