Draw the visual styles on Navara as one post pass behind an optional render.setStyle capability

CRT, NVG, FLIR, Anime and Noir run their Cesium shaders through a GLSL prelude with the exact sRGB transfer, crossfade in the stage order, fade the Navara stars with Anime, and report the visualStyles capability through engineCapabilities.
This commit is contained in:
German Khokhlov committed 2026-11-03 21:02:50 +04:00
1 parent 00dfed7959
commit 4c28cc0212
16 files changed
+1805 -8

No files matched your search

+57 -5
View File
@@ -1,4 +1,5 @@
import { PostProcessStage } from 'cesium';
import { engineCapabilities } from '../globe/contract.js';
import {
bloomStrengthFromIntensity,
clampBloomIntensity,
@@ -11,6 +12,42 @@ import {
sharpenAmount,
} from './visualPresets.js';
/**
* A style stage drawn by the globe engine (render.setStyle) instead of a
* Cesium post stage: the same { uniforms, enabled } shape, so the owner of
* the stages writes its uniforms as it writes a Cesium stage's; each write
* reaches the engine. A value the engine refuses (NaN from an old link,
* say) stays here and is reported, as a Cesium uniform would just draw it.
*/
export function engineStyleStage(render, name, shader) {
const values = { intensity: 0 };
if (shader.fragmentShader.includes('uniform float time')) values.time = 0;
for (const [uniform, meta] of Object.entries(shader.uniforms || {}))
values[uniform] = meta.default;
const { intensity, ...uniforms } = values;
render.setStyle(name, { intensity, uniforms });
const forward = (key, value) => {
try {
if (key === 'intensity') render.setStyle(name, { intensity: value });
else render.setStyle(name, { uniforms: { [key]: value } });
} catch (error) {
console.warn(`The ${name} style refused ${key}=${value}:`, error);
}
};
return {
name: `godsEyeView_${name}`,
enabled: false,
uniforms: new Proxy(values, {
set(target, key, value) {
target[key] = value;
if (typeof key === 'string' && Object.hasOwn(values, key))
forward(key, value);
return true;
},
}),
};
}
/** Own the post-process stages and their animation, without DOM dependencies. */
export class VisualEffects {
/**
@@ -37,6 +74,10 @@ export class VisualEffects {
// only); an engine without a viewer (Navara) sharpens in its pipeline.
this.engineRender =
typeof engine?.render?.setSharpen === 'function' ? engine.render : null;
// Without a viewer, an engine that draws the styles itself
// (render.setStyle) gets them through stand-in stages (initStyles).
this.engineStyles =
!viewer && engineCapabilities(engine).visualStyles ? engine.render : null;
this.requestRender = requestRender;
this.holdRender = holdRender;
this.releaseRender = releaseRender;
@@ -61,11 +102,21 @@ export class VisualEffects {
this.previousBloom = null;
}
// Post-process stages are Cesium's: without a viewer (the Navara engine)
// there are none, and the style and bloom controls change nothing (the
// engine sharpens, see engineRender).
// Post-process stages are Cesium's. Without a viewer (the Navara engine)
// an engine that draws the styles gets stand-in stages (engineStyleStage),
// which start every style at 0 on it; otherwise there are none and the
// style controls change nothing (the engine sharpens, see engineRender).
initStyles() {
if (!this.viewer || this.stopped || this.stageEntries.length) return;
if (this.stopped || this.stageEntries.length) return;
if (!this.viewer) {
if (!this.engineStyles) return;
const drawn = this.engineStyles.visualStyles;
for (const [name, shader] of Object.entries(STYLES))
if (drawn.includes(name))
this.stages[name] = engineStyleStage(this.engineStyles, name, shader);
this.stageEntries = Object.entries(this.stages);
return;
}
for (const [name, shader] of Object.entries(STYLES)) {
const uniforms = { intensity: 0.0 };
if (shader.fragmentShader.includes('uniform float time'))
@@ -127,9 +178,10 @@ export class VisualEffects {
stage.enabled = value > 0.001;
// Anime's bands turn the faint Milky Way into blotches, and depth cannot
// tell the sky from far geometry, so the glow fades with the style.
// The engine fades its own stars with Anime (render.setStyle).
if (
stage === this.stages.anime &&
this.viewer.scene.skyBox?.milkyWayVisibility !== undefined
this.viewer?.scene.skyBox?.milkyWayVisibility !== undefined
)
this.viewer.scene.skyBox.milkyWayVisibility = 1 - value;
if (stage.enabled && stage.uniforms.time !== undefined)
+128
View File
@@ -1,6 +1,8 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { VisualEffects } from './visualEffects.js';
import { STYLES } from './visualPresets.js';
import { createFakeEngine } from '../globe/testing/fakeEngine.js';
import { MILKY_WAY_INTENSITY, StarrySky } from '../sky/starrySky.js';
import {
GLOBAL_POST_DEFAULTS,
@@ -338,6 +340,132 @@ test('without a viewer the engine sharpens with the same amount scale', () => {
effects.destroy();
});
// A fake engine drawing the app's styles (render.setStyle), as Navara does.
function styledEngine() {
return createFakeEngine({
kind: 'navara',
visualStyles: Object.fromEntries(
Object.entries(STYLES).map(([name, shader]) => [
name,
[
...Object.keys(shader.uniforms),
...(shader.fragmentShader.includes('uniform float time')
? ['time']
: []),
],
]),
),
});
}
function engineFixture(engine = styledEngine()) {
let time = 0;
let next = 0;
const frames = new Map();
const holds = new Set();
const requests = [];
const effects = new VisualEffects({
viewer: null,
engine,
requestRender: (reason) => requests.push(reason),
holdRender: (reason) => holds.add(reason),
releaseRender: (reason) => holds.delete(reason),
requestFrame: (callback) => {
const id = next++;
frames.set(id, callback);
return id;
},
cancelFrame: (id) => frames.delete(id),
now: () => time,
wallNow: () => 10_000 + time,
});
const tick = (value) => {
time = value;
const pending = [...frames.values()];
frames.clear();
for (const callback of pending) callback();
};
return { engine, effects, holds, requests, tick };
}
test('without a viewer an engine that draws styles gets stand-in stages', () => {
const { engine, effects } = engineFixture();
effects.initStyles();
assert.deepEqual(Object.keys(effects.stages), Object.keys(STYLES));
// Every style starts at 0 with its defaults on the engine.
for (const name of Object.keys(STYLES)) {
assert.equal(engine.render.style(name).intensity, 0);
assert.equal(effects.stages[name].enabled, false);
}
assert.deepEqual(engine.render.style('thermal').uniforms, {
sensitivity: 0.75,
bloom: 0.65,
mode: 0,
pixelation: 1.5,
palette: 0,
time: 0,
});
// A uniform write reaches the engine, as a Cesium stage uniform would.
effects.stages.thermal.uniforms.palette = 1;
assert.equal(engine.render.style('thermal').uniforms.palette, 1);
assert.equal(effects.stages.thermal.uniforms.palette, 1);
// A value the engine refuses stays local and is reported, not thrown.
const warn = console.warn;
const warned = [];
console.warn = (...args) => warned.push(args);
try {
effects.stages.noir.uniforms.grainAmt = Number.NaN;
} finally {
console.warn = warn;
}
assert.equal(warned.length, 1);
assert.equal(engine.render.style('noir').uniforms.grainAmt, 0.5);
effects.initStyles();
assert.equal(Object.keys(effects.stages).length, 5, 'initialised once');
effects.destroy();
});
test('an engine crossfade eases both styles and animates time on the engine', () => {
const { engine, effects, holds, tick } = engineFixture();
effects.initStyles();
effects.startTransition('thermal', 0, 1);
tick(250);
assert.equal(engine.render.style('thermal').intensity, 0.5);
assert.equal(effects.stages.thermal.enabled, true);
assert.ok(holds.has('style-anim'));
tick(500);
assert.equal(engine.render.style('thermal').intensity, 1);
// The FLIR clock keeps running on the engine while the style shows.
tick(1500);
assert.equal(engine.render.style('thermal').uniforms.time, 1.5);
effects.startTransition('thermal', 1, 0);
effects.startTransition('noir', 0, 1);
tick(1750);
assert.equal(engine.render.style('thermal').intensity, 0.5);
assert.equal(engine.render.style('noir').intensity, 0.5);
tick(2000);
tick(2016);
assert.equal(engine.render.style('thermal').intensity, 0);
assert.equal(engine.render.style('noir').intensity, 1);
assert.equal(holds.has('style-anim'), false, 'noir does not animate');
effects.destroy();
});
test('Anime on an engine without a viewer reaches no sky box', () => {
const { engine, effects } = engineFixture();
effects.initStyles();
effects.setStageIntensity(effects.stages.anime, 0.7);
assert.equal(engine.render.style('anime').intensity, 0.7);
effects.destroy();
});
test('without the visualStyles capability an engine gets no stages', () => {
const { effects } = engineFixture(createFakeEngine({ kind: 'navara' }));
effects.initStyles();
assert.deepEqual(effects.stages, {});
effects.destroy();
});
test('with a viewer the Cesium stage sharpens and the engine mirrors its state', () => {
let state = { enabled: false, amount: 1 };
const engine = {