Files
gods-eye-view/src/globe/navara/sharpen.js
T
German Khokhlov e804351205 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.
2026-10-16 19:04:57 +04:00

152 lines
5.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// The app's Sharpen control on Navara: an unsharp mask as a Navara custom
// effect (public EffectDesc API), with the maths of the Cesium sharpen stage
// (src/ui/visualPresets.js SHARPEN_SHADER): a 3×3 box blur, then
// center + (center − blur) × amount, clamped to [0, 1].
//
// Where it sits: right after tone mapping (after gev's overlay of sprites,
// points and lines, which Cesium's sharpen stage also sharpens) and before
// SMAA or FXAA (before `final` in a look without them), as Cesium runs its
// custom stages after
// its tone mapping and before FXAA. After tone mapping the colours are the
// display's [0, 1] range, so highlights do not blow up; anti-aliasing after
// it smooths the stair steps sharpening brings out. Cesium sharpens
// gamma-encoded colours (its tone mapping ends with czm_inverseGamma) while
// Navara's buffers stay linear until the final copy, so the mask encodes
// with Cesium's gamma 2.2 and decodes after: the same amount sharpens the
// same.
//
// The sky is left alone: Navara's clouds are dithered (a fine grain at the
// `low` quality the look uses), and the mask turned that grain into visible
// noise. A pixel whose scene depth (the MRT pass's packed depth buffer, as
// the S1 drape reads it) is the far plane is sky or cloud; it keeps its
// colour, and costs one texture read instead of nine.
//
// Its own pass: a convolution reads the pass input, so merged into the tone
// mapping pass it would blur colours not yet tone mapped; Navara 0.2.1 also
// has no public call to merge effects. A disabled pass is skipped: off costs
// nothing.
import { Effect as NavaraEffect, EffectDesc } from '@navaramap/three';
import { Effect, EffectAttribute } from 'postprocessing';
import { RGBADepthPacking, Uniform } from 'three';
export const SHARPEN_EFFECT_KEY = 'gevSharpen';
const fragmentShader = /* glsl */ `
uniform float amount;
uniform sampler2D tSceneDepth;
uniform bool uSkipSky;
const float SKY_DEPTH = 0.99999;
vec3 encodeGamma(vec3 color) {
return pow(max(color, vec3(0.0)), vec3(1.0 / 2.2));
}
vec3 sampleGamma(vec2 uv) {
return encodeGamma(texture2D(inputBuffer, uv).rgb);
}
void mainImage(const in vec4 inputColor, const in vec2 uv, out vec4 outputColor) {
// The far plane, with a margin: packing to RGBA8 rounds near 1.0.
if (uSkipSky && unpackRGBAToDepth(texture2D(tSceneDepth, uv)) > SKY_DEPTH) {
outputColor = inputColor;
return;
}
vec2 texel = texelSize;
vec3 center = encodeGamma(inputColor.rgb);
vec3 blur = (
sampleGamma(uv + vec2(-texel.x, -texel.y)) +
sampleGamma(uv + vec2( 0.0, -texel.y)) +
sampleGamma(uv + vec2( texel.x, -texel.y)) +
sampleGamma(uv + vec2(-texel.x, 0.0)) +
center +
sampleGamma(uv + vec2( texel.x, 0.0)) +
sampleGamma(uv + vec2(-texel.x, texel.y)) +
sampleGamma(uv + vec2( 0.0, texel.y)) +
sampleGamma(uv + vec2( texel.x, texel.y))
) / 9.0;
vec3 sharpened = clamp(center + (center - blur) * amount, 0.0, 1.0);
outputColor = vec4(pow(sharpened, vec3(2.2)), inputColor.a);
}
`;
class UnsharpMaskEffect extends Effect {
constructor(amount) {
super('GevUnsharpMaskEffect', fragmentShader, {
attributes: EffectAttribute.CONVOLUTION,
uniforms: new Map([
['amount', new Uniform(amount)],
['tSceneDepth', new Uniform(null)],
['uSkipSky', new Uniform(false)],
]),
});
}
}
/** config.gevSharpen: { amount } (Cesium's `amount` uniform). */
export class SharpenEffectDesc extends EffectDesc {
static key = SHARPEN_EFFECT_KEY;
static insertAfter = ['gevOverlay', 'toneMapping'];
static insertBefore = ['smaa', 'fxaa', 'final'];
constructor(view, ctx, config) {
super(view, ctx, config);
this.amount = config?.[SHARPEN_EFFECT_KEY]?.amount ?? 1;
}
createPass() {
const camera = this.view.camera.raw;
if (!camera) throw new Error('Camera not available for the sharpening');
this.effect = new UnsharpMaskEffect(this.amount);
return new NavaraEffect(camera, this.effect);
}
// Each frame: the scene depth for the sky mask, when Navara packs it as
// RGBA (0.2.1 does); otherwise the whole frame is sharpened.
update() {
if (!this.effect) return;
const mrt = this.find('mrt');
const depth =
mrt?.depthBufferPacking === RGBADepthPacking ? mrt.depthBuffer : null;
const uniforms = this.effect.uniforms;
uniforms.get('tSceneDepth').value = depth ?? null;
uniforms.get('uSkipSky').value = Boolean(depth);
}
onUpdateConfig(updates) {
super.onUpdateConfig(updates);
const amount = updates?.[SHARPEN_EFFECT_KEY]?.amount;
if (amount === undefined) return;
this.amount = amount;
if (this.effect) this.effect.uniforms.get('amount').value = amount;
}
}
/**
* The sharpening port createNavaraEngine takes: `set` shows a state
* ({ enabled, amount }, already checked). The pass is added the first time
* the sharpening is on, so a view that never sharpens has none.
* @param {object} view an initialised ThreeView
*/
export function createNavaraSharpen(view) {
view.registerEffect(SHARPEN_EFFECT_KEY, SharpenEffectDesc);
let handle = null;
return {
set({ enabled, amount }) {
if (!handle) {
if (!enabled) return;
handle = view.addEffect({
[SHARPEN_EFFECT_KEY]: { amount },
visible: true,
});
return;
}
handle.update({ [SHARPEN_EFFECT_KEY]: { amount }, visible: enabled });
},
destroy() {
handle?.delete();
handle = null;
},
};
}