feat(render): a renderer can draw weather: the weather capability adds globe.weather (clouds, fog, haze, rain and snow, 0 to 1, and the wind they drift on); the view shows the conditions observed under the camera, and the Display panel's Weather group sets them by hand

This commit is contained in:
Sameh Khamis committed 2026-11-13 11:02:56 -08:00
1 parent 639e92b4f7
commit 0b1dc325bc
29 files changed
+869 -27

No files matched your search

+65
View File
@@ -4642,6 +4642,71 @@ export function portChecks() {
},
),
check(
'globe.weather',
'weather starts clear, keeps what a set leaves out, and clamps',
(h, r) => {
const first = r.globe.weather.get();
for (const key of ['clouds', 'fog', 'haze', 'rain', 'snow'])
equal(first[key], 0, `${key} clear at first`);
equal(first.wind.speedMps, 0, 'no wind at first');
r.globe.weather.set({ clouds: 0.6, rain: 2 });
r.globe.weather.set({
fog: -1,
wind: { speedMps: 8, fromDeg: -45 },
});
const after = r.globe.weather.get();
near(after.clouds, 0.6, 1e-9, 'clouds kept');
equal(after.rain, 1, 'rain clamped to 1');
equal(after.fog, 0, 'fog clamped to 0');
equal(after.snow, 0, 'snow untouched');
near(after.wind.speedMps, 8, 1e-9, 'wind speed');
near(after.wind.fromDeg, 315, 1e-9, 'wind direction wrapped');
after.clouds = 0;
near(r.globe.weather.get().clouds, 0.6, 1e-9, 'get hands out a copy');
},
{ capability: 'weather' },
),
check(
'globe.weather.drawn',
'clouds and fog change the drawn frame; clearing them brings it back',
async (h, r) => {
if (h.pixels === false) return { skip: 'reads pixels' };
// Low and looking toward the horizon, where fog and a cloud layer
// both show.
r.camera.setView({
destination: { ...AUSTIN, height: 600 },
orientation: { heading: 0, pitch: -0.12, roll: 0 },
});
const frame = async () => {
await drawn(h, r, 4);
await settled(r);
r.frame.renderNow();
return r.readPixels({ scale: 0.1 }).data;
};
const apart = (a, b) => {
let sum = 0;
for (let i = 0; i < a.length; i += 4)
for (let c = 0; c < 3; c++) sum += Math.abs(a[i + c] - b[i + c]);
return sum / ((a.length / 4) * 3);
};
const clear = await frame();
r.globe.weather.set({ clouds: 1, fog: 1 });
const weathered = await frame();
ok(
apart(clear, weathered) > 6,
`weather changes the frame (${apart(clear, weathered).toFixed(1)})`,
);
r.globe.weather.set({ clouds: 0, fog: 0 });
const again = await frame();
ok(
apart(clear, again) < 3,
`cleared, the frame is back (${apart(clear, again).toFixed(1)})`,
);
},
{ capability: 'weather' },
),
// --- Effects, credits, quality ---------------------------------------
check(
'effects.styles',
+2
View File
@@ -152,6 +152,8 @@ export type Capability =
| 'terrain'
/** Atmosphere scattering applied to drawn models. */
| 'modelAtmosphere'
/** Clouds, fog, haze, rain and snow around the view (`globe.weather`). */
| 'weather'
/** Imagery draped onto 3D tiles as well as the globe. */
| 'imageryOnTiles'
/** A screen-split comparison between imagery layers. */
+14 -8
View File
@@ -76,20 +76,26 @@ function nestedShape(type) {
return null;
}
/** `[member, optional]` for every member, nested shapes dotted. */
function members(type, prefix = '') {
/**
* `[member, optional]` for every member, nested shapes dotted; a member of
* an optional shape is as optional as the shape.
*/
function members(type, prefix = '', inOptional = false) {
const out = [];
for (const property of checker.getPropertiesOfType(type)) {
const name = `${prefix}${property.name}`;
const optional = Boolean(property.flags & ts.SymbolFlags.Optional);
const optional =
inOptional || Boolean(property.flags & ts.SymbolFlags.Optional);
out.push([name, optional]);
const declaration = property.valueDeclaration ?? property.declarations?.[0];
const propertyType = checker.getTypeOfSymbolAtLocation(
property,
declaration,
);
const declared = checker.getTypeOfSymbolAtLocation(property, declaration);
// An optional member's shape, without the `undefined` its `?` adds.
const propertyType =
property.flags & ts.SymbolFlags.Optional
? checker.getNonNullableType(declared)
: declared;
const nested = nestedShape(propertyType);
if (nested) out.push(...members(nested, `${name}.`));
if (nested) out.push(...members(nested, `${name}.`, optional));
}
return out;
}
+42
View File
@@ -215,6 +215,36 @@ export interface SkySettings {
lighting: boolean;
}
/**
* `weather`: the conditions drawn around the view. Each strength is 0..1,
* 0 drawing none of it; all are 0 at first. Clouds are a layer above the
* ground that the camera can fly into and above; fog and haze thin what is
* seen with distance, fog close in, haze far off; rain and snow fall around
* the eye below the clouds and are not drawn from above them. A renderer
* fades what makes no sense at the camera's height (rain from orbit) by
* itself. None of it is picked or answers height queries. The application sets the
* conditions observed at the view, which the user can override.
*/
export interface WeatherSettings {
/** Cloud cover. */
clouds: number;
/** Fog: 1 is about 500 m visibility or less. */
fog: number;
/** Haze. */
haze: number;
/** Rain. */
rain: number;
/** Snow. */
snow: number;
/** What the clouds and precipitation drift with. */
wind: {
/** Metres per second. */
speedMps: number;
/** Where the wind blows from, degrees clockwise from north. */
fromDeg: number;
};
}
/**
* View-lit shading of the drawn terrain's own relief (the ellipsoid's
* curvature where terrain has no normals); darkens only, at most
@@ -295,6 +325,18 @@ export interface GlobePort {
get(): SkySettings;
set(settings: Partial<SkySettings>): void;
};
/**
* `weather`. `set` keeps each strength it is not given or given as no
* number, clamps the others to 0..1, and asks for a frame. `wind` is taken
* whole: a speed below 0 or missing is 0, the direction is wrapped into
* [0, 360) and a missing one is 0. `get` answers a copy of what was set;
* at first every strength and the wind speed are 0 (`CLEAR_WEATHER` in
* `src/render/weather.js`, whose `mergeWeather` is these rules).
*/
readonly weather?: {
get(): WeatherSettings;
set(settings: Partial<WeatherSettings>): void;
};
/** Whether that part of the base has finished loading what is in view. */
isSettled(kind: SettleKind): boolean;
on(event: 'progress', cb: (progress: LoadProgress) => void): Off;
+5
View File
@@ -19,6 +19,7 @@ export const CAPABILITIES = Object.freeze([
'finestHeights',
'terrain',
'modelAtmosphere',
'weather',
'imageryOnTiles',
'splitSwipe',
'imageryFrames',
@@ -147,6 +148,9 @@ export const PORTS = deepFreeze({
['setSurfaceShading', 'surfaceShading'],
['imagery.getSplit', 'splitSwipe'],
['imagery.setSplit', 'splitSwipe'],
['weather', 'weather'],
['weather.get', 'weather'],
['weather.set', 'weather'],
]),
draw: Object.fromEntries([
...required(
@@ -353,6 +357,7 @@ export const ROLES = deepFreeze([
members: [
'effects',
'globe.sky',
'globe.weather',
'globe.setSurfaceShading',
'imageryLayer.lit',
],
+11
View File
@@ -14,6 +14,7 @@ import { alongLength } from '../../geo/polyline.js';
import { clusterScreenPoints } from '../../geo/screenCluster.js';
import { createInputRouter } from '../input/router.js';
import { cssColor } from '../color.js';
import { CLEAR_WEATHER, mergeWeather } from '../weather.js';
import { CAPABILITIES, CONTRACT_VERSION } from '../contract/index.js';
import { createStubCamera } from './camera.js';
import { motionPosition } from '../motion/iconMotion.js';
@@ -1659,6 +1660,16 @@ export function createStubRenderer(options = {}) {
};
},
};
if (capabilities.has('weather')) {
let weather = CLEAR_WEATHER;
globe.weather = {
get: () => mergeWeather(weather),
set(patch) {
weather = mergeWeather(weather, patch);
requestRender('weather');
},
};
}
if (capabilities.has('globeVisibility'))
globe.isGlobeVisible = () => globeVisible;
if (capabilities.has('surfaceIdentity'))
+2 -1
View File
@@ -20,7 +20,7 @@ test('the stub passes the conformance suite', async () => {
.filter((r) => r.status === 'skip')
.map((r) => r.id);
// It draws only models into pixels, so the sweep's, a lit shape's, a line outline's and
// the blends' (over a drawn shape) and video tiles' pixels go unread; its camera takes no user gestures: the drag and wheel scripts are for a
// the blends' (over a drawn shape), video tiles' and the weather's pixels go unread; its camera takes no user gestures: the drag and wheel scripts are for a
// camera that does (a controller over it, say).
assert.deepEqual(skipped, [
'effects.screenModulation.pixels',
@@ -29,6 +29,7 @@ test('the stub passes the conformance suite', async () => {
'draw.sets.blend',
'draw.models.blend',
'globe.imagery.video',
'globe.weather.drawn',
...CAMERA_SCRIPTS.filter(usesInput).map((s) => `camera.trace.${s.name}`),
]);
});
+42
View File
@@ -0,0 +1,42 @@
/**
* The weather a renderer draws (`globe.weather`, the `weather` capability):
* clear at first, and how a `set` merges into what was set. Shared by every
* renderer that lists `weather`, so they keep and clamp it alike.
*/
/** @type {Readonly<import('./contract/globe').WeatherSettings>} */
export const CLEAR_WEATHER = Object.freeze({
clouds: 0,
fog: 0,
haze: 0,
rain: 0,
snow: 0,
wind: Object.freeze({ speedMps: 0, fromDeg: 0 }),
});
const STRENGTHS = ['clouds', 'fog', 'haze', 'rain', 'snow'];
const unit = (value, fallback) =>
Number.isFinite(value) ? Math.min(1, Math.max(0, value)) : fallback;
/**
* `current` with `patch` applied: each strength given is clamped to 0..1,
* one not given (or not a number) is kept; `wind` is taken whole, its speed
* at least 0 and its direction in [0, 360).
* @param {import('./contract/globe').WeatherSettings} current
* @param {Partial<import('./contract/globe').WeatherSettings>} [patch]
* @returns {import('./contract/globe').WeatherSettings}
*/
export function mergeWeather(current, patch = {}) {
const next = { ...current, wind: { ...current.wind } };
for (const key of STRENGTHS) next[key] = unit(patch[key], current[key]);
if (patch.wind) {
const speed = Number(patch.wind.speedMps);
const from = Number(patch.wind.fromDeg);
next.wind = {
speedMps: Number.isFinite(speed) ? Math.max(0, speed) : 0,
fromDeg: Number.isFinite(from) ? ((from % 360) + 360) % 360 : 0,
};
}
return next;
}
+33
View File
@@ -0,0 +1,33 @@
import assert from 'node:assert/strict';
import { test } from 'node:test';
import { CLEAR_WEATHER, mergeWeather } from './weather.js';
test('clear weather draws nothing', () => {
for (const key of ['clouds', 'fog', 'haze', 'rain', 'snow'])
assert.equal(CLEAR_WEATHER[key], 0, key);
assert.deepEqual(CLEAR_WEATHER.wind, { speedMps: 0, fromDeg: 0 });
});
test('a set keeps what it is not given and clamps strengths', () => {
const a = mergeWeather(CLEAR_WEATHER, { clouds: 0.4, rain: 3 });
assert.equal(a.clouds, 0.4);
assert.equal(a.rain, 1);
const b = mergeWeather(a, { fog: -1, snow: Number.NaN });
assert.equal(b.fog, 0);
assert.equal(b.snow, 0);
assert.equal(b.clouds, 0.4, 'clouds kept');
assert.equal(b.rain, 1, 'rain kept');
});
test('wind is taken whole, its speed at least 0, its direction wrapped', () => {
const a = mergeWeather(CLEAR_WEATHER, {
wind: { speedMps: 12, fromDeg: -90 },
});
assert.deepEqual(a.wind, { speedMps: 12, fromDeg: 270 });
const b = mergeWeather(a, { wind: { speedMps: -3, fromDeg: 725 } });
assert.deepEqual(b.wind, { speedMps: 0, fromDeg: 5 });
const c = mergeWeather(b, { clouds: 1 });
assert.deepEqual(c.wind, b.wind, 'wind kept');
assert.notEqual(c.wind, b.wind, 'a copy');
});