Drive the Navara volumetric clouds from the cockpit weather behind an optional render.setCloudCover capability

The cockpit's weather feed moves out of the Cesium cloud pass and serves both: Cesium keeps its own cloud canvas, an engine with its own clouds takes their cover from the same observations, with no second WebGL canvas.
This commit is contained in:
German Khokhlov committed 2026-11-03 21:22:34 +04:00
1 parent d8c63c69d8
commit f0d38897f4
15 files changed
+1004 -79

No files matched your search

+21
View File
@@ -0,0 +1,21 @@
// The cloud cover port createNavaraEngine takes (render.setCloudCover): the
// cockpit's weather clouds drawn by Navara's own volumetric clouds, the
// look's cloud effect (look.js), instead of a second WebGL canvas. Only a
// look that draws clouds has it (the cinematic look; not on a
// mobile-optimized view). Destroying it gives the look its own cover back.
/**
* @param {{ hasClouds: boolean, setCloudCoverage: (coverage: number | null) => boolean }} look
* @returns {{ set: (state: { coverage: number | null }) => void, destroy: () => void } | null}
*/
export function createNavaraCloudCover(look) {
if (!look?.hasClouds) return null;
return {
set({ coverage }) {
look.setCloudCoverage(coverage);
},
destroy() {
look.setCloudCoverage(null);
},
};
}
+21
View File
@@ -0,0 +1,21 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { createNavaraCloudCover } from './cockpitClouds.js';
test('only a look with clouds gives the engine a cloud cover port', () => {
assert.equal(createNavaraCloudCover(null), null);
assert.equal(
createNavaraCloudCover({ hasClouds: false, setCloudCoverage() {} }),
null,
);
const set = [];
const port = createNavaraCloudCover({
hasClouds: true,
setCloudCoverage: (coverage) => set.push(coverage),
});
port.set({ coverage: 0.4 });
port.set({ coverage: null });
port.set({ coverage: 0 });
port.destroy();
assert.deepEqual(set, [0.4, null, 0, null], 'destroy gives the look its own');
});
+23 -1
View File
@@ -9,10 +9,12 @@
import * as geo from '../../geo/index.js';
import {
BLOOM_INITIAL,
CLOUD_COVER_INITIAL,
SHARPEN_INITIAL,
STYLE_INITIAL,
createEngineEvent,
mergeBloom,
mergeCloudCover,
mergeSharpen,
mergeStyle,
} from '../contract.js';
@@ -93,6 +95,7 @@ function rayRaisedEllipsoid({ origin, direction }, height) {
* appClock?: import('../../time/clock.js').AppClock,
* sharpen?: { set: (state: { enabled: boolean, amount: number }) => void, destroy: () => void },
* bloom?: { set: (state: { enabled: boolean, strength: number }) => void, destroy: () => void },
* clouds?: { set: (state: { coverage: number | null }) => void, destroy: () => void },
* styles?: { names: readonly string[], uniformNames: (name: string) => string[], defaults: (name: string) => Record<string, number>, set: (name: string, state: { intensity: number, uniforms: Record<string, number> }) => void, destroy: () => void },
* }} options `hooks` are internals.js; `container` defaults to the
* canvas's parent; `sharpen` draws render.setSharpen (sharpen.js; without
@@ -100,7 +103,9 @@ function rayRaisedEllipsoid({ origin, direction }, height) {
* `styles` draws render.setStyle (stylePasses.js; without it the engine
* has no visualStyles capability) and the engine destroys it; `bloom`
* draws render.setBloom (bloom.js; without it no bloom capability), also
* destroyed with the engine;
* destroyed with the engine; `clouds` draws render.setCloudCover
* (cockpitClouds.js; without it, as on a look without clouds, no
* cloudCover capability), destroyed with the engine;
* `appClock` (the engine clock's time) defaults to the page's app clock;
* `movers` is engine.movers on the view's three runtime (movers/index.js;
* without it createSet throws), `lines` engine.lines (lines/index.js;
@@ -121,6 +126,7 @@ export function createNavaraEngine(
sharpen: sharpenPort = null,
styles: stylesPort = null,
bloom: bloomPort = null,
clouds: cloudsPort = null,
} = {},
) {
if (!view?.camera?.raw)
@@ -331,6 +337,20 @@ export function createNavaraEngine(
bloom: () => ({ ...bloom }),
}
: {};
let cloudCover = { ...CLOUD_COVER_INITIAL };
const cloudRender = cloudsPort
? {
setCloudCover(options) {
const next = mergeCloudCover(cloudCover, options);
const changed = next.coverage !== cloudCover.coverage;
cloudCover = next;
if (!changed || destroyed) return;
cloudsPort.set({ ...next });
view.forceUpdate();
},
cloudCover: () => ({ ...cloudCover }),
}
: {};
return {
kind: 'navara',
host: { container: container ?? canvas.parentElement, canvas, size },
@@ -361,6 +381,7 @@ export function createNavaraEngine(
sharpen: () => ({ ...sharpen }),
...styleRender,
...bloomRender,
...cloudRender,
},
surface,
input: input.input,
@@ -383,6 +404,7 @@ export function createNavaraEngine(
sharpenPort?.destroy();
stylesPort?.destroy();
bloomPort?.destroy();
cloudsPort?.destroy();
moversPort?.destroy();
linesPort?.destroy();
},
@@ -291,10 +291,34 @@ test('setBloom hands changed states to the bloom port and draws a frame', () =>
assert.equal(destroyed, 1);
});
test('setCloudCover hands changed covers to the clouds port and draws a frame', () => {
const view = fakeView();
const shown = [];
let destroyed = 0;
const engine = createNavaraEngine(view, {
hooks,
clouds: { set: (state) => shown.push(state), destroy: () => destroyed++ },
});
assertGlobeEngine(engine);
assert.equal(engineCapabilities(engine).cloudCover, true);
assert.deepEqual(engine.render.cloudCover(), { coverage: null });
engine.render.setCloudCover({ coverage: 0.6 });
assert.deepEqual(view.calls.at(-1), ['forceUpdate']);
const calls = view.calls.length;
engine.render.setCloudCover({ coverage: 0.6 });
assert.equal(view.calls.length, calls, 'an unchanged cover draws nothing');
engine.render.setCloudCover({ coverage: null });
assert.throws(() => engine.render.setCloudCover({ coverage: 7 }), TypeError);
assert.deepEqual(shown, [{ coverage: 0.6 }, { coverage: null }]);
engine.destroy();
assert.equal(destroyed, 1);
});
test('without a styles port the engine has no visualStyles capability', () => {
const { engine } = setup();
assert.equal(engineCapabilities(engine).visualStyles, false);
assert.equal(engineCapabilities(engine).bloom, false);
assert.equal(engineCapabilities(engine).cloudCover, false);
assert.equal(engine.render.setStyle, undefined);
assert.equal(engine.render.setBloom, undefined);
});
+2
View File
@@ -24,6 +24,7 @@ import { buildNavaraLook } from './look.js';
import { createNavaraSharpen } from './sharpen.js';
import { createNavaraStyles } from './stylePasses.js';
import { createNavaraBloom } from './bloom.js';
import { createNavaraCloudCover } from './cockpitClouds.js';
import { retroShader } from '../../styles/retro.js';
import { nightVisionShader } from '../../styles/surveillance.js';
import { thermalShader } from '../../styles/thermal.js';
@@ -159,6 +160,7 @@ export async function createNavaraGlobe({
...(appClock ? { appClock } : {}),
sharpen: createNavaraSharpen(view),
bloom: createNavaraBloom(view),
clouds: createNavaraCloudCover(look),
styles: createNavaraStyles(view, {
shaders: STYLE_SHADERS,
onStarsDimmed: (amount) => look.setStarsDimmed(amount),
+17
View File
@@ -16,6 +16,7 @@ import {
cloudTerrainOffset,
landscapeSunTime,
CUMULUS_BASES,
LOCAL_CLOUD_COVERAGE,
globeLightingAt,
starsVisibleAt,
sunDateStale,
@@ -205,12 +206,28 @@ export function buildNavaraLook(
plan.meshes.find((desc) => Object.hasOwn(desc, 'stars'))?.stars
?.intensity ?? NAVARA_STARS_INTENSITY;
let starsDimmed = 0;
let cloudCoverage = LOCAL_CLOUD_COVERAGE;
return {
handles,
frame,
resetDaylight() {
daylightTime = null;
},
/** Whether the look draws volumetric clouds (cinematic, not on mobile). */
hasClouds: Boolean(clouds),
/**
* The clouds' cover (0..1), or null for the look's own
* (LOCAL_CLOUD_COVERAGE); true when it changed. Where and when they
* show stays the look's rule (height, sunlight).
*/
setCloudCoverage(coverage) {
if (!clouds) return false;
const next = coverage ?? LOCAL_CLOUD_COVERAGE;
if (next === cloudCoverage) return false;
cloudCoverage = next;
clouds.update({ clouds: { coverage: next } });
return true;
},
/** Dims the stars by `amount` (0 none, 1 hidden); true when it changed. */
setStarsDimmed(amount) {
const next = Math.min(1, Math.max(0, Number(amount) || 0));
+30
View File
@@ -509,3 +509,33 @@ test('Anime fades the stars from the look own brightness and back', () => {
);
assert.equal(photoreal.setStarsDimmed(1), false);
});
test('the cockpit sets the cinematic clouds cover and null gives the look its own', async () => {
const { LOCAL_CLOUD_COVERAGE } = await import('./viewOptions.js');
const view = fakeView(0);
const look = warmCinematic(view);
const clouds = view.added.find(({ desc }) => Object.hasOwn(desc, 'clouds'));
assert.equal(look.hasClouds, true);
assert.equal(look.setCloudCoverage(null), false, 'already the look own');
assert.equal(look.setCloudCoverage(0.7), true);
assert.equal(look.setCloudCoverage(0.7), false);
assert.equal(look.setCloudCoverage(0), true);
assert.equal(look.setCloudCoverage(null), true);
assert.deepEqual(
clouds.updates.filter((update) => update.clouds),
[
{ clouds: { coverage: 0.7 } },
{ clouds: { coverage: 0 } },
{ clouds: { coverage: LOCAL_CLOUD_COVERAGE } },
],
);
// The mobile cinematic and the minimal looks draw no clouds.
const mobile = buildNavaraLook(
fakeView(0),
plugin([]),
navaraScenePlan('cinematic', { mobile: true }),
() => 0,
);
assert.equal(mobile.hasClouds, false);
assert.equal(mobile.setCloudCoverage(0.5), false);
});