mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-20 10:12:18 +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
@@ -48,6 +48,12 @@ const NO_LINES = Object.freeze({
|
||||
},
|
||||
list: () => [],
|
||||
});
|
||||
const NO_MESHES = Object.freeze({
|
||||
createMesh() {
|
||||
throw new Error('Navara meshes need the view runtime');
|
||||
},
|
||||
list: () => [],
|
||||
});
|
||||
const NO_GROUND = Object.freeze({
|
||||
createSet() {
|
||||
throw new Error("Navara ground needs the view's three runtime (ground/)");
|
||||
@@ -125,6 +131,7 @@ export function createNavaraEngine(
|
||||
lines: linesPort = null,
|
||||
ground: groundPort = null,
|
||||
imagery: imageryPort = null,
|
||||
meshes: meshesPort = null,
|
||||
sharpen: sharpenPort = null,
|
||||
styles: stylesPort = null,
|
||||
bloom: bloomPort = null,
|
||||
@@ -305,6 +312,7 @@ export function createNavaraEngine(
|
||||
},
|
||||
};
|
||||
|
||||
const meshes = meshesPort?.({ camera: model.camera, picker }) ?? null;
|
||||
let sharpen = { ...SHARPEN_INITIAL };
|
||||
let destroyed = false;
|
||||
// render.visualStyles/setStyle/style, with a styles port only.
|
||||
@@ -434,6 +442,7 @@ export function createNavaraEngine(
|
||||
movers: moversPort?.movers ?? NO_MOVERS,
|
||||
lines: linesPort?.lines ?? NO_LINES,
|
||||
ground: groundPort?.ground ?? NO_GROUND,
|
||||
meshes: meshes?.meshes ?? NO_MESHES,
|
||||
imagery: imagery?.imagery ?? NO_IMAGERY,
|
||||
destroy() {
|
||||
if (destroyed) return;
|
||||
@@ -456,6 +465,7 @@ export function createNavaraEngine(
|
||||
moversPort?.destroy();
|
||||
linesPort?.destroy();
|
||||
groundPort?.destroy();
|
||||
meshes?.destroy();
|
||||
},
|
||||
isDestroyed: () => destroyed,
|
||||
// Navara only, not contract: the post pipeline as QA reads it (the
|
||||
|
||||
@@ -34,6 +34,7 @@ import { animeShader } from '../../styles/anime.js';
|
||||
import { noirShader } from '../../styles/noir.js';
|
||||
import { createNavaraMovers } from './movers/index.js';
|
||||
import { createNavaraLines } from './lines/index.js';
|
||||
import { createNavaraMeshes } from './meshes.js';
|
||||
import { createNavaraGround } from './ground/index.js';
|
||||
import { createGroundPass } from './ground/pass.js';
|
||||
import { createNavaraImagery } from './imagery/index.js';
|
||||
@@ -174,6 +175,7 @@ export async function createNavaraGlobe({ container, search, hd = false }) {
|
||||
),
|
||||
}),
|
||||
lines: createNavaraLines(view, { overlay }),
|
||||
meshes: (parts) => createNavaraMeshes(view, { MeshDesc, ...parts }),
|
||||
ground: createNavaraGround(view, { createPass: createGroundPass }),
|
||||
imagery: (parts) =>
|
||||
createNavaraImagery(view, { ...parts, createPass: createImageryPass }),
|
||||
|
||||
@@ -0,0 +1,191 @@
|
||||
import {
|
||||
BufferAttribute,
|
||||
BufferGeometry,
|
||||
CanvasTexture,
|
||||
Color,
|
||||
DoubleSide,
|
||||
Group,
|
||||
Mesh,
|
||||
MeshBasicMaterial,
|
||||
Raycaster,
|
||||
SRGBColorSpace,
|
||||
Texture,
|
||||
VideoTexture,
|
||||
} from 'three';
|
||||
import { createMeshRegistry } from '../meshes.js';
|
||||
import { cartesian3 } from '../../geo/index.js';
|
||||
import { attachSceneDepth, createSceneDepthTest } from './sceneDepth.js';
|
||||
|
||||
const KIND = 'gevWorldMesh';
|
||||
const NEAR_SURFACE_M = 2;
|
||||
const registered = new WeakSet();
|
||||
|
||||
export function createNavaraMeshes(view, { MeshDesc, camera, picker }) {
|
||||
if (!registered.has(view)) {
|
||||
view.registerMesh(
|
||||
KIND,
|
||||
class extends MeshDesc {
|
||||
constructor(owner, ctx, config) {
|
||||
super(owner, ctx, config);
|
||||
this.resource = config[KIND];
|
||||
}
|
||||
// Transparent, drawn after the effects: the colours are the ones
|
||||
// asked for (as Cesium draws them), not tone-mapped with the look.
|
||||
// The scene-depth test hides a mesh behind terrain, tiles and Earth.
|
||||
getPassKey() {
|
||||
return 'transparent';
|
||||
}
|
||||
createMesh() {
|
||||
attachSceneDepth(this.resource.group, this.ctx);
|
||||
return this.resource.group;
|
||||
}
|
||||
onDestroy() {
|
||||
this.resource.dispose();
|
||||
super.onDestroy();
|
||||
}
|
||||
},
|
||||
);
|
||||
registered.add(view);
|
||||
}
|
||||
const registry = createMeshRegistry(
|
||||
(state) => {
|
||||
const group = new Group();
|
||||
const geometry = new BufferGeometry();
|
||||
const material = new MeshBasicMaterial({
|
||||
side: DoubleSide,
|
||||
transparent: true,
|
||||
depthWrite: false,
|
||||
toneMapped: false,
|
||||
});
|
||||
const mesh = new Mesh(geometry, material);
|
||||
const sceneDepth = createSceneDepthTest();
|
||||
sceneDepth.material(material);
|
||||
sceneDepth.object(group);
|
||||
sceneDepth.object(mesh);
|
||||
let depthFree = false;
|
||||
const pigment = new Color();
|
||||
const bindDepth = mesh.onBeforeRender;
|
||||
mesh.onBeforeRender = function onBeforeRender(...args) {
|
||||
bindDepth.apply(this, args);
|
||||
if (depthFree) sceneDepth.uniforms.uGevSceneDepthOn.value = 0;
|
||||
// The pass still scales by the view's exposure: undo it.
|
||||
material.color
|
||||
.copy(pigment)
|
||||
.multiplyScalar(1 / Math.max(1, view.toneMappingExposure ?? 1));
|
||||
};
|
||||
group.frustumCulled = mesh.frustumCulled = false;
|
||||
group.add(mesh);
|
||||
let texture,
|
||||
image,
|
||||
freed = false;
|
||||
const dispose = () => {
|
||||
if (freed) return;
|
||||
freed = true;
|
||||
geometry.dispose();
|
||||
material.dispose();
|
||||
texture?.dispose();
|
||||
};
|
||||
function update(next, patch) {
|
||||
if ('positions' in patch || 'indices' in patch || 'uv' in patch) {
|
||||
const p = next.positions;
|
||||
const local = new Float32Array(p.length);
|
||||
for (let i = 0; i < p.length; i++) local[i] = p[i] - p[i % 3];
|
||||
mesh.position.set(p[0], p[1], p[2]);
|
||||
geometry.setAttribute('position', new BufferAttribute(local, 3));
|
||||
geometry.setIndex(new BufferAttribute(next.indices, 1));
|
||||
if (next.uv)
|
||||
geometry.setAttribute('uv', new BufferAttribute(next.uv, 2));
|
||||
else geometry.deleteAttribute('uv');
|
||||
geometry.computeBoundingSphere();
|
||||
}
|
||||
if (image !== next.image) {
|
||||
texture?.dispose();
|
||||
image = next.image;
|
||||
texture = !image
|
||||
? null
|
||||
: image.tagName === 'VIDEO'
|
||||
? new VideoTexture(image)
|
||||
: image.getContext
|
||||
? new CanvasTexture(image)
|
||||
: new Texture(image);
|
||||
if (texture) {
|
||||
texture.colorSpace = SRGBColorSpace;
|
||||
texture.needsUpdate = true;
|
||||
}
|
||||
material.map = texture;
|
||||
material.needsUpdate = true;
|
||||
} else if (texture && !texture.isVideoTexture && 'image' in patch)
|
||||
texture.needsUpdate = true;
|
||||
pigment.setRGB(...next.color.slice(0, 3), SRGBColorSpace);
|
||||
material.color.copy(pigment);
|
||||
material.opacity = next.color[3];
|
||||
depthFree = next.depthTest === 'always';
|
||||
material.depthTest = !depthFree;
|
||||
group.visible = next.show;
|
||||
}
|
||||
let holder;
|
||||
try {
|
||||
update(state, state);
|
||||
holder = view.addMesh({ [KIND]: { group, dispose } });
|
||||
} catch (error) {
|
||||
dispose();
|
||||
throw error;
|
||||
}
|
||||
return {
|
||||
group,
|
||||
mesh,
|
||||
update,
|
||||
destroy() {
|
||||
try {
|
||||
holder.delete();
|
||||
} finally {
|
||||
dispose();
|
||||
}
|
||||
},
|
||||
};
|
||||
},
|
||||
() => view.forceUpdate(),
|
||||
);
|
||||
const raycaster = new Raycaster();
|
||||
const removePicks = picker.registerMeshPicker((at) => {
|
||||
const candidates = [...registry.live.values()].filter(
|
||||
({ state }) => state.show && state.pickId != null,
|
||||
);
|
||||
if (!candidates.length) return [];
|
||||
const ray = camera.pickRay(at);
|
||||
if (!ray) return [];
|
||||
raycaster.ray.origin.copy(ray.origin);
|
||||
raycaster.ray.direction.copy(ray.direction).normalize();
|
||||
// The rendered surface hides what lies behind it. Right after another
|
||||
// click Navara's depth read can answer with the camera itself: a
|
||||
// surface nearer than the near plane is no surface, and the globe
|
||||
// stands in for it.
|
||||
const surface = picker.pickPosition(at);
|
||||
let depth = surface ? cartesian3.distance(ray.origin, surface) : Infinity;
|
||||
if (depth < NEAR_SURFACE_M) {
|
||||
const globe = picker.pickGlobe?.(at);
|
||||
depth = globe ? cartesian3.distance(ray.origin, globe) : Infinity;
|
||||
}
|
||||
const hits = [];
|
||||
for (const { state, backend } of candidates) {
|
||||
backend.group.updateWorldMatrix(true, true);
|
||||
const hit = raycaster.intersectObject(backend.mesh, false)[0];
|
||||
if (!hit || (state.depthTest !== 'always' && hit.distance > depth + 0.1))
|
||||
continue;
|
||||
hits.push({
|
||||
onTop: state.depthTest === 'always' ? 1 : 0,
|
||||
order: 0,
|
||||
depth: hit.distance,
|
||||
result: { id: state.pickId, owner: state.id },
|
||||
});
|
||||
}
|
||||
return hits;
|
||||
});
|
||||
return {
|
||||
meshes: registry.meshes,
|
||||
destroy() {
|
||||
removePicks();
|
||||
registry.destroy();
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -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();
|
||||
});
|
||||
@@ -0,0 +1,109 @@
|
||||
// Navara draws its transparent scene after the post effects, into a target
|
||||
// that does not hold the scene's depth, so a transparent mesh behind a wall,
|
||||
// terrain or the Earth painted through them. Moving such a mesh into the
|
||||
// opaque scene (as CCTV did) would also run it through the effects and change
|
||||
// its look. This test keeps the mesh in the transparent pass and drops every
|
||||
// fragment that lies behind the depth Navara packs after its opaque pass
|
||||
// (globe, 3D tiles, MRT and opaque meshes): an unhidden fragment is drawn
|
||||
// exactly as before.
|
||||
import { RGBADepthPacking, Vector2 } from 'three';
|
||||
|
||||
const KEY = 'gevSceneDepth';
|
||||
|
||||
// Log-depth units (about 0.01 % of the distance): a stem's foot or a ground
|
||||
// overlay on the very surface it stands on is not cut by its own ground.
|
||||
export const SCENE_DEPTH_BIAS = 5e-6;
|
||||
|
||||
const PARS = /* glsl */ `
|
||||
uniform sampler2D tGevSceneDepth;
|
||||
uniform vec2 uGevTargetSize;
|
||||
uniform float uGevSceneDepthOn;
|
||||
`;
|
||||
|
||||
const TEST = /* glsl */ `
|
||||
if (uGevSceneDepthOn > 0.5) {
|
||||
vec2 gevDepthSize = vec2(textureSize(tGevSceneDepth, 0));
|
||||
ivec2 gevTexel = ivec2(gl_FragCoord.xy * gevDepthSize / uGevTargetSize);
|
||||
float gevScene = unpackRGBAToDepth(texelFetch(tGevSceneDepth, gevTexel, 0));
|
||||
#if defined( USE_LOGARITHMIC_DEPTH_BUFFER )
|
||||
float gevDepth = vIsPerspective == 0.0
|
||||
? gl_FragCoord.z
|
||||
: log2(vFragDepth) * logDepthBufFC * 0.5;
|
||||
#else
|
||||
float gevDepth = gl_FragCoord.z;
|
||||
#endif
|
||||
if (gevScene < 1.0 && gevDepth > gevScene + ${SCENE_DEPTH_BIAS.toExponential()})
|
||||
discard;
|
||||
}
|
||||
`;
|
||||
|
||||
/** Patch a fragment shader source with the scene-depth test. */
|
||||
export function withSceneDepthTest(fragmentShader) {
|
||||
const anchor = '#include <logdepthbuf_fragment>';
|
||||
if (!fragmentShader.includes(anchor))
|
||||
throw new Error('A scene-depth test needs logdepthbuf_fragment');
|
||||
const pars = fragmentShader.includes('#include <packing>')
|
||||
? PARS
|
||||
: `#include <packing>\n${PARS}`;
|
||||
return fragmentShader
|
||||
.replace('void main() {', `${pars}\nvoid main() {`)
|
||||
.replace(anchor, `${anchor}\n${TEST}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* One scene-depth test for a set of transparent meshes: `material()` patches
|
||||
* each material, `object()` binds the depth before each draw, and the mesh
|
||||
* descriptor calls `attach(ctx)` with its view context once created.
|
||||
*/
|
||||
export function createSceneDepthTest() {
|
||||
const uniforms = {
|
||||
tGevSceneDepth: { value: null },
|
||||
uGevTargetSize: { value: new Vector2(1, 1) },
|
||||
uGevSceneDepthOn: { value: 0 },
|
||||
};
|
||||
let ctx = null;
|
||||
const bind = (renderer) => {
|
||||
const mrt = ctx?.findEffect?.('mrt');
|
||||
const texture =
|
||||
mrt?.depthBufferPacking === RGBADepthPacking ? mrt.depthBuffer : null;
|
||||
uniforms.tGevSceneDepth.value = texture ?? null;
|
||||
uniforms.uGevSceneDepthOn.value = texture ? 1 : 0;
|
||||
const target = renderer.getRenderTarget?.();
|
||||
if (target) uniforms.uGevTargetSize.value.set(target.width, target.height);
|
||||
else renderer.getDrawingBufferSize?.(uniforms.uGevTargetSize.value);
|
||||
};
|
||||
const test = {
|
||||
uniforms,
|
||||
material(material) {
|
||||
const previousCompile = material.onBeforeCompile;
|
||||
const previousKey = material.customProgramCacheKey;
|
||||
material.onBeforeCompile = (shader, renderer) => {
|
||||
Object.assign(shader.uniforms, uniforms);
|
||||
shader.fragmentShader = withSceneDepthTest(shader.fragmentShader);
|
||||
previousCompile?.call(material, shader, renderer);
|
||||
};
|
||||
material.customProgramCacheKey = () =>
|
||||
`${previousKey ? previousKey.call(material) : ''}_gevSceneDepth`;
|
||||
material.needsUpdate = true;
|
||||
return material;
|
||||
},
|
||||
object(object) {
|
||||
const previous = object.onBeforeRender;
|
||||
object.onBeforeRender = function onBeforeRender(renderer, ...rest) {
|
||||
bind(renderer);
|
||||
return previous?.call(this, renderer, ...rest);
|
||||
};
|
||||
object.userData[KEY] = test;
|
||||
return object;
|
||||
},
|
||||
attach(next) {
|
||||
ctx = next ?? null;
|
||||
},
|
||||
};
|
||||
return test;
|
||||
}
|
||||
|
||||
/** Give an object's scene-depth test (if any) its descriptor's context. */
|
||||
export function attachSceneDepth(object, ctx) {
|
||||
object?.userData?.[KEY]?.attach(ctx);
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import {
|
||||
BasicDepthPacking,
|
||||
DataTexture,
|
||||
MeshBasicMaterial,
|
||||
RGBADepthPacking,
|
||||
ShaderLib,
|
||||
} from 'three';
|
||||
import {
|
||||
SCENE_DEPTH_BIAS,
|
||||
createSceneDepthTest,
|
||||
withSceneDepthTest,
|
||||
} from './sceneDepth.js';
|
||||
import { createPackRenderer } from '../../scenes/dataPacks/display.js';
|
||||
import { createNavaraMeshes } from './meshes.js';
|
||||
|
||||
const count = (text, part) => text.split(part).length - 1;
|
||||
|
||||
test('the scene-depth test goes after the log depth and brings packing once', () => {
|
||||
for (const source of [
|
||||
ShaderLib.basic.fragmentShader,
|
||||
'#include <common>\n#include <packing>\nvoid main() {\n#include <logdepthbuf_fragment>\n}',
|
||||
]) {
|
||||
const patched = withSceneDepthTest(source);
|
||||
assert.equal(count(patched, '#include <packing>'), 1);
|
||||
assert.equal(count(patched, 'uniform sampler2D tGevSceneDepth;'), 1);
|
||||
const depth = patched.indexOf('#include <logdepthbuf_fragment>');
|
||||
const test = patched.indexOf('if (uGevSceneDepthOn > 0.5)');
|
||||
assert.ok(depth >= 0 && test > depth, 'tested after gl_FragDepth is set');
|
||||
assert.ok(
|
||||
patched.indexOf('uniform float uGevSceneDepthOn;') <
|
||||
patched.indexOf('void main() {'),
|
||||
);
|
||||
assert.match(patched, /gevDepth > gevScene \+ 5e-6\)\s*discard;/);
|
||||
}
|
||||
assert.ok(SCENE_DEPTH_BIAS > 0 && SCENE_DEPTH_BIAS < 1e-4);
|
||||
assert.throws(() => withSceneDepthTest('void main() {}'), /logdepthbuf/);
|
||||
});
|
||||
|
||||
test('the test binds the MRT pass packed depth before each draw, and only packed RGBA depth', () => {
|
||||
const depth = createSceneDepthTest();
|
||||
const material = depth.material(new MeshBasicMaterial());
|
||||
const shader = {
|
||||
uniforms: {},
|
||||
vertexShader: ShaderLib.basic.vertexShader,
|
||||
fragmentShader: ShaderLib.basic.fragmentShader,
|
||||
};
|
||||
material.onBeforeCompile(shader, null);
|
||||
assert.match(material.customProgramCacheKey(), /_gevSceneDepth$/);
|
||||
assert.equal(shader.uniforms.tGevSceneDepth, depth.uniforms.tGevSceneDepth);
|
||||
assert.match(shader.fragmentShader, /discard;/);
|
||||
|
||||
const drawn = [];
|
||||
const object = depth.object({
|
||||
userData: {},
|
||||
onBeforeRender: (renderer) => drawn.push(renderer),
|
||||
});
|
||||
const texture = new DataTexture();
|
||||
const mrt = { depthBuffer: texture, depthBufferPacking: RGBADepthPacking };
|
||||
const renderer = { getRenderTarget: () => ({ width: 640, height: 360 }) };
|
||||
// Before the descriptor attaches its context the test stays off.
|
||||
object.onBeforeRender(renderer);
|
||||
assert.equal(depth.uniforms.uGevSceneDepthOn.value, 0);
|
||||
depth.attach({ findEffect: (key) => (key === 'mrt' ? mrt : undefined) });
|
||||
object.onBeforeRender(renderer);
|
||||
assert.equal(depth.uniforms.tGevSceneDepth.value, texture);
|
||||
assert.equal(depth.uniforms.uGevSceneDepthOn.value, 1);
|
||||
assert.deepEqual(depth.uniforms.uGevTargetSize.value.toArray(), [640, 360]);
|
||||
assert.deepEqual(drawn, [renderer, renderer], 'the mesh hook still runs');
|
||||
// Another packing would be read wrong: the mesh draws as before.
|
||||
mrt.depthBufferPacking = BasicDepthPacking;
|
||||
object.onBeforeRender(renderer);
|
||||
assert.equal(depth.uniforms.uGevSceneDepthOn.value, 0);
|
||||
assert.equal(depth.uniforms.tGevSceneDepth.value, null);
|
||||
});
|
||||
|
||||
/** A Navara view that builds each descriptor as the SDK does. */
|
||||
function fakeView() {
|
||||
const kinds = new Map();
|
||||
const created = [];
|
||||
const mrt = {
|
||||
depthBuffer: new DataTexture(),
|
||||
depthBufferPacking: RGBADepthPacking,
|
||||
};
|
||||
const ctx = { findEffect: (key) => (key === 'mrt' ? mrt : undefined) };
|
||||
class MeshDesc {
|
||||
constructor(view, context, config) {
|
||||
this.ctx = context;
|
||||
this.config = config;
|
||||
}
|
||||
onDestroy() {}
|
||||
}
|
||||
const view = {
|
||||
forceUpdate() {},
|
||||
registerMesh: (kind, desc) => kinds.set(kind, desc),
|
||||
addMesh(config) {
|
||||
const kind = Object.keys(config)[0];
|
||||
const desc = new (kinds.get(kind))(view, ctx, config);
|
||||
const object = desc.createMesh();
|
||||
created.push({ kind, pass: desc.getPassKey(), object });
|
||||
return { delete: () => desc.onDestroy() };
|
||||
},
|
||||
};
|
||||
return { view, MeshDesc, created, mrt };
|
||||
}
|
||||
|
||||
/** Every renderable under an object compiles with the test and binds depth. */
|
||||
function assertTested(object, mrt) {
|
||||
const renderables = [];
|
||||
object.traverse?.((child) => {
|
||||
if (child.material) renderables.push(child);
|
||||
});
|
||||
if (!object.traverse && object.material) renderables.push(object);
|
||||
assert.ok(renderables.length > 0);
|
||||
for (const child of renderables) {
|
||||
const shader = {
|
||||
uniforms: { ...(child.material.uniforms ?? {}) },
|
||||
vertexShader: '',
|
||||
fragmentShader:
|
||||
child.material.fragmentShader ?? ShaderLib.basic.fragmentShader,
|
||||
};
|
||||
child.material.onBeforeCompile(shader, null);
|
||||
assert.match(shader.fragmentShader, /if \(uGevSceneDepthOn > 0\.5\)/);
|
||||
assert.ok(shader.uniforms.tGevSceneDepth);
|
||||
child.onBeforeRender(
|
||||
{
|
||||
getRenderTarget: () => ({ width: 8, height: 8 }),
|
||||
getViewport: (target) => target.set(0, 0, 8, 8),
|
||||
},
|
||||
null,
|
||||
null,
|
||||
child.geometry,
|
||||
child.material,
|
||||
);
|
||||
assert.equal(shader.uniforms.tGevSceneDepth.value, mrt.depthBuffer);
|
||||
assert.equal(shader.uniforms.uGevSceneDepthOn.value, 1);
|
||||
}
|
||||
}
|
||||
|
||||
test('pack meshes stay transparent and test the scene depth', () => {
|
||||
const packs = fakeView();
|
||||
const owned = [];
|
||||
const port = createNavaraMeshes(packs.view, {
|
||||
MeshDesc: packs.MeshDesc,
|
||||
camera: {},
|
||||
picker: { registerMeshPicker: () => () => {} },
|
||||
});
|
||||
const engine = { meshes: port.meshes, render: { request() {} } };
|
||||
createPackRenderer(engine).image(
|
||||
{ id: 'p', placement: { bounds: [0, 0, 0.01, 0.01], height: 300 } },
|
||||
{},
|
||||
{ own: (fn) => owned.push(fn) },
|
||||
);
|
||||
assert.equal(packs.created.length, 1);
|
||||
assert.equal(packs.created[0].pass, 'transparent');
|
||||
assertTested(packs.created[0].object, packs.mrt);
|
||||
assert.equal(packs.created[0].object.children[0].material.depthTest, true);
|
||||
for (const release of owned) release();
|
||||
port.destroy();
|
||||
});
|
||||
Reference in new issue
Block a user