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

+17 -41
View File
@@ -1,56 +1,25 @@
// engine.lines on Navara: each line is one Navara mesh descriptor (public
// MeshDesc API, the transparent pass, unlit) holding its ribbon
// (lineMesh.js). Geodesic arcs are densified on the CPU (src/globe/lines.js)
// engine.lines on Navara: each line is a ribbon (lineMesh.js) in gev's
// overlay (overlay.js: drawn after tone mapping, so its colour is exact). Geodesic arcs are densified on the CPU (src/globe/lines.js)
// since the ribbon draws straight segments. Every change asks for one frame;
// nothing animates on its own, so an idle view stays idle. Lines are not
// pickable on Navara yet (trails never take a click; see phase 3 Task 4).
import { MeshDesc } from '@navaramap/three';
import { createLineRegistry, densifyGeodesic } from '../../lines.js';
import { createLineMesh } from './lineMesh.js';
const DESC_NAME = 'gevLine';
const registered = new WeakSet();
class GevLineDesc extends MeshDesc {
constructor(view, ctx, config) {
super(view, ctx, config);
this.gev = config[DESC_NAME];
}
getPassKey() {
return 'transparent';
}
createMesh() {
return this.gev.line.group;
}
update() {
const canvas = this.ctx.getRenderer().domElement;
this.gev.line.update(canvas.clientWidth, canvas.clientHeight);
}
onDestroy() {
this.gev.line.dispose();
super.onDestroy();
}
}
/**
* @param {object} view an initialised Navara ThreeView
* @param {{ overlay: ReturnType<import('../overlay.js').createNavaraOverlay> }} ports
* @returns {{ lines: import('../../contract.js').GlobeLines, destroy: () => void }}
*/
export function createNavaraLines(view) {
if (!registered.has(view)) {
view.registerMesh(DESC_NAME, GevLineDesc);
registered.add(view);
}
export function createNavaraLines(view, { overlay }) {
const request = () => view.forceUpdate();
// What CPU picks need of each line: its drawn positions and matrix.
const drawn = new Map();
const registry = createLineRegistry((options) => {
const line = createLineMesh(options);
const line = createLineMesh(options, {
overlayUniforms: overlay.uniforms,
});
const state = {
options,
flat: new Float64Array(0),
@@ -59,8 +28,14 @@ export function createNavaraLines(view) {
};
drawn.set(options.id, state);
const canvas = view.canvas;
if (canvas) line.update(canvas.clientWidth || 1, canvas.clientHeight || 1);
const handle = view.addMesh({ lit: false, [DESC_NAME]: { line } });
const viewport = () =>
line.update(
canvas?.clientWidth || 1,
canvas?.clientHeight || 1,
overlay.ctx?.getRenderer().getPixelRatio() ?? 1,
);
viewport();
overlay.add(line.group, viewport);
return {
setPositions(flat) {
state.flat = options.arc === 'geodesic' ? densifyGeodesic(flat) : flat;
@@ -83,7 +58,8 @@ export function createNavaraLines(view) {
},
destroy() {
drawn.delete(options.id);
handle.delete();
overlay.remove(line.group);
line.dispose();
request();
},
};
+53 -16
View File
@@ -1,10 +1,12 @@
// The three side of a Navara line: a ribbon of one quad per segment whose
// vertices are pushed apart in the vertex shader to the line's width in CSS
// pixels (projected ends, the screen-space normal), so the width holds at any
// distance. A second copy of the ribbon, drawn only where the scene hides it
// (depthFunc GreaterDepth) with the alpha scaled, is depthFailAlpha. Dashes
// in pixels are cut in the fragment shader along the segment's screen
// direction (as Cesium's PolylineDash); the tactical dash is the launches'
// distance. Drawn after tone mapping (overlay.js) with the depth test in the
// shader (overlayDepth.js); a second copy of the ribbon, drawn only where the
// scene hides it with the alpha scaled, is depthFailAlpha. Dashes in CSS
// pixels are cut in the fragment shader along the segment's screen
// direction (as Cesium's PolylineDash; gl_FragCoord over the pixel ratio, so
// the period is the same on any screen); the tactical dash is the launches'
// pattern over the fraction along the whole line. Positions are relative to
// the first point (the group sits there, in doubles), the model matrix
// multiplies the group's transform (an orbit ring turned by GMST).
@@ -13,13 +15,16 @@ import {
BufferAttribute,
BufferGeometry,
DoubleSide,
GreaterDepth,
Group,
LessEqualDepth,
Matrix4,
Mesh,
MeshBasicMaterial,
} from 'three';
import {
OVERLAY_DEPTH_FRAGMENT,
OVERLAY_DEPTH_MODES,
OVERLAY_DEPTH_VERTEX,
} from '../overlayDepth.js';
const VERTEX_HEAD = /* glsl */ `
attribute vec3 gevEnd;
@@ -30,6 +35,7 @@ uniform float uGevWidthPx;
varying float vGevS;
varying float vGevT;
varying vec2 vGevDirection;
${OVERLAY_DEPTH_VERTEX}
`;
const VERTEX_PROJECT = /* glsl */ `
@@ -50,11 +56,14 @@ vec4 mvPosition = modelViewMatrix * vec4(gevSide.x < 0.5 ? position : gevEnd, 1.
vGevS = gevS;
vGevT = gevSide.y * 0.5 + 0.5;
vGevDirection = gevDirection;
vGevDepthFree = 0.0;
`;
const FRAGMENT_HEAD = /* glsl */ `
uniform vec4 uGevColor;
uniform float uGevAlphaScale;
uniform float uGevPixelRatio;
${OVERLAY_DEPTH_FRAGMENT}
uniform float uGevDash;
uniform vec2 uGevDashPx;
uniform vec2 uGevTactical;
@@ -80,12 +89,13 @@ float gevTactical() {
const FRAGMENT_COLOR = /* glsl */ `
#include <color_fragment>
if (gevOverlayHidden()) discard;
diffuseColor = uGevColor;
diffuseColor.a *= uGevAlphaScale;
if (uGevDash > 1.5) {
diffuseColor.a *= gevTactical();
} else if (uGevDash > 0.5) {
float gevAlong = dot(gl_FragCoord.xy, vGevDirection);
float gevAlong = dot(gl_FragCoord.xy / uGevPixelRatio, vGevDirection);
if (mod(gevAlong, uGevDashPx.x + uGevDashPx.y) > uGevDashPx.x) discard;
}
`;
@@ -170,11 +180,23 @@ export function buildRibbon(flat, origin) {
return geometry;
}
// Overlay uniforms of a line drawn outside an overlay (tests): no depth.
const DETACHED_OVERLAY = Object.freeze({
tGevSceneDepth: { value: null },
uGevResolution: { value: [0, 0] },
});
/** @param {ReturnType<import('../../lines.js').normalizeLineOptions>} options */
export function createLineMesh(options) {
export function createLineMesh(
options,
{ overlayUniforms = DETACHED_OVERLAY } = {},
) {
const dash = options.dash;
const shared = {
uGevViewport: { value: [1, 1] },
uGevPixelRatio: { value: 1 },
tGevSceneDepth: overlayUniforms.tGevSceneDepth,
uGevResolution: overlayUniforms.uGevResolution,
uGevWidthPx: { value: options.widthPx },
uGevColor: { value: [...options.color] },
uGevDash: { value: dash?.kind === 'tactical' ? 2 : dash ? 1 : 0 },
@@ -185,14 +207,19 @@ export function createLineMesh(options) {
value: dash?.kind === 'tactical' ? [dash.groups, dash.dashes] : [1, 1],
},
};
const material = (alphaScale, depthFunc) => {
const material = (alphaScale, depthMode) => {
const value = new MeshBasicMaterial({
transparent: true,
depthTest: false,
depthWrite: false,
depthFunc,
side: DoubleSide,
});
const uniforms = { ...shared, uGevAlphaScale: { value: alphaScale } };
const uniforms = {
...shared,
uGevAlphaScale: { value: alphaScale },
uGevDepthMode: { value: depthMode },
};
value.userData.gevDepthMode = depthMode;
value.onBeforeCompile = (shader) => patchLineShader(shader, uniforms);
value.customProgramCacheKey = () => 'gev-line';
return value;
@@ -200,7 +227,10 @@ export function createLineMesh(options) {
const group = new Group();
group.matrixAutoUpdate = false;
group.frustumCulled = false;
const main = new Mesh(new BufferGeometry(), material(1, LessEqualDepth));
const main = new Mesh(
new BufferGeometry(),
material(1, OVERLAY_DEPTH_MODES.normal),
);
main.frustumCulled = false;
main.renderOrder = options.order;
group.add(main);
@@ -208,7 +238,7 @@ export function createLineMesh(options) {
if (options.depthFailAlpha > 0) {
behind = new Mesh(
main.geometry,
material(options.depthFailAlpha, GreaterDepth),
material(options.depthFailAlpha, OVERLAY_DEPTH_MODES.fail),
);
behind.frustumCulled = false;
behind.renderOrder = options.order;
@@ -247,9 +277,16 @@ export function createLineMesh(options) {
show(visible) {
group.visible = visible;
},
/** Each drawn frame: the viewport the pixel width is measured in. */
update(width, height) {
shared.uGevViewport.value = [width, height];
/**
* Each drawn frame: the viewport (CSS px) the width is measured in and
* the device pixel ratio the dashes are measured with. In place: no
* allocation per frame.
*/
update(width, height, pixelRatio = 1) {
const viewport = shared.uGevViewport.value;
viewport[0] = width;
viewport[1] = height;
shared.uGevPixelRatio.value = pixelRatio;
},
dispose() {
main.geometry.dispose();
+11 -1
View File
@@ -67,7 +67,17 @@ test('a line places its group at the first point under its model matrix', () =>
// (6378137, 0, 0) turned 90° about z.
assert.ok(Math.abs(origin.x) < 1e-6 && Math.abs(origin.y - 6_378_137) < 1e-6);
const [main, behind] = line.meshes();
assert.equal(behind.material.depthFunc, GreaterDepth);
// The overlay tests depth in the shader: normal for the line, drawn
// only where the scene is nearer for its depth-fail copy.
assert.equal(main.material.depthTest, false);
assert.equal(main.material.userData.gevDepthMode, 1);
assert.equal(behind.material.userData.gevDepthMode, 2);
// Viewport and pixel ratio update in place (no allocation per frame).
const viewport = line.uniforms.uGevViewport.value;
line.update(1280, 800, 2);
assert.equal(line.uniforms.uGevViewport.value, viewport);
assert.deepEqual(viewport, [1280, 800]);
assert.equal(line.uniforms.uGevPixelRatio.value, 2);
assert.equal(main.geometry, behind.geometry);
assert.equal(line.uniforms.uGevDash.value, 2);
assert.deepEqual(line.uniforms.uGevTactical.value, [4, 100]);