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
@@ -12,6 +12,7 @@ import { createCesiumCamera } from './camera.js';
|
||||
import { createCesiumClock } from './clock.js';
|
||||
import { createCesiumCredits } from './credits.js';
|
||||
import { createCesiumFrames } from './frames.js';
|
||||
import { createCesiumMeshes } from './meshes.js';
|
||||
import { createCesiumGround } from './ground.js';
|
||||
import { createCesiumImagery } from './imagery.js';
|
||||
import { createCesiumInput } from './input.js';
|
||||
@@ -67,6 +68,7 @@ export function createCesiumEngine(viewer, options = {}) {
|
||||
requestRender,
|
||||
...(options.groundFactories ? { factories: options.groundFactories } : {}),
|
||||
});
|
||||
const meshes = createCesiumMeshes(viewer, { requestRender });
|
||||
const maps = createCesiumMaps(viewer, {
|
||||
...options,
|
||||
credits: credits.credits,
|
||||
@@ -77,7 +79,8 @@ export function createCesiumEngine(viewer, options = {}) {
|
||||
identify: (picked) =>
|
||||
movers.identify(picked) ??
|
||||
lines.identify(picked) ??
|
||||
ground.identify(picked),
|
||||
ground.identify(picked) ??
|
||||
meshes.identify(picked),
|
||||
});
|
||||
const parts = {
|
||||
camera: createCesiumCamera(viewer),
|
||||
@@ -88,10 +91,14 @@ export function createCesiumEngine(viewer, options = {}) {
|
||||
movers,
|
||||
lines,
|
||||
ground,
|
||||
meshes,
|
||||
render,
|
||||
// Mover sets in a surface `exclude` are hidden for the sample.
|
||||
surface: createCesiumSurface(viewer, {
|
||||
engineExclude: (entry) => movers.primitiveOf(entry),
|
||||
engineExclude: (entry) =>
|
||||
movers.primitiveOf(entry) ??
|
||||
meshes.primitiveOf(entry) ??
|
||||
lines.primitiveOf(entry),
|
||||
}),
|
||||
input: createCesiumInput(viewer, { ...options, picker: picker.picker }),
|
||||
credits,
|
||||
|
||||
@@ -67,6 +67,7 @@ test('the engine draws the visual styles, bloom and contact sonar, not the cloud
|
||||
lines: true,
|
||||
ground: true,
|
||||
imagery: true,
|
||||
meshes: true,
|
||||
});
|
||||
assert.equal(engine.contactSonarDiagnostics().contacts, 0);
|
||||
engine.destroy();
|
||||
|
||||
@@ -231,6 +231,16 @@ export function createCesiumLines(
|
||||
});
|
||||
return {
|
||||
lines: registry.lines,
|
||||
primitiveOf(handle) {
|
||||
const state = [...registry.live.values()].find(
|
||||
(entry) => entry.line === handle,
|
||||
);
|
||||
if (!state) return null;
|
||||
const entry = ordered.find(
|
||||
({ slot }) => slot.get(0)?.gevLine === state.options.id,
|
||||
);
|
||||
return entry?.slot ?? null;
|
||||
},
|
||||
/** The live line a Cesium pick result belongs to, with its pickId, or null. */
|
||||
identify(picked) {
|
||||
const owner = picked?.primitive?.gevLine;
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
import * as Cesium from 'cesium';
|
||||
import { createMeshRegistry } from '../meshes.js';
|
||||
|
||||
/** World triangles with flat colour or a live image, drawn on both sides. */
|
||||
export function createCesiumMeshes(viewer, { requestRender = () => {} } = {}) {
|
||||
let root;
|
||||
let removeFrame;
|
||||
const owned = new Map();
|
||||
const registry = createMeshRegistry((state) => {
|
||||
root ??= viewer.scene.primitives.add(new Cesium.PrimitiveCollection());
|
||||
let primitive;
|
||||
let material;
|
||||
removeFrame ??= viewer.scene.postRender.addEventListener(() => {
|
||||
for (const { state, backend } of registry.live.values()) {
|
||||
if (state.show && !backend.ready()) {
|
||||
requestRender('meshes');
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
function build(next) {
|
||||
const attributes = {
|
||||
position: new Cesium.GeometryAttribute({
|
||||
componentDatatype: Cesium.ComponentDatatype.DOUBLE,
|
||||
componentsPerAttribute: 3,
|
||||
values: next.positions,
|
||||
}),
|
||||
};
|
||||
if (next.uv)
|
||||
attributes.st = new Cesium.GeometryAttribute({
|
||||
componentDatatype: Cesium.ComponentDatatype.FLOAT,
|
||||
componentsPerAttribute: 2,
|
||||
values: next.uv,
|
||||
});
|
||||
const geometry = new Cesium.Geometry({
|
||||
attributes,
|
||||
indices: next.indices,
|
||||
primitiveType: Cesium.PrimitiveType.TRIANGLES,
|
||||
boundingSphere: Cesium.BoundingSphere.fromVertices(next.positions),
|
||||
});
|
||||
Cesium.GeometryPipeline.computeNormal(geometry);
|
||||
const color = new Cesium.Color(...next.color);
|
||||
const nextMaterial = next.image
|
||||
? Cesium.Material.fromType('Image', {
|
||||
image: next.image,
|
||||
color,
|
||||
transparent: true,
|
||||
})
|
||||
: Cesium.Material.fromType('Color', { color });
|
||||
const nextPrimitive = new Cesium.Primitive({
|
||||
geometryInstances: new Cesium.GeometryInstance({
|
||||
geometry,
|
||||
id: next.pickId,
|
||||
}),
|
||||
appearance: new Cesium.MaterialAppearance({
|
||||
material: nextMaterial,
|
||||
materialSupport: next.image
|
||||
? Cesium.MaterialAppearance.MaterialSupport.TEXTURED
|
||||
: Cesium.MaterialAppearance.MaterialSupport.BASIC,
|
||||
flat: true,
|
||||
faceForward: true,
|
||||
translucent: true,
|
||||
renderState: {
|
||||
cull: { enabled: false },
|
||||
depthTest: { enabled: next.depthTest !== 'always' },
|
||||
depthMask: false,
|
||||
},
|
||||
}),
|
||||
asynchronous: false,
|
||||
allowPicking: next.pickId != null,
|
||||
show: next.show,
|
||||
});
|
||||
nextPrimitive.gevMesh = next.id;
|
||||
root.add(nextPrimitive);
|
||||
if (primitive) {
|
||||
owned.delete(primitive);
|
||||
root.remove(primitive);
|
||||
}
|
||||
if (material && !material.isDestroyed()) material.destroy();
|
||||
primitive = nextPrimitive;
|
||||
material = nextMaterial;
|
||||
owned.set(primitive, state);
|
||||
}
|
||||
build(state);
|
||||
return {
|
||||
primitive: () => primitive,
|
||||
ready: () =>
|
||||
primitive.ready &&
|
||||
material._loadedImages.length === 0 &&
|
||||
(!state.image ||
|
||||
(material._textures.image &&
|
||||
material._textures.image !== material._defaultTexture &&
|
||||
(state.image.tagName === 'VIDEO' ||
|
||||
material._texturePaths.image === state.image))),
|
||||
update(next, patch) {
|
||||
if (
|
||||
['positions', 'indices', 'uv', 'depthTest', 'pickId'].some(
|
||||
(key) => key in patch,
|
||||
) ||
|
||||
Boolean(next.image) !== Boolean(state.image)
|
||||
)
|
||||
build(next);
|
||||
else {
|
||||
primitive.show = next.show;
|
||||
material.uniforms.color = new Cesium.Color(...next.color);
|
||||
if (next.image) {
|
||||
material.uniforms.image = next.image;
|
||||
if ('image' in patch) material._texturePaths.image = undefined;
|
||||
}
|
||||
}
|
||||
},
|
||||
destroy() {
|
||||
owned.delete(primitive);
|
||||
root.remove(primitive);
|
||||
if (!material.isDestroyed()) material.destroy();
|
||||
if (!registry.live.size) {
|
||||
removeFrame?.();
|
||||
removeFrame = null;
|
||||
}
|
||||
},
|
||||
};
|
||||
}, requestRender);
|
||||
return {
|
||||
meshes: registry.meshes,
|
||||
identify(picked) {
|
||||
const state = owned.get(picked?.primitive);
|
||||
return state
|
||||
? {
|
||||
id: state.pickId,
|
||||
owner: state.id,
|
||||
visible: state.show,
|
||||
pickable: state.pickId != null,
|
||||
}
|
||||
: null;
|
||||
},
|
||||
primitiveOf(handle) {
|
||||
const entry = registry.live.get(handle?.id);
|
||||
return entry?.handle === handle ? entry.backend.primitive() : null;
|
||||
},
|
||||
destroy() {
|
||||
registry.destroy();
|
||||
removeFrame?.();
|
||||
removeFrame = null;
|
||||
if (root) viewer.scene.primitives.remove(root);
|
||||
root = null;
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createCesiumMoversEngine } from '../../testSupport/cesiumMoversEngine.mjs';
|
||||
|
||||
const triangles = {
|
||||
id: 'mesh',
|
||||
positions: [6378137, 0, 0, 6378137, 20, 0, 6378137, 0, 20],
|
||||
indices: [0, 1, 2],
|
||||
};
|
||||
|
||||
test('Cesium meshes draw indexed triangles and restyle without rebuilding', (t) => {
|
||||
const h = createCesiumMoversEngine();
|
||||
t.after(() => h.engine.destroy());
|
||||
const mesh = h.engine.meshes.createMesh({ ...triangles, pickId: 'feature' });
|
||||
const first = h.primitive(mesh.id);
|
||||
assert.deepEqual(
|
||||
[...first.geometryInstances.geometry.attributes.position.values],
|
||||
triangles.positions,
|
||||
);
|
||||
assert.equal(
|
||||
first.geometryInstances.geometry.attributes.normal.values.length,
|
||||
9,
|
||||
);
|
||||
assert.equal(first.allowPicking, true);
|
||||
assert.equal(first.asynchronous, false);
|
||||
assert.equal(first.appearance.renderState.cull.enabled, false);
|
||||
assert.equal(first.appearance.renderState.depthMask, false);
|
||||
mesh.update({ color: [1, 0, 0, 0.2], show: false });
|
||||
assert.equal(h.primitive(mesh.id), first);
|
||||
assert.equal(first.appearance.material.uniforms.color.alpha, 0.2);
|
||||
assert.equal(first.show, false);
|
||||
mesh.update({ depthTest: 'always', show: true, pickId: 'replacement' });
|
||||
const second = h.primitive(mesh.id);
|
||||
assert.notEqual(second, first);
|
||||
assert.equal(first.isDestroyed(), true);
|
||||
assert.equal(second.appearance.renderState.depthTest.enabled, false);
|
||||
h.viewer.scene.pick = () => ({ primitive: second, id: 'replacement' });
|
||||
assert.equal(h.engine.picker.pick({ x: 1, y: 1 }).id, 'replacement');
|
||||
assert.equal(h.engine.picker.pick({ x: 1, y: 1 }).owner, mesh.id);
|
||||
mesh.destroy();
|
||||
mesh.destroy();
|
||||
assert.equal(second.isDestroyed(), true);
|
||||
assert.equal(h.primitive(mesh.id), undefined);
|
||||
});
|
||||
|
||||
test('Cesium mesh image refresh invalidates a reused canvas and releases frame listeners', (t) => {
|
||||
let requests = 0;
|
||||
const h = createCesiumMoversEngine({
|
||||
engineOptions: { requestRender: () => requests++ },
|
||||
});
|
||||
t.after(() => h.engine.destroy());
|
||||
const frame = h.viewer.scene.postRender;
|
||||
const before = frame.numberOfListeners;
|
||||
const canvas = new HTMLCanvasElement();
|
||||
canvas.width = canvas.height = 8;
|
||||
const mesh = h.engine.meshes.createMesh({
|
||||
...triangles,
|
||||
uv: [0, 0, 1, 0, 0, 1],
|
||||
image: canvas,
|
||||
});
|
||||
const material = h.primitive(mesh.id).appearance.material;
|
||||
material._texturePaths.image = canvas;
|
||||
mesh.update({ image: canvas });
|
||||
assert.equal(material._texturePaths.image, undefined);
|
||||
assert.equal(material.uniforms.image, canvas);
|
||||
assert.equal(frame.numberOfListeners, before + 1);
|
||||
h.primitive(mesh.id)._ready = true;
|
||||
material._textures.image = { destroy() {} };
|
||||
material._texturePaths.image = canvas;
|
||||
material._loadedImages.push({ id: 'image', image: canvas });
|
||||
const pending = requests;
|
||||
h.frame();
|
||||
assert.ok(requests > pending, 'a queued upload requests another frame');
|
||||
material._loadedImages.length = 0;
|
||||
const uploaded = requests;
|
||||
h.frame();
|
||||
assert.equal(requests, uploaded, 'an uploaded still image can stop drawing');
|
||||
mesh.destroy();
|
||||
assert.equal(frame.numberOfListeners, before);
|
||||
assert.equal(material.isDestroyed(), true);
|
||||
});
|
||||
|
||||
test('ray obstruction hides excluded mesh and line handles and restores them on a failed query', (t) => {
|
||||
const h = createCesiumMoversEngine();
|
||||
t.after(() => h.engine.destroy());
|
||||
const mesh = h.engine.meshes.createMesh(triangles);
|
||||
const primitive = h.primitive(mesh.id);
|
||||
const line = h.engine.lines.createLine({
|
||||
id: 'line',
|
||||
pickId: 'line',
|
||||
widthPx: 2,
|
||||
color: [1, 1, 1, 1],
|
||||
});
|
||||
line.setPositions([
|
||||
{ x: 6378137, y: 0, z: 0 },
|
||||
{ x: 6378137, y: 20, z: 0 },
|
||||
]);
|
||||
const root = [...Array(h.viewer.scene.primitives.length)].map((_, i) =>
|
||||
h.viewer.scene.primitives.get(i),
|
||||
);
|
||||
const slot = root
|
||||
.flatMap((r) => [...Array(r.length)].map((_, i) => r.get(i)))
|
||||
.find((p) => p.get?.(0)?.gevLine === 'line');
|
||||
h.viewer.scene.pickFromRay = () => {
|
||||
assert.equal(primitive.show, false);
|
||||
assert.equal(slot.show, false);
|
||||
throw new Error('ray failed');
|
||||
};
|
||||
assert.throws(
|
||||
() =>
|
||||
h.engine.surface.raycast({
|
||||
origin: { x: 6378137, y: 0, z: 0 },
|
||||
direction: { x: 1, y: 0, z: 0 },
|
||||
far: 100,
|
||||
exclude: [mesh, line],
|
||||
}),
|
||||
/ray failed/,
|
||||
);
|
||||
assert.equal(primitive.show, true);
|
||||
assert.equal(slot.show, true);
|
||||
});
|
||||
@@ -7,6 +7,7 @@
|
||||
// set is hidden for the sample instead (synchronous: nothing draws in
|
||||
// between) and shown again after it.
|
||||
|
||||
import * as Cesium from 'cesium';
|
||||
import { firstVisibleTileset } from '../../services/groundSnap.js';
|
||||
import { normalizeSurfaceOptions } from '../surfaceDetail.js';
|
||||
import { toGeo3 } from './convert.js';
|
||||
@@ -63,6 +64,21 @@ export function createCesiumSurface(
|
||||
scene.globe ? finite(scene.globe.getHeight(cartographic)) : undefined;
|
||||
|
||||
const surface = {
|
||||
raycast({ origin, direction, far, exclude = [] }) {
|
||||
if (!scene.pickFromRay || !Number.isFinite(far) || far <= 0)
|
||||
return undefined;
|
||||
return excluding(exclude, (native) => {
|
||||
const hit = scene.pickFromRay(
|
||||
new Cesium.Ray(origin, direction),
|
||||
native,
|
||||
);
|
||||
if (!hit?.position) return undefined;
|
||||
const distance = Cesium.Cartesian3.distance(origin, hit.position);
|
||||
return distance <= far
|
||||
? { position: toGeo3(hit.position), distance, object: hit.object }
|
||||
: undefined;
|
||||
});
|
||||
},
|
||||
sampleHeight,
|
||||
// Cesium samples the tiles it has loaded and does not say which tile
|
||||
// answered.
|
||||
|
||||
Reference in new issue
Block a user