mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-17 00:32:21 +00:00
Draw CCTV camera icons through the renderer ports and take rendering.js off the Cesium list
The camera icons are now a draw-port icon set kept by an icon book; only icons a view pass or restyle changes are written, in one write per pass. Picks read the renderer's hit, the view pass and focus pass project through the camera pose (geo/frustum.js poseProjector, matching the renderer's projection and near clip) and the query port, and tile readiness for ground sampling comes from the globe port.
This commit is contained in:
1 parent
2a607ec4d8
commit
1d08043bf9
20 files changed
+810
-457
No files matched your search
@@ -60,3 +60,42 @@ export function sphereOutside(planes, center, radius) {
|
||||
if (dot(normal, center) + w < -radius) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Canvas coordinates (CSS px, origin top left) of world points on a `width`
|
||||
* by `height` canvas, as the default renderer projects them from `pose`
|
||||
* (its frustum's projection matrix times its view matrix, centred, no
|
||||
* offset): one pose read for many points, rather than a renderer call per
|
||||
* point. Null behind the eye and before the projection's clip plane at
|
||||
* `2·far·near / (far + near)` (a little past `near`; `2·near` with no far
|
||||
* plane), where the renderer's own projection answers nothing.
|
||||
* @param {{position: object, direction: object, up: object, fovy: number,
|
||||
* aspectRatio: number, near: number, far: number}} pose
|
||||
* @param {number} width
|
||||
* @param {number} height
|
||||
* @param {{x: number, y: number}} [out] Written and returned by every call.
|
||||
* @returns {(p: {x: number, y: number, z: number}) =>
|
||||
* ({x: number, y: number}|null)}
|
||||
*/
|
||||
export function poseProjector(pose, width, height, out = { x: 0, y: 0 }) {
|
||||
const { position: e, direction: d, up: u, near, far } = pose;
|
||||
const r = cross(d, u);
|
||||
const tanY = Math.tan(0.5 * pose.fovy);
|
||||
const tanX = pose.aspectRatio * tanY;
|
||||
// With no far plane the clip plane is at twice `near`, its limit.
|
||||
const clipDepth = Number.isFinite(far)
|
||||
? (2 * far * near) / (far + near)
|
||||
: 2 * near;
|
||||
return (p) => {
|
||||
const vx = p.x - e.x;
|
||||
const vy = p.y - e.y;
|
||||
const vz = p.z - e.z;
|
||||
const depth = vx * d.x + vy * d.y + vz * d.z;
|
||||
if (!(depth > 0) || depth < clipDepth) return null;
|
||||
const x = (vx * r.x + vy * r.y + vz * r.z) / (depth * tanX);
|
||||
const y = (vx * u.x + vy * u.y + vz * u.z) / (depth * tanY);
|
||||
out.x = (x + 1) * 0.5 * width;
|
||||
out.y = (1 - y) * 0.5 * height;
|
||||
return out;
|
||||
};
|
||||
}
|
||||
+108
-1
@@ -1,7 +1,7 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import * as Cesium from 'cesium';
|
||||
import { frustumPlanes, sphereOutside } from './frustum.js';
|
||||
import { frustumPlanes, poseProjector, sphereOutside } from './frustum.js';
|
||||
|
||||
/** A camera pose and the Cesium frustum it describes. */
|
||||
function camera({ position, direction, up, fovy, aspectRatio, near, far }) {
|
||||
@@ -72,3 +72,110 @@ test('the frustum culls spheres as the default renderer does', () => {
|
||||
assert.equal(sphereOutside(planes, behind, 1), true);
|
||||
}
|
||||
});
|
||||
|
||||
test('points project to the canvas as the default renderer projects them', () => {
|
||||
const eye = Cesium.Cartesian3.fromDegrees(-0.12, 51.5, 600);
|
||||
const target = Cesium.Cartesian3.fromDegrees(-0.11, 51.505, 0);
|
||||
const direction = Cesium.Cartesian3.normalize(
|
||||
Cesium.Cartesian3.subtract(target, eye, new Cesium.Cartesian3()),
|
||||
new Cesium.Cartesian3(),
|
||||
);
|
||||
const east = unit(-eye.y, eye.x, 0);
|
||||
const up = Cesium.Cartesian3.normalize(
|
||||
Cesium.Cartesian3.cross(east, direction, new Cesium.Cartesian3()),
|
||||
new Cesium.Cartesian3(),
|
||||
);
|
||||
// Landscape and portrait canvases, each at a near and a far `near`.
|
||||
for (const [width, height, near] of [
|
||||
[1280, 720, 0.1],
|
||||
[1280, 720, 25],
|
||||
[600, 900, 0.1],
|
||||
]) {
|
||||
const { pose } = camera({
|
||||
position: { x: eye.x, y: eye.y, z: eye.z },
|
||||
direction: { x: direction.x, y: direction.y, z: direction.z },
|
||||
up: { x: up.x, y: up.y, z: up.z },
|
||||
fovy: 0.9,
|
||||
aspectRatio: width / height,
|
||||
near,
|
||||
far: 5e8,
|
||||
});
|
||||
// The renderer's view-projection: its frustum's projection matrix times
|
||||
// the view matrix of the eye's right, up and direction.
|
||||
const frustum = new Cesium.PerspectiveFrustum({
|
||||
// Cesium's `fov` is across the canvas's wider side.
|
||||
fov:
|
||||
pose.aspectRatio <= 1
|
||||
? pose.fovy
|
||||
: 2 * Math.atan(Math.tan(pose.fovy / 2) * pose.aspectRatio),
|
||||
aspectRatio: pose.aspectRatio,
|
||||
near,
|
||||
far: pose.far,
|
||||
});
|
||||
const right = Cesium.Cartesian3.cross(
|
||||
direction,
|
||||
up,
|
||||
new Cesium.Cartesian3(),
|
||||
);
|
||||
const view = Cesium.Matrix4.computeView(
|
||||
eye,
|
||||
direction,
|
||||
up,
|
||||
right,
|
||||
new Cesium.Matrix4(),
|
||||
);
|
||||
const m = Cesium.Matrix4.multiply(
|
||||
frustum.projectionMatrix,
|
||||
view,
|
||||
new Cesium.Matrix4(),
|
||||
);
|
||||
const project = poseProjector(pose, width, height);
|
||||
let shown = 0;
|
||||
for (let i = 0; i < 400; i++) {
|
||||
const p = Cesium.Cartesian3.fromDegrees(
|
||||
-0.12 + ((i % 20) - 10) * 0.002,
|
||||
51.5 + (Math.floor(i / 20) - 10) * 0.002,
|
||||
(i % 9) * 80,
|
||||
);
|
||||
const clip = Cesium.Matrix4.multiplyByVector(
|
||||
m,
|
||||
new Cesium.Cartesian4(p.x, p.y, p.z, 1),
|
||||
new Cesium.Cartesian4(),
|
||||
);
|
||||
const ours = project(p);
|
||||
if (!(clip.w > 0) || clip.z < 0) {
|
||||
assert.equal(ours, null, `${i} is not drawn`);
|
||||
continue;
|
||||
}
|
||||
shown += 1;
|
||||
const x = (clip.x / clip.w + 1) * 0.5 * width;
|
||||
const y = (1 - clip.y / clip.w) * 0.5 * height;
|
||||
// Off-screen points land far out: compared to a part in 1e9.
|
||||
const close = (a, b) => Math.abs(a - b) <= 1e-6 + 1e-9 * Math.abs(b);
|
||||
assert.ok(close(ours.x, x), `${i} x`);
|
||||
assert.ok(close(ours.y, y), `${i} y`);
|
||||
}
|
||||
assert.ok(shown > 0 && shown < 400, 'some points in front, some not');
|
||||
}
|
||||
// Just before the clip plane past `near`: nothing, as the renderer.
|
||||
const { pose } = camera({
|
||||
position: { x: 0, y: 0, z: 0 },
|
||||
direction: { x: 1, y: 0, z: 0 },
|
||||
up: { x: 0, y: 0, z: 1 },
|
||||
fovy: 1,
|
||||
aspectRatio: 1,
|
||||
near: 10,
|
||||
far: 1000,
|
||||
});
|
||||
const project = poseProjector(pose, 100, 100);
|
||||
const clipDepth = (2 * 1000 * 10) / 1010;
|
||||
assert.equal(project({ x: clipDepth - 1e-6, y: 0, z: 0 }), null);
|
||||
assert.deepEqual(project({ x: clipDepth + 1e-6, y: 0, z: 0 }), {
|
||||
x: 50,
|
||||
y: 50,
|
||||
});
|
||||
// An infinite far plane clips at twice `near`, the limit of the finite rule.
|
||||
const endless = poseProjector({ ...pose, far: Infinity }, 100, 100);
|
||||
assert.equal(endless({ x: 20 - 1e-6, y: 0, z: 0 }), null);
|
||||
assert.deepEqual(endless({ x: 20 + 1e-6, y: 0, z: 0 }), { x: 50, y: 50 });
|
||||
});
|
||||
Reference in new issue
Block a user