mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-17 00:32:21 +00:00
Move the flights and military layers onto the renderer ports
The flights and military layers now reach the renderer only through its ports: motion, queries, state, tracking, enrichment, evidence and policy read the camera pose, project through the query port, request and suspend frames through the frame port, and key per-frame caches on the frames drawn rather than the scene's frame number. Policy is plain data, the enrichment view test is pose-based, and the tracked aircraft is an icon in a draw-port set with a plain holder standing in for the entity that Cockpit, the tracked card and share links read. The mesh floor sampler can sample through the query port, excluding named draw sets, and both layers use it that way. A pick of another layer's tracked icon is never taken for one of a layer's own contacts. Twelve layer files leave the Cesium allow-list.
This commit is contained in:
1 parent
00a0a0dbc7
commit
23d56f8393
40 files changed
+1638
-908
No files matched your search
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* @module geo/frustum
|
||||
* @description The view frustum of a camera pose as the default renderer
|
||||
* culls with it (`PerspectiveFrustum.computeCullingVolume` and
|
||||
* `CullingVolume.computeVisibility`, centred, no offset), so code that culls
|
||||
* by what is on screen agrees with what the renderer draws. No engine.
|
||||
*/
|
||||
import { add, cross, dot, normalize, scale, sub } from './vector.js';
|
||||
|
||||
/**
|
||||
* The six planes (`{normal, w}`, inside where `normal · p + w ≥ 0`) of the
|
||||
* frustum `pose` looks through: a camera port pose's `position`,
|
||||
* `direction`, `up`, `fovy`, `aspectRatio`, `near` and `far`.
|
||||
* @param {{position: object, direction: object, up: object, fovy: number,
|
||||
* aspectRatio: number, near: number, far: number}} pose
|
||||
* @returns {Array<{normal: {x: number, y: number, z: number}, w: number}>}
|
||||
*/
|
||||
export function frustumPlanes(pose) {
|
||||
const { position, direction, up, near, far } = pose;
|
||||
const t = near * Math.tan(0.5 * pose.fovy);
|
||||
const b = -t;
|
||||
const r = pose.aspectRatio * t;
|
||||
const l = -r;
|
||||
|
||||
const right = cross(direction, up);
|
||||
const nearCenter = add(position, scale(direction, near));
|
||||
const farCenter = add(position, scale(direction, far));
|
||||
const plane = (normal) => ({ normal, w: -dot(normal, position) });
|
||||
|
||||
const left = plane(
|
||||
normalize(
|
||||
cross(normalize(sub(add(nearCenter, scale(right, l)), position)), up),
|
||||
),
|
||||
);
|
||||
const rightPlane = plane(
|
||||
normalize(cross(up, sub(add(nearCenter, scale(right, r)), position))),
|
||||
);
|
||||
const bottom = plane(
|
||||
normalize(cross(right, sub(add(nearCenter, scale(up, b)), position))),
|
||||
);
|
||||
const top = plane(
|
||||
normalize(cross(sub(add(nearCenter, scale(up, t)), position), right)),
|
||||
);
|
||||
const nearPlane = { normal: direction, w: -dot(direction, nearCenter) };
|
||||
const back = scale(direction, -1);
|
||||
const farPlane = { normal: back, w: -dot(back, farCenter) };
|
||||
return [left, rightPlane, bottom, top, nearPlane, farPlane];
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a sphere is wholly outside `planes` (from `frustumPlanes`), as
|
||||
* the default renderer's `CullingVolume.computeVisibility` reports
|
||||
* `Intersect.OUTSIDE`.
|
||||
* @param {ReturnType<typeof frustumPlanes>} planes
|
||||
* @param {{x: number, y: number, z: number}} center
|
||||
* @param {number} radius
|
||||
*/
|
||||
export function sphereOutside(planes, center, radius) {
|
||||
for (const { normal, w } of planes)
|
||||
if (dot(normal, center) + w < -radius) return true;
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import * as Cesium from 'cesium';
|
||||
import { frustumPlanes, sphereOutside } from './frustum.js';
|
||||
|
||||
/** A camera pose and the Cesium frustum it describes. */
|
||||
function camera({ position, direction, up, fovy, aspectRatio, near, far }) {
|
||||
const frustum = new Cesium.PerspectiveFrustum({
|
||||
fov:
|
||||
aspectRatio <= 1 ? fovy : 2 * Math.atan(Math.tan(fovy / 2) * aspectRatio),
|
||||
aspectRatio,
|
||||
near,
|
||||
far,
|
||||
});
|
||||
const volume = frustum.computeCullingVolume(
|
||||
new Cesium.Cartesian3(position.x, position.y, position.z),
|
||||
new Cesium.Cartesian3(direction.x, direction.y, direction.z),
|
||||
new Cesium.Cartesian3(up.x, up.y, up.z),
|
||||
);
|
||||
return {
|
||||
pose: { position, direction, up, fovy, aspectRatio, near, far },
|
||||
volume,
|
||||
};
|
||||
}
|
||||
|
||||
const unit = (x, y, z) => {
|
||||
const m = Math.hypot(x, y, z);
|
||||
return { x: x / m, y: y / m, z: z / m };
|
||||
};
|
||||
|
||||
test('the frustum culls spheres as the default renderer does', () => {
|
||||
const eye = Cesium.Cartesian3.fromDegrees(-122.4, 37.6, 9000);
|
||||
const down = unit(-eye.x, -eye.y, -eye.z);
|
||||
const east = unit(-eye.y, eye.x, 0);
|
||||
const up = Cesium.Cartesian3.normalize(
|
||||
Cesium.Cartesian3.cross(east, down, new Cesium.Cartesian3()),
|
||||
new Cesium.Cartesian3(),
|
||||
);
|
||||
for (const aspectRatio of [0.6, 1, 1.8]) {
|
||||
const { pose, volume } = camera({
|
||||
position: { x: eye.x, y: eye.y, z: eye.z },
|
||||
direction: down,
|
||||
up: { x: up.x, y: up.y, z: up.z },
|
||||
fovy: 1.0,
|
||||
aspectRatio,
|
||||
near: 0.1,
|
||||
far: 5e8,
|
||||
});
|
||||
const planes = frustumPlanes(pose);
|
||||
let outside = 0;
|
||||
for (let i = 0; i < 400; i++) {
|
||||
const lon = -122.4 + ((i % 20) - 10) * 0.03;
|
||||
const lat = 37.6 + (Math.floor(i / 20) - 10) * 0.03;
|
||||
const center = Cesium.Cartesian3.fromDegrees(lon, lat, (i % 7) * 400);
|
||||
for (const radius of [1, 500]) {
|
||||
const theirs =
|
||||
volume.computeVisibility(
|
||||
new Cesium.BoundingSphere(center, radius),
|
||||
) === Cesium.Intersect.OUTSIDE;
|
||||
assert.equal(
|
||||
sphereOutside(planes, center, radius),
|
||||
theirs,
|
||||
`${i} ${radius}`,
|
||||
);
|
||||
if (theirs) outside += 1;
|
||||
}
|
||||
}
|
||||
// Some in, some out, so both answers are exercised.
|
||||
assert.ok(outside > 0 && outside < 800);
|
||||
// Behind the eye.
|
||||
const behind = Cesium.Cartesian3.fromDegrees(-122.4, 37.6, 20000);
|
||||
assert.equal(sphereOutside(planes, behind, 1), true);
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user