mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-22 03:02:20 +00:00
Draw CCTV and data packs through engine.meshes
Add indexed world triangles to the required globe contract on Cesium, Navara and the fake engine. Own geometry, textures, visibility, depth tests, pick identities and teardown inside each engine. Reused canvas images request another frame until their new pixels upload. Move obstruction rays and owned mesh, line and mover exclusions into the Cesium surface adapter. Draw CCTV monitor planes and viewshed volumes through shared mesh handles. Draw data-pack points, straight lines, raised polygons, image rectangles and media captions through movers, lines and meshes. Delete the feature renderers for each engine and retain decoded assets across scene rebinds. Register the modules, dependency, format scope and reduced coupling baseline. Cesium is the reference for geometry, colours, image placement, marker sizes, caption offsets, picking and resource ownership. Navara now uses straight pack lines and per-position polygon heights without edge densification. Navara meshes draw in the transparent pass after the effects, with the scene-depth test and the view's exposure undone, as the pack meshes did: in the scene pass the landscape look's exposure washed a red pack image to white. The scene-depth helper moves into the Navara engine, and the stems helper that only its test still used is deleted. The mesh picker takes the globe when Navara's depth read answers with the camera itself, which it does right after another click and which hid a polygon from a quick second click. The 256 px media caption is not pickable: its transparent square took clicks meant for nearby pack features, and no action targets it. Annotation drapes remain on their existing renderers because bounded building classification, animated fills and wire cages need a separate contract extension to preserve Cesium behaviour. Coupling falls from 344 to 210 Cesium layer-renderer lines, 1294 to 794 Navara layer-renderer lines, 9 to 7 external Cesium import files, 1026 to 1005 viewer lines, 9 to 6 extension reads and 42 to 39 engine-name lines. Other counts do not grow. The full unit suite, import directions, package boundaries, formatting and coupling checks pass. No user-visible strings change. In the browser, keyless: the contract suite passes on Navara and on Cesium apart from the camera-events check that also fails intermittently before this change; CCTV v2 (58 checks), the CCTV and director engine switches pass, with the pack image at the same pixel as before on both engines. The mesh contract fixture now sits at 3 km, above the terrain Navara draws there. The director interactions script starts at its view in the link so the app's arrival flight no longer lands after the shot, and waits for the click's asynchronous pick; its last step, the next-shot action, fails the same way before this change.
This commit is contained in:
1 parent
3907b7084e
commit
f4b83a3a31
57 files changed
+1891
-1588
No files matched your search
@@ -1,133 +0,0 @@
|
||||
// The CCTV meshes on Cesium (behind render/meshes.js): the monitor plane is
|
||||
// a plane Entity with an image material, the viewshed volume a synchronous
|
||||
// Primitive of six triangles in a flat translucent colour, both faces drawn,
|
||||
// never picked. The activation probe's ray pick lives here too, while the
|
||||
// Cesium engine has no surface.raycast.
|
||||
|
||||
import * as Cesium from 'cesium';
|
||||
import { frustumVolumeGeometryData } from '../../../data/cctvViewshed.js';
|
||||
import { PLANE_IMAGE_ALPHA } from './style.js';
|
||||
|
||||
const toColor = (tint) => new Cesium.Color(...tint.rgba());
|
||||
|
||||
/** The frustum volume's Primitive over positions { mount, tl, tr, br, bl }. */
|
||||
export function createFrustumVolumePrimitive(positions, tint) {
|
||||
const { positions: flat, indices } = frustumVolumeGeometryData(positions);
|
||||
const geometry = new Cesium.Geometry({
|
||||
attributes: {
|
||||
position: new Cesium.GeometryAttribute({
|
||||
componentDatatype: Cesium.ComponentDatatype.DOUBLE,
|
||||
componentsPerAttribute: 3,
|
||||
values: flat,
|
||||
}),
|
||||
},
|
||||
indices,
|
||||
primitiveType: Cesium.PrimitiveType.TRIANGLES,
|
||||
boundingSphere: Cesium.BoundingSphere.fromVertices(Array.from(flat)),
|
||||
});
|
||||
return new Cesium.Primitive({
|
||||
geometryInstances: new Cesium.GeometryInstance({
|
||||
geometry,
|
||||
attributes: {
|
||||
color: Cesium.ColorGeometryInstanceAttribute.fromColor(toColor(tint)),
|
||||
},
|
||||
}),
|
||||
appearance: new Cesium.PerInstanceColorAppearance({
|
||||
flat: true,
|
||||
translucent: true,
|
||||
// The camera is routinely inside or behind a cone.
|
||||
renderState: { cull: { enabled: false } },
|
||||
}),
|
||||
asynchronous: false,
|
||||
// Clicks fall through to the markers, wedges and plane.
|
||||
allowPicking: false,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {{ viewer: Cesium.Viewer }} cesium The engine's Cesium extension.
|
||||
*/
|
||||
export function createCctvCesiumMeshes({ viewer: { entities, scene } }) {
|
||||
const planes = new Map();
|
||||
const volumes = new Set();
|
||||
const dimensions = (shape) =>
|
||||
new Cesium.Cartesian2(shape.halfW * 2, shape.halfH * 2);
|
||||
return {
|
||||
createMaterial(image) {
|
||||
const property = image
|
||||
? new Cesium.ImageMaterialProperty({
|
||||
image,
|
||||
transparent: true,
|
||||
color: Cesium.Color.WHITE.withAlpha(PLANE_IMAGE_ALPHA),
|
||||
})
|
||||
: new Cesium.ColorMaterialProperty(Cesium.Color.WHITE);
|
||||
return {
|
||||
property,
|
||||
setImage(next) {
|
||||
property.image = next;
|
||||
},
|
||||
dispose() {},
|
||||
};
|
||||
},
|
||||
createPlane(cameraId, material, shape, positions, orientation) {
|
||||
const entity = entities.add({
|
||||
id: `cctv-${cameraId}-plane`,
|
||||
show: false,
|
||||
position: positions.capCenter,
|
||||
orientation: Cesium.Quaternion.clone(orientation),
|
||||
plane: {
|
||||
plane: new Cesium.Plane(Cesium.Cartesian3.UNIT_Z, 0.0),
|
||||
dimensions: dimensions(shape),
|
||||
material: material.property,
|
||||
},
|
||||
});
|
||||
const plane = {
|
||||
entity,
|
||||
get show() {
|
||||
return entity.show;
|
||||
},
|
||||
set show(value) {
|
||||
entity.show = value;
|
||||
},
|
||||
destroy() {
|
||||
if (!planes.delete(entity)) return;
|
||||
if (entities.getById(entity.id) === entity) entities.remove(entity);
|
||||
},
|
||||
};
|
||||
planes.set(entity, plane);
|
||||
return plane;
|
||||
},
|
||||
updatePlane({ entity }, shape, positions, orientation) {
|
||||
entity.position = positions.capCenter;
|
||||
entity.orientation = Cesium.Quaternion.clone(orientation);
|
||||
if (entity.plane) entity.plane.dimensions = dimensions(shape);
|
||||
},
|
||||
createVolume(positions, tint) {
|
||||
const primitive = createFrustumVolumePrimitive(positions, tint);
|
||||
scene.primitives.add(primitive);
|
||||
volumes.add(primitive);
|
||||
return {
|
||||
primitive,
|
||||
destroy() {
|
||||
if (volumes.delete(primitive)) scene.primitives.remove(primitive);
|
||||
},
|
||||
};
|
||||
},
|
||||
// The engine's pick result keeps Cesium's own id: the plane Entity.
|
||||
planeFromPick: (picked) => planes.get(picked.id) ?? null,
|
||||
drawnBy: (native) => native.entity ?? native.primitive,
|
||||
obstructionRay(origin, direction, far, exclude) {
|
||||
if (!scene.pickFromRay) return undefined;
|
||||
const hit = scene.pickFromRay(new Cesium.Ray(origin, direction), exclude);
|
||||
return hit?.position &&
|
||||
Cesium.Cartesian3.distance(origin, hit.position) <= far
|
||||
? hit
|
||||
: undefined;
|
||||
},
|
||||
destroy() {
|
||||
for (const plane of [...planes.values()]) plane.destroy();
|
||||
for (const primitive of volumes) scene.primitives.remove(primitive);
|
||||
volumes.clear();
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
// The CCTV cameras on the globe contract (engine.movers, engine.lines,
|
||||
// The CCTV cameras on the globe contract (engine.movers, engine.lines, engine.meshes,
|
||||
// engine.input, engine.picker, engine.camera, engine.projector,
|
||||
// engine.surface, engine.credits), the same on every engine:
|
||||
// - the camera markers, one sprite set (24 px, shrinking with distance,
|
||||
@@ -9,8 +9,8 @@
|
||||
// or depth fail changes;
|
||||
// - the calibration gizmo: rings and arrow shafts as lines, arrow tips and
|
||||
// handles in one point set, drags through engine.input;
|
||||
// - the monitor plane and the viewshed volume behind render/meshes.js, the
|
||||
// engine-specific part the contract has no primitive for yet.
|
||||
// - the monitor plane and the viewshed volume as world triangles through
|
||||
// engine.meshes (render/meshes.js).
|
||||
// The layer (../index.js) keeps the records, the policy and the style
|
||||
// choices (../rendering.js, ./style.js).
|
||||
|
||||
@@ -86,16 +86,11 @@ export function planeOutline(shape, positions, orientation) {
|
||||
|
||||
/**
|
||||
* @param {import('../../../globe/contract.js').GlobeEngine} engine
|
||||
* @param {{ state: object, services: object, navaraMeshes?: Function }} options
|
||||
* The layer's state and services; `navaraMeshes` is the Navara meshes
|
||||
* backend (render/navaraMeshes.js), which the Navara mode passes in.
|
||||
* @param {{ state: object, services: object }} options
|
||||
*/
|
||||
export function createCctvDisplay(
|
||||
engine,
|
||||
{ state, services = {}, navaraMeshes } = {},
|
||||
) {
|
||||
export function createCctvDisplay(engine, { state, services = {} } = {}) {
|
||||
if (!engine) throw new TypeError('The CCTV display needs a globe engine');
|
||||
const meshes = createCctvMeshes(engine, { navaraMeshes });
|
||||
const meshes = createCctvMeshes(engine);
|
||||
const markers = [];
|
||||
const markerHandles = new WeakSet();
|
||||
const changedMarkers = new Set();
|
||||
@@ -328,25 +323,6 @@ export function createCctvDisplay(
|
||||
const engineObjects = (handles = []) => [
|
||||
...new Set(handles.flatMap(drawnObjects)),
|
||||
];
|
||||
/**
|
||||
* The same for a ray pick that skips objects by pick id (Cesium's): every
|
||||
* marker, as the whole set was, and the listed lines, planes and volumes.
|
||||
*/
|
||||
function rayExclusions(handles = []) {
|
||||
const exclude = markers.map((marker) => marker.id);
|
||||
for (const handle of handles) {
|
||||
if (!handle || ownsMarker(handle)) continue;
|
||||
if (lines.get(handle.id) === handle) exclude.push(handle.pickId);
|
||||
else if (planes.has(handle))
|
||||
exclude.push(
|
||||
handle.outline.pickId,
|
||||
...[meshes.drawnBy(handle.mesh)].filter(Boolean),
|
||||
);
|
||||
else exclude.push(...[meshes.drawnBy(handle)].filter(Boolean));
|
||||
}
|
||||
return exclude;
|
||||
}
|
||||
|
||||
// ── Gizmo ───────────────────────────────────────────────────────────────
|
||||
function createGizmoRenderer() {
|
||||
const handles = new Map();
|
||||
@@ -727,12 +703,7 @@ export function createCctvDisplay(
|
||||
far,
|
||||
exclude: [markerSet, ...engineObjects(exclude)].filter(Boolean),
|
||||
});
|
||||
return meshes.obstructionRay(
|
||||
origin,
|
||||
direction,
|
||||
far,
|
||||
rayExclusions(exclude),
|
||||
);
|
||||
return undefined;
|
||||
},
|
||||
createPlaneMaterial: (image) => meshes.createMaterial(image),
|
||||
createTestPlaneMaterial: () => meshes.createMaterial(null),
|
||||
|
||||
@@ -359,17 +359,11 @@ for (const makeHarness of CCTV_HARNESSES) {
|
||||
near(geo.cartesian3.distance(corners[index], expected), 0, 'corner');
|
||||
}
|
||||
if (h.cesium) {
|
||||
const entity = h.viewer.entities.getById('cctv-a-plane');
|
||||
const now = Cesium.JulianDate.now();
|
||||
assert.equal(entity.show, true);
|
||||
assert.deepEqual(
|
||||
entity.plane.dimensions.getValue(now),
|
||||
new Cesium.Cartesian2(40, 22.5),
|
||||
);
|
||||
assert.equal(entity.plane.outline, undefined, 'the outline is a line');
|
||||
assert.equal(entity.plane.material.color.getValue(now).alpha, 0.95);
|
||||
assert.equal(entity.plane.material.transparent.getValue(now), true);
|
||||
h.scriptPicks({ native: { id: entity, primitive: {} } });
|
||||
const primitive = h.primitive('cctv-a-plane');
|
||||
assert.equal(primitive.show, true);
|
||||
assert.equal(primitive.appearance.material.uniforms.color.alpha, 0.95);
|
||||
assert.equal(primitive.allowPicking, true);
|
||||
h.scriptPicks({ native: { id: 'cctv-a-plane', primitive } });
|
||||
assert.equal(
|
||||
h.display.extractPickedCameraId(h.display.pick({ x: 1, y: 1 })),
|
||||
null,
|
||||
@@ -433,10 +427,10 @@ for (const makeHarness of CCTV_HARNESSES) {
|
||||
assert.equal(excludeObjects.length, 6);
|
||||
const direction = p(0, 1, 0);
|
||||
if (h.cesium) {
|
||||
const primitive = volume.native.primitive;
|
||||
const primitive = h.primitive(volume.mesh.id);
|
||||
assert.equal(primitive.allowPicking, false);
|
||||
assert.equal(primitive.asynchronous, false);
|
||||
assert.ok(h.viewer.scene.primitives.contains(primitive));
|
||||
assert.ok(primitive);
|
||||
let query;
|
||||
h.viewer.scene.pickFromRay = (ray, exclusions) => {
|
||||
query = { ray, exclusions };
|
||||
@@ -458,15 +452,10 @@ for (const makeHarness of CCTV_HARNESSES) {
|
||||
h.display.obstructionRay(POSITIONS.mount, direction, exclude, 30),
|
||||
);
|
||||
assert.ok(query.ray instanceof Cesium.Ray);
|
||||
assert.deepEqual(
|
||||
query.exclusions.slice(0, 2),
|
||||
['a', 'b'],
|
||||
'every marker',
|
||||
);
|
||||
assert.ok(query.exclusions.includes('cctv-a-ray-tl'));
|
||||
assert.ok(query.exclusions.includes(primitive));
|
||||
assert.equal(primitive.show, true, 'the excluded volume is restored');
|
||||
assert.ok(query.exclusions.every((item) => typeof item !== 'string'));
|
||||
h.display.removeVolume(volume);
|
||||
assert.ok(!h.viewer.scene.primitives.contains(primitive));
|
||||
assert.equal(h.primitive(volume.mesh.id), undefined);
|
||||
} else {
|
||||
assert.equal(
|
||||
h.display.obstructionRay(POSITIONS.mount, direction, [], 30),
|
||||
@@ -598,34 +587,11 @@ test('the plane outline closes on its first corner', () => {
|
||||
assert.deepEqual({ ...corners[2] }, { x: 12, y: -1, z: 0 });
|
||||
});
|
||||
|
||||
test('on Navara the display draws its plane through the backend it is given', async (t) => {
|
||||
const { createCctvNavaraMeshes } = await import('./navaraMeshes.js');
|
||||
const engine = createFakeEngine({ kind: 'navara' });
|
||||
const descriptors = new Map();
|
||||
const view = {
|
||||
registerMesh: (key, value) => descriptors.set(key, value),
|
||||
addMesh(config) {
|
||||
const key = Object.keys(config)[0];
|
||||
const desc = new (descriptors.get(key))(view, {}, config);
|
||||
desc.createMesh();
|
||||
return { delete: () => desc.onDestroy() };
|
||||
},
|
||||
};
|
||||
class MeshDesc {
|
||||
onDestroy() {}
|
||||
}
|
||||
const pickers = new Set();
|
||||
engine.picker.registerMeshPicker = (pick) => {
|
||||
pickers.add(pick);
|
||||
return () => pickers.delete(pick);
|
||||
};
|
||||
engine.extensions = { navara: { view, MeshDesc } };
|
||||
const state = { _recordById: new Map(), _projectionEntities: [] };
|
||||
const display = createCctvDisplay(engine, {
|
||||
state,
|
||||
navaraMeshes: createCctvNavaraMeshes,
|
||||
});
|
||||
test('the display draws and picks a plane on the fake engine', (t) => {
|
||||
const engine = createFakeEngine();
|
||||
t.after(() => engine.destroy());
|
||||
const state = { _recordById: new Map(), _projectionEntities: [] };
|
||||
const display = createCctvDisplay(engine, { state });
|
||||
display.attach();
|
||||
const record = recordFor(state, 'a');
|
||||
const runtime = { planeMaterial: display.createPlaneMaterial(null) };
|
||||
@@ -637,12 +603,11 @@ test('on Navara the display draws its plane through the backend it is given', as
|
||||
{ x: 0, y: 0, z: 0, w: 1 },
|
||||
);
|
||||
record.projection = runtime;
|
||||
assert.equal(pickers.size, 1);
|
||||
const picked = { id: 'cctv-a-plane', owner: 'cctv-a-plane' };
|
||||
assert.equal(display.extractPickedCameraId(picked), null, 'hidden');
|
||||
display.setPlaneVisible(runtime, true);
|
||||
assert.equal(runtime.planeEntity.mesh.native.group.visible, true);
|
||||
assert.equal(engine.fake.mesh(picked.id).show, true);
|
||||
assert.equal(display.extractPickedCameraId(picked), 'a');
|
||||
display.destroy();
|
||||
assert.equal(pickers.size, 0);
|
||||
assert.deepEqual(engine.meshes.list(), []);
|
||||
});
|
||||
@@ -1,171 +1,151 @@
|
||||
// The CCTV meshes the globe contract has no primitive for yet, behind one
|
||||
// seam: the monitor plane (a quad textured with a live canvas or video) and
|
||||
// the viewshed volume (the frustum pyramid in a flat translucent colour,
|
||||
// both faces, not pickable). Movers draw a glTF per instance at a uniform
|
||||
// scale and lines draw strips, so neither carries a texture that changes
|
||||
// every frame or a pyramid whose corners move independently.
|
||||
//
|
||||
// This is the only CCTV module that reads engine.extensions. Each engine
|
||||
// draws through its own backend: render/cesiumMeshes.js (Cesium entities and
|
||||
// primitives) and render/navaraMeshes.js (three meshes in Navara's view),
|
||||
// which the Navara mode loads and passes in, as Navara modules reach the app
|
||||
// only through its import(). The handles here keep what the layer asked
|
||||
// for, so an engine with no backend (the fake engine) draws nothing and
|
||||
// still answers. The plane's outline is an engine line, drawn by the display.
|
||||
// CCTV's live monitor quad and translucent viewshed triangles.
|
||||
import * as geo from '../../../geo/index.js';
|
||||
import { frustumVolumeGeometryData } from '../../../data/cctvViewshed.js';
|
||||
import { PLANE_IMAGE_ALPHA } from './style.js';
|
||||
|
||||
import { createCctvCesiumMeshes } from './cesiumMeshes.js';
|
||||
function planeGeometry(shape, positions, orientation) {
|
||||
const rotation = geo.matrix3.fromQuaternion(
|
||||
orientation,
|
||||
geo.matrix3.create(),
|
||||
);
|
||||
const flat = [];
|
||||
for (const [x, y] of [
|
||||
[-1, 1],
|
||||
[1, 1],
|
||||
[-1, -1],
|
||||
[1, -1],
|
||||
]) {
|
||||
const p = geo.matrix3.multiplyByVector(
|
||||
rotation,
|
||||
{ x: x * shape.halfW, y: y * shape.halfH, z: 0 },
|
||||
geo.cartesian3.create(),
|
||||
);
|
||||
geo.cartesian3.add(p, positions.capCenter, p);
|
||||
flat.push(p.x, p.y, p.z);
|
||||
}
|
||||
return {
|
||||
positions: flat,
|
||||
indices: [0, 2, 1, 2, 3, 1],
|
||||
uv: [0, 1, 1, 1, 0, 0, 1, 0],
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import('../../../globe/contract.js').GlobeEngine} engine
|
||||
* @param {{ navaraMeshes?: (engine: object, navara: object) => object }} [options]
|
||||
* The Navara backend's factory (render/navaraMeshes.js).
|
||||
*/
|
||||
export function createCctvMeshes(engine, { navaraMeshes } = {}) {
|
||||
const cesium = engine.extensions?.cesium;
|
||||
const navara = engine.extensions?.navara;
|
||||
export function createCctvMeshes(engine) {
|
||||
const materials = new Set();
|
||||
const planes = new Set();
|
||||
const volumes = new Set();
|
||||
const planeByNative = new Map();
|
||||
const backend = cesium
|
||||
? createCctvCesiumMeshes(cesium)
|
||||
: navara && navaraMeshes
|
||||
? navaraMeshes(engine, navara)
|
||||
: null;
|
||||
let destroyed = false;
|
||||
|
||||
let volumeId = 0;
|
||||
const alive = () => {
|
||||
if (destroyed) throw new Error('CCTV meshes are destroyed');
|
||||
};
|
||||
|
||||
const appearance = (material) => ({
|
||||
image: material.image,
|
||||
color: [1, 1, 1, material.image ? PLANE_IMAGE_ALPHA : 1],
|
||||
});
|
||||
return {
|
||||
/** A plane material showing `image` (a canvas or video), or plain white. */
|
||||
createMaterial(image) {
|
||||
alive();
|
||||
const material = {
|
||||
image: image ?? null,
|
||||
native: null,
|
||||
disposed: false,
|
||||
setImage(next) {
|
||||
if (material.disposed) return;
|
||||
material.image = next;
|
||||
material.native?.setImage(next);
|
||||
for (const plane of planes)
|
||||
if (plane.material === material)
|
||||
plane.mesh.update(appearance(material));
|
||||
},
|
||||
dispose() {
|
||||
if (material.disposed) return;
|
||||
material.disposed = true;
|
||||
materials.delete(material);
|
||||
material.native?.dispose();
|
||||
material.native = null;
|
||||
},
|
||||
};
|
||||
material.native = backend?.createMaterial(material.image) ?? null;
|
||||
materials.add(material);
|
||||
return material;
|
||||
},
|
||||
/**
|
||||
* The monitor plane, hidden: `shape` { halfW, halfH } metres, centred on
|
||||
* positions.capCenter, its local +Z along `orientation`'s normal.
|
||||
*/
|
||||
createPlane(cameraId, material, shape, positions, orientation) {
|
||||
alive();
|
||||
if (!materials.has(material))
|
||||
throw new Error('CCTV plane material is not owned');
|
||||
const id = `cctv-${cameraId}-plane`;
|
||||
const mesh = engine.meshes.createMesh({
|
||||
id,
|
||||
pickId: id,
|
||||
show: false,
|
||||
...planeGeometry(shape, positions, orientation),
|
||||
...appearance(material),
|
||||
});
|
||||
const plane = {
|
||||
id: `cctv-${cameraId}-plane`,
|
||||
id,
|
||||
cameraId,
|
||||
material,
|
||||
shape,
|
||||
positions,
|
||||
orientation,
|
||||
visible: false,
|
||||
native: null,
|
||||
mesh,
|
||||
destroyed: false,
|
||||
get show() {
|
||||
return !plane.destroyed && plane.visible;
|
||||
return mesh.show();
|
||||
},
|
||||
set show(value) {
|
||||
if (plane.destroyed) return;
|
||||
plane.visible = Boolean(value);
|
||||
if (plane.native) plane.native.show = plane.visible;
|
||||
mesh.show(value);
|
||||
},
|
||||
destroy() {
|
||||
if (plane.destroyed) return;
|
||||
plane.destroyed = true;
|
||||
planes.delete(plane);
|
||||
if (plane.native) {
|
||||
planeByNative.delete(plane.native);
|
||||
plane.native.destroy();
|
||||
plane.native = null;
|
||||
}
|
||||
mesh.destroy();
|
||||
},
|
||||
};
|
||||
if (backend) {
|
||||
plane.native = backend.createPlane(
|
||||
cameraId,
|
||||
material.native,
|
||||
shape,
|
||||
positions,
|
||||
orientation,
|
||||
);
|
||||
planeByNative.set(plane.native, plane);
|
||||
}
|
||||
planes.add(plane);
|
||||
return plane;
|
||||
},
|
||||
updatePlane(plane, shape, positions, orientation) {
|
||||
if (!planes.has(plane)) return;
|
||||
plane.mesh.update(planeGeometry(shape, positions, orientation));
|
||||
Object.assign(plane, { shape, positions, orientation });
|
||||
if (plane.native)
|
||||
backend.updatePlane(plane.native, shape, positions, orientation);
|
||||
},
|
||||
/** The viewshed volume over positions { mount, tl, tr, br, bl }. */
|
||||
createVolume(positions, tint) {
|
||||
alive();
|
||||
const mesh = engine.meshes.createMesh({
|
||||
id: `cctv-volume-${++volumeId}`,
|
||||
...frustumVolumeGeometryData(positions),
|
||||
color: tint.rgba(),
|
||||
});
|
||||
const volume = {
|
||||
positions,
|
||||
tint,
|
||||
native: null,
|
||||
mesh,
|
||||
destroyed: false,
|
||||
destroy() {
|
||||
if (volume.destroyed) return;
|
||||
volume.destroyed = true;
|
||||
volumes.delete(volume);
|
||||
volume.native?.destroy();
|
||||
volume.native = null;
|
||||
mesh.destroy();
|
||||
},
|
||||
};
|
||||
volume.native = backend?.createVolume(positions, tint) ?? null;
|
||||
volumes.add(volume);
|
||||
return volume;
|
||||
},
|
||||
/** The live, shown plane an engine pick result names, or null. */
|
||||
planeFromPick(picked) {
|
||||
if (!backend || !picked) return null;
|
||||
const plane = planeByNative.get(backend.planeFromPick(picked));
|
||||
return plane?.show ? plane : null;
|
||||
},
|
||||
/** What draws a plane or volume, for an engine `exclude` list. */
|
||||
drawnBy(handle) {
|
||||
return backend && handle?.native
|
||||
? backend.drawnBy(handle.native)
|
||||
: undefined;
|
||||
},
|
||||
/**
|
||||
* The first scene object along a ray within `far` metres, skipping
|
||||
* `exclude` (pick ids and drawn objects), on an engine without
|
||||
* surface.raycast; undefined where the backend has no ray pick.
|
||||
*/
|
||||
obstructionRay(origin, direction, far, exclude) {
|
||||
return backend?.obstructionRay?.(origin, direction, far, exclude);
|
||||
return (
|
||||
[...planes].find(
|
||||
(plane) =>
|
||||
plane.show &&
|
||||
plane.id === picked?.id &&
|
||||
(picked.owner == null || picked.owner === plane.id),
|
||||
) ?? null
|
||||
);
|
||||
},
|
||||
drawnBy: (handle) => handle?.mesh,
|
||||
destroy() {
|
||||
if (destroyed) return;
|
||||
destroyed = true;
|
||||
for (const plane of [...planes]) plane.destroy();
|
||||
for (const volume of [...volumes]) volume.destroy();
|
||||
for (const material of [...materials]) material.dispose();
|
||||
backend?.destroy();
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -18,107 +18,58 @@ const positions = {
|
||||
};
|
||||
const identity = { x: 0, y: 0, z: 0, w: 1 };
|
||||
|
||||
/** A backend that records what it is asked to draw. */
|
||||
function recordingBackend(calls) {
|
||||
const native = (kind, args) => {
|
||||
const object = {
|
||||
kind,
|
||||
args,
|
||||
show: false,
|
||||
destroy: () => calls.push(['destroy', kind]),
|
||||
import { createCesiumMoversEngine } from '../../../testSupport/cesiumMoversEngine.mjs';
|
||||
|
||||
for (const [name, make] of [
|
||||
['fake', () => ({ engine: createFakeEngine() })],
|
||||
['Cesium', createCesiumMoversEngine],
|
||||
]) {
|
||||
test(`CCTV mesh ownership, live image and geometry updates (${name})`, (t) => {
|
||||
const h = make();
|
||||
t.after(() => h.engine.destroy());
|
||||
const meshes = createCctvMeshes(h.engine);
|
||||
const material = meshes.createMaterial(null);
|
||||
const plane = meshes.createPlane('a', material, shape, positions, identity);
|
||||
const volume = meshes.createVolume(positions, viewshedColors(4).fill);
|
||||
assert.deepEqual(h.engine.meshes.list(), ['cctv-a-plane', volume.mesh.id]);
|
||||
assert.equal(plane.show, false);
|
||||
plane.show = true;
|
||||
assert.equal(
|
||||
meshes.planeFromPick({ id: plane.id, owner: plane.id }),
|
||||
plane,
|
||||
);
|
||||
plane.show = false;
|
||||
assert.equal(meshes.planeFromPick({ id: plane.id }), null);
|
||||
assert.equal(meshes.drawnBy(plane), plane.mesh);
|
||||
assert.equal(meshes.drawnBy(volume), volume.mesh);
|
||||
const image = new HTMLCanvasElement();
|
||||
image.width = image.height = 8;
|
||||
material.setImage(image);
|
||||
const moved = {
|
||||
...positions,
|
||||
capCenter: { ...positions.capCenter, x: positions.capCenter.x + 3 },
|
||||
};
|
||||
calls.push(['create', kind, object]);
|
||||
return object;
|
||||
};
|
||||
return {
|
||||
createMaterial: (image) => ({
|
||||
...native('material', [image]),
|
||||
setImage: (image) => calls.push(['image', image]),
|
||||
dispose: () => calls.push(['dispose', 'material']),
|
||||
}),
|
||||
createPlane: (...args) => native('plane', args),
|
||||
updatePlane: (plane, ...args) => calls.push(['update', ...args]),
|
||||
createVolume: (...args) => native('volume', args),
|
||||
planeFromPick: (picked) => picked.native,
|
||||
drawnBy: (object) => object,
|
||||
destroy: () => calls.push(['destroy', 'backend']),
|
||||
};
|
||||
}
|
||||
|
||||
test('an engine without a backend keeps the handles and draws nothing', () => {
|
||||
const engine = createFakeEngine();
|
||||
const meshes = createCctvMeshes(engine);
|
||||
const material = meshes.createMaterial(null);
|
||||
const plane = meshes.createPlane('a', material, shape, positions, identity);
|
||||
assert.equal(plane.id, 'cctv-a-plane');
|
||||
assert.equal(plane.show, false);
|
||||
plane.show = true;
|
||||
assert.equal(plane.show, true);
|
||||
assert.equal(meshes.planeFromPick({ id: plane.id }), null);
|
||||
assert.equal(meshes.drawnBy(plane), undefined);
|
||||
assert.equal(
|
||||
meshes.obstructionRay(positions.mount, positions.tl, 10, []),
|
||||
undefined,
|
||||
);
|
||||
assert.throws(
|
||||
() => meshes.createPlane('b', { image: null }, shape, positions, identity),
|
||||
/not owned/,
|
||||
);
|
||||
meshes.destroy();
|
||||
assert.equal(plane.show, false);
|
||||
assert.equal(material.disposed, true);
|
||||
assert.throws(() => meshes.createMaterial(null), /destroyed/);
|
||||
});
|
||||
|
||||
test('the Navara backend the Navara mode passes in draws the meshes', () => {
|
||||
const engine = createFakeEngine({ kind: 'navara' });
|
||||
const navara = { view: {}, MeshDesc: class {} };
|
||||
engine.extensions = { navara };
|
||||
const calls = [];
|
||||
let given;
|
||||
const meshes = createCctvMeshes(engine, {
|
||||
navaraMeshes(target, extension) {
|
||||
given = { target, extension };
|
||||
return recordingBackend(calls);
|
||||
},
|
||||
meshes.updatePlane(plane, shape, moved, identity);
|
||||
const state = h.engine.fake?.mesh(plane.id);
|
||||
const primitive = h.primitive?.(plane.id);
|
||||
const flat =
|
||||
state?.positions ??
|
||||
primitive.geometryInstances.geometry.attributes.position.values;
|
||||
assert.equal(flat[0], moved.capCenter.x - shape.halfW);
|
||||
assert.equal(
|
||||
state?.image ?? primitive.appearance.material.uniforms.image,
|
||||
image,
|
||||
);
|
||||
assert.throws(
|
||||
() => meshes.createPlane('b', {}, shape, positions, identity),
|
||||
/not owned/,
|
||||
);
|
||||
meshes.destroy();
|
||||
meshes.destroy();
|
||||
assert.equal(plane.show, false);
|
||||
assert.equal(material.disposed, true);
|
||||
assert.deepEqual(h.engine.meshes.list(), []);
|
||||
material.setImage(null);
|
||||
assert.throws(() => meshes.createMaterial(null), /destroyed/);
|
||||
});
|
||||
assert.equal(given.target, engine);
|
||||
assert.equal(given.extension, navara);
|
||||
const material = meshes.createMaterial('canvas');
|
||||
material.setImage('video');
|
||||
const plane = meshes.createPlane('a', material, shape, positions, identity);
|
||||
const volume = meshes.createVolume(positions, viewshedColors(4).fill);
|
||||
const created = calls.filter(([type]) => type === 'create');
|
||||
assert.deepEqual(
|
||||
created.map(([, kind]) => kind),
|
||||
['material', 'plane', 'volume'],
|
||||
);
|
||||
assert.deepEqual(calls[1], ['image', 'video']);
|
||||
const [, , drawnPlane] = created[1];
|
||||
assert.equal(drawnPlane.args[1], material.native, 'its drawn material');
|
||||
plane.show = true;
|
||||
assert.equal(drawnPlane.show, true);
|
||||
assert.equal(meshes.planeFromPick({ native: drawnPlane }), plane);
|
||||
plane.show = false;
|
||||
assert.equal(meshes.planeFromPick({ native: drawnPlane }), null);
|
||||
meshes.updatePlane(plane, shape, positions, identity);
|
||||
assert.deepEqual(calls.at(-1), ['update', shape, positions, identity]);
|
||||
assert.equal(meshes.drawnBy(volume), created[2][2]);
|
||||
meshes.destroy();
|
||||
meshes.destroy();
|
||||
assert.deepEqual(calls.slice(-4), [
|
||||
['destroy', 'plane'],
|
||||
['destroy', 'volume'],
|
||||
['dispose', 'material'],
|
||||
['destroy', 'backend'],
|
||||
]);
|
||||
});
|
||||
|
||||
test('a Navara engine without the backend draws nothing', () => {
|
||||
const engine = createFakeEngine({ kind: 'navara' });
|
||||
engine.extensions = { navara: {} };
|
||||
const meshes = createCctvMeshes(engine);
|
||||
const volume = meshes.createVolume(positions, viewshedColors(4).fill);
|
||||
assert.equal(meshes.drawnBy(volume), undefined);
|
||||
meshes.destroy();
|
||||
});
|
||||
}
|
||||
@@ -1,306 +0,0 @@
|
||||
// The CCTV meshes on Navara (behind render/meshes.js, loaded through
|
||||
// import()): the monitor plane and the viewshed volume as three meshes in
|
||||
// Navara's view, depth-tested against the scene, and a mesh picker for the
|
||||
// plane's own triangles.
|
||||
|
||||
import {
|
||||
BufferAttribute,
|
||||
BufferGeometry,
|
||||
CanvasTexture,
|
||||
DoubleSide,
|
||||
Group,
|
||||
LinearFilter,
|
||||
Mesh,
|
||||
MeshBasicMaterial,
|
||||
Quaternion,
|
||||
Raycaster,
|
||||
SRGBColorSpace,
|
||||
Vector3,
|
||||
VideoTexture,
|
||||
} from 'three';
|
||||
import * as geo from '../../../geo/index.js';
|
||||
import { frustumVolumeGeometryData } from '../../../data/cctvViewshed.js';
|
||||
import { PLANE_IMAGE_ALPHA } from './style.js';
|
||||
|
||||
const KIND = 'gevCctvMesh';
|
||||
const registered = new WeakSet();
|
||||
|
||||
/** Local mesh coordinates keep ECEF precision in the double world matrix. */
|
||||
function placeGeometry(mesh, positions, indices, origin) {
|
||||
const local = new Float32Array(positions.length);
|
||||
for (let i = 0; i < positions.length; i += 3) {
|
||||
local[i] = positions[i] - origin.x;
|
||||
local[i + 1] = positions[i + 1] - origin.y;
|
||||
local[i + 2] = positions[i + 2] - origin.z;
|
||||
}
|
||||
mesh.geometry.setAttribute('position', new BufferAttribute(local, 3));
|
||||
mesh.geometry.setIndex(Array.from(indices));
|
||||
mesh.geometry.computeBoundingSphere();
|
||||
mesh.matrix.makeTranslation(origin.x, origin.y, origin.z);
|
||||
mesh.matrixWorldNeedsUpdate = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import('../../../globe/contract.js').GlobeEngine} engine
|
||||
* @param {{ view: object, MeshDesc: Function }} navara The engine's Navara
|
||||
* extension.
|
||||
*/
|
||||
export function createCctvNavaraMeshes(engine, { view, MeshDesc }) {
|
||||
if (!view?.addMesh || !MeshDesc)
|
||||
throw new TypeError('CCTV requires Navara MeshDesc support');
|
||||
if (!registered.has(view)) {
|
||||
view.registerMesh(
|
||||
KIND,
|
||||
class extends MeshDesc {
|
||||
constructor(owner, ctx, config) {
|
||||
super(owner, ctx, config);
|
||||
this.cctv = config[KIND];
|
||||
}
|
||||
// The opaque scene draws after the scene depth is copied into its
|
||||
// target, so walls and terrain hide the frame and volume; Navara's
|
||||
// transparent pass runs after the effects without that depth and
|
||||
// painted them through.
|
||||
getPassKey() {
|
||||
return 'opaque';
|
||||
}
|
||||
createMesh() {
|
||||
return this.cctv.group;
|
||||
}
|
||||
onDestroy() {
|
||||
this.cctv.dispose();
|
||||
super.onDestroy();
|
||||
}
|
||||
},
|
||||
);
|
||||
registered.add(view);
|
||||
}
|
||||
const owned = new Set();
|
||||
const materials = new Set();
|
||||
const planes = new Set();
|
||||
const raycaster = new Raycaster();
|
||||
let destroyed = false;
|
||||
const request = () => engine.render.request('cctv-mesh');
|
||||
const alive = () => {
|
||||
if (destroyed) throw new Error('CCTV meshes are destroyed');
|
||||
};
|
||||
|
||||
function createMaterial(image) {
|
||||
alive();
|
||||
const material = new MeshBasicMaterial({
|
||||
color: 0xffffff,
|
||||
transparent: true,
|
||||
opacity: PLANE_IMAGE_ALPHA,
|
||||
side: DoubleSide,
|
||||
depthWrite: false,
|
||||
toneMapped: false,
|
||||
});
|
||||
const resource = {
|
||||
material,
|
||||
image: null,
|
||||
texture: null,
|
||||
disposed: false,
|
||||
setImage(next) {
|
||||
if (resource.disposed) return;
|
||||
if (next === resource.image) {
|
||||
if (resource.texture && !resource.texture.isVideoTexture)
|
||||
resource.texture.needsUpdate = true;
|
||||
request();
|
||||
return;
|
||||
}
|
||||
const texture =
|
||||
next?.tagName === 'VIDEO'
|
||||
? new VideoTexture(next)
|
||||
: new CanvasTexture(next);
|
||||
texture.colorSpace = SRGBColorSpace;
|
||||
texture.minFilter = texture.magFilter = LinearFilter;
|
||||
texture.generateMipmaps = false;
|
||||
const previous = resource.texture;
|
||||
resource.image = next;
|
||||
resource.texture = texture;
|
||||
material.map = texture;
|
||||
material.needsUpdate = true;
|
||||
previous?.dispose();
|
||||
request();
|
||||
},
|
||||
dispose() {
|
||||
if (resource.disposed) return;
|
||||
resource.disposed = true;
|
||||
resource.texture?.dispose();
|
||||
resource.texture = resource.image = null;
|
||||
material.map = null;
|
||||
material.dispose();
|
||||
materials.delete(resource);
|
||||
},
|
||||
};
|
||||
materials.add(resource);
|
||||
try {
|
||||
if (image) resource.setImage(image);
|
||||
return resource;
|
||||
} catch (error) {
|
||||
resource.dispose();
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
function ownMesh(mesh, disposeExtra = () => {}) {
|
||||
alive();
|
||||
const group = new Group();
|
||||
group.frustumCulled = mesh.frustumCulled = false;
|
||||
mesh.matrixAutoUpdate = false;
|
||||
group.add(mesh);
|
||||
let freed = false;
|
||||
let handle;
|
||||
const dispose = () => {
|
||||
if (freed) return;
|
||||
freed = true;
|
||||
mesh.geometry.dispose();
|
||||
disposeExtra();
|
||||
owned.delete(resource);
|
||||
};
|
||||
const resource = {
|
||||
group,
|
||||
mesh,
|
||||
get show() {
|
||||
return !freed && group.visible;
|
||||
},
|
||||
set show(value) {
|
||||
if (freed || group.visible === Boolean(value)) return;
|
||||
group.visible = Boolean(value);
|
||||
request();
|
||||
},
|
||||
destroy() {
|
||||
if (freed) return;
|
||||
try {
|
||||
handle?.delete();
|
||||
} finally {
|
||||
dispose();
|
||||
request();
|
||||
}
|
||||
},
|
||||
};
|
||||
owned.add(resource);
|
||||
try {
|
||||
handle = view.addMesh({ [KIND]: { group, dispose } });
|
||||
return resource;
|
||||
} catch (error) {
|
||||
dispose();
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
function updatePlane(plane, shape, positions, orientation) {
|
||||
if (!planes.has(plane)) return;
|
||||
// The native plane's quaternion and dimensions, including its UVs.
|
||||
const q = new Quaternion(
|
||||
orientation.x,
|
||||
orientation.y,
|
||||
orientation.z,
|
||||
orientation.w,
|
||||
);
|
||||
const flat = [];
|
||||
for (const [x, y] of [
|
||||
[-1, 1],
|
||||
[1, 1],
|
||||
[-1, -1],
|
||||
[1, -1],
|
||||
]) {
|
||||
const p = new Vector3(x * shape.halfW, y * shape.halfH, 0)
|
||||
.applyQuaternion(q)
|
||||
.add(positions.capCenter);
|
||||
flat.push(p.x, p.y, p.z);
|
||||
}
|
||||
placeGeometry(plane.mesh, flat, [0, 2, 1, 2, 3, 1], positions.capCenter);
|
||||
request();
|
||||
}
|
||||
|
||||
/** The nearest shown plane's triangles along a ray, from either side. */
|
||||
function pickPlane(ray) {
|
||||
if (destroyed || !ray) return undefined;
|
||||
raycaster.ray.origin.copy(ray.origin);
|
||||
raycaster.ray.direction.copy(ray.direction).normalize();
|
||||
let nearest;
|
||||
for (const plane of planes) {
|
||||
if (!plane.show) continue;
|
||||
plane.group.updateWorldMatrix(true, true);
|
||||
const hit = raycaster.intersectObject(plane.mesh, false)[0];
|
||||
if (hit && (!nearest || hit.distance < nearest.distance))
|
||||
nearest = { ...hit, plane };
|
||||
}
|
||||
return nearest;
|
||||
}
|
||||
|
||||
const removeMeshPicker = engine.picker.registerMeshPicker((at) => {
|
||||
const hit = pickPlane(engine.camera.pickRay(at));
|
||||
if (!hit) return [];
|
||||
// The rendered depth includes terrain, tiles and foreground models.
|
||||
const surface = engine.picker.pickPosition(at);
|
||||
if (
|
||||
surface &&
|
||||
geo.cartesian3.distance(engine.camera.livePosition, surface) + 0.1 <
|
||||
hit.distance
|
||||
)
|
||||
return [];
|
||||
return [
|
||||
{
|
||||
onTop: 0,
|
||||
order: 0,
|
||||
depth: hit.distance,
|
||||
result: { id: hit.plane.id, owner: hit.plane.id },
|
||||
},
|
||||
];
|
||||
});
|
||||
|
||||
return {
|
||||
createMaterial,
|
||||
createPlane(cameraId, material, shape, positions, orientation) {
|
||||
alive();
|
||||
if (!materials.has(material) || material.disposed)
|
||||
throw new Error('CCTV plane material is not owned');
|
||||
const mesh = new Mesh(new BufferGeometry(), material.material);
|
||||
mesh.geometry.setAttribute(
|
||||
'uv',
|
||||
new BufferAttribute(new Float32Array([0, 1, 1, 1, 0, 0, 1, 0]), 2),
|
||||
);
|
||||
// A failed descriptor disposes the geometry; the material stays its own.
|
||||
let plane = null;
|
||||
plane = ownMesh(mesh, () => planes.delete(plane));
|
||||
plane.id = `cctv-${cameraId}-plane`;
|
||||
plane.cameraId = cameraId;
|
||||
planes.add(plane);
|
||||
updatePlane(plane, shape, positions, orientation);
|
||||
plane.show = false;
|
||||
return plane;
|
||||
},
|
||||
updatePlane,
|
||||
createVolume(positions, tint) {
|
||||
alive();
|
||||
const data = frustumVolumeGeometryData(positions);
|
||||
const material = new MeshBasicMaterial({
|
||||
transparent: true,
|
||||
opacity: tint.alpha,
|
||||
side: DoubleSide,
|
||||
depthWrite: false,
|
||||
toneMapped: false,
|
||||
});
|
||||
material.color.setRGB(tint.red, tint.green, tint.blue, SRGBColorSpace);
|
||||
const mesh = new Mesh(new BufferGeometry(), material);
|
||||
placeGeometry(mesh, data.positions, data.indices, positions.mount);
|
||||
return ownMesh(mesh, () => material.dispose());
|
||||
},
|
||||
pickPlane,
|
||||
// The mesh picker's result names the plane by its id.
|
||||
planeFromPick(picked) {
|
||||
for (const plane of planes)
|
||||
if (picked.id === plane.id && picked.owner === plane.id) return plane;
|
||||
return null;
|
||||
},
|
||||
drawnBy: (native) => native.group,
|
||||
destroy() {
|
||||
if (destroyed) return;
|
||||
destroyed = true;
|
||||
removeMeshPicker();
|
||||
for (const resource of [...owned]) resource.destroy();
|
||||
for (const material of [...materials]) material.dispose();
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -1,274 +0,0 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import { Group, Vector3 } from 'three';
|
||||
import * as Cesium from 'cesium';
|
||||
import { createCctvNavaraMeshes } from './navaraMeshes.js';
|
||||
import { viewshedColors } from '../../../data/cctvViewshed.js';
|
||||
|
||||
function harness() {
|
||||
const scene = new Group();
|
||||
const kinds = new Map();
|
||||
const handles = new Set();
|
||||
let requests = 0;
|
||||
let fail = false;
|
||||
let failedGeometryDisposals = 0;
|
||||
class MeshDesc {
|
||||
onDestroy() {}
|
||||
}
|
||||
const view = {
|
||||
registerMesh: (kind, desc) => kinds.set(kind, desc),
|
||||
addMesh(config) {
|
||||
if (fail) {
|
||||
config.gevCctvMesh.group.children[0].geometry.addEventListener(
|
||||
'dispose',
|
||||
() => failedGeometryDisposals++,
|
||||
);
|
||||
throw new Error('descriptor failed');
|
||||
}
|
||||
const kind = Object.keys(config)[0];
|
||||
const desc = new (kinds.get(kind))(view, {}, config);
|
||||
// Depth-tested against the scene: the frame must not paint through walls.
|
||||
assert.equal(desc.getPassKey(), 'opaque');
|
||||
const object = desc.createMesh();
|
||||
scene.add(object);
|
||||
const handle = {
|
||||
delete() {
|
||||
handles.delete(handle);
|
||||
scene.remove(object);
|
||||
desc.onDestroy();
|
||||
},
|
||||
};
|
||||
handles.add(handle);
|
||||
return handle;
|
||||
},
|
||||
};
|
||||
const pickers = new Set();
|
||||
const engine = {
|
||||
render: { request: () => requests++ },
|
||||
camera: { livePosition: { x: 0, y: 0, z: 0 }, pickRay: () => null },
|
||||
picker: {
|
||||
pickPosition: () => undefined,
|
||||
registerMeshPicker(pick) {
|
||||
pickers.add(pick);
|
||||
return () => pickers.delete(pick);
|
||||
},
|
||||
},
|
||||
};
|
||||
const owner = createCctvNavaraMeshes(engine, { view, MeshDesc });
|
||||
return {
|
||||
owner,
|
||||
engine,
|
||||
scene,
|
||||
handles,
|
||||
pickers,
|
||||
requests: () => requests,
|
||||
failedGeometryDisposals: () => failedGeometryDisposals,
|
||||
fail: () => (fail = true),
|
||||
};
|
||||
}
|
||||
|
||||
const shape = { halfW: 80, halfH: 45 };
|
||||
const positions = { capCenter: { x: 6378137, y: 1234, z: 5678 } };
|
||||
const identity = { x: 0, y: 0, z: 0, w: 1 };
|
||||
const canvas = () => ({ width: 8, height: 8, tagName: 'CANVAS' });
|
||||
|
||||
test('Navara CCTV plane preserves native rotated geometry, UVs and metre precision', (t) => {
|
||||
const h = harness();
|
||||
t.after(() => h.owner.destroy());
|
||||
const q = Cesium.Quaternion.fromAxisAngle(
|
||||
Cesium.Cartesian3.normalize(
|
||||
new Cesium.Cartesian3(1, 2, 3),
|
||||
new Cesium.Cartesian3(),
|
||||
),
|
||||
0.75,
|
||||
);
|
||||
const material = h.owner.createMaterial(canvas());
|
||||
const plane = h.owner.createPlane('a', material, shape, positions, q);
|
||||
const matrix = Cesium.Matrix3.fromQuaternion(q);
|
||||
plane.group.updateWorldMatrix(true, true);
|
||||
const attribute = plane.mesh.geometry.getAttribute('position');
|
||||
const uv = plane.mesh.geometry.getAttribute('uv');
|
||||
for (const [i, [x, y]] of [
|
||||
[-1, 1],
|
||||
[1, 1],
|
||||
[-1, -1],
|
||||
[1, -1],
|
||||
].entries()) {
|
||||
const native = Cesium.Matrix3.multiplyByVector(
|
||||
matrix,
|
||||
new Cesium.Cartesian3(x * shape.halfW, y * shape.halfH, 0),
|
||||
new Cesium.Cartesian3(),
|
||||
);
|
||||
Cesium.Cartesian3.add(native, positions.capCenter, native);
|
||||
const actual = new Vector3()
|
||||
.fromBufferAttribute(attribute, i)
|
||||
.applyMatrix4(plane.mesh.matrixWorld);
|
||||
assert.ok(Cesium.Cartesian3.distance(native, actual) < 0.00001);
|
||||
assert.equal(uv.getX(i), (x + 1) / 2);
|
||||
assert.equal(uv.getY(i), (y + 1) / 2);
|
||||
}
|
||||
assert.equal(plane.show, false);
|
||||
assert.equal(plane.id, 'cctv-a-plane');
|
||||
plane.show = true;
|
||||
assert.equal(plane.group.visible, true);
|
||||
assert.equal(material.material.opacity, 0.95);
|
||||
assert.equal(material.material.depthWrite, false);
|
||||
});
|
||||
|
||||
test('Navara CCTV picks actual visible plane triangles from either side, nearest first', (t) => {
|
||||
const h = harness();
|
||||
t.after(() => h.owner.destroy());
|
||||
const make = (id, z) => {
|
||||
const plane = h.owner.createPlane(
|
||||
id,
|
||||
h.owner.createMaterial(canvas()),
|
||||
shape,
|
||||
{ capCenter: { ...positions.capCenter, z } },
|
||||
identity,
|
||||
);
|
||||
plane.show = true;
|
||||
return plane;
|
||||
};
|
||||
const far = make('far', 5600);
|
||||
const near = make('near', 5700);
|
||||
const ray = {
|
||||
origin: { ...positions.capCenter, z: 5800 },
|
||||
direction: { x: 0, y: 0, z: -1 },
|
||||
};
|
||||
assert.equal(h.owner.pickPlane(ray).plane, near);
|
||||
assert.equal(h.owner.pickPlane(ray).distance, 100);
|
||||
near.show = false;
|
||||
assert.equal(h.owner.pickPlane(ray).plane, far);
|
||||
assert.equal(
|
||||
h.owner.pickPlane({
|
||||
...ray,
|
||||
origin: { ...ray.origin, x: ray.origin.x + 81 },
|
||||
}),
|
||||
undefined,
|
||||
);
|
||||
assert.equal(
|
||||
h.owner.pickPlane({
|
||||
origin: { ...ray.origin, z: 5500 },
|
||||
direction: { x: 0, y: 0, z: 1 },
|
||||
}).plane,
|
||||
far,
|
||||
);
|
||||
far.destroy();
|
||||
assert.equal(h.owner.pickPlane(ray), undefined);
|
||||
});
|
||||
|
||||
test('Navara CCTV releases replaced video textures and ignores retired image updates', () => {
|
||||
const h = harness();
|
||||
const first = canvas();
|
||||
const material = h.owner.createMaterial(first);
|
||||
let firstDisposed = 0;
|
||||
material.texture.addEventListener('dispose', () => firstDisposed++);
|
||||
const version = material.texture.version;
|
||||
material.setImage(first);
|
||||
assert.ok(material.texture.version > version);
|
||||
let cancelled = 0;
|
||||
const video = {
|
||||
tagName: 'VIDEO',
|
||||
requestVideoFrameCallback: () => 17,
|
||||
cancelVideoFrameCallback: (id) => {
|
||||
assert.equal(id, 17);
|
||||
cancelled++;
|
||||
},
|
||||
};
|
||||
material.setImage(video);
|
||||
assert.equal(firstDisposed, 1);
|
||||
assert.equal(material.texture.isVideoTexture, true);
|
||||
material.setImage(canvas());
|
||||
assert.equal(cancelled, 1);
|
||||
let finalDisposed = 0;
|
||||
material.texture.addEventListener('dispose', () => finalDisposed++);
|
||||
const plane = h.owner.createPlane('a', material, shape, positions, identity);
|
||||
plane.destroy();
|
||||
plane.destroy();
|
||||
assert.equal(finalDisposed, 0, 'the plane leaves its material to its owner');
|
||||
material.dispose();
|
||||
const requests = h.requests();
|
||||
material.setImage(video);
|
||||
assert.equal(h.requests(), requests);
|
||||
assert.equal(finalDisposed, 1);
|
||||
assert.equal(h.handles.size, 0);
|
||||
h.owner.destroy();
|
||||
h.owner.destroy();
|
||||
assert.equal(finalDisposed, 1);
|
||||
});
|
||||
|
||||
test('Navara CCTV rolls back a failed descriptor and rejects new resources after destroy', () => {
|
||||
const h = harness();
|
||||
const material = h.owner.createMaterial(canvas());
|
||||
let releases = 0;
|
||||
material.material.addEventListener('dispose', () => releases++);
|
||||
h.fail();
|
||||
assert.throws(
|
||||
() => h.owner.createPlane('a', material, shape, positions, identity),
|
||||
/descriptor failed/,
|
||||
);
|
||||
assert.equal(releases, 0, 'the material stays with its owner');
|
||||
assert.equal(h.failedGeometryDisposals(), 1);
|
||||
assert.equal(h.handles.size, 0);
|
||||
h.owner.destroy();
|
||||
assert.equal(releases, 1);
|
||||
assert.throws(() => h.owner.createMaterial(canvas()), /destroyed/);
|
||||
});
|
||||
|
||||
test('Navara CCTV volume preserves native world vertices and releases geometry/material', () => {
|
||||
const h = harness();
|
||||
const mount = positions.capCenter;
|
||||
const p = (x, y, z) => ({ x: mount.x + x, y: mount.y + y, z: mount.z + z });
|
||||
const volume = h.owner.createVolume(
|
||||
{
|
||||
mount,
|
||||
tl: p(-5, 5, 100),
|
||||
tr: p(5, 5, 100),
|
||||
br: p(5, -5, 100),
|
||||
bl: p(-5, -5, 100),
|
||||
},
|
||||
viewshedColors(30).fill,
|
||||
);
|
||||
assert.equal(volume.mesh.geometry.index.count, 18);
|
||||
assert.equal(volume.mesh.geometry.getAttribute('position').count, 5);
|
||||
assert.equal(volume.mesh.matrix.elements[12], mount.x);
|
||||
let geometryReleases = 0,
|
||||
materialReleases = 0;
|
||||
volume.mesh.geometry.addEventListener('dispose', () => geometryReleases++);
|
||||
volume.mesh.material.addEventListener('dispose', () => materialReleases++);
|
||||
h.owner.destroy();
|
||||
h.owner.destroy();
|
||||
assert.equal(geometryReleases, 1);
|
||||
assert.equal(materialReleases, 1);
|
||||
assert.equal(h.handles.size, 0);
|
||||
});
|
||||
|
||||
test('Navara CCTV plane picks go through a mesh picker that defers to nearer depth', () => {
|
||||
const h = harness();
|
||||
const plane = h.owner.createPlane(
|
||||
'a',
|
||||
h.owner.createMaterial(canvas()),
|
||||
shape,
|
||||
positions,
|
||||
identity,
|
||||
);
|
||||
plane.show = true;
|
||||
const [pick] = h.pickers;
|
||||
h.engine.camera.livePosition = { ...positions.capCenter, z: 5800 };
|
||||
h.engine.camera.pickRay = () => ({
|
||||
origin: { ...positions.capCenter, z: 5800 },
|
||||
direction: { x: 0, y: 0, z: -1 },
|
||||
});
|
||||
const [hit] = pick({ x: 1, y: 1 });
|
||||
assert.deepEqual(hit.result, { id: 'cctv-a-plane', owner: 'cctv-a-plane' });
|
||||
assert.equal(hit.depth, 122);
|
||||
assert.equal(h.owner.planeFromPick(hit.result), plane);
|
||||
assert.equal(
|
||||
h.owner.planeFromPick({ id: 'cctv-a-plane', owner: 'sibling' }),
|
||||
null,
|
||||
);
|
||||
h.engine.picker.pickPosition = () => ({ ...positions.capCenter, z: 5750 });
|
||||
assert.deepEqual(pick({ x: 1, y: 1 }), [], 'a wall in front hides it');
|
||||
h.owner.destroy();
|
||||
assert.equal(h.pickers.size, 0);
|
||||
});
|
||||
Reference in new issue
Block a user