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:
German Khokhlov committed 2026-11-07 13:57:29 +04:00
1 parent 3907b7084e
commit f4b83a3a31
57 files changed
+1891 -1588

No files matched your search

+304
View File
@@ -0,0 +1,304 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { Group, Vector3 } from 'three';
import * as Cesium from 'cesium';
import { createNavaraMeshes } from './meshes.js';
import { createCctvMeshes } from '../../layers/cctv/render/meshes.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 = {
forceUpdate: () => requests++,
registerMesh: (kind, desc) => kinds.set(kind, desc),
addMesh(config) {
if (fail) {
config.gevWorldMesh.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);
// After the effects, depth-tested against the scene's packed depth.
assert.equal(desc.getPassKey(), 'transparent');
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 port = createNavaraMeshes(view, {
MeshDesc,
camera: engine.camera,
picker: engine.picker,
});
engine.meshes = port.meshes;
const owner = createCctvMeshes(engine);
return {
owner,
port,
drawn: () => scene.children.at(-1)?.children[0],
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();
h.port.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);
const drawn = h.drawn();
drawn.updateWorldMatrix(true, true);
const attribute = drawn.geometry.getAttribute('position');
const uv = drawn.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(drawn.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(drawn.parent.visible, true);
assert.equal(drawn.material.opacity, 0.95);
assert.equal(drawn.material.depthWrite, false);
});
test('world mesh picks test both faces, visibility, depth and retirement', () => {
const h = harness();
const plane = h.owner.createPlane(
'a',
h.owner.createMaterial(canvas()),
shape,
positions,
identity,
);
plane.show = true;
const [pick] = h.pickers;
let ray = {
origin: { ...positions.capCenter, z: 5800 },
direction: { x: 0, y: 0, z: -1 },
};
h.engine.camera.pickRay = () => ray;
assert.equal(pick({})[0].depth, 122);
assert.deepEqual(pick({})[0].result, { id: plane.id, owner: plane.id });
ray = {
origin: { ...positions.capCenter, z: 5500 },
direction: { x: 0, y: 0, z: 1 },
};
assert.equal(pick({})[0].depth, 178);
ray.origin.x += 81;
assert.deepEqual(pick({}), []);
ray.origin.x -= 81;
// A depth read that answers with the camera itself hides nothing; the
// globe stands in for the surface.
h.engine.picker.pickPosition = () => ({ ...ray.origin });
h.engine.picker.pickGlobe = () => ({ ...positions.capCenter, z: 6000 });
assert.equal(pick({})[0].depth, 178);
h.engine.picker.pickGlobe = () => ({ ...positions.capCenter, z: 5550 });
assert.deepEqual(pick({}), [], 'a nearer globe still hides it');
h.engine.picker.pickPosition = () => ({ ...positions.capCenter, z: 5550 });
assert.deepEqual(pick({}), [], 'a nearer wall hides the mesh');
plane.mesh.update({ depthTest: 'always' });
assert.equal(pick({}).length, 1);
plane.show = false;
assert.deepEqual(pick({}), []);
h.owner.destroy();
h.port.destroy();
assert.equal(h.pickers.size, 0);
assert.equal(h.handles.size, 0);
});
test('world meshes release replaced images and video callbacks exactly once', () => {
const h = harness();
const first = canvas();
const material = h.owner.createMaterial(first);
const plane = h.owner.createPlane('a', material, shape, positions, identity);
const drawn = h.drawn();
let disposed = 0;
drawn.material.map.addEventListener('dispose', () => disposed++);
const version = drawn.material.map.version;
material.setImage(first);
assert.ok(drawn.material.map.version > version);
let cancelled = 0;
const video = {
tagName: 'VIDEO',
requestVideoFrameCallback: () => 17,
cancelVideoFrameCallback: (id) => {
assert.equal(id, 17);
cancelled++;
},
};
material.setImage(video);
assert.equal(disposed, 1);
assert.equal(drawn.material.map.isVideoTexture, true);
material.setImage(canvas());
assert.equal(cancelled, 1);
let finalDisposed = 0;
drawn.material.map.addEventListener('dispose', () => finalDisposed++);
plane.destroy();
plane.destroy();
assert.equal(finalDisposed, 1, 'the mesh owns its texture');
material.dispose();
const requests = h.requests();
material.setImage(video);
assert.equal(h.requests(), requests);
h.owner.destroy();
h.port.destroy();
assert.equal(finalDisposed, 1);
});
test('world mesh descriptor failure releases geometry and rejects later creates after teardown', () => {
const h = harness();
h.fail();
assert.throws(
() =>
h.owner.createPlane(
'a',
h.owner.createMaterial(canvas()),
shape,
positions,
identity,
),
/descriptor failed/,
);
assert.equal(h.failedGeometryDisposals(), 1);
assert.equal(h.handles.size, 0);
assert.deepEqual(h.port.meshes.list(), []);
h.owner.destroy();
h.port.destroy();
assert.throws(() => h.owner.createMaterial(null), /destroyed/);
});
test('world volume preserves world vertices and releases geometry and 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 });
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,
);
const mesh = h.drawn();
mesh.updateWorldMatrix(true, true);
assert.equal(mesh.geometry.index.count, 18);
assert.equal(mesh.geometry.getAttribute('position').count, 5);
const first = new Vector3()
.fromBufferAttribute(mesh.geometry.getAttribute('position'), 0)
.applyMatrix4(mesh.matrixWorld);
assert.equal(first.x, mount.x);
let geometryReleases = 0,
materialReleases = 0;
mesh.geometry.addEventListener('dispose', () => geometryReleases++);
mesh.material.addEventListener('dispose', () => materialReleases++);
h.owner.destroy();
h.port.destroy();
h.port.destroy();
assert.equal(geometryReleases, 1);
assert.equal(materialReleases, 1);
assert.equal(h.handles.size, 0);
});
test('world mesh picks retain the nearest shown triangle among several meshes', () => {
const h = harness();
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);
h.engine.camera.pickRay = () => ({
origin: { ...positions.capCenter, z: 5800 },
direction: { x: 0, y: 0, z: -1 },
});
const nearest = () =>
[...h.pickers]
.flatMap((pick) => pick({}))
.sort((a, b) => a.depth - b.depth)[0];
assert.equal(nearest().result.id, near.id);
assert.equal(nearest().depth, 100);
assert.equal(h.owner.planeFromPick({ id: near.id, owner: 'sibling' }), null);
near.show = false;
assert.equal(nearest().result.id, far.id);
far.destroy();
assert.equal(nearest(), undefined);
h.owner.destroy();
h.port.destroy();
});