fix(globe): Navara sprites, points and lines drawn after tone mapping

Review of Tasks 2–3: on Navara the overlays went through the look's
tone mapping and grading (magenta came out as 247/40/228, green as
147/228/89 in the cinematic look), where Cesium draws billboards and
polylines in the colours asked for.

src/globe/navara/overlay.js: a public Navara EffectDesc (gevOverlay)
inserted after toneMapping and before the gev sharpening, SMAA/FXAA and
final, whose pass renders one three scene over the tone-mapped input
buffer (in Navara's own Pass wrapper: Navara inserts a bare pass only
when it is an instance of its own postprocessing copy); it costs nothing
while the scene is empty. The chain has no depth buffer, so each overlay
material tests the scene depth itself (overlayDepth.js): its log depth
against Navara's MRT depth (RGBA packed), modes always / normal /
depth-fail, disableWithinM as a per-vertex escape. Sprites and points
(moverMesh.js) and line ribbons (lineMesh.js) live in that scene with
their per-frame work as overlay hooks; models stay Navara meshes, lit
and graded like the tiles. The sharpening now sits after the overlay
(Cesium's sharpen stage sharpens billboards too).

Also (review): Navara pixel dashes are measured in CSS pixels
(gl_FragCoord over uGevPixelRatio), and the line viewport uniform is
updated in place.

Measured: a magenta sprite draws 255/0/255. The contract suite's colour
checks are now within 40 of the asked colour on every channel on both
engines (were: dominant channels); Cesium 68/68, Navara 74/74 (its
sharpen-order check accepts the overlay between tone mapping and the
sharpening). Shell: cesium 23/23, cross 4/4; navara 23/25 — the two map
stack pictures from space fail on stddev 7.2/7.6 against 8, as on the
Task 3 commit run at the same hour (the cinematic sun follows the clock
into night over the reset view), not from this change.
This commit is contained in:
German Khokhlov committed 2026-10-16 19:04:57 +04:00
1 parent 3c2e924363
commit e804351205
11 files changed
+388 -125

No files matched your search

+72 -38
View File
@@ -1,10 +1,12 @@
// engine.movers on Navara: each set is one Navara mesh descriptor (public
// MeshDesc API) holding its instanced meshes (moverMesh.js, modelMover.js).
// Pickable sets register a PickableInstancedMeshWrapper so Navara's pick
// pass sees each instance; pickIds.js turns its batch ids back into
// instances. commit() uploads and asks for one frame (view.forceUpdate): a
// descriptor's update() runs only on frames the view draws anyway (S5), so
// an idle view stays idle.
// engine.movers on Navara. Sprites and points are one instanced quad mesh
// per set (moverMesh.js) in gev's overlay (overlay.js: drawn after tone
// mapping, so their colours are exact); models are a Navara mesh descriptor
// (public MeshDesc API) holding one InstancedMesh per glTF mesh
// (modelMover.js), lit and graded like the scene. Pickable sets register a
// PickableInstancedMeshWrapper so Navara's pick pass sees each instance;
// pickIds.js turns its batch ids back into instances. commit() uploads and
// asks for one frame (view.forceUpdate); per-frame work runs only on frames
// the view draws anyway (S5), so an idle view stays idle.
import {
MeshDesc,
@@ -28,13 +30,8 @@ class GevMoverSetDesc extends MeshDesc {
this.wrappers = [];
}
// Sprites and points are screen overlays: the transparent pass, unlit.
getPassKey() {
return this.gev?.kind === 'model' ? super.getPassKey() : 'transparent';
}
createMesh() {
const { mover, pickable, onPickable, kind } = this.gev;
const { mover, pickable, onPickable } = this.gev;
const register = (mesh, key) => {
if (!pickable) return;
const wrapper = new PickableInstancedMeshWrapper(
@@ -46,10 +43,6 @@ class GevMoverSetDesc extends MeshDesc {
this.wrappers.push(key);
onPickable(wrapper);
};
if (kind !== 'model') {
register(mover.mesh, this.id);
return mover.mesh;
}
const group = new Group();
group.frustumCulled = false;
// Meshes arrive with the glTF; they join the group (and the pick pass).
@@ -65,21 +58,7 @@ class GevMoverSetDesc extends MeshDesc {
}
update() {
const raw = this.view.camera.raw;
const height = this.ctx.getRenderer().domElement.clientHeight;
if (!raw || !(height > 0)) return;
raw.updateMatrixWorld();
const e = raw.matrixWorld.elements;
const cameraPosition = { x: e[12], y: e[13], z: e[14] };
const cameraHeight =
Math.hypot(cameraPosition.x, cameraPosition.y, cameraPosition.z) -
EARTH_RADIUS_M;
this.gev.mover.update({
cameraPosition,
cameraHeight,
fovDeg: raw.fov,
viewportHeightPx: height,
});
updateMover(this.view, this.ctx, this.gev.mover);
}
onDestroy() {
@@ -90,13 +69,38 @@ class GevMoverSetDesc extends MeshDesc {
}
}
// The camera a mover needs each drawn frame (scratch: no allocation).
const frameCamera = {
cameraPosition: { x: 0, y: 0, z: 0 },
cameraHeight: 0,
fovDeg: 60,
viewportHeightPx: 0,
};
function updateMover(view, ctx, mover) {
const raw = view.camera.raw;
const height = ctx?.getRenderer().domElement.clientHeight;
if (!raw || !(height > 0)) return;
raw.updateMatrixWorld();
const e = raw.matrixWorld.elements;
const position = frameCamera.cameraPosition;
position.x = e[12];
position.y = e[13];
position.z = e[14];
frameCamera.cameraHeight =
Math.hypot(position.x, position.y, position.z) - EARTH_RADIUS_M;
frameCamera.fovDeg = raw.fov;
frameCamera.viewportHeightPx = height;
mover.update(frameCamera);
}
/**
* @param {object} view an initialised Navara ThreeView
* @param {{ overlay: ReturnType<import('../overlay.js').createNavaraOverlay> }} ports
* @returns {{ movers: import('../../contract.js').GlobeMovers,
* resolvePick: (batchId: number) => ({ set: string, instance: number, pickId: any } | null),
* destroy: () => void }}
*/
export function createNavaraMovers(view) {
export function createNavaraMovers(view, { overlay }) {
if (!registered.has(view)) {
view.registerMesh(DESC_NAME, GevMoverSetDesc);
registered.add(view);
@@ -108,13 +112,43 @@ export function createNavaraMovers(view) {
return loader.loadAsync(url);
};
const request = () => view.forceUpdate();
const overlaySet = (options, store) => {
const mover = createMoverMesh(options, store, {
onTextureLoad: request,
overlayUniforms: overlay.uniforms,
});
const key = `gevMovers:${options.id}`;
if (options.pickable) {
const wrapper = new PickableInstancedMeshWrapper(
mover.mesh,
options.capacity,
overlay.ctx,
);
overlay.registerPickable(key, wrapper);
pickIndex.add(options.id, wrapper, (index) => store.pickIds[index]);
}
overlay.add(mover.mesh, () => updateMover(view, overlay.ctx, mover));
return {
commit() {
mover.commit(store.dirty());
request();
},
destroy() {
pickIndex.remove(options.id);
if (options.pickable) overlay.unregisterPickable(key);
overlay.remove(mover.mesh);
mover.dispose();
request();
},
};
};
const registry = createMoverRegistry((options, store) => {
const mover =
options.kind === 'model'
? createModelMover(options, store, { load: loadGltf, onLoad: request })
: createMoverMesh(options, store, { onTextureLoad: request });
if (options.kind !== 'model') return overlaySet(options, store);
const mover = createModelMover(options, store, {
load: loadGltf,
onLoad: request,
});
const handle = view.addMesh({
...(options.kind === 'model' ? {} : { lit: false }),
[DESC_NAME]: {
mover,
kind: options.kind,
+29 -10
View File
@@ -23,6 +23,11 @@ import {
originFor,
packInstances,
} from './pack.js';
import {
OVERLAY_DEPTH_FRAGMENT,
OVERLAY_DEPTH_MODES,
OVERLAY_DEPTH_VERTEX,
} from '../overlayDepth.js';
const VERTEX_HEAD = /* glsl */ `
attribute vec4 gevStyle;
@@ -37,6 +42,7 @@ uniform float uGevOutlinePx;
varying vec4 vGevColor;
varying vec2 vGevLocal;
varying float vGevOutline;
${OVERLAY_DEPTH_VERTEX}
`;
const VERTEX_PROJECT = /* glsl */ `
@@ -66,6 +72,7 @@ gl_Position = projectionMatrix * mvPosition;
vGevColor = gevColor;
vGevLocal = gevCorner;
vGevOutline = uGevOutlinePx / max(gevSizePx, 1e-3);
vGevDepthFree = gevDistance < uGevDisableWithinM ? 1.0 : 0.0;
`;
const VERTEX_UV = /* glsl */ `
@@ -75,23 +82,18 @@ vMapUv = mix(gevUv.xy, gevUv.zw, uv);
#endif
`;
const VERTEX_DEPTH = /* glsl */ `
#include <logdepthbuf_vertex>
#ifdef USE_LOGARITHMIC_DEPTH_BUFFER
if (gevDistance < uGevDisableWithinM) vFragDepth = 1.0;
#endif
`;
const FRAGMENT_HEAD = /* glsl */ `
varying vec4 vGevColor;
varying vec2 vGevLocal;
varying float vGevOutline;
${OVERLAY_DEPTH_FRAGMENT}
`;
const fragmentColor = (point) =>
point
? /* glsl */ `
#include <color_fragment>
if (gevOverlayHidden()) discard;
float gevRadius = length(vGevLocal) * 2.0;
if (gevRadius > 1.0) discard;
diffuseColor *= vGevColor;
@@ -99,9 +101,16 @@ if (gevRadius > 1.0 - vGevOutline) diffuseColor.rgb = vec3(0.0);
`
: /* glsl */ `
#include <color_fragment>
if (gevOverlayHidden()) discard;
diffuseColor *= vGevColor;
`;
// Overlay uniforms of a mesh drawn outside an overlay (tests): no depth.
const DETACHED_OVERLAY = Object.freeze({
tGevSceneDepth: { value: null },
uGevResolution: { value: [0, 0] },
});
/** Patches three's MeshBasicMaterial shaders into the screen-space quad. */
export function patchMoverShader(shader, uniforms, { point = false } = {}) {
Object.assign(shader.uniforms, uniforms);
@@ -117,7 +126,6 @@ export function patchMoverShader(shader, uniforms, { point = false } = {}) {
);
vertex = replace(vertex, '#include <project_vertex>', VERTEX_PROJECT);
vertex = replace(vertex, '#include <uv_vertex>', VERTEX_UV);
vertex = replace(vertex, '#include <logdepthbuf_vertex>', VERTEX_DEPTH);
let fragment = replace(
shader.fragmentShader,
'void main() {',
@@ -161,7 +169,7 @@ function atlasTexture(atlas, onLoad) {
export function createMoverMesh(
options,
store,
{ onTextureLoad = () => {} } = {},
{ onTextureLoad = () => {}, overlayUniforms = DETACHED_OVERLAY } = {},
) {
const { capacity, kind, depthTest } = options;
const point = kind === 'point';
@@ -192,12 +200,23 @@ export function createMoverMesh(
value: typeof depthTest === 'object' ? depthTest.disableWithinM : -1,
},
uGevOutlinePx: { value: point ? options.point.outlinePx : 0 },
uGevDepthMode: {
value:
depthTest === 'always'
? OVERLAY_DEPTH_MODES.always
: OVERLAY_DEPTH_MODES.normal,
},
// Shared by every overlay: the scene depth and the target's size.
tGevSceneDepth: overlayUniforms.tGevSceneDepth,
uGevResolution: overlayUniforms.uGevResolution,
};
const material = new MeshBasicMaterial({
map: point ? null : atlasTexture(options.sprite.atlas, onTextureLoad),
transparent: true,
alphaTest: 0.02,
depthTest: depthTest !== 'always',
// Drawn after tone mapping, with no depth buffer: the shader tests the
// scene depth itself (overlayDepth.js).
depthTest: false,
depthWrite: false,
side: DoubleSide,
});
+10 -3
View File
@@ -32,10 +32,14 @@ test('the shader patch sizes the quad in pixels and colours it per instance', ()
/uGevFootprint \* max\(-mvCenter\.z, 0\.0\)/,
);
assert.match(shader.vertexShader, /vMapUv = mix\(gevUv\.xy, gevUv\.zw, uv\)/);
assert.match(shader.vertexShader, /vFragDepth = 1\.0;/);
assert.match(
shader.vertexShader,
/vGevDepthFree = gevDistance < uGevDisableWithinM/,
);
assert.match(shader.fragmentShader, /if \(gevOverlayHidden\(\)\) discard;/);
assert.doesNotMatch(shader.vertexShader, /#include <project_vertex>/);
assert.match(shader.fragmentShader, /diffuseColor \*= vGevColor;/);
assert.doesNotMatch(shader.fragmentShader, /discard/);
assert.doesNotMatch(shader.fragmentShader, /gevRadius > 1\.0\) discard/);
const point = patchMoverShader(basicShader(), {}, { point: true });
assert.match(point.fragmentShader, /if \(gevRadius > 1\.0\) discard;/);
});
@@ -46,7 +50,9 @@ test('a sprite mesh uploads committed instances relative to its origin', () => {
const { mesh, uniforms } = mover;
assert.equal(mesh.renderOrder, 3);
assert.equal(mesh.count, 0);
assert.equal(mesh.material.depthTest, true);
// The overlay tests depth in the shader, against the scene's.
assert.equal(mesh.material.depthTest, false);
assert.equal(uniforms.uGevDepthMode.value, 1);
assert.equal(uniforms.uGevDisableWithinM.value, 2000);
assert.deepEqual(uniforms.uGevScaleByDistance.value, [1000, 1, 100000, 0.5]);
store.set(0, { position: { x: 6_378_137, y: 10, z: 20 } });
@@ -98,6 +104,7 @@ test('points have no atlas and draw a disc', () => {
const mover = createMoverMesh(options, createMoverStore(options));
assert.equal(mover.mesh.material.map, null);
assert.equal(mover.mesh.material.depthTest, false);
assert.equal(mover.uniforms.uGevDepthMode.value, 0, "'always': no test");
assert.equal(mover.uniforms.uGevOutlinePx.value, 1);
mover.dispose();
});