Ridge backdrop A+D: baked ridge planes over the live sky (Vienna) (#163)
All checks were successful
Rat dev preview / Build dev preview (push) Successful in 3m6s

This commit is contained in:
Nibu 2026-11-14 11:07:58 +00:00 • committed by Void
commit 5b7b4c2eeb
77 changed files with 5162 additions and 2562 deletions

View file

@ -4,37 +4,31 @@
config says `backgroundScene.style: 'raymarch'`; the tier flag still defaults to `off`, and null still
means "keep the CSS ground". Everything below this section describes the opt-in ray-march scene.
- `ridge/Ridge.ts` `createRidgeScene({ canvas, tier, probe?, onProbe?, onBake?, sky? })` returns `{ login, dispose }` or
null (tier `off`, no 2D context). Each of four ridge layers is a function of world-x (`ridge/ridgeWorld.ts` `profile`:
ridged multifractal value noise, each octave folded at its crest and weighted by the previous one, domain-warped; one
function, a parameter row per layer; nothing repeats). World-x is baked in chunks (1.2 viewport widths, integer pixels)
onto plain 2D canvases over a baked sky gradient on the host canvas. Per layer a track holds three chunk canvases: two
the window can reach and one ahead. The first view bakes only the first chunk of each layer; the others are baked in
tasks of about 6 ms (`setTimeout`; a chunk is baked, put on a canvas with its in-scatter, and blurred in column bands, each sized from the last band's time). Per frame the only motion is the
compositor sliding the track with a CSS transform (a Web Animation of `translate3d`, one chunk long, `fill: forwards`,
150-420 s per viewport width, farther is slower); when it finishes the chunks move one slot left and the track restarts
at 0 in the same task (no jump), and the canvas that left is baked with the next stretch of world. A second animation
sways the whole rig (up to 5% of the width sideways, 3% of the height down, 150 s loop): the slow yaw and pitch.
No script runs per frame. The host canvas and the layers' parent must be positioned and the canvas should fill the parent.
- `ridge/ridgeBake.ts` is the pixel function (pure, no DOM): per column the crest's slope gives a normal, lit against the
sun (`sunDirection`: x = 0.9 sin(2 pi \* `sunAzimuth`), so the sun is toward +x for azimuth 0 to 0.5, strongest at 0.25, toward -x for 0.5 to 1; with no `sunAzimuth` the face lighting falls back to 0.3 and the in-scatter is off); the face under it has a light-to-haze gradient, erosion
streaks (a folded noise at gully width, darker with steepness) and a lit rim on the crest. Contrast scales with
`transmittance`; the base colour, haze and blur are the aerial perspective below, not redone.
- Paused when the tab is hidden; under `prefers-reduced-motion` the layers are painted but never played.
`tier: 'still'` flattens sky and layers into the host canvas at once and leaves no extra elements.
- `ridge/Ridge.ts` `createRidgeScene({ canvas, tier, probe?, onProbe?, sky? })` returns `{ login, dispose }` or
null (tier `off`, no 2D context). It paints the sky (`ridge/physicalSky.ts`, from the `SkyColours` the host passes) on the host
canvas at the viewport's size in canvas pixels, then the stars baked into it, a twinkle overlay canvas for the swinging ones
(`ridge/stars.ts`), the baked ridge planes (below) and, when it rains, tiled rain streaks slid down on the compositor (`ridge/rain.ts`).
With no planes (the load failed) the scene shows sky, stars and weather only. No script runs per frame except the twinkle, at most every 50 ms.
The host canvas and its parent must be positioned and the canvas should fill the parent. A resize rebuilds it; so does a sky change
(`index.ts`).
- Paused when the tab is hidden; under `prefers-reduced-motion` the rain is paused and the stars stay at base brightness.
`tier: 'still'` paints the sky and stars and adds no overlay, no rain and no loop.
- Cadence guard: for the first 2 s after start (after 5 warm-up frames) rAF intervals are sampled; if their p95 is over
33 ms the scene flattens to a still image (`ridge/probe.ts`) and stops. `?probe=1` (or `probe: true`) keeps sampling
33 ms the scene settles to a still image (`ridge/probe.ts`) and stops. `?probe=1` (or `probe: true`) keeps sampling
and shows `n / median / p95` in a corner readout.
- `ridge/skyColour.ts` `skyColours()` is the one place sky colours come from: a fixed dusk placeholder until Void's
`src/sky/sun.js` `light()` lands (PR #20); replace its body, keep the signature.
- Aerial perspective (`ridge/ridgeWorld.ts` `AERIAL`, `transmittance`, `layerColour`, `edgeBlur`, `scatterAlpha`) is applied
once while the layers are baked, from the `SkyColours` the one seam returns (`sunAzimuth` optional: omit it for no
in-scatter). Layer distance d = 1 - depth; transmittance T = exp(-1.6 d) (0.24 / 0.37 / 0.58 / 1.00 far to near); colour is
`T * ground + (1 - T) * haze`, then chroma drops by `0.5 (1 - T)`; the edge is blurred up to 2 px (canvas pixels at DPR 1,
via the 2D `filter`, skipped where unsupported, e.g. Safari); a faint warm in-scatter (at most 0.16 alpha) peaks at the
sun's azimuth in world-x (two incommensurate cosines, so not periodic). Nothing runs per frame. The in-scatter lobe is
painted into each chunk, so it drifts with the layer rather than staying fixed under the sun; that is a known
approximation until the sun is a real, fixed light.
- `ridge/skyColour.ts` `skyColours()` is the fallback three-stop palette (top, mid, horizon) a scene gets with no `sky` option; the host
passes `ridgeSkyFor` (skyForState.ts) instead.
- Ridge planes: `ridge/manifest.ts` `loadManifest` reads `${BASE_URL}public/ridge/index.json` (config `backgroundScene.place` picks the entry,
unknown or absent: the index default), then the place's `manifest.json` (v1, written by `scripts/ridge-bake/export.py`; it is the one JSON
the backdrop reads). `ridge/frameChoice.ts` picks the two baked frames the sky's sun lies between and a blend weight; `ridge/Backdrop.ts`
decodes only those. `index.ts` `prepare()` loads manifest and planes with the scene chunk, so `createRidgeScene({ backdrop })` stays
synchronous, and a rebuild keeps the old scene up until the new one's planes have loaded, then swaps. The rig is one div of three plane groups (far, mid, near; DOM order is paint order; no z-index, no fixed position, nothing
outside the scene's parent), each drifting on `Element.animate` (transform only, 150 s loop, paused when the tab is hidden, motion is
reduced or the scene is still); a still draws the planes into the host canvas. Two frames are a true crossfade (`frameChoice.ts`
`crossfade`: a at 1 - w plus b at w, summed in an isolated group, `plus-lighter` in the rig and `lighter` in an offscreen for the still), because
the planes are haze as alpha and b laid over a would double the haze. A failure anywhere (404, bad manifest, an image that
will not decode) is the sky-only scene on `FALLBACK_VIEW`, told to the host by the live scene alone, through `onPlanesFail` when the live scene has no planes and `onPlanesOk` when a later one has them (never from a stale or disposed build); it is never `onCompileFail`.
`vite.config.js` copies `public/ridge` into `dist/public/`.
- `harness/scene/bundle.cjs <out> ridge` builds a standalone page (`?tier=`, `?probe=1`).
- Not yet measured on a phone: see the PR description.
@ -78,13 +72,10 @@ holds a GL context gives no 2D one, so a GL failure after context creation (comp
x `AMBIENT_GAIN` 0.25 x `NIGHT_LIFT` up to 4; now `ambient_linear` as baked, so midnight's fill is about 2.5 to 3.5 times darker than
the lifted one). `HAZE_K` (0.0004 per metre) is unchanged.
## Ridge scene: Vienna, the real horizon and the weather
## Ridge scene: Vienna and the weather
The ridge sky is the physical atmosphere (`ridge/earthAtmosphere.ts`) lit by the real sun over the viewpoint, 48.1929826 N, 16.3534237 E
(`ridge/vienna.ts`). Its farthest layer is the real horizon of that viewpoint, standing still under the sun: `ridge/viennaHorizon.json`,
baked by `scripts/bake-horizon.py` from Copernicus DEM GLO-30 (nine 1 degree tiles, about 380 MB, kept out of the repo). The file carries its
own `source` block with the dataset, tiles, licence and the attribution the licence requires. Its elevation range is 2.1 degrees, so the
layer draws the shape of the ring stretched to a fixed relief, not its scale.
(`ridge/vienna.ts`).
The weather is one number (`ridge/weather.ts`), from the page URL's `?weather=`, else `weather.json` beside `config.json`, else clear.
The browser never calls a weather service. `weather.json` is written by Eureka's refresher (gamecult-ops `docs/weather-feed.md`, a 5-minute timer; the only writer) from GeoSphere Austria's TAWES station 11035 (Wien Hohe Warte), credit
@ -92,7 +83,7 @@ The browser never calls a weather service. `weather.json` is written by Eureka's
glow_wm2, sunshine_s_10min, attribution}`, null for a missing measurement. A file fetched more than 30 minutes ago, or holding an observation more than 40 minutes old, or from another station, is a clear
sky. Rain is the gauge; cloud is an estimate from the global radiation against Haurwitz's clear-sky value, cross-checked by the sunshine
duration, with the humidity as the fallback when the sun is low (`ridge/weatherFeed.ts`). The weather draws a cloud deck in the sky
(`ridge/clouds.ts`), thickens the layers' valley mist, and lays tiled rain streaks over everything (`ridge/rain.ts`, compositor-driven, none in a still).
(`ridge/clouds.ts`) and lays tiled rain streaks over the sky (`ridge/rain.ts`, compositor-driven, none in a still).
## Entry points

View file

@ -1,4 +1,4 @@
import React, { MouseEventHandler, useState } from 'react';
import React, { MouseEventHandler, useEffect, useState } from 'react';
import {
Box,
config,
@ -15,8 +15,9 @@ import FocusTrap from 'focus-trap-react';
import { stopPropagation } from '../../utils/keyboard';
import { useSkyState } from '../../hooks/useSkyState';
import { SKY_STATES, setSkyState, type SkyState } from './skyState';
import { loadManifest } from './ridge/manifest';
import {
HORIZON_CREDIT,
terrainCredit,
WEATHER_CREDIT,
WEATHER_CREDIT_URL,
WEATHER_DATASET,
@ -51,6 +52,20 @@ export function SkyStateControl() {
const sceneStyle: ServedSceneStyle =
getSceneStyleOverride() ?? (configuredStyle === 'terrain' ? 'terrain' : 'ridge');
const place = backgroundScene?.place;
const [credit, setCredit] = useState<string>();
const ridgeOn = !!backgroundScene && sceneStyle === 'ridge';
useEffect(() => {
if (!ridgeOn) return undefined;
let live = true;
loadManifest(import.meta.env.BASE_URL, place).then(
(m) => live && setCredit(terrainCredit(m)),
() => live && setCredit(undefined)
);
return () => {
live = false;
};
}, [ridgeOn, place]);
const open: MouseEventHandler<HTMLButtonElement> = (evt) => {
setCords(evt.currentTarget.getBoundingClientRect());
};
@ -132,6 +147,11 @@ export function SkyStateControl() {
<Text size="L400" priority="300">
Data
</Text>
{credit && (
<Text size="T200" priority="300">
{credit}
</Text>
)}
<Text size="T200" priority="300">
Datenquelle: GeoSphere Austria -{' '}
<a href={WEATHER_CREDIT_URL} target="_blank" rel="noreferrer noopener">
@ -139,9 +159,6 @@ export function SkyStateControl() {
</a>
. {WEATHER_DATASET}.
</Text>
<Text size="T200" priority="300">
{HORIZON_CREDIT}
</Text>
</Box>
)}
</Box>

View file

@ -4,6 +4,13 @@ import type { Scene } from './Scene';
import { startBackgroundScene } from './index';
import { setSkyClock, setSkyState } from './skyState';
import { ridgeSkyFor } from './skyForState';
import { forgetManifests } from './ridge/manifest';
import { wireIndex, wireManifest } from './ridge/manifest.fixture';
import { FALLBACK_VIEW } from './ridge/view';
import { frameChoice } from './ridge/frameChoice';
import { sunAt } from './ridge/sunAt';
import { ridgeWeather } from './ridge/weather';
import { parseWeatherFile, WEATHER_ATTRIBUTION, weatherFromFile } from './ridge/weatherFeed';
const { createRidgeScene, createScene } = vi.hoisted(() => ({
createRidgeScene: vi.fn(),
@ -20,9 +27,11 @@ vi.mock('./skyForState', async (importOriginal) => {
return {
...actual,
ridgeSkyFor: (...args: Parameters<typeof actual.ridgeSkyFor>) => {
if (args.length !== 1) return actual.ridgeSkyFor(...args);
if (!baked.has(args[0])) baked.set(args[0], actual.ridgeSkyFor(args[0]));
return baked.get(args[0]) as ReturnType<typeof actual.ridgeSkyFor>;
const [state, view] = args;
// One bake per state and place; the view is the one owner of the place, so it is part of what a sky is.
const key = `${state}@${JSON.stringify(view.place)}`;
if (!baked.has(key)) baked.set(key, actual.ridgeSkyFor(...args));
return baked.get(key) as ReturnType<typeof actual.ridgeSkyFor>;
},
};
});
@ -62,7 +71,7 @@ describe('startBackgroundScene follows the sky state', () => {
createRidgeScene.mockReturnValue(fakeScene());
const scene = await startBackgroundScene(config, caps, { canvas });
expect(createRidgeScene).toHaveBeenCalledOnce();
expect(hex(createRidgeScene.mock.calls[0][0].sky.top)).toBe(hex(ridgeSkyFor('night').top));
expect(hex(createRidgeScene.mock.calls[0][0].sky.top)).toBe(hex(ridgeSkyFor('night', FALLBACK_VIEW).top));
scene?.dispose();
});
@ -81,7 +90,7 @@ describe('startBackgroundScene follows the sky state', () => {
setSkyState('night');
createRidgeScene.mockReturnValue(fakeScene());
const scene = await startBackgroundScene(config, caps, { canvas });
expect(hex(createRidgeScene.mock.calls[0][0].sky.top)).toBe(hex(ridgeSkyFor('night').top));
expect(hex(createRidgeScene.mock.calls[0][0].sky.top)).toBe(hex(ridgeSkyFor('night', FALLBACK_VIEW).top));
scene?.dispose();
});
@ -93,7 +102,7 @@ describe('startBackgroundScene follows the sky state', () => {
setSkyState('day');
await flush();
expect(first.dispose).toHaveBeenCalledOnce();
expect(hex(createRidgeScene.mock.calls[1][0].sky.top)).toBe(hex(ridgeSkyFor('day').top));
expect(hex(createRidgeScene.mock.calls[1][0].sky.top)).toBe(hex(ridgeSkyFor('day', FALLBACK_VIEW).top));
scene?.dispose();
expect(second.dispose).toHaveBeenCalledOnce();
});
@ -106,7 +115,7 @@ describe('startBackgroundScene follows the sky state', () => {
const scene = await started;
await flush();
expect(createRidgeScene).toHaveBeenCalledTimes(2);
expect(hex(createRidgeScene.mock.calls[1][0].sky.top)).toBe(hex(ridgeSkyFor('day').top));
expect(hex(createRidgeScene.mock.calls[1][0].sky.top)).toBe(hex(ridgeSkyFor('day', FALLBACK_VIEW).top));
expect(a.dispose).toHaveBeenCalledOnce();
scene?.dispose();
expect(b.dispose).toHaveBeenCalledOnce();
@ -186,7 +195,7 @@ describe('startBackgroundScene follows the sky state', () => {
// The sky last built is dusk's, and it is the live scene: a login reaches it and nothing else.
const built = createRidgeScene.mock.calls.length;
expect(hex(createRidgeScene.mock.calls[built - 1][0].sky.top)).toBe(
hex(ridgeSkyFor('dusk').top)
hex(ridgeSkyFor('dusk', FALLBACK_VIEW).top)
);
const live = scenes[built - 1];
scene?.login();
@ -209,12 +218,12 @@ describe('startBackgroundScene follows the sky state', () => {
await flush();
// The initial build and one rebuild, for dusk; day's rebuild stood down before building anything.
expect(createRidgeScene).toHaveBeenCalledTimes(2);
expect(hex(createRidgeScene.mock.calls[1][0].sky.top)).toBe(hex(ridgeSkyFor('dusk').top));
expect(hex(createRidgeScene.mock.calls[1][0].sky.top)).toBe(hex(ridgeSkyFor('dusk', FALLBACK_VIEW).top));
scene?.dispose();
});
// A second sky change `ticks` microtasks after the first. True when it landed mid-build: the build for the first
// change finished, was found stale and dropped (never shown), and only the newest sky was announced.
// change finished loading, was found stale and was never made (so it never showed or spoke), and only the newest sky was announced.
const changeAfter = async (ticks: number) => {
createRidgeScene.mockReset();
setSkyState('auto');
@ -229,7 +238,7 @@ describe('startBackgroundScene follows the sky state', () => {
const at = `at ${ticks} microtasks`;
const built = createRidgeScene.mock.calls.length;
expect(hex(createRidgeScene.mock.calls[built - 1][0].sky.top), at).toBe(
hex(ridgeSkyFor('dusk').top)
hex(ridgeSkyFor('dusk', FALLBACK_VIEW).top)
);
// The newest build is live, every earlier one disposed exactly once, and only the live one is shown.
const live = [a, b, c][built - 1];
@ -241,7 +250,7 @@ describe('startBackgroundScene follows the sky state', () => {
});
scene?.dispose();
expect(live.dispose, at).toHaveBeenCalledOnce();
return built === 3 && onActive.mock.calls.length === 1;
return built === 2 && onActive.mock.calls.length === 1;
};
it('a sky change at any point of a build leaves only the newer build live; the stale one is disposed', async () => {
@ -255,7 +264,7 @@ describe('startBackgroundScene follows the sky state', () => {
// This can fail after a correct refactor that changes the await count in create/skyKey and moves every offset
// off the build. Then widen the range of offsets until some land again; do not weaken the assertion.
expect(landed.length, 'at least one offset must land mid-build').toBeGreaterThan(0);
});
}, 30000);
it('leaves the ray-march scene alone: it has one fixed dusk', async () => {
const inner = fakeScene() as unknown as Scene;
@ -282,3 +291,428 @@ describe('startBackgroundScene follows the sky state', () => {
expect(onCompileFail).toHaveBeenCalledWith(boom);
});
});
describe('the ridge planes', () => {
const served = (over: Record<string, unknown> = {}) => {
const files: Record<string, unknown> = {
'/public/ridge/index.json': wireIndex,
'/public/ridge/vienna-abc123abc123/manifest.json': wireManifest(),
'/public/ridge/graz-def456def456/manifest.json': wireManifest({
place: { ...(wireManifest() as any).place, id: 'graz', name: 'Graz' },
}),
...over,
};
vi.stubGlobal(
'fetch',
vi.fn(async (url: string) =>
url in files
? { ok: true, status: 200, json: async () => files[url] }
: { ok: false, status: 404, json: async () => ({}) }
)
);
};
const images = (reject = false) => {
const loaded: string[] = [];
vi.stubGlobal(
'Image',
class {
src = '';
decoding = '';
cloneNode = () => this;
decode = async () => {
if (reject) throw new Error('bad image');
loaded.push(this.src);
};
}
);
return loaded;
};
beforeEach(() => forgetManifests());
it('hands the scene the planes of the frame the sky was baked for, over the manifest view', async () => {
served();
const loaded = images();
createRidgeScene.mockReturnValue(fakeScene());
const scene = await startBackgroundScene(config, caps, { canvas });
const opts = createRidgeScene.mock.calls[0][0];
expect(opts.backdrop.manifest.view.place.id).toBe('vienna');
expect(opts.sky.physical.view).toBe(opts.backdrop.manifest.view);
// Midnight in Vienna: the night frame, three planes.
expect(loaded.sort()).toEqual([
'/public/ridge/vienna-abc123abc123/night-far.webp',
'/public/ridge/vienna-abc123abc123/night-mid.webp',
'/public/ridge/vienna-abc123abc123/night-near.webp',
]);
scene?.dispose();
});
it('config place picks the index entry; an unknown id falls back to the default', async () => {
served();
images();
createRidgeScene.mockReturnValue(fakeScene());
(await startBackgroundScene({ ...config, place: 'graz' }, caps, { canvas }))?.dispose();
expect(createRidgeScene.mock.calls[0][0].backdrop.manifest.view.place.id).toBe('graz');
forgetManifests();
(await startBackgroundScene({ ...config, place: 'nowhere' }, caps, { canvas }))?.dispose();
expect(createRidgeScene.mock.calls[1][0].backdrop.manifest.view.place.id).toBe('vienna');
});
const skyOnly = async (onPlanesFail = vi.fn(), onCompileFail = vi.fn()) => {
createRidgeScene.mockReturnValue(fakeScene());
const scene = await startBackgroundScene(config, caps, { canvas, onPlanesFail, onCompileFail });
const opts = createRidgeScene.mock.calls[0][0];
expect(scene).not.toBeNull();
expect(opts.backdrop).toBeUndefined();
expect(opts.sky.physical.view).toBe(FALLBACK_VIEW);
expect(onPlanesFail).toHaveBeenCalledOnce();
expect(onCompileFail).not.toHaveBeenCalled();
scene?.dispose();
};
it('a 404 manifest gives the sky-only scene with the fallback view, one onPlanesFail and no onCompileFail', async () => {
served({ '/public/ridge/index.json': undefined });
vi.stubGlobal(
'fetch',
vi.fn(async () => ({ ok: false, status: 404, json: async () => ({}) }))
);
images();
await skyOnly();
});
});
describe('the ridge planes failing', () => {
it('a rejecting image gives the same sky-only scene, and a rebuild does not report again', async () => {
vi.stubGlobal(
'fetch',
vi.fn(async (url: string) => ({
ok: true,
status: 200,
json: async () => (url.endsWith('index.json') ? wireIndex : wireManifest()),
}))
);
vi.stubGlobal(
'Image',
class {
decode = async () => {
throw new Error('bad image');
};
}
);
forgetManifests();
createRidgeScene.mockReturnValue(fakeScene());
const onPlanesFail = vi.fn();
const onCompileFail = vi.fn();
const scene = await startBackgroundScene(config, caps, { canvas, onPlanesFail, onCompileFail });
expect(createRidgeScene.mock.calls[0][0].backdrop).toBeUndefined();
setSkyState('day');
await flush();
expect(createRidgeScene).toHaveBeenCalledTimes(2);
expect(onPlanesFail).toHaveBeenCalledOnce();
expect(onCompileFail).not.toHaveBeenCalled();
scene?.dispose();
});
});
describe('the ridge planes and the live scene', () => {
const serve = (manifest: Record<string, unknown> = wireManifest()) =>
vi.stubGlobal(
'fetch',
vi.fn(async (url: string) => ({
ok: true,
status: 200,
json: async () => (url.endsWith('index.json') ? wireIndex : manifest),
}))
);
/** Image stub whose decode waits on `gate` (when given) and then resolves or rejects as `outcome` says now. */
const decoder = () => {
const ctl = {
pending: [] as Array<() => void>,
hold: false,
fail: false,
urls: [] as string[],
};
vi.stubGlobal(
'Image',
class {
src = '';
decoding = '';
cloneNode = () => this;
decode = () => {
const fails = ctl.fail; // the outcome belongs to the request, whenever it settles
return new Promise<void>((resolve, reject) => {
const settle = () => {
ctl.urls.push(this.src);
if (fails) reject(new Error('bad image'));
else resolve();
};
if (ctl.hold) ctl.pending.push(settle);
else settle();
});
};
}
);
return {
ctl,
release: async () => {
ctl.hold = false;
ctl.pending.splice(0).forEach((s) => s());
await flush();
},
};
};
beforeEach(() => forgetManifests());
it('says nothing about planes when they load', async () => {
serve();
decoder();
createRidgeScene.mockReturnValue(fakeScene());
const onPlanesFail = vi.fn();
const onPlanesOk = vi.fn();
const scene = await startBackgroundScene(config, caps, { canvas, onPlanesFail, onPlanesOk });
setSkyState('day');
await flush();
expect(createRidgeScene).toHaveBeenCalledTimes(2);
expect(createRidgeScene.mock.calls.every((c) => c[0].backdrop)).toBe(true);
expect(onPlanesFail).not.toHaveBeenCalled();
expect(onPlanesOk).not.toHaveBeenCalled();
scene?.dispose();
});
it('chooses frames by the sun over the manifest place, not the fallback place', async () => {
const w = wireManifest() as any;
// A day where it is night by the clock, in the south, has the noon sun high and to the north; at Vienna it is low in the south: a different frame.
serve({ ...w, place: { ...w.place, lat: -33, lon: -163.65, tz_h: -11 } });
decoder();
setSkyState('day');
createRidgeScene.mockReturnValue(fakeScene());
(await startBackgroundScene(config, caps, { canvas }))?.dispose();
const opts = createRidgeScene.mock.calls[0][0];
const { manifest } = opts.backdrop;
const own = frameChoice(manifest.frames, opts.sky.physical.sun);
const fallbackSun = ridgeSkyFor('day', FALLBACK_VIEW).physical!.sun;
const theirs = frameChoice(manifest.frames, fallbackSun);
expect(own.a.key).not.toBe(theirs.a.key);
expect(opts.backdrop.choice.a.key).toBe(own.a.key);
expect(opts.backdrop.choice.b?.key ?? null).toBe(own.b?.key ?? null);
});
it("keys a rebuild by the manifest's place: the same sky state over a rebaked place rebuilds", async () => {
serve();
decoder();
const [a, b] = [fakeScene(), fakeScene()];
createRidgeScene.mockReturnValueOnce(a).mockReturnValueOnce(b);
const w = wireManifest() as any;
const scene = await startBackgroundScene(config, caps, { canvas });
// 'auto' at midnight and 'night' are one sky: no rebuild, while the place is the one drawn.
setSkyState('night');
await flush();
expect(createRidgeScene).toHaveBeenCalledOnce();
forgetManifests();
serve({ ...w, place: { ...w.place, id: 'graz' } });
setSkyState('auto');
await flush();
expect(createRidgeScene).toHaveBeenCalledTimes(2);
expect(createRidgeScene.mock.calls[1][0].backdrop.manifest.view.place.id).toBe('graz');
scene?.dispose();
});
it("reads the weather against the manifest place's sun, not the fallback place's", async () => {
const w = wireManifest() as any;
const place = { ...w.place, id: 'weather-place', lat: -33, lon: -163.65, tz_h: -11 };
const now = new Date('2026-11-11T10:50:00Z');
const wx = {
fetched_at: '2026-11-11T10:45:00Z',
observed_at: '2026-11-11T10:40:00Z',
station: 11035,
precip_mm_10min: 0,
humidity_pct: 60,
sunshine_s_10min: null,
glow_wm2: 100,
attribution: WEATHER_ATTRIBUTION,
};
vi.stubGlobal(
'fetch',
vi.fn(async (url: string) => ({
ok: true,
status: 200,
json: async () =>
url.endsWith('weather.json') ? wx : url.endsWith('index.json') ? wireIndex : { ...w, place },
}))
);
decoder();
setSkyClock(() => now);
createRidgeScene.mockReturnValue(fakeScene());
(await startBackgroundScene(config, caps, { canvas }))?.dispose();
const file = parseWeatherFile(wx)!;
const at = (p: { lat: number; lon: number }) =>
weatherFromFile(file, sunAt({ id: 'x', altitudeKm: 0, ...p }, new Date(Date.parse(wx.observed_at) - 5 * 60_000)).elevation);
expect(at(FALLBACK_VIEW.place)).not.toBe(at(place));
expect(ridgeWeather()).toBe(at(place));
});
it('a failed load is the sky alone on the fallback view, never the manifest place', async () => {
serve();
const { ctl } = decoder();
ctl.fail = true;
createRidgeScene.mockReturnValue(fakeScene());
const scene = await startBackgroundScene(config, caps, { canvas });
const opts = createRidgeScene.mock.calls[0][0];
expect(opts.backdrop).toBeUndefined();
expect(opts.sky.physical.view).toBe(FALLBACK_VIEW);
scene?.dispose();
});
it('a rebuild keeps the old scene until the new planes have loaded, then swaps', async () => {
serve();
const d = decoder();
const first = fakeScene();
const second = fakeScene();
createRidgeScene.mockReturnValueOnce(first).mockReturnValueOnce(second);
const onActive = vi.fn();
const scene = await startBackgroundScene(config, caps, { canvas, onActive });
d.ctl.hold = true;
setSkyState('day');
await flush();
expect(d.ctl.pending.length).toBeGreaterThan(0);
expect(first.dispose).not.toHaveBeenCalled();
expect(createRidgeScene).toHaveBeenCalledOnce();
scene?.login();
expect(first.login).toHaveBeenCalledOnce();
await d.release();
expect(createRidgeScene).toHaveBeenCalledTimes(2);
expect(first.dispose).toHaveBeenCalledOnce();
expect(second.dispose).not.toHaveBeenCalled();
expect(onActive).toHaveBeenCalledOnce();
expect(second.login).toHaveBeenCalledOnce(); // replayed onto the swapped-in scene
scene?.dispose();
expect(second.dispose).toHaveBeenCalledOnce();
});
it('a build that fails after the scene is disposed makes nothing and says nothing', async () => {
serve();
const d = decoder();
createRidgeScene.mockReturnValue(fakeScene());
const onPlanesFail = vi.fn();
const scene = await startBackgroundScene(config, caps, { canvas, onPlanesFail });
d.ctl.hold = true;
d.ctl.fail = true;
setSkyState('day');
await flush();
scene?.dispose();
await d.release();
expect(createRidgeScene).toHaveBeenCalledOnce();
expect(onPlanesFail).not.toHaveBeenCalled();
});
it('a superseded build that fails does not speak for the live scene, which has planes', async () => {
serve();
const d = decoder();
createRidgeScene.mockReturnValue(fakeScene());
const onPlanesFail = vi.fn();
const scene = await startBackgroundScene(config, caps, { canvas, onPlanesFail });
d.ctl.hold = true;
d.ctl.fail = true;
setSkyState('day'); // stale: its decodes will reject
await flush();
d.ctl.fail = false;
setSkyState('dusk'); // the live one: its decodes will succeed
await flush();
const stale = d.ctl.pending.splice(0, 6);
d.ctl.pending.length = 0;
d.ctl.hold = false;
stale.forEach((s) => s()); // the first six are day's, settled in order
await flush();
expect(onPlanesFail).not.toHaveBeenCalled();
scene?.dispose();
});
it('a superseded build that finishes after the newer one touches nothing of the live scene', async () => {
serve();
const d = decoder();
const [first, second, third] = [fakeScene(), fakeScene(), fakeScene()];
createRidgeScene.mockReturnValueOnce(first).mockReturnValueOnce(second).mockReturnValueOnce(third);
const onActive = vi.fn();
const scene = await startBackgroundScene(config, caps, { canvas, onActive });
d.ctl.hold = true;
setSkyState('day'); // stale: its decodes are held until after the newer build is live
await flush();
const dayDecodes = d.ctl.pending.splice(0);
setSkyState('dusk');
await flush();
const duskDecodes = d.ctl.pending.splice(0);
expect(dayDecodes.length).toBeGreaterThan(0);
expect(duskDecodes.length).toBeGreaterThan(0);
duskDecodes.forEach((s) => s());
await flush();
expect(createRidgeScene).toHaveBeenCalledTimes(2);
expect(first.dispose).toHaveBeenCalledOnce();
expect(onActive).toHaveBeenCalledOnce();
dayDecodes.forEach((s) => s()); // the stale build finishes last
await flush();
expect(createRidgeScene).toHaveBeenCalledTimes(2);
expect(second.dispose).not.toHaveBeenCalled(); // the live scene is not disposed by a build that lost
expect(onActive).toHaveBeenCalledOnce();
scene?.login();
expect(second.login).toHaveBeenCalledOnce();
scene?.dispose();
expect(second.dispose).toHaveBeenCalledOnce();
expect(third.dispose).not.toHaveBeenCalled();
});
it('a build that finishes after dispose makes nothing, shows nothing and disposes nothing twice', async () => {
serve();
const d = decoder();
const first = fakeScene();
createRidgeScene.mockReturnValueOnce(first).mockReturnValueOnce(fakeScene());
const onActive = vi.fn();
const scene = await startBackgroundScene(config, caps, { canvas, onActive });
d.ctl.hold = true;
setSkyState('day');
await flush();
scene?.dispose();
await d.release();
expect(createRidgeScene).toHaveBeenCalledOnce();
expect(first.dispose).toHaveBeenCalledOnce();
expect(onActive).not.toHaveBeenCalled();
});
it('a rebuild that makes no scene says nothing about planes and leaves the host inactive', async () => {
serve();
const d = decoder();
createRidgeScene.mockReturnValueOnce(fakeScene()).mockReturnValueOnce(null);
const onPlanesFail = vi.fn();
const onInactive = vi.fn();
const scene = await startBackgroundScene(config, caps, { canvas, onPlanesFail, onInactive });
d.ctl.fail = true;
setSkyState('day');
await flush();
expect(onInactive).toHaveBeenCalledOnce();
expect(onPlanesFail).not.toHaveBeenCalled();
scene?.dispose();
});
it('tells the host when the live scene loses its planes and when it gets them back', async () => {
serve();
const d = decoder();
d.ctl.fail = true;
createRidgeScene.mockReturnValue(fakeScene());
const onPlanesFail = vi.fn();
const onPlanesOk = vi.fn();
const scene = await startBackgroundScene(config, caps, { canvas, onPlanesFail, onPlanesOk });
expect(onPlanesFail).toHaveBeenCalledOnce();
setSkyState('day'); // still failing: the host is not told twice
await flush();
expect(onPlanesFail).toHaveBeenCalledOnce();
expect(onPlanesOk).not.toHaveBeenCalled();
d.ctl.fail = false;
setSkyState('dusk');
await flush();
expect(onPlanesOk).toHaveBeenCalledOnce();
expect(createRidgeScene.mock.calls[2][0].backdrop).toBeDefined();
d.ctl.fail = true;
setSkyState('night');
await flush();
expect(onPlanesFail).toHaveBeenCalledTimes(2);
scene?.dispose();
});
});

View file

@ -3,6 +3,12 @@ import { BackgroundSceneConfig, Capabilities, SceneTier, resolveTier } from './t
import { debugOverlaysAllowed } from './debugOverlays';
import { getSkyState, subscribe } from './skyState';
import type { SkyState } from './skyState';
import type { SkyColours } from './ridge/skyColour';
import type { Backdrop } from './ridge/Backdrop';
import { loadManifest } from './ridge/manifest';
import type { RidgeManifest } from './ridge/manifest';
import { FALLBACK_VIEW } from './ridge/view';
import { frameChoice } from './ridge/frameChoice';
export type { Scene, SceneStats } from './Scene';
export type { SceneStyle, SceneTier } from './tier';
@ -14,35 +20,106 @@ export type StartOptions = Omit<SceneOptions, 'tier' | 'patchSource' | 'onCompil
onInactive?: () => void;
/** A rebuild made a scene again: the host lets it show. */
onActive?: () => void;
/** The live scene is the sky alone because the baked ridge planes did not load (no manifest, a missing image). The host may fill the ground. Told when the live scene changes to that state, never for a build that did not win. */
onPlanesFail?: (e: Error) => void;
/** The live scene has its planes after `onPlanesFail` said it had none: the host lets go of the ground it filled. */
onPlanesOk?: () => void;
};
/** The observation file the ridge sky reads: the page's own, beside config.json. The browser calls no weather service. */
const weatherUrl = (): string => `${import.meta.env.BASE_URL.replace(/\/$/, '')}/weather.json`;
async function create(
/**
* The baked place's manifest, or why there is none. Its View is the one owner of the sun's place and the camera for the weather, the sky,
* the rebuild key and the planes; a scene with no manifest has FALLBACK_VIEW.
*/
async function manifestOf(
config: BackgroundSceneConfig | undefined
): Promise<{ manifest: RidgeManifest | null; fail: Error | null }> {
try {
return { manifest: await loadManifest(import.meta.env.BASE_URL, config?.place), fail: null };
} catch (e) {
return { manifest: null, fail: e instanceof Error ? e : new Error(String(e)) };
}
}
/** A scene made ready: every await is done, so `make` is synchronous and a rebuild can keep the old scene until the instant it swaps. */
interface Prepared {
make: () => Scene | null;
/** Why this scene is the sky alone, when the baked planes did not load; null when it has planes or none are asked for. */
planesFail: Error | null;
}
async function prepare(
config: BackgroundSceneConfig | undefined,
options: StartOptions & { tier: SceneTier; debug?: boolean },
state: SkyState
): Promise<Scene | null> {
): Promise<Prepared> {
// Whatever throws in making the scene is a compile failure, as whatever throws in loading it.
const guarded = (make: () => Scene | null): Prepared => ({
planesFail: null,
make: () => {
try {
return make();
} catch (e) {
options.onCompileFail?.(e instanceof Error ? e : new Error(String(e)));
return null;
}
},
});
try {
// Loaded here, with the scenes, so the main chunk does not carry the terrain sky or the baked tables.
const { loadWeather, ridgeSkyFor, terrainSkyFor } = await import('./skyForState');
if (config?.style === 'raymarch') return (await import('./Scene')).createScene(options);
if (config?.style === 'raymarch') {
const { createScene } = await import('./Scene');
return guarded(() => createScene(options));
}
if (config?.style === 'terrain') {
const { createTerrainScene } = await import('./terrain/Terrain');
const sky = terrainSkyFor(state);
const scene = createTerrainScene({ ...options, ...sky });
// No WebGL2 (or no usable GL): the terrain's sky alone on the same canvas, not bare ground.
if (scene) return scene;
const { createTerrainSkyScene } = await import('./terrain/TerrainSky2D');
return createTerrainSkyScene({ ...options, ...sky });
const sky = terrainSkyFor(state);
// No WebGL2 (or no usable GL): the terrain's sky alone on the same canvas, not bare ground.
return guarded(
() =>
createTerrainScene({ ...options, ...sky }) ??
createTerrainSkyScene({ ...options, ...sky })
);
}
const { createRidgeScene } = await import('./ridge/Ridge');
await loadWeather(weatherUrl());
return createRidgeScene({ ...options, sky: ridgeSkyFor(state) });
const { manifest, fail } = await manifestOf(config);
await loadWeather(weatherUrl(), (manifest?.view ?? FALLBACK_VIEW).place);
// The planes load with the scene chunk and the weather: createRidgeScene stays synchronous. Any failure is the sky alone.
let backdrop: Backdrop | null = null;
let sky: SkyColours | undefined;
let planesFail: Error | null = fail;
if (manifest) {
try {
const manifestSky = ridgeSkyFor(state, manifest.view);
sky = manifestSky;
const choice = frameChoice(
manifest.frames,
(manifestSky.physical as NonNullable<typeof manifestSky.physical>).sun
);
const { loadBackdrop } = await import('./ridge/Backdrop');
backdrop = await loadBackdrop(manifest, choice);
if (!backdrop) throw new Error('ridge planes did not load');
} catch (e) {
backdrop = null;
sky = undefined;
planesFail = e instanceof Error ? e : new Error(String(e));
}
}
const made = guarded(() =>
createRidgeScene({
...options,
sky: sky ?? ridgeSkyFor(state, FALLBACK_VIEW),
backdrop: backdrop ?? undefined,
})
);
return { ...made, planesFail };
} catch (e) {
options.onCompileFail?.(e instanceof Error ? e : new Error(String(e)));
return null;
return { make: () => null, planesFail: null };
}
}
@ -58,8 +135,9 @@ async function skyKey(
try {
const { loadWeather, ridgeSkyKey, terrainSkyFor } = await import('./skyForState');
if (config?.style === 'terrain') return JSON.stringify(terrainSkyFor(state));
await loadWeather(weatherUrl());
return ridgeSkyKey(state);
const view = (await manifestOf(config)).manifest?.view ?? FALLBACK_VIEW;
await loadWeather(weatherUrl(), view.place);
return ridgeSkyKey(state, view);
} catch {
// The chunk failed to load: no key, so every change rebuilds (and `create` reports the failure).
return null;
@ -87,11 +165,20 @@ export async function startBackgroundScene(
const tier = resolveTier(config, capabilities);
if (tier === 'off') return null;
const debug = debugOverlaysAllowed(config);
const build = (state: SkyState) => create(config, { ...options, tier, debug }, state);
// What the host was last told of the planes: only the live scene speaks, and only when its state changes.
let planesFailed = false;
const announce = (planesFail: Error | null) => {
if (planesFail && !planesFailed) options.onPlanesFail?.(planesFail);
if (!planesFail && planesFailed) options.onPlanesOk?.();
planesFailed = planesFail !== null;
};
const prepared = (state: SkyState) => prepare(config, { ...options, tier, debug }, state);
const state = getSkyState();
const key0 = config?.style === 'raymarch' ? null : await skyKey(config, state);
const first = await build(state);
const firstReady = await prepared(state);
const first = firstReady.make();
if (!first || config?.style === 'raymarch') return first;
announce(firstReady.planesFail);
let inner: Scene | null = first;
let built = key0;
@ -108,14 +195,14 @@ export async function startBackgroundScene(
generation += 1;
const mine = generation;
built = key;
// The old scene stays up while the next one loads; a build that is stale or after dispose is never made, so it never speaks.
const ready = await prepared(next);
if (disposed || mine !== generation) return;
inner?.dispose();
inner = null;
const scene = await build(next);
if (disposed || mine !== generation) {
scene?.dispose();
return;
}
const scene = ready.make();
inner = scene;
if (scene) announce(ready.planesFail);
if (!scene) options.onInactive?.();
else {
options.onActive?.();

View file

@ -0,0 +1,44 @@
import { describe, expect, it, vi } from 'vitest';
import { loadBackdrop } from './Backdrop';
import { frameChoice } from './frameChoice';
import { parseManifest } from './manifest';
import { wireManifest } from './manifest.fixture';
const manifest = parseManifest(wireManifest(), '/d/');
const img = (src: string) => ({ src } as unknown as HTMLImageElement);
describe('loadBackdrop', () => {
it('loads the planes of the needed frames only, by manifest directory', async () => {
const load = vi.fn(async (u: string) => img(u));
const one = await loadBackdrop(
manifest,
frameChoice(manifest.frames, { elevation: 60, azimuth: 180 }),
load
);
expect(load.mock.calls.map((c) => c[0]).sort()).toEqual([
'/d/noon-far.webp',
'/d/noon-mid.webp',
'/d/noon-near.webp',
]);
expect(one?.b).toBeNull();
load.mockClear();
const two = await loadBackdrop(
manifest,
frameChoice(manifest.frames, { elevation: 10, azimuth: 130 }),
load
);
expect(load).toHaveBeenCalledTimes(6);
expect(two?.b?.far.src).toBe('/d/morning-far.webp');
expect(two?.a.near.src).toBe('/d/dawn-near.webp');
});
it('is null, and does not throw, when any image rejects', async () => {
const load = vi.fn(async (u: string) => {
if (u.includes('mid')) throw new Error('decode');
return img(u);
});
await expect(
loadBackdrop(manifest, frameChoice(manifest.frames, { elevation: 60, azimuth: 180 }), load)
).resolves.toBeNull();
});
});

View file

@ -0,0 +1,44 @@
// The decoded planes the scene composes: only the frames the choice needs. Failure is a value (null), never a throw.
import type { FrameChoice } from './frameChoice';
import type { Frame, PlaneName, RidgeManifest } from './manifest';
import { PLANE_NAMES } from './manifest';
export type Planes = Record<PlaneName, HTMLImageElement>;
export interface Backdrop {
manifest: RidgeManifest;
choice: FrameChoice;
/** Decoded planes of frame a, and of frame b when the choice has one. */
a: Planes;
b: Planes | null;
}
export type ImageLoader = (url: string) => Promise<HTMLImageElement>;
/** The browser's decoder: an <img> that has decoded, so the first paint does not wait on it. */
export const loadImage: ImageLoader = async (url) => {
const img = new Image();
img.decoding = 'async';
img.src = url;
await img.decode();
return img;
};
export async function loadBackdrop(
manifest: RidgeManifest,
choice: FrameChoice,
load: ImageLoader = loadImage
): Promise<Backdrop | null> {
const planes = async (f: Frame): Promise<Planes> => {
const [far, mid, near] = await Promise.all(
PLANE_NAMES.map((n) => load(`${manifest.dir}${f.planes[n]}`))
);
return { far, mid, near };
};
try {
const [a, b] = await Promise.all([planes(choice.a), choice.b ? planes(choice.b) : null]);
return { manifest, choice, a, b };
} catch {
return null;
}
}

View file

@ -1,24 +1,16 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { createRidgeScene } from './Ridge';
import { bakeSlice, BakeParams, layerPaint } from './ridgeBake';
import { valueNoise, hash2 } from './noise';
import { FrameSampler, frameStats, SLOW_P95_MS, WARMUP_FRAMES, WINDOW_MS } from './probe';
import {
AERIAL,
LAYERS,
edgeBlur,
hazeColour,
layerColour,
layerSize,
profile,
ridgeY,
scatterAlpha,
transmittance,
} from './ridgeWorld';
import { skyColours, SkyColours } from './skyColour';
import type { SkyColours } from './skyColour';
import { makeStars } from './stars';
import { makePhysicalSky } from './physicalSky';
import { makePhysicalSky, paintSky as paintPhysicalSky } from './physicalSky';
import { startBackgroundScene } from '../index';
import { frameChoice } from './frameChoice';
import { parseManifest } from './manifest';
import { ELSEWHERE_VIEW, wireManifest } from './manifest.fixture';
import type { Backdrop } from './Backdrop';
import { FALLBACK_VIEW, windowFor } from './view';
describe('noise', () => {
it('is deterministic and in [0, 1)', () => {
@ -36,111 +28,6 @@ describe('noise', () => {
});
});
describe('ridge profile', () => {
it('stays in [0, 1] for any world-x, negative and far included, and layers differ', () => {
for (const spec of LAYERS) {
for (let i = -300; i < 300; i += 7) {
for (const off of [0, 1234.5, 98765.25]) {
const p = profile(spec, i / 50 + off);
expect(p).toBeGreaterThanOrEqual(0);
expect(p).toBeLessThanOrEqual(1);
}
}
}
expect(profile(LAYERS[0], 0.3)).not.toBeCloseTo(profile(LAYERS[1], 0.3), 3);
});
it('ridge tops lie inside the layer band', () => {
const size = layerSize(400, 800, 1);
for (const spec of LAYERS) {
for (let x = -500; x < 5 * size.period; x += 17) {
const y = ridgeY(spec, x / size.period, size.height, 800);
expect(y).toBeGreaterThanOrEqual(0);
expect(y).toBeLessThanOrEqual(size.height);
}
}
});
it('layers get darker and slower toward the viewer, and their sizes are integers', () => {
const sky = skyColours();
const lum = (c: readonly number[]) => c[0] + c[1] + c[2];
for (let i = 1; i < LAYERS.length; i += 1) {
expect(lum(layerColour(LAYERS[i], sky))).toBeLessThan(lum(layerColour(LAYERS[i - 1], sky)));
expect(LAYERS[i].seconds).toBeLessThan(LAYERS[i - 1].seconds);
}
for (const [w, h, s] of [
[393, 851, 2],
[1280, 720, 1],
[412.5, 915.3, 2],
] as const) {
const z = layerSize(w, h, s);
expect(
Number.isInteger(z.period) && Number.isInteger(z.chunk) && Number.isInteger(z.height)
).toBe(true);
expect(z.chunk).toBeGreaterThan(z.period);
}
});
});
describe('aerial perspective', () => {
const sky = skyColours();
const lum = (c: readonly number[]) => 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2];
const chroma = (c: readonly number[]) => Math.max(...c) - Math.min(...c);
const far = LAYERS[0];
const near = LAYERS[LAYERS.length - 1];
it('transmittance is Beer-Lambert in distance: 1 at the viewer, exp(-k d) behind, strictly falling', () => {
expect(transmittance(near)).toBe(1);
for (const l of LAYERS)
expect(transmittance(l)).toBeCloseTo(Math.exp(-AERIAL.extinction * (1 - l.depth)), 12);
for (let i = 1; i < LAYERS.length; i += 1)
expect(transmittance(LAYERS[i])).toBeGreaterThan(transmittance(LAYERS[i - 1]));
// two layers' optical depths add: T(a)*T(b) = T(a+b)
const t = (d: number) => Math.exp(-AERIAL.extinction * d);
expect(t(0.3) * t(0.4)).toBeCloseTo(t(0.7), 12);
});
it('the nearest layer is exactly the ground colour; the farthest tends to the haze', () => {
expect(layerColour(near, sky)).toEqual([...sky.ground]);
const h = hazeColour(sky);
const f = layerColour(far, sky);
const dist = (a: readonly number[], b: readonly number[]) =>
Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]);
expect(dist(f, h)).toBeLessThan(dist(sky.ground, h) * (transmittance(far) + 0.2));
});
it('colour moves toward the haze and loses chroma with distance, beyond what the mix alone does', () => {
const h = hazeColour(sky);
for (const l of LAYERS.slice(0, -1)) {
const t = transmittance(l);
const c = layerColour(l, sky);
const mixed = [0, 1, 2].map((i) => sky.ground[i] * t + h[i] * (1 - t));
expect(chroma(c)).toBeLessThan(chroma(mixed));
expect(Math.abs(lum(c) - lum(mixed))).toBeLessThan(1.5); // desaturation keeps luma
}
});
it('edge softening grows with distance and is zero at the viewer', () => {
expect(edgeBlur(near)).toBe(0);
for (let i = 1; i < LAYERS.length; i += 1)
expect(edgeBlur(LAYERS[i])).toBeLessThan(edgeBlur(LAYERS[i - 1]));
expect(edgeBlur(far)).toBeLessThanOrEqual(AERIAL.blurPx);
});
it('in-scatter is faint, grows with distance, is off without a sun, and never repeats', () => {
const az = 0.3;
const s = { ...sky, sunAzimuth: az };
const lobe = (t: number) => scatterAlpha(far, s, t);
expect(lobe(az * 1.9)).toBeGreaterThan(lobe(az * 1.9 + 0.95));
for (const period of [1, 1.9, 3.8, 5.7, 19])
expect(Math.abs(lobe(0.7 + period) - lobe(0.7))).toBeGreaterThan(1e-4);
expect(lobe(az * 1.9)).toBeLessThanOrEqual(AERIAL.scatter);
expect(scatterAlpha(near, s, az)).toBe(0);
expect(scatterAlpha(LAYERS[1], s, az * 1.9)).toBeLessThan(scatterAlpha(far, s, az * 1.9));
expect(scatterAlpha(far, { ...sky, sunAzimuth: undefined }, az)).toBe(0);
});
});
describe('frame sampler', () => {
const feed = (s: FrameSampler, dt: number, frames: number, from = 0) => {
let t = from;
@ -242,26 +129,22 @@ function fakeEl(tag = 'div') {
};
return el;
}
// A day sky has no stars, so the scene tests below see only the ridge loop; the dusk default
// A day sky has no stars, so the scene tests above see only the sky; the dusk default
// carries stars and a twinkle overlay, tested in 'the starfield in the scene'.
const DAY: SkyColours = {
top: [96, 150, 220],
mid: [150, 190, 235],
horizon: [240, 215, 190],
ground: [7, 17, 26],
sunAzimuth: 0.3,
};
const draws: string[] = [];
/** Every ImageData put on any canvas, in order. */
const puts: any[] = [];
/** For every drawImage: the source canvas's width as it was at the moment of the draw. */
const drawnWidths: number[] = [];
function ctx(withFilter = false) {
const images: unknown[][] = [];
/** Every RGBA grid the host canvas was painted from (the small canvas's putImageData). */
const painted: Uint8ClampedArray[] = [];
function ctx() {
const c: any = {
globalCompositeOperation: 'source-over',
createLinearGradient: () => ({ addColorStop: vi.fn() }),
fillRect() {
draws.push(c.globalCompositeOperation === 'source-atop' ? 'scatter' : 'sky');
draws.push('sky');
},
save() {},
restore() {},
@ -272,7 +155,9 @@ function ctx(withFilter = false) {
width: w,
height: h,
}),
putImageData: (img: any) => puts.push(img),
putImageData(img: { data: Uint8ClampedArray }) {
painted.push(img.data);
},
beginPath() {},
moveTo() {},
stroke() {},
@ -292,13 +177,13 @@ function ctx(withFilter = false) {
if (c.arcing) c.arcing = false;
else draws.push('fill');
},
drawImage: (src: any) => {
drawnWidths.push(src.width);
draws.push('flatten');
drawImage(...a: unknown[]) {
images.push(Object.assign(a, { alpha: c.globalAlpha, op: c.globalCompositeOperation }));
},
globalAlpha: 1,
globalCompositeOperation: 'source-over',
set fillStyle(_v: unknown) {},
};
if (withFilter) c.filter = 'none';
return c;
}
@ -310,8 +195,8 @@ let search: string;
beforeEach(() => {
rafQueue = [];
draws.length = 0;
puts.length = 0;
drawnWidths.length = 0;
images.length = 0;
painted.length = 0;
search = '';
motion = { matches: false, addEventListener: vi.fn(), removeEventListener: vi.fn() };
doc = Object.assign(new EventTarget(), {
@ -347,11 +232,6 @@ function host() {
parent.appendChild(canvas);
return { parent, canvas };
}
/** The rig holds one track per layer, farthest first; each track holds three chunk canvases. */
const rigOf = (parent: any) => parent.children.find((c: any) => c.tag === 'div');
const tracksOf = (parent: any): any[] => rigOf(parent).children;
const animsOf = (parent: any): any[] =>
[rigOf(parent), ...tracksOf(parent)].flatMap((e: any) => e.anims);
const runFrames = (dt: number, n: number, from = 0) => {
let t = from;
for (let i = 0; i < n && rafQueue.length; i += 1) {
@ -375,29 +255,27 @@ describe('createRidgeScene', () => {
expect(createRidgeScene({ canvas, tier: 'full', sky: DAY })).toBeNull();
});
it('bakes the sky once, and per layer only the first chunk before the first frame, at integer sizes, farthest first', () => {
it('paints the sky once at the viewport in canvas pixels and adds nothing to the page in a clear day', () => {
const { parent, canvas } = host();
const baked: any[] = [];
const s = createRidgeScene({ canvas, tier: 'full', sky: DAY, onBake: (b) => baked.push(b) })!;
const s = createRidgeScene({ canvas, tier: 'full', sky: DAY })!;
expect(s).not.toBeNull();
const tracks = tracksOf(parent);
expect(tracks).toHaveLength(LAYERS.length);
tracks.forEach((t: any, i: number) => {
expect(t.children).toHaveLength(3);
expect(t.animate).toHaveBeenCalledTimes(Number.isFinite(LAYERS[i].seconds) ? 1 : 0);
t.children.forEach((c: any) => {
expect(Number.isInteger(c.width) && Number.isInteger(c.height)).toBe(true);
expect(c.width).toBe(layerSize(400, 800, 2).chunk);
});
});
expect(baked.map((b) => [b.layer, b.chunk, b.first])).toEqual(
LAYERS.map((l) => [l.index, 0, true])
);
expect([canvas.width, canvas.height]).toEqual([800, 1600]);
expect(draws.filter((d) => d === 'sky')).toHaveLength(1);
expect(parent.children).toEqual([canvas]);
s.dispose();
});
it('after the layers are painted, a smooth run draws nothing more and ends its rAF loop', () => {
it("paints the sky through the camera of the physical sky's own View, not a default one", () => {
const { canvas } = host();
const physical = makePhysicalSky('day', new Date('2026-06-21T10:40:00Z'), 0, ELSEWHERE_VIEW);
const s = createRidgeScene({ canvas, tier: 'full', sky: { ...DAY, physical } })!;
const want = paintPhysicalSky(physical, 400, 800, windowFor(ELSEWHERE_VIEW, 400, 800).camera);
expect(painted).toHaveLength(1);
expect(Array.from(painted[0])).toEqual(Array.from(want.data));
s.dispose();
});
it('a smooth run draws nothing more and ends its rAF loop', () => {
const { canvas } = host();
const s = createRidgeScene({ canvas, tier: 'full', sky: DAY })!;
const before = draws.length;
@ -407,53 +285,23 @@ describe('createRidgeScene', () => {
s.dispose();
});
it('p95 over 33 ms across 2 s flattens to a still image and removes the layers', () => {
const { parent, canvas } = host();
it('p95 over 33 ms across 2 s settles to a still and reports it', () => {
const { canvas } = host();
const onProbe = vi.fn();
const s = createRidgeScene({ canvas, tier: 'full', onProbe, sky: DAY })!;
runFrames(100, 100);
expect(draws.filter((d) => d === 'flatten')).toHaveLength(LAYERS.length);
expect(parent.children).toEqual([canvas]);
expect(onProbe).toHaveBeenLastCalledWith(expect.objectContaining({ still: true }));
expect(rafQueue).toHaveLength(0);
s.dispose();
});
it("'still' tier flattens immediately and never starts a loop", () => {
it("'still' tier never starts a loop", () => {
const { parent, canvas } = host();
createRidgeScene({ canvas, tier: 'still' });
expect(parent.children).toEqual([canvas]);
expect(draws.filter((d) => d === 'flatten')).toHaveLength(LAYERS.length);
expect(rafQueue).toHaveLength(0);
});
it('prefers-reduced-motion: layers are built but never played, and nothing is sampled', () => {
motion.matches = true;
const { canvas } = host();
const s = createRidgeScene({ canvas, tier: 'full', sky: DAY })!;
const anims = animsOf(canvas.parentElement);
expect(anims.length).toBe(LAYERS.filter((l) => Number.isFinite(l.seconds)).length + 1); // one drift per drifting layer (the horizon does not) and the sway
expect(anims.every((a: any) => a.pause.mock.calls.length > 0)).toBe(true);
anims.forEach((a: any) => expect(a.play).not.toHaveBeenCalled());
expect(rafQueue).toHaveLength(0);
s.dispose();
});
it('pauses the drift when the tab is hidden and resumes when it is visible', () => {
const { parent, canvas } = host();
const s = createRidgeScene({ canvas, tier: 'full', sky: DAY })!;
const anims = animsOf(parent);
anims.forEach((a: any) => a.pause.mockClear());
doc.visibilityState = 'hidden';
doc.dispatchEvent(new Event('visibilitychange'));
anims.forEach((a: any) => expect(a.pause).toHaveBeenCalled());
anims.forEach((a: any) => a.play.mockClear());
doc.visibilityState = 'visible';
doc.dispatchEvent(new Event('visibilitychange'));
anims.forEach((a: any) => expect(a.play).toHaveBeenCalled());
s.dispose();
});
it('?probe=1 keeps the rAF loop alive and shows median and p95', () => {
search = '?probe=1';
const { parent, canvas } = host();
@ -469,9 +317,8 @@ describe('createRidgeScene', () => {
it('?probe=1 does nothing where the overlays are not allowed: no readout, the loop ends', () => {
search = '?probe=1';
const { parent, canvas } = host();
const before = parent.children.length;
const s = createRidgeScene({ canvas, tier: 'full', sky: DAY })!;
expect(parent.children.length).toBe(before + 1); // the rig
expect(parent.children).toEqual([canvas]);
runFrames(16.7, 300);
expect(rafQueue.length).toBe(0);
s.dispose();
@ -485,31 +332,19 @@ describe('createRidgeScene', () => {
(window as any).innerHeight = 0;
const s = createRidgeScene({ canvas, tier: 'full', sky: DAY });
expect(s).not.toBeNull();
expect(parent.children).toEqual([canvas]);
expect(draws).toHaveLength(0);
// Still zero: a resize does not paint either.
window.dispatchEvent(new Event('resize'));
await new Promise((r) => setTimeout(r, 300));
expect(parent.children).toEqual([canvas]);
expect(draws).toHaveLength(0);
(window as any).innerWidth = 400;
(window as any).innerHeight = 800;
window.dispatchEvent(new Event('resize'));
await new Promise((r) => setTimeout(r, 300));
expect(tracksOf(parent)).toHaveLength(LAYERS.length);
expect(draws.filter((d) => d === 'sky')).toHaveLength(1);
s!.dispose();
expect(parent.children).toEqual([canvas]);
});
it('dispose cancels the loop, the animations and the layers', () => {
const { parent, canvas } = host();
const s = createRidgeScene({ canvas, tier: 'full', sky: DAY })!;
const anims = animsOf(parent);
s.dispose();
anims.forEach((a: any) => expect(a.cancel).toHaveBeenCalled());
expect(parent.children).toEqual([canvas]);
expect(rafQueue).toHaveLength(0);
});
});
describe('the door', () => {
@ -530,65 +365,6 @@ describe('the door', () => {
});
});
describe('aerial perspective in the scene', () => {
it('is baked once: one scatter fill per hazy layer, none per frame, the nearest layer has none', () => {
const { canvas } = host();
const s = createRidgeScene({ canvas, tier: 'full', sky: DAY })!;
expect(draws.filter((d) => d === 'scatter')).toHaveLength(LAYERS.length - 1);
const before = draws.length;
runFrames(16.7, 400);
expect(draws.length).toBe(before);
s.dispose();
});
it('a sky without a sun bakes no scatter', () => {
const { canvas } = host();
const s = createRidgeScene({
canvas,
tier: 'full',
sky: { ...DAY, sunAzimuth: undefined },
})!;
expect(draws.filter((d) => d === 'scatter')).toHaveLength(0);
s.dispose();
});
it('softens with the canvas filter where it exists, blur scaled by DPR, from pixels baked past the chunk edges', () => {
const { parent, canvas } = host();
const filters: string[] = [];
const orig = (document as any).createElement;
(document as any).createElement = (t: string) => {
const el = orig(t);
el.getContext = vi.fn(() => {
const c = ctx(true);
let f = 'none';
Object.defineProperty(c, 'filter', {
get: () => f,
set: (v: string) => {
f = v;
filters.push(v);
},
});
return c;
});
return el;
};
const s = createRidgeScene({ canvas, tier: 'full', sky: DAY })!;
const blurs = filters.filter((f) => f !== 'none');
expect(blurs).toHaveLength(LAYERS.length - 1); // nearest is sharp
expect(blurs[0]).toBe(`blur(${edgeBlur(LAYERS[0]) * 2}px)`); // DPR 2
expect(parent.children).toHaveLength(2); // sky canvas and the rig
s.dispose();
});
it("'still' tier keeps the sky and flattens the aerial layers without extra elements", () => {
const { parent, canvas } = host();
createRidgeScene({ canvas, tier: 'still' });
expect(parent.children).toEqual([canvas]);
expect(draws.filter((d) => d === 'sky')).toHaveLength(1);
expect(draws.filter((d) => d === 'flatten')).toHaveLength(LAYERS.length);
});
});
describe('the starfield in the scene', () => {
const count = (kind: string) => draws.filter((d) => d === kind).length;
const night = {
@ -602,7 +378,7 @@ describe('the starfield in the scene', () => {
const calls = motion.addEventListener.mock.calls.filter((c: any[]) => c[0] === name);
return calls[calls.length - 1][1] as () => void;
};
// Everything but the overlay's own marks: the bake and the ridges.
// Everything but the overlay's own marks: the sky and its stars.
const baked = () => draws.filter((d) => d !== 'twinkle' && d !== 'clear').length;
it('bakes every star once into the sky; per frame only the overlay is touched', () => {
@ -610,8 +386,8 @@ describe('the starfield in the scene', () => {
const s = createRidgeScene({ canvas, tier: 'full', sky: night })!;
expect(count('star')).toBe(N);
expect(count('sky')).toBe(1);
expect(parent.children[1].anims).toHaveLength(0); // the overlay: above the sky, under the ridges
expect(parent.children).toHaveLength(3);
expect(parent.children[1].anims).toHaveLength(0); // the overlay, above the sky
expect(parent.children).toHaveLength(2);
const before = baked();
runFrames(16.7, 300); // 5 s at 60 fps; the overlay repaints at 20 fps
expect(baked()).toBe(before);
@ -627,7 +403,7 @@ describe('the starfield in the scene', () => {
const { parent, canvas } = host();
const s = createRidgeScene({ canvas, tier: 'full', sky: DAY })!;
expect(count('star')).toBe(0);
expect(parent.children).toHaveLength(2);
expect(parent.children).toHaveLength(1);
runFrames(16.7, 60);
expect(count('clear')).toBe(0);
s.dispose();
@ -687,13 +463,12 @@ describe('the starfield in the scene', () => {
s.dispose();
});
it('flattening to a still (slow frames) drops the overlay and leaves every star baked under the ridges', () => {
it('flattening to a still (slow frames) drops the overlay and leaves every star baked into the sky', () => {
const { parent, canvas } = host();
const s = createRidgeScene({ canvas, tier: 'full', sky: night })!;
runFrames(100, 100);
expect(parent.children).toEqual([canvas]);
expect(count('star')).toBe(N);
expect(draws.indexOf('flatten')).toBeGreaterThan(draws.lastIndexOf('star'));
expect(rafQueue).toHaveLength(0);
s.dispose();
});
@ -707,125 +482,14 @@ describe('the starfield in the scene', () => {
});
});
describe('endless drift: chunks baked on demand and recycled', () => {
const size = layerSize(400, 800, 2); // the fake host: 400 x 800 CSS px at DPR 2
const until = async (cond: () => boolean) => {
const t0 = Date.now();
while (!cond()) {
if (Date.now() - t0 > 10000) throw new Error('timed out waiting for the background bakes');
await new Promise((r) => setTimeout(r, 5));
}
};
/** The pixels chunk `m` of layer `li` must hold: the pure bake of its world columns, no blur. */
const expected = (li: number, m: number) => {
const p: BakeParams = {
spec: LAYERS[li],
sky: DAY,
period: size.period,
scale: 2,
viewH: 1600,
rows: size.height,
bandH: size.height,
px0: m * size.chunk,
cols: size.chunk,
};
const out = new Uint8ClampedArray(p.cols * p.rows * 4);
bakeSlice(out, p, layerPaint(p.spec, DAY), 0, p.cols);
return Buffer.from(out.buffer);
};
/** Each distinct ImageData put, in order of first appearance: one per baked chunk. */
const baked = () => [...new Set(puts)].map((img) => Buffer.from(img.data.buffer));
const start = () => {
const { parent, canvas } = host();
const records: any[] = [];
const s = createRidgeScene({ canvas, tier: 'full', sky: DAY, onBake: (b) => records.push(b) })!;
return { parent, s, records };
};
it('bakes every chunk from its own world columns: no offset, the next chunk continues the last', async () => {
const { s, records } = start();
await until(() => records.length === 3 * LAYERS.length);
// First view: chunk 0 of each layer; then, per layer in queue order, chunks 1 and 2.
const order = [
...LAYERS.map((l) => [l.index, 0]),
...LAYERS.flatMap((l) => [
[l.index, 1],
[l.index, 2],
]),
];
expect(records.map((r) => [r.layer, r.chunk])).toEqual(order);
const imgs = baked();
expect(imgs).toHaveLength(order.length);
order.forEach(([li, m], i) => expect(imgs[i].equals(expected(li, m))).toBe(true));
s.dispose();
});
it('when the drift animation ends, the chunks move one slot left and the track restarts', async () => {
const { parent, s, records } = start();
await until(() => records.length === 3 * LAYERS.length);
const track = tracksOf(parent)[1]; // the first layer that drifts: the horizon, [0], stands still
const [e0, e1, e2] = track.children;
const first = track.anims[0];
// One chunk of drift, not one viewport: the track slides by the width of a chunk.
expect(first.keyframes[1].transform).toContain(`${-size.chunk / 2}px`);
expect(track.animate).toHaveBeenCalledTimes(1);
first.finish();
expect(first.cancel).toHaveBeenCalled();
expect(track.animate).toHaveBeenCalledTimes(2);
const w = size.chunk / 2;
expect([e1.style.left, e2.style.left, e0.style.left]).toEqual(['0px', `${w}px`, `${2 * w}px`]);
expect(track.anims[1].keyframes[1].transform).toContain(`${-w}px`);
s.dispose();
});
it('the canvas that left is baked with the next stretch of world, and each rotation after it, without end', async () => {
const { parent, s, records } = start();
await until(() => records.length === 3 * LAYERS.length);
const track = tracksOf(parent)[1];
const done = () => records.filter((r) => r.layer === 1).map((r) => r.chunk);
for (let m = 3; m <= 5; m += 1) {
track.anims[track.anims.length - 1].finish();
await until(() => done().includes(m));
}
expect(done()).toEqual([0, 1, 2, 3, 4, 5]);
// The pixels of chunks 3 to 5 are the world's, not a repeat of an earlier one.
const imgs = baked();
const layer1 = imgs.filter((_, i) => records[i].layer === 1);
[3, 4, 5].forEach((m, k) => expect(layer1[3 + k].equals(expected(1, m))).toBe(true));
expect(layer1[3].equals(layer1[0])).toBe(false);
s.dispose();
});
it('with the blur, each chunk is blurred from a scratch canvas of its own width: seams stay opaque and exact', async () => {
const orig = (document as any).createElement;
(document as any).createElement = (tag: string) => {
const el = orig(tag);
el.getContext = vi.fn(() => ctx(true));
return el;
};
const { s, records } = start();
await until(() => records.length === 3 * LAYERS.length);
const want = new Set(
LAYERS.filter((l) => edgeBlur(l) > 0).map(
(l) => size.chunk + 2 * Math.ceil(3 * edgeBlur(l) * 2)
)
);
expect(want.size).toBeGreaterThan(1); // the layers' pads differ, which is what a shared canvas gets wrong
// Every blur reads a source exactly as wide as that chunk's padded bake, at the moment of the draw.
expect(new Set(drawnWidths)).toEqual(want);
s.dispose();
});
});
describe('rain in the scene', () => {
const rainy = (w: number): SkyColours => ({
...DAY,
physical: makePhysicalSky('day', new Date('2026-06-21T10:40:00Z'), w),
physical: makePhysicalSky('day', new Date('2026-06-21T10:40:00Z'), w, FALLBACK_VIEW),
});
const rainEls = (parent: any) =>
parent.children.filter((c: any) => c.tag === 'div' && c !== rigOf(parent));
const rainEls = (parent: any) => parent.children.filter((c: any) => c.tag === 'div');
it('lays one falling tile per rain layer over the ridges when it rains, each slid by its own height on the compositor', () => {
it('lays one falling tile per rain layer over the sky when it rains, each slid by its own height on the compositor', () => {
const { parent, canvas } = host();
const s = createRidgeScene({ canvas, tier: 'full', sky: rainy(1) })!;
const els = rainEls(parent);
@ -860,3 +524,226 @@ describe('rain in the scene', () => {
s.dispose();
});
});
describe('the ridge planes in the scene', () => {
const manifest = parseManifest(wireManifest(), '/d/');
const fakeImg = (src: string) => {
const el = fakeEl('img');
el.src = src;
el.cloneNode = () => fakeImg(src);
return el as unknown as HTMLImageElement;
};
const planes = (k: string) => ({
far: fakeImg(`${k}-far`),
mid: fakeImg(`${k}-mid`),
near: fakeImg(`${k}-near`),
});
const backdropAt = (elevation: number, azimuth: number): Backdrop => {
const choice = frameChoice(manifest.frames, { elevation, azimuth });
return { manifest, choice, a: planes(choice.a.key), b: choice.b ? planes(choice.b.key) : null };
};
const NOON = backdropAt(60, 180);
const BLEND = backdropAt(10, 130);
const night: SkyColours = {
...DAY,
top: [10, 14, 40],
mid: [30, 34, 80],
horizon: [60, 50, 90],
physical: makePhysicalSky('night', new Date('2026-06-21T22:40:00Z'), 1, FALLBACK_VIEW),
};
const rigOf = (parent: any) =>
parent.children.find((c: any) => c.children.some((g: any) => g.anims.length));
it('stacks the rig after the twinkle canvas and before the rain, with no z-index and no fixed position', () => {
const { parent, canvas } = host();
const s = createRidgeScene({ canvas, tier: 'full', sky: night, backdrop: NOON })!;
const kinds = parent.children.map((c: any) => c.tag);
expect(kinds).toEqual(['canvas', 'canvas', 'div', 'div', 'div']);
expect(parent.children[2].children).toHaveLength(3);
parent.children.forEach((c: any) => {
expect(c.style.zIndex).toBeUndefined();
expect(c.style.position).not.toBe('fixed');
});
s.dispose();
expect(parent.children).toEqual([canvas]);
});
it('is three groups, far to near, each placed by windowFor and drifting by its depth of driftPx', () => {
const { parent, canvas } = host();
const s = createRidgeScene({ canvas, tier: 'full', sky: DAY, backdrop: NOON })!;
const rig = rigOf(parent);
const win = windowFor(manifest.view, 400, 800);
expect(rig.children).toHaveLength(3);
rig.children.forEach((g: any, k: number) => {
expect(g.style.width).toBe(`${win.plane.w}px`);
expect(g.style.height).toBe(`${win.plane.h}px`);
expect(g.style.bottom).toBe('0px');
expect(g.style.left).toBe(`${(400 - win.plane.w) / 2}px`);
expect(g.children.map((i: any) => i.src)).toEqual([`noon-${['far', 'mid', 'near'][k]}`]);
const { keyframes, options } = g.anims[0];
const reach = win.driftPx * [0.35, 0.7, 1][k];
expect(keyframes).toEqual([
{ transform: `translate3d(${-reach}px,0,0)` },
{ transform: `translate3d(${reach}px,0,0)` },
]);
expect(options).toMatchObject({
duration: 75000,
iterations: Infinity,
direction: 'alternate',
easing: 'ease-in-out',
});
});
s.dispose();
});
it('crossfades a into b: a at 1 - w, b added at w, in a group of its own', () => {
const { parent, canvas } = host();
const BLEND3 = backdropAt(4, 130); // w = 0.2, not the symmetric half
const s = createRidgeScene({ canvas, tier: 'full', sky: DAY, backdrop: BLEND3 })!;
const { w } = BLEND3.choice;
expect(w).toBeCloseTo(0.2, 6);
const g = rigOf(parent).children[1];
expect(g.style.isolation).toBe('isolate');
expect(
g.children.map((i: any) => [i.src, i.style.opacity, i.style.mixBlendMode ?? null])
).toEqual([
['dawn-mid', String(1 - w), null],
['morning-mid', String(w), 'plus-lighter'],
]);
s.dispose();
});
it('a lone frame is drawn at full opacity with no blend mode', () => {
const { parent, canvas } = host();
const s = createRidgeScene({ canvas, tier: 'full', sky: DAY, backdrop: NOON })!;
const g = rigOf(parent).children[1];
expect(
g.children.map((i: any) => [i.src, i.style.opacity, i.style.mixBlendMode ?? null])
).toEqual([['noon-mid', '1', null]]);
s.dispose();
});
it('adds no rAF or timer callback of its own: the drift is Element.animate, the stars alone loop', () => {
const { canvas } = host();
const s = createRidgeScene({ canvas, tier: 'full', sky: DAY, backdrop: NOON })!;
expect(rafQueue).toHaveLength(1); // the frame sampler, as without planes
s.dispose();
const bare = host();
const b = createRidgeScene({ canvas: bare.canvas, tier: 'full', sky: DAY })!;
expect(rafQueue).toHaveLength(1);
b.dispose();
});
it('pauses the drift under reduced motion, in a hidden tab, and plays it again', () => {
const { parent, canvas } = host();
const s = createRidgeScene({ canvas, tier: 'full', sky: DAY, backdrop: NOON })!;
const anims = rigOf(parent).children.map((g: any) => g.anims[0]);
anims.forEach((a: any) => expect(a.play).toHaveBeenCalled());
anims.forEach((a: any) => a.pause.mockClear());
motion.matches = true;
motion.addEventListener.mock.calls
.filter((c: any[]) => c[0] === 'change')
.forEach((c: any[]) => c[1]());
anims.forEach((a: any) => expect(a.pause).toHaveBeenCalled());
motion.matches = false;
anims.forEach((a: any) => a.pause.mockClear());
doc.visibilityState = 'hidden';
doc.dispatchEvent(new Event('visibilitychange'));
anims.forEach((a: any) => expect(a.pause).toHaveBeenCalled());
s.dispose();
anims.forEach((a: any) => expect(a.cancel).toHaveBeenCalled());
});
it("a still sums each blended plane in an offscreen (a at 1 - w, then b lighter at w) and draws it at windowFor's placement", () => {
const { parent, canvas } = host();
const BLEND3 = backdropAt(4, 130);
createRidgeScene({ canvas, tier: 'still', sky: DAY, backdrop: BLEND3 });
const { w } = BLEND3.choice;
const win = windowFor(manifest.view, 400, 800);
const planesDrawn = images.filter((a) => (a[0] as any).src) as any[];
expect(planesDrawn.map((a) => [a[0].src, a.alpha, a.op])).toEqual(
['far', 'mid', 'near'].flatMap((n) => [
[`dawn-${n}`, 1 - w, 'source-over'],
[`morning-${n}`, w, 'lighter'],
])
);
// Each sum then meets the host canvas once, plainly, at the plane's placement.
const summed = images.filter((a) => !(a[0] as any).src && (a[0] as any).width) as any[];
expect(summed).toHaveLength(3);
summed.forEach((a) => {
expect([a[1], a[2], a[3], a[4]]).toEqual([
(800 - win.plane.w * 2) / 2,
1600 - win.plane.h * 2,
win.plane.w * 2,
win.plane.h * 2,
]);
expect([a.alpha, a.op]).toEqual([1, 'source-over']);
expect([a[0].width, a[0].height]).toEqual([win.plane.w * 2, win.plane.h * 2]);
});
expect(parent.children).toEqual([canvas]);
});
it('a lone frame in a still goes straight to the host canvas at full alpha', () => {
const { canvas } = host();
createRidgeScene({ canvas, tier: 'still', sky: DAY, backdrop: NOON });
const win = windowFor(manifest.view, 400, 800);
const planesDrawn = images.filter((a) => (a[0] as any).src) as any[];
expect(planesDrawn.map((a) => [a[0].src, a.alpha, a.op])).toEqual(
['far', 'mid', 'near'].map((n) => [`noon-${n}`, 1, 'source-over'])
);
const [, x, y, dw, dh] = planesDrawn[0];
expect([x, y, dw, dh]).toEqual([
(800 - win.plane.w * 2) / 2,
1600 - win.plane.h * 2,
win.plane.w * 2,
win.plane.h * 2,
]);
});
it('settles to a still with the planes painted when the frame rate is poor', () => {
const { parent, canvas } = host();
createRidgeScene({ canvas, tier: 'full', sky: DAY, backdrop: NOON });
runFrames(100, 400);
expect(images.filter((a) => (a[0] as any).src)).toHaveLength(3);
expect(parent.children).toEqual([canvas]);
});
it('with no backdrop it is the sky alone', () => {
const { parent, canvas } = host();
createRidgeScene({ canvas, tier: 'full', sky: DAY });
expect(parent.children).toEqual([canvas]);
expect(images.filter((a) => (a[0] as any).src)).toHaveLength(0);
});
});
describe('the scene wiring', () => {
it('sizes from the window when the canvas has no client size', () => {
const { canvas } = host();
canvas.clientWidth = 0;
canvas.clientHeight = 0;
(window as any).innerWidth = 300;
(window as any).innerHeight = 500;
createRidgeScene({ canvas, tier: 'full', sky: DAY })!.dispose();
expect([canvas.width, canvas.height]).toEqual([600, 1000]);
});
it('a burst of resizes rebuilds once, after 250 ms, and not after dispose', async () => {
vi.useFakeTimers();
(window as any).setTimeout = setTimeout;
const { canvas } = host();
const s = createRidgeScene({ canvas, tier: 'full', sky: DAY })!;
const skies = () => draws.filter((d) => d === 'sky').length;
window.dispatchEvent(new Event('resize'));
vi.advanceTimersByTime(200);
window.dispatchEvent(new Event('resize'));
vi.advanceTimersByTime(200);
expect(skies()).toBe(1);
vi.advanceTimersByTime(60);
expect(skies()).toBe(2);
s.dispose();
window.dispatchEvent(new Event('resize'));
vi.advanceTimersByTime(1000);
expect(skies()).toBe(2);
vi.useRealTimers();
});
});

View file

@ -1,32 +1,14 @@
/* eslint-disable no-param-reassign -- the bake writes into the canvases and layer records it is handed */
import type { Scene, SceneOptions } from '../Scene';
import { overlayRequested } from '../debugOverlays';
import { FrameSampler, FrameStats } from './probe';
import { skyColours, SkyColours, Srgb } from './skyColour';
import { paintSky as paintPhysicalSky } from './physicalSky';
import { windowFor } from './view';
import { crossfade } from './frameChoice';
import { activeAt, drawStars, drawTwinkle as paintTwinkle, makeStars, Star } from './stars';
import { bakeSlice, BakeParams, layerPaint } from './ridgeBake';
import {
LAYERS,
LayerSpec,
edgeBlur,
HORIZON_TRACK_SHIFT,
layerSize,
scatterAlpha,
transmittance,
} from './ridgeWorld';
import { hfovDeg } from './horizon';
import { viewFor, VFOV_DEG } from './skyDome';
import { drawRainTile, RAIN_TILES } from './rain';
/** One chunk's bake: `ms` is the summed task time, `maxTaskMs` the longest single task; `first` marks the bake of the first view. */
export interface BakeRecord {
layer: number;
chunk: number;
ms: number;
maxTaskMs: number;
first: boolean;
}
import type { Backdrop, Planes } from './Backdrop';
import { PLANE_NAMES } from './manifest';
export type RidgeOptions = Pick<SceneOptions, 'canvas' | 'tier'> & {
/** Show the rAF median/p95 readout. Default: the page URL has `?probe=1`, where `debug` allows it. */
@ -37,8 +19,8 @@ export type RidgeOptions = Pick<SceneOptions, 'canvas' | 'tier'> & {
onProbe?: (s: FrameStats & { still: boolean }) => void;
/** The sky's colours; default `skyColours()`, the one function to replace with Void's sun.js. */
sky?: SkyColours;
/** Called after each chunk is baked (a chunk is one layer's stretch of world-x): the cost record. */
onBake?: (b: BakeRecord) => void;
/** The baked ridge planes, already decoded; none is the sky-only scene (`view.ts` FALLBACK_VIEW). */
backdrop?: Backdrop;
};
const css = (c: Srgb): string => `rgb(${c[0]},${c[1]},${c[2]})`;
@ -79,7 +61,12 @@ function paintSky(
const ctx = context2d(canvas);
if (!ctx) return false;
if (sky.physical) {
const px = paintPhysicalSky(sky.physical, cssW, cssH);
const px = paintPhysicalSky(
sky.physical,
cssW,
cssH,
windowFor(sky.physical.view, cssW, cssH).camera
);
const small = makeCanvas();
small.width = px.w;
small.height = px.h;
@ -107,217 +94,26 @@ function paintSky(
const clock = (): number => (typeof performance !== 'undefined' ? performance.now() : Date.now());
type Size = ReturnType<typeof layerSize>;
const SCATTER_STOPS = 16;
/** Warm in-scatter over the silhouette only (source-atop): a horizontal gradient over the chunk's world-x. */
function scatter(
ctx: CanvasRenderingContext2D,
spec: LayerSpec,
sky: SkyColours,
widthPx: number,
heightPx: number,
t0: number,
tPerPx: number,
bandX = 0,
bandW = widthPx
) {
if (sky.sunAzimuth === undefined || transmittance(spec) >= 1) return; // the nearest layer takes none
const g = ctx.createLinearGradient(0, 0, widthPx, 0);
const [r, gr, b] = sky.horizon;
for (let k = 0; k <= SCATTER_STOPS; k += 1) {
const a = scatterAlpha(spec, sky, t0 + (tPerPx * widthPx * k) / SCATTER_STOPS);
g.addColorStop(k / SCATTER_STOPS, `rgba(${r},${gr},${b},${a})`);
}
ctx.globalCompositeOperation = 'source-atop';
ctx.fillStyle = g;
ctx.fillRect(bandX, 0, bandW, heightPx);
ctx.globalCompositeOperation = 'source-over';
}
/** A chunk's bake in slices: `step(budgetMs)` returns true once the chunk is on its canvas. */
interface Job {
step(budgetMs: number): boolean;
}
/**
* One layer's stretch of world-x `[m * chunk, (m + 1) * chunk)` painted onto `el`, in three phases of
* column bands, each band sized to the task budget by measuring the last one: bake the pixels
* (`bakeSlice`), put them on a canvas with the in-scatter, and (where the 2D `filter` exists) blur them
* into `el`. The blur needs neighbours, so the pixels are baked `pad` columns past both ends and
* the bottom into a scratch canvas this job owns, and each blurred band reads `pad` columns either side of
* itself: seams show no fade. The scratch is made when the job first runs and dropped when it ends.
*/
function chunkJob(
el: HTMLCanvasElement,
makeCanvas: () => HTMLCanvasElement,
spec: LayerSpec,
sky: SkyColours,
size: Size,
viewH: number,
scale: number,
m: number,
record: (ms: number, maxTaskMs: number) => void
): Job | null {
const ctx = context2d(el);
if (!ctx) return null;
const blur = edgeBlur(spec) * scale;
const pad = blur > 0 && 'filter' in ctx ? Math.ceil(3 * blur) : 0;
const cols = size.chunk + 2 * pad;
const rows = size.height + pad;
const params: BakeParams = {
spec,
sky,
horizonView:
spec.horizon && sky.physical
? {
azimuthDeg: viewFor(sky.physical.sun, size.period / viewH).azimuthDeg,
hfovDeg: hfovDeg(size.period / viewH, VFOV_DEG),
}
: undefined,
period: size.period,
scale,
viewH,
rows,
bandH: size.height,
px0: m * size.chunk - pad,
cols,
};
const paint = layerPaint(spec, sky);
const t0World = params.px0 / size.period;
let scratch: HTMLCanvasElement | undefined;
let sctx: CanvasRenderingContext2D | null = ctx;
let img: ImageData | undefined;
/** 0 bake, 1 put and in-scatter, 2 blur into el; a chunk with no blur ends after 1. */
let phase = 0;
let pos = 0;
let sliceCols = 32;
let total = 0;
let longest = 0;
const last = pad > 0 ? 2 : 1;
const start = (): boolean => {
if (pad > 0) {
scratch = makeCanvas();
scratch.width = cols;
scratch.height = rows;
sctx = context2d(scratch);
} else {
el.width = size.chunk;
el.height = size.height;
}
if (!sctx) return false;
img = sctx.createImageData(cols, rows);
return true;
};
const work = (x: number, n: number) => {
if (!img || !sctx) return;
if (phase === 0) bakeSlice(img.data, params, paint, x, x + n);
else if (phase === 1) {
sctx.putImageData(img, 0, 0, x, 0, n, rows);
scatter(sctx, spec, sky, cols, rows, t0World, 1 / size.period, x, n);
} else {
// One draw: a blurred draw cut into bands costs more in the margins than it saves in task length.
ctx.drawImage(scratch as HTMLCanvasElement, -pad, 0);
}
};
return {
step(budgetMs) {
const t0 = clock();
// Allocating the canvases and the pixel buffer is a task of its own.
if (!img) {
if (!start()) return true;
if (Number.isFinite(budgetMs)) {
total += clock() - t0;
longest = Math.max(longest, clock() - t0);
return false;
}
}
while (phase <= last && clock() - t0 < budgetMs) {
const end = phase === 2 ? size.chunk : cols;
if (pos === 0 && phase === 2) {
el.width = size.chunk;
el.height = size.height;
ctx.filter = `blur(${blur}px)`;
}
const n = phase === 2 ? end : Math.min(end - pos, sliceCols);
const s0 = clock();
work(pos, n);
pos += n;
const dt = Math.max(clock() - s0, 0.02);
sliceCols = Number.isFinite(budgetMs)
? Math.max(8, Math.min(512, Math.round((n * budgetMs * 0.4) / dt)))
: cols;
if (pos >= end) {
if (phase === 2) ctx.filter = 'none';
phase += 1;
pos = 0;
if (phase === 1 || phase === 2) sliceCols = Number.isFinite(budgetMs) ? 64 : cols;
}
}
const dt = clock() - t0;
total += dt;
longest = Math.max(longest, dt);
const done = phase > last;
if (done) {
img = undefined;
if (scratch) {
scratch.width = 0;
scratch.height = 0;
}
scratch = undefined;
record(total, longest);
}
return done;
},
};
}
interface Chunk {
el: HTMLCanvasElement;
/** World chunk index it holds or is being baked to hold. */
id: number;
job?: Job;
ready: boolean;
}
/** One layer: a track that drifts by one chunk per animation, holding three chunk canvases. */
interface LayerState {
spec: LayerSpec;
track: HTMLElement;
/** Slot 0 and 1 are what the window can see; slot 2 is the one ahead, baked in the background. */
slots: Chunk[];
anim?: Animation;
}
interface Built {
rig: HTMLElement;
layers: LayerState[];
sway?: Animation;
size: Size;
/** CSS px to canvas px, and the viewport's height in canvas px, as baked. */
scale: number;
viewH: number;
/** Makes a canvas, bound to the document the scene was built in. */
makeCanvas: () => HTMLCanvasElement;
/** The twinkling stars' own canvas, between the sky and the ridges; absent in a day sky and in 'still'. */
/** The twinkling stars' own canvas, over the sky; absent in a day sky and in 'still'. */
twinkle?: HTMLCanvasElement;
/** The ridge planes' rig, over the twinkle canvas and under the rain; absent in 'still' (the planes are drawn into the host canvas). */
rig?: { el: HTMLElement; anims: (Animation | undefined)[] };
/** The rain's tiles and their falling, present only when the weather rains (`rain.ts`). */
rain?: { els: HTMLElement[]; anims: (Animation | undefined)[] };
}
/** Yaw and pitch: the camera's slow sway, a loop of this many seconds over the whole rig. */
const SWAY_SECONDS = 150;
const SLICE_BUDGET_MS = 6;
/** The twinkle overlay redraws at most this often: the swings are 2 to 7 s, no need for 60 fps. */
const TWINKLE_FRAME_MS = 50;
/** Far, mid and near slide this share of the drift room (`windowFor` driftPx); one leg of the back-and-forth takes DRIFT_LEG_S, so the loop is 150 s. Authored, tunable by eye. */
const DRIFT_DEPTH = [0.35, 0.7, 1] as const;
const DRIFT_LEG_S = 75;
/**
* The ridge scene: same contract as `createScene` ({ login, dispose }, null for "keep the CSS
* ground"), no WebGL. The layers are painted once on the CPU; per frame the only work is the
* browser compositor sliding each layer with a CSS transform (a Web Animation, no script). The host
* canvas holds the baked sky; it and the layers' parent must be positioned, and the canvas should fill it.
* ground"), no WebGL. It draws the sky, the stars, the baked ridge planes when given a `backdrop` (none: the sky alone) and the rain. The host canvas holds the baked sky; it and
* its parent must be positioned, and the canvas should fill the parent.
*/
export function createRidgeScene(opts: RidgeOptions): Scene | null {
const tier = opts.tier ?? 'full';
@ -334,9 +130,6 @@ export function createRidgeScene(opts: RidgeOptions): Scene | null {
let twRaf = 0;
let twLast = 0;
let still = tier === 'still';
let jobTimer: ReturnType<typeof setTimeout> | undefined;
let queue: Chunk[] = [];
let generation = 0;
let disposed = false;
let raf = 0;
let readout: HTMLElement | undefined;
@ -349,15 +142,10 @@ export function createRidgeScene(opts: RidgeOptions): Scene | null {
return { w, h, scale: (window.devicePixelRatio || 1) >= 2 ? 2 : 1 };
};
const teardownLayers = () => {
generation += 1;
clearTimeout(jobTimer);
jobTimer = undefined;
queue = [];
built?.sway?.cancel();
built?.layers.forEach((l) => l.anim?.cancel());
built?.rig.remove();
const teardown = () => {
built?.twinkle?.remove();
built?.rig?.anims.forEach((a) => a?.cancel());
built?.rig?.el.remove();
built?.rain?.anims.forEach((a) => a?.cancel());
built?.rain?.els.forEach((e) => e.remove());
cancelAnimationFrame(twRaf);
@ -368,111 +156,7 @@ export function createRidgeScene(opts: RidgeOptions): Scene | null {
/** True while there is nothing to paint on: the canvas has no client size and the window itself is 0 x 0 (`viewSize` falls back to the window). */
const hasNoArea = () => {
const { w, h, scale } = viewSize();
const size = layerSize(w, h, scale);
return size.period < 1 || size.height < 1 || Math.round(h * scale) < 1;
};
/**
* Start baking chunk `c` of layer `l`. `first` runs it to the end in this task (the first view);
* otherwise it joins the queue, which `pump` works through in short tasks. False when the canvas has no 2D context.
*/
const bake = (l: LayerState, c: Chunk, first: boolean): boolean => {
if (!built) return false;
const { scale, viewH, makeCanvas } = built;
const job = chunkJob(
c.el,
makeCanvas,
l.spec,
sky,
built.size,
viewH,
scale,
c.id,
(ms, maxTaskMs) => {
c.ready = true;
c.job = undefined;
opts.onBake?.({ layer: l.spec.index, chunk: c.id, ms, maxTaskMs, first });
}
);
if (!job) return false;
c.ready = false;
c.job = job;
if (first) job.step(Infinity);
else queue.push(c);
return true;
};
const pump = () => {
if (jobTimer !== undefined || !queue.length) return;
const gen = generation;
jobTimer = setTimeout(() => {
jobTimer = undefined;
if (gen !== generation) return;
const c = queue[0];
if (!c.job || c.job.step(SLICE_BUDGET_MS)) queue.shift();
pump();
}, 0);
};
/** Bake a still-queued chunk to the end now: only when the drift has caught up with the background work. */
const flush = (c: Chunk) => {
while (c.job && !c.job.step(Infinity));
queue = queue.filter((q) => q !== c);
};
const chunkSeconds = (spec: LayerSpec, size: Size) => (spec.seconds * size.chunk) / size.period;
/** One chunk of drift as one animation, holding its end until `rotate` hands the track the next chunk. */
const startDrift = (l: LayerState, onEnd: (l: LayerState) => void) => {
if (!built || !Number.isFinite(l.spec.seconds)) return; // the horizon layer does not drift
const { size, scale } = built;
l.anim = l.track.animate?.(
[
{ transform: 'translate3d(0,0,0)' },
{ transform: `translate3d(${-size.chunk / scale}px,0,0)` },
],
{ duration: chunkSeconds(l.spec, size) * 1000, easing: 'linear', fill: 'forwards' }
);
if (l.anim) l.anim.onfinish = () => onEnd(l);
};
/**
* The window has drifted one chunk. Slot 1 is now slot 0 and slot 2 slot 1, at the same screen place
* (the track restarts at 0 and the chunks move one slot left in the same task, so nothing jumps); the
* canvas that left becomes slot 2 and is baked with the world one chunk further on, off the critical path.
*/
const rotate = (l: LayerState) => {
if (!built || !built.layers.includes(l)) return;
const [s0, s1, s2] = l.slots;
flush(s1);
flush(s2);
l.slots = [s1, s2, s0];
s0.id = s2.id + 1;
const { size, scale } = built;
l.slots.forEach((c, i) => {
c.el.style.left = `${(i * size.chunk) / scale}px`;
});
l.anim?.cancel();
startDrift(l, rotate);
bake(l, s0, false);
pump();
syncPlayState();
};
/** The camera's slow yaw and pitch: the rig eases sideways (up to 5% of the width) and down (up to 3% of the height) and back, in a loop. */
const startSway = (rig: HTMLElement, w: number, h: number) => {
const x = 0.05 * w;
const y = 0.03 * h;
const at = (fx: number, fy: number) => `translate3d(${-x * fx}px,${y * fy}px,0)`;
return rig.animate?.(
[
{ transform: at(0, 0), offset: 0 },
{ transform: at(0.55, 1), offset: 0.35 },
{ transform: at(1, 0.3), offset: 0.7 },
{ transform: at(0, 0), offset: 1 },
],
{ duration: SWAY_SECONDS * 1000, iterations: Infinity, easing: 'ease-in-out' }
);
return Math.round(w * scale) < 1 || Math.round(h * scale) < 1;
};
/**
@ -525,14 +209,115 @@ export function createRidgeScene(opts: RidgeOptions): Scene | null {
return { els, anims };
};
/** Bake the sky, then the layers. Returns false when a 2D context is missing or there is no area yet. */
/**
* The ridge over the sky: one div holding a group per plane (far, mid, near: DOM order is paint order, stacking is by order alone), each the frame's
* image and, when the choice blends two frames, the second over it at the blend weight. A group slides sideways on the compositor by
* `driftPx` times its depth; the baker's pixels are never touched.
*/
const startRig = (bd: Backdrop, w: number, h: number): NonNullable<Built['rig']> => {
const win = windowFor(bd.manifest.view, w, h);
const el = document.createElement('div');
el.setAttribute('aria-hidden', 'true');
Object.assign(el.style, {
position: 'absolute',
left: '0',
top: '0',
width: '100%',
height: '100%',
overflow: 'hidden',
pointerEvents: 'none',
});
const anims: (Animation | undefined)[] = [];
PLANE_NAMES.forEach((name, k) => {
const group = document.createElement('div');
Object.assign(group.style, {
position: 'absolute',
left: `${(w - win.plane.w) / 2}px`,
bottom: `${win.plane.bottom}px`,
width: `${win.plane.w}px`,
height: `${win.plane.h}px`,
willChange: 'transform',
});
const layer = (planes: Planes, opacity: number, blend?: string) => {
const img = planes[name].cloneNode() as HTMLImageElement;
Object.assign(img.style, {
position: 'absolute',
left: '0',
top: '0',
width: '100%',
height: '100%',
opacity: String(opacity),
});
if (blend) img.style.mixBlendMode = blend;
group.appendChild(img);
};
// One rule (`crossfade`): a at 1 - w, b added to it at w; the group is its own isolated backdrop, so the sum meets the sky once.
const fade = crossfade(bd.choice);
group.style.isolation = 'isolate';
layer(bd.a, fade.a);
if (bd.b) layer(bd.b, fade.b, 'plus-lighter');
el.appendChild(group);
const reach = win.driftPx * DRIFT_DEPTH[k];
anims.push(
group.animate?.(
[
{ transform: `translate3d(${-reach}px,0,0)` },
{ transform: `translate3d(${reach}px,0,0)` },
],
{
duration: DRIFT_LEG_S * 1000,
iterations: Infinity,
direction: 'alternate',
easing: 'ease-in-out',
}
)
);
});
parent.appendChild(el);
return { el, anims };
};
/** Paint the ridge planes into the host canvas, a and b crossfaded, far to near: the still scene's one image. */
const paintPlanes = (bd: Backdrop) => {
const ctx = context2d(canvas);
if (!ctx) return;
const { w, h, scale } = viewSize();
const win = windowFor(bd.manifest.view, w, h);
const dw = win.plane.w * scale;
const dh = win.plane.h * scale;
const x = (canvas.width - dw) / 2;
const y = canvas.height - dh - win.plane.bottom * scale;
const fade = crossfade(bd.choice);
// A blend is summed in an offscreen of its own, as the rig sums it in an isolated group: 'lighter' on the host canvas would add to the sky.
const sum = bd.b ? document.createElement('canvas') : null;
if (sum) {
sum.width = dw;
sum.height = dh;
}
const sctx = sum ? context2d(sum) : null;
PLANE_NAMES.forEach((name) => {
if (!bd.b || !sum || !sctx) {
ctx.drawImage(bd.a[name], x, y, dw, dh);
return;
}
sctx.clearRect(0, 0, dw, dh);
sctx.globalCompositeOperation = 'source-over';
sctx.globalAlpha = fade.a;
sctx.drawImage(bd.a[name], 0, 0, dw, dh);
sctx.globalCompositeOperation = 'lighter';
sctx.globalAlpha = fade.b;
sctx.drawImage(bd.b[name], 0, 0, dw, dh);
ctx.drawImage(sum, x, y, dw, dh);
});
};
/** Paint the sky, the stars and the rain. Returns false when a 2D context is missing or there is no area yet. */
const build = (): boolean => {
teardownLayers();
teardown();
// A zero-size canvas throws on drawImage; wait for the first resize that gives the host an area.
if (hasNoArea()) return false;
const { w, h, scale } = viewSize();
const size = layerSize(w, h, scale);
canvas.width = size.period;
canvas.width = Math.round(w * scale);
canvas.height = Math.round(h * scale);
if (!paintSky(canvas, sky, w, h, () => document.createElement('canvas'))) return false;
// Every star is baked into the sky; the twinkling ones get their own canvas, which a per-frame
@ -557,75 +342,10 @@ export function createRidgeScene(opts: RidgeOptions): Scene | null {
parent.appendChild(twinkle);
}
field = stars;
// The rig carries the camera's sway; each layer's track carries its drift; chunks sit on the track.
const rig = document.createElement('div');
rig.setAttribute('aria-hidden', 'true');
Object.assign(rig.style, {
position: 'absolute',
left: '0',
bottom: '0',
width: '100%',
height: '100%',
pointerEvents: 'none',
willChange: 'transform',
});
const layers: LayerState[] = LAYERS.map((spec) => {
const track = document.createElement('div');
Object.assign(track.style, {
position: 'absolute',
left: spec.horizon ? `${(-HORIZON_TRACK_SHIFT * size.period) / scale}px` : '0',
bottom: '0',
width: `${size.period / scale}px`,
height: `${size.height / scale}px`,
willChange: 'transform',
});
const slots: Chunk[] = [0, 1, 2].map((i) => {
const el = document.createElement('canvas');
el.width = size.chunk;
el.height = size.height;
Object.assign(el.style, {
position: 'absolute',
bottom: '0',
width: `${size.chunk / scale}px`,
height: `${size.height / scale}px`,
});
return { el, id: i, ready: false };
});
return { spec, track, slots };
});
const doc = document;
built = {
rig,
layers,
size,
scale,
viewH: canvas.height,
makeCanvas: () => doc.createElement('canvas'),
twinkle,
};
// First view: only slot 0 of each layer, in this task. The window sees nothing else for the first
// fifth of a chunk's drift; slots 1 and 2 are baked in short tasks behind it.
if (!layers.every((l) => bake(l, l.slots[0], true))) {
teardownLayers();
return false;
}
layers.forEach((l) => {
l.slots.forEach((c, i) => {
c.el.setAttribute('aria-hidden', 'true');
c.el.style.left = `${(i * size.chunk) / scale}px`;
l.track.appendChild(c.el);
});
rig.appendChild(l.track);
bake(l, l.slots[1], false);
bake(l, l.slots[2], false);
});
// DOM order is paint order: farthest first, so the nearest ridge ends up on top.
parent.appendChild(rig);
layers.forEach((l) => startDrift(l, rotate));
built.sway = startSway(rig, w, h);
built = { twinkle };
if (opts.backdrop && !still) built.rig = startRig(opts.backdrop, w, h);
if (tier !== 'still' && sky.physical && sky.physical.weather.rain > 0)
built.rain = startRain(sky.physical.weather.rain, sky.horizon, w, h, scale);
pump();
syncPlayState();
return true;
};
@ -658,9 +378,7 @@ export function createRidgeScene(opts: RidgeOptions): Scene | null {
};
function syncPlayState() {
const anims = built
? [built.sway, ...built.layers.map((l) => l.anim), ...(built.rain?.anims ?? [])]
: [];
const anims = [...(built?.rig?.anims ?? []), ...(built?.rain?.anims ?? [])];
anims.forEach((a) => (moving() ? a?.play() : a?.pause()));
if (!built?.twinkle) return;
if (moving()) {
@ -672,26 +390,14 @@ export function createRidgeScene(opts: RidgeOptions): Scene | null {
}
}
/** Flatten the sky and layers into the host canvas and drop the layers: a still image, no compositor layers left. */
/** Drop the twinkle and the rain, leaving the painted sky as a still image. */
const settleToStill = () => {
if (still && !built) return;
still = true;
const ctx = context2d(canvas);
if (ctx && built) {
const { size } = built;
built.layers.forEach((l) => {
// Freeze where the drift is, so the swap to a still does not jump; whatever is baked and in view.
const dur = chunkSeconds(l.spec, size) * 1000;
const p = Math.min(1, Math.max(0, Number(l.anim?.currentTime ?? 0) / dur));
const x = -Math.round(p * size.chunk);
l.slots.slice(0, 2).forEach((c, i) => {
if (c.ready) ctx.drawImage(c.el, x + i * size.chunk, canvas.height - size.height);
});
});
}
teardownLayers();
teardown();
cancelAnimationFrame(raf);
raf = 0;
if (opts.backdrop) paintPlanes(opts.backdrop);
report(true);
};
@ -773,7 +479,7 @@ export function createRidgeScene(opts: RidgeOptions): Scene | null {
return {
login() {
// No camera to pull back; the ridges are the same before and after.
// No camera to pull back; the sky is the same before and after.
},
dispose() {
if (disposed) return;
@ -783,7 +489,7 @@ export function createRidgeScene(opts: RidgeOptions): Scene | null {
document.removeEventListener('visibilitychange', onVisibility);
window.removeEventListener('resize', onResize);
reducedMotion?.removeEventListener?.('change', syncPlayState);
teardownLayers();
teardown();
readout?.remove();
},
};

View file

@ -1,11 +1,13 @@
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { describe, expect, it } from 'vitest';
import { HORIZON_CREDIT, WEATHER_CREDIT, WEATHER_CREDIT_URL, WEATHER_DATASET } from './credits';
import { HORIZON_ATTRIBUTION } from './horizon';
import { WEATHER_CREDIT, WEATHER_CREDIT_URL, WEATHER_DATASET, terrainCredit } from './credits';
import { parseIndex, parseManifest } from './manifest';
import { wireManifest } from './manifest.fixture';
import { WEATHER_ATTRIBUTION } from './weatherFeed';
describe('the credits the Sky menu shows', () => {
it("are the sources' own", () => {
expect(HORIZON_CREDIT).toBe(HORIZON_ATTRIBUTION);
expect(WEATHER_CREDIT).toBe(WEATHER_ATTRIBUTION);
});
it('credit GeoSphere with its requested line, and state a licence only with the dataset named', () => {
@ -14,3 +16,26 @@ describe('the credits the Sky menu shows', () => {
expect(WEATHER_DATASET).toMatch(/TAWES.*10-minute.*CC BY 4\.0/);
});
});
describe('the terrain credit', () => {
const shipped = (): { attribution: { credit_line: string; licence: string } } | undefined => {
const root = join(process.cwd(), 'public', 'ridge');
const index = parseIndex(JSON.parse(readFileSync(join(root, 'index.json'), 'utf8')));
return JSON.parse(
readFileSync(join(root, index.places[index.default], 'manifest.json'), 'utf8')
);
};
it("is the shipped manifest's attribution, credit line then licence", () => {
const wire = shipped();
const manifest = parseManifest(wireManifest({ attribution: wire?.attribution }), '/d/');
expect(terrainCredit(manifest)).toBe(
`${wire?.attribution.credit_line} ${wire?.attribution.licence}`
);
expect(terrainCredit(manifest)).toMatch(/Copernicus/);
});
it('is absent without a manifest', () => {
expect(terrainCredit(undefined)).toBeUndefined();
});
});

View file

@ -1,11 +1,16 @@
// What the Ridge background owes its data sources, shown in the Sky menu (SkyStateControl). Plain strings in their own module so the
// main chunk carries these and not the baked horizon table or the weather parser; credits.test.ts holds them equal to the sources'.
// main chunk carries these and not the weather parser; credits.test.ts holds them equal to the sources'. The terrain credit is the loaded
// manifest's own `attribution` (terrainCredit): the baker writes it, so no constant here can drift from the bake.
import type { RidgeManifest } from './manifest';
/** GeoSphere's own requested credit line. It names no licence: that is the dataset's (below). */
export const WEATHER_CREDIT = 'Datenquelle: GeoSphere Austria - https://data.hub.geosphere.at';
export const WEATHER_CREDIT_URL = 'https://data.hub.geosphere.at';
/** The dataset the weather is read from, and its licence as the dataset page names it (data.hub.geosphere.at, tawes-v1-10min). */
export const WEATHER_DATASET = 'TAWES weather stations, 10-minute data (station 11035), CC BY 4.0';
/** The terrain silhouette's credit, as the Copernicus DEM licence asks (equal to `HORIZON_ATTRIBUTION`). */
export const HORIZON_CREDIT =
'Contains modified Copernicus data: DEM © DLR e.V. 2010-2014 and © Airbus Defence and Space GmbH 2014-2018 provided under COPERNICUS by the European Union and ESA; all rights reserved.';
/** The terrain's credit line and licence as the loaded bake states them; undefined until a manifest has loaded (no manifest, no line). */
export const terrainCredit = (
manifest: Pick<RidgeManifest, 'attribution'> | undefined
): string | undefined =>
manifest ? `${manifest.attribution.creditLine} ${manifest.attribution.licence}` : undefined;

View file

@ -0,0 +1,117 @@
import { describe, expect, it } from 'vitest';
import { BLEND_EDGE, crossfade, frameChoice } from './frameChoice';
import { parseManifest } from './manifest';
import { wireManifest } from './manifest.fixture';
const { frames } = parseManifest(wireManifest(), '/d/');
const at = (elevation: number, azimuth: number) => frameChoice(frames, { elevation, azimuth });
const keys = (c: ReturnType<typeof at>) => [c.a.key, c.b?.key ?? null];
describe('frameChoice', () => {
it('blends continuously with elevation inside a half', () => {
const ws = [3, 6, 9, 12, 15, 18].map((el) => at(el, 130).w);
ws.forEach((w, i) => i && expect(w).toBeGreaterThan(ws[i - 1]));
});
it('weights by the elevation between the bracketing keys, a at the lower', () => {
// Morning half: night -12, dawn 0, morning 20, noon 60. Sun 10 deg is half way from dawn to morning.
const c = at(10, 130);
expect(keys(c)).toEqual(['dawn', 'morning']);
expect(c.w).toBeCloseTo(0.5, 6);
});
it('uses the afternoon keys after the noon azimuth and the morning keys before', () => {
expect(keys(at(10, 130))).toEqual(['dawn', 'morning']);
expect(keys(at(-1, 250))).toEqual(['dusk', 'golden']);
expect(at(-1, 250).w).toBeCloseTo(1.5 / 8.5, 6);
});
it('noon is the same single frame from both halves', () => {
expect(keys(at(60, 170))).toEqual(['noon', null]);
expect(keys(at(60, 190))).toEqual(['noon', null]);
expect(keys(at(75, 190))).toEqual(['noon', null]);
});
it('is the night key alone at or below its elevation, in either half', () => {
expect(keys(at(-12, 10))).toEqual(['night', null]);
expect(keys(at(-40, 300))).toEqual(['night', null]);
});
it('blends night into dawn before sunrise and dusk into night after sunset', () => {
expect(keys(at(-6, 90))).toEqual(['night', 'dawn']);
expect(keys(at(-6, 270))).toEqual(['night', 'dusk']);
});
it('collapses a blend within 2% of either end to one frame, and w stays in 0..1', () => {
expect(keys(at(0.2, 130))).toEqual(['dawn', null]); // w = 0.01
expect(keys(at(19.8, 130))).toEqual(['morning', null]); // w = 0.99
const els = Array.from({ length: 325 }, (_, i) => -30 + i * 0.37);
els.forEach((el) =>
[0, 100, 179, 181, 270].forEach((az) => {
const c = at(el, az);
expect(c.w).toBeGreaterThanOrEqual(0);
expect(c.w).toBeLessThanOrEqual(1);
expect(c.b === null).toBe(c.w === 0);
})
);
});
it("splits the halves at the noon frame's azimuth, wherever the bake put it", () => {
const w = wireManifest() as any;
const shifted = parseManifest(
{
...w,
frames: w.frames.map((f: any) =>
f.key === 'noon' ? { ...f, sun: { ...f.sun, az: 170 } } : f
),
},
'/d/'
);
const pick = (az: number) => frameChoice(shifted.frames, { elevation: 10, azimuth: az });
expect([pick(169).a.key, pick(169).b?.key]).toEqual(['dawn', 'morning']);
expect([pick(171).a.key, pick(171).b?.key]).toEqual(['golden', 'noon']);
});
it('collapses exactly at BLEND_EDGE and no further: a blend of 3% still has two frames', () => {
expect(BLEND_EDGE).toBe(0.02);
// Morning half dawn 0 .. morning 20: w = el / 20.
expect(keys(at(0.6, 130))).toEqual(['dawn', 'morning']); // w = 0.03
expect(keys(at(19.4, 130))).toEqual(['dawn', 'morning']); // w = 0.97
expect(keys(at(0.2, 130))).toEqual(['dawn', null]); // w = 0.01
expect(keys(at(19.8, 130))).toEqual(['morning', null]); // w = 0.99
});
});
describe('crossfade: the one rule of a blend', () => {
// A baked plane is haze as alpha: no pixel opaque, the same alpha in every frame. Frames as (premultiplied) colour and alpha.
const planeA = { c: 0.2, alpha: 0.6 };
const planeB = { c: 0.5, alpha: 0.6 };
const sum = (c: ReturnType<typeof at>) => {
const f = crossfade(c);
const b = c.b ? planeB : { c: 0, alpha: 0 };
return { c: planeA.c * f.a + b.c * f.b, alpha: planeA.alpha * f.a + b.alpha * f.b };
};
it('is a lerp of the two frames: colour and haze alike, never denser than either', () => {
[0.03, 0.2, 0.5, 0.8, 0.97].forEach((w) => {
const c = { a: frames[0], b: frames[1], w };
const got = sum(c);
expect(got.c).toBeCloseTo(planeA.c * (1 - w) + planeB.c * w, 12);
expect(got.alpha).toBeCloseTo(0.6, 12); // equal alphas stay equal: the haze is not doubled
expect(got.alpha).toBeLessThanOrEqual(Math.max(planeA.alpha, planeB.alpha) + 1e-12);
});
});
it('has no jump where the choice collapses: the blend either side of an edge is within the edge of the lone frame', () => {
const gap = Math.abs(planeA.c - planeB.c);
const high = sum({ a: frames[0], b: frames[1], w: 1 - BLEND_EDGE });
const low = sum({ a: frames[0], b: frames[1], w: BLEND_EDGE });
expect(Math.abs(high.c - planeB.c)).toBeLessThanOrEqual(BLEND_EDGE * gap + 1e-12); // collapse shows b alone
expect(Math.abs(low.c - planeA.c)).toBeLessThanOrEqual(BLEND_EDGE * gap + 1e-12); // collapse shows a alone
expect(high.alpha).toBeCloseTo(planeB.alpha, 12);
});
it('a lone frame is full weight alone', () => {
expect(crossfade({ a: frames[0], b: null, w: 0 })).toEqual({ a: 1, b: 0 });
});
});

View file

@ -0,0 +1,55 @@
// Which baked frames the live sun lies between, and how far. Pure. The frames are ordered by the sun elevation they were baked for, a
// morning half rising from the night key through dawn and morning to noon, an afternoon half falling from noon through golden and dusk
// to night; the two halves share noon and the night key. The half is the live sun's side of the noon frame's azimuth.
import type { Frame, FrameKey } from './manifest';
export interface FrameChoice {
/** The frame shown alone when b is null, else weighted 1 - w. */
a: Frame;
/** The frame crossfaded in at `w`, or null when one frame stands alone. */
b: Frame | null;
/** Weight of b in 0..1; 0 when b is null. */
w: number;
}
/** Blends this close to either frame are not worth a second image. */
export const BLEND_EDGE = 0.02;
/**
* The one rule of a blend, for the rig and the still alike: a true crossfade, a at 1 - w plus b at w, summed (premultiplied) in a group of
* their own. The baked planes carry haze as alpha (no pixel is opaque), so b laid over a at opacity w would double the haze mid-blend
* and jump when the choice collapses to b alone; the sum is a lerp of the two frames and never denser than either.
*/
export function crossfade(choice: FrameChoice): { a: number; b: number } {
return choice.b ? { a: 1 - choice.w, b: choice.w } : { a: 1, b: 0 };
}
const MORNING: readonly FrameKey[] = ['night', 'dawn', 'morning', 'noon'];
const AFTERNOON: readonly FrameKey[] = ['night', 'dusk', 'golden', 'noon'];
export function frameChoice(
frames: readonly Frame[],
sun: { elevation: number; azimuth: number }
): FrameChoice {
const by = (k: FrameKey): Frame => {
const f = frames.find((x) => x.key === k);
if (!f) throw new Error(`frameChoice: no ${k} frame`);
return f;
};
const noon = by('noon');
const half = (sun.azimuth < noon.sun.az ? MORNING : AFTERNOON).map(by);
// Ascending in elevation along the half, whatever the bake's numbers: sort so a hand-made manifest cannot invert the order.
half.sort((p, q) => p.sun.el - q.sun.el);
const lowest = half[0];
const highest = half[half.length - 1];
if (sun.elevation <= lowest.sun.el) return { a: lowest, b: null, w: 0 };
if (sun.elevation >= highest.sun.el) return { a: highest, b: null, w: 0 };
let i = 0;
while (half[i + 1].sun.el < sun.elevation) i += 1;
const lo = half[i];
const hi = half[i + 1];
const w = (sun.elevation - lo.sun.el) / (hi.sun.el - lo.sun.el);
if (w < BLEND_EDGE) return { a: lo, b: null, w: 0 };
if (w > 1 - BLEND_EDGE) return { a: hi, b: null, w: 0 };
return { a: lo, b: hi, w };
}

View file

@ -0,0 +1,32 @@
import { describe, expect, it } from 'vitest';
import { inkContrast, themeInk } from './frameLuma';
import { luminance } from '../../../../sky/skyTokens.js';
describe('inkContrast', () => {
it('reads the ink from the themes: dark Noonday ink by day and dusk, light Midnight ink at night', () => {
expect(themeInk('day')).toBe('#342d25');
expect(themeInk('dusk')).toBe('#342d25');
expect(themeInk('night')).toBe('#aeccff');
});
it('gives the known ratios on a white and on a black ridge', () => {
const dark = luminance('#342d25');
const light = luminance('#aeccff');
const day = inkContrast({ min: 1, max: 1 }, 'day');
expect(day.atMax).toBeCloseTo(1.05 / (dark + 0.05), 10);
expect(day.atMax).toBeCloseTo(13.6, 0);
const night = inkContrast({ min: 0, max: 0 }, 'night');
expect(night.atMin).toBeCloseTo((light + 0.05) / 0.05, 10);
expect(night.atMin).toBeCloseTo(12.9, 0);
});
it('the worst contrast is where the range comes closest to the ink', () => {
const ink = luminance('#342d25');
const across = inkContrast({ min: 0, max: 1 }, 'day');
expect(across.worst).toBe(1);
expect(inkContrast({ min: ink + 0.2, max: 0.9 }, 'day').worst).toBeCloseTo(
(ink + 0.2 + 0.05) / (ink + 0.05),
10
);
});
});

View file

@ -0,0 +1,33 @@
// What a baked frame's recorded luma (manifest `frames[].luma`, WCAG linear relative luminance of the composited planes where they are
// solid enough, export.py) means for the text over it: the contrast of the state's theme ink against the ridge's own extremes, no scrim.
// Data for ridge-ad-legibility, which sizes the scrim from it and owns the 4.5:1 pass/fail; nothing here gates.
import { baked } from '../../../../sky/baked';
import { luminance } from '../../../../sky/skyTokens.js';
import type { FixedSkyState } from '../skyBand';
export interface FrameLuma {
min: number;
max: number;
}
/** The theme text ink skyBand.ts's header derives: Noonday Sky's for day and dusk, Midnight Sky's for night. */
export const themeInk = (state: FixedSkyState): string =>
(state === 'night' ? baked.midnight : baked.noonday).tokens.Surface.OnContainer;
const ratio = (a: number, b: number): number => (Math.max(a, b) + 0.05) / (Math.min(a, b) + 0.05);
/**
* WCAG contrast of the state's ink over a plain backdrop of the ridge's darkest and of its lightest luminance, and `worst`: the lowest
* contrast over every luminance between them (the ink's own luminance clamped into the range, where the two are closest).
*/
export function inkContrast(
luma: FrameLuma,
state: FixedSkyState
): { atMin: number; atMax: number; worst: number } {
const ink = luminance(themeInk(state));
return {
atMin: ratio(ink, luma.min),
atMax: ratio(ink, luma.max),
worst: ratio(ink, Math.min(luma.max, Math.max(luma.min, ink))),
};
}

View file

@ -1,30 +0,0 @@
import { describe, expect, it } from 'vitest';
import bake from './viennaHorizon.json';
import { VIENNA } from './vienna';
import { horizonDistanceKm, horizonElevation, horizonNormal } from './horizon';
describe('the baked horizon', () => {
it('was baked from the viewpoint the sun is computed for', () => {
expect(bake.viewpoint.lat).toBe(VIENNA.lat);
expect(bake.viewpoint.lon).toBe(VIENNA.lon);
expect(bake.viewpoint.groundM / 1000).toBeCloseTo(VIENNA.altitudeKm, 2);
});
it('carries its source and licence beside the data', () => {
expect(bake.source.licence).toMatch(/attribution required/);
expect(bake.source.attribution).toMatch(/COPERNICUS/);
});
it('is a closed ring of plausible angles: the Wienerwald NW, open Marchfeld E', () => {
expect(bake.elevationDeg).toHaveLength(720);
expect(horizonElevation(0)).toBeCloseTo(horizonElevation(360), 6);
expect(horizonElevation(305)).toBeGreaterThan(horizonElevation(70));
expect(Math.max(...bake.elevationDeg)).toBeLessThan(5);
expect(horizonDistanceKm(305)).toBeGreaterThan(1);
});
it('interpolates between samples and returns unit normals', () => {
expect(horizonElevation(100.25)).toBeCloseTo(
(bake.elevationDeg[200] + bake.elevationDeg[201]) / 2,
6
);
expect(Math.hypot(...horizonNormal(250))).toBeCloseTo(1, 6);
});
});

View file

@ -1,54 +0,0 @@
// The terrain horizon seen from the viewpoint, baked from open DEM by `scripts/bake-horizon.py` (source, licence and attribution are
// in the file's `source` block and in `HORIZON_ATTRIBUTION`).
import bake from './viennaHorizon.json';
export const HORIZON_ATTRIBUTION = bake.source.attribution;
const N = bake.elevationDeg.length;
/** Index pair and weight of an azimuth (degrees clockwise from north) on the baked ring. */
function at(azimuth: number): [number, number, number] {
const f = (((azimuth / bake.stepDeg) % N) + N) % N;
const i = Math.floor(f);
return [i, (i + 1) % N, f - i];
}
const lerp = (arr: readonly number[], az: number): number => {
const [a, b, t] = at(az);
return arr[a] * (1 - t) + arr[b] * t;
};
/** Elevation of the horizon, degrees above the astronomical horizon, at an azimuth. */
export const horizonElevation = (azimuth: number): number => lerp(bake.elevationDeg, azimuth);
/** Distance of the terrain that sets the horizon, km. */
export const horizonDistanceKm = (azimuth: number): number => lerp(bake.distanceKm, azimuth);
/** Unit surface normal of the terrain at the horizon point: x east, y up, z north. */
export function horizonNormal(azimuth: number): [number, number, number] {
const e = lerp(bake.slopeEast, azimuth);
const n = lerp(bake.slopeNorth, azimuth);
const l = Math.hypot(e, 1, n);
return [-e / l, 1 / l, -n / l];
}
/** The elevations blurred over +-SMOOTH_DEG (a box, around the ring): at 30 m a city's roofs are the horizon's teeth, and a silhouette of them reads as a fence. */
const SMOOTH_DEG = 1.5;
const SMOOTH = Array.from({ length: N }, (_, i) => {
const k = Math.round(SMOOTH_DEG / bake.stepDeg);
let sum = 0;
for (let d = -k; d <= k; d += 1) sum += bake.elevationDeg[(i + d + N) % N];
return sum / (2 * k + 1);
});
const MIN_E = Math.min(...SMOOTH);
const RANGE_E = Math.max(...SMOOTH) - MIN_E;
/** The horizon at an azimuth as 0..1 of the ring's own range (the lowest point of the horizon 0, the highest 1): the silhouette's shape, not its scale. */
export const horizonShape = (azimuth: number): number => (lerp(SMOOTH, azimuth) - MIN_E) / RANGE_E;
/** Real range of the smoothed horizon, degrees: what `horizonShape` stretches to the layer's relief. */
export const HORIZON_RANGE_DEG = RANGE_E;
/** Horizontal field of view, degrees, of a camera with the sky's vertical field (`skyDome.ts` VFOV_DEG) at an aspect ratio. */
export const hfovDeg = (aspect: number, vfovDeg: number): number =>
(2 * Math.atan(aspect * Math.tan((vfovDeg * Math.PI) / 360)) * 180) / Math.PI;

View file

@ -1,65 +0,0 @@
import { describe, expect, it } from 'vitest';
import { hfovDeg, horizonShape } from './horizon';
import { HORIZON_TRACK_SHIFT, LAYERS, layerProfile, profile } from './ridgeWorld';
import { layerPaint } from './ridgeBake';
import { weatherOf } from './weather';
import { skyColours } from './skyColour';
import { makePhysicalSky } from './physicalSky';
import { VFOV_DEG } from './skyDome';
describe('the horizon layer', () => {
const far = LAYERS[0];
const view = { azimuthDeg: 200, hfovDeg: hfovDeg(16 / 9, VFOV_DEG) };
it('is the farthest layer, real and standing still', () => {
expect(far.horizon).toBe(true);
expect(Number.isFinite(far.seconds)).toBe(false);
expect(LAYERS.slice(1).every((l) => !l.horizon && Number.isFinite(l.seconds))).toBe(true);
});
it('reads world-x as azimuth across the viewport: the left edge is half the field to the left of the view, the right edge half to the right', () => {
const left = layerProfile(far, HORIZON_TRACK_SHIFT, view);
const right = layerProfile(far, HORIZON_TRACK_SHIFT + 1, view);
expect(left).toBeCloseTo(horizonShape(200 - view.hfovDeg / 2), 9);
expect(right).toBeCloseTo(horizonShape(200 + view.hfovDeg / 2), 9);
});
it('stays in 0..1 and covers a real range of shape, not a line', () => {
const v = Array.from({ length: 200 }, (_, i) =>
layerProfile(far, -0.06 + (i / 200) * 1.2, { azimuthDeg: 305, hfovDeg: view.hfovDeg })
);
expect(Math.min(...v)).toBeGreaterThanOrEqual(0);
expect(Math.max(...v)).toBeLessThanOrEqual(1);
expect(Math.max(...v) - Math.min(...v)).toBeGreaterThan(0.2);
});
it('is the noise profile for the other layers, and for the horizon layer when no view is known', () => {
expect(layerProfile(LAYERS[2], 0.4, view)).toBe(profile(LAYERS[2], 0.4));
expect(layerProfile(far, 0.4)).toBe(profile(far, 0.4));
});
it('the horizontal field of a 1:1 view is the vertical one, and grows with the aspect', () => {
expect(hfovDeg(1, VFOV_DEG)).toBeCloseTo(VFOV_DEG, 9);
expect(hfovDeg(16 / 9, VFOV_DEG)).toBeGreaterThan(VFOV_DEG);
});
});
describe('weather in the layers', () => {
it("mist multiplies the layers' own, and clear air leaves it alone", () => {
const base = skyColours();
const day = new Date('2026-06-21T10:40:00Z');
const clear = layerPaint(LAYERS[1], { ...base, physical: makePhysicalSky('day', day, 0) });
const misty = layerPaint(LAYERS[1], { ...base, physical: makePhysicalSky('day', day, 0.8) });
expect(clear.mistK).toBe(1);
expect(misty.mistK).toBeGreaterThan(2);
expect(layerPaint(LAYERS[1], base).mistK).toBe(1);
});
});
describe('the weather number', () => {
it('a number that is not finite is a clear sky, not an error', () => {
expect(weatherOf(Number.NaN).value).toBe(0);
expect(weatherOf(Infinity).value).toBe(0);
expect(weatherOf(0.5).value).toBe(0.5);
});
});

View file

@ -0,0 +1,76 @@
// A hand-written manifest v1 and index v1 (wire form, as export.py writes them): the runtime's tests need no real bake and no decoder.
import { FRAME_KEYS } from './manifest';
import type { View } from './view';
const SUN: Record<typeof FRAME_KEYS[number], [number, number]> = {
dawn: [100, 0],
morning: [130, 20],
noon: [180, 60],
golden: [240, 6],
dusk: [262, -2.5],
night: [0, -12],
};
export const wireManifest = (over: Record<string, unknown> = {}): Record<string, unknown> => ({
schema: 1,
baker_version: 1,
bake_id: 'abc123abc123',
place: {
id: 'vienna',
name: 'Vienna',
lat: 48.19,
lon: 16.35,
altitude_km: 0.183,
tz_h: 1,
tz_name: 'CET',
date: '2026-06-21',
},
view: {
look_az_deg: 214,
window_deg: 72,
plane_w: 1280,
plane_h: 600,
px_per_deg_h: 1280 / 72,
px_per_deg_v: 17.7,
horizon_row: 314.5,
sky_row: 0,
near_row: 599,
},
frames: FRAME_KEYS.map((key) => ({
key,
sun: { az: SUN[key][0], el: SUN[key][1] },
hour_utc: 0,
planes: Object.fromEntries(
['far', 'mid', 'near'].map((n) => [
n,
{ file: `${key}-${n}.webp`, bytes: 1, w: 1280, h: 600 },
])
),
bytes: 3,
luma: { min: 0.1, max: 0.5 },
})),
attribution: {
credit_line: 'credit',
licence: 'licence',
dem_source: 'dem',
url: 'https://example.test',
},
...over,
});
export const wireIndex = {
schema: 1,
default: 'vienna',
places: { vienna: 'vienna-abc123abc123', graz: 'graz-def456def456' },
};
/** A View that differs from FALLBACK_VIEW in every field the sky reads (place, altitude, look azimuth, window, horizon row), so a test can tell the two apart. */
export const ELSEWHERE_VIEW: View = {
place: { id: 'elsewhere', lat: -33.9, lon: 151.2, altitudeKm: 1.5 },
lookAzDeg: 20,
windowDeg: 90,
planeW: 1600,
planeH: 700,
pxPerDegH: 1600 / 90,
horizonRow: 250,
};

View file

@ -0,0 +1,108 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import { forgetManifests, loadManifest, parseManifest } from './manifest';
import { wireIndex, wireManifest } from './manifest.fixture';
describe('parseManifest', () => {
it('reads a v1 manifest into the View the sky and planes share', () => {
const m = parseManifest(wireManifest(), '/public/ridge/v/');
expect(m.dir).toBe('/public/ridge/v/');
expect(m.view).toMatchObject({
lookAzDeg: 214,
windowDeg: 72,
planeW: 1280,
planeH: 600,
pxPerDegH: 1280 / 72, // not px_per_deg_v (17.7): a real bake's two differ 10x
horizonRow: 314.5,
});
expect(m.view.place).toEqual({ id: 'vienna', lat: 48.19, lon: 16.35, altitudeKm: 0.183 });
expect(m.frames.map((f) => f.key)).toEqual([
'dawn',
'morning',
'noon',
'golden',
'dusk',
'night',
]);
expect(m.frames[2]).toMatchObject({
sun: { az: 180, el: 60 },
planes: { far: 'noon-far.webp', near: 'noon-near.webp' },
});
expect(m.attribution.creditLine).toBe('credit');
});
it('rejects a missing field, a wrong schema, a plane wider than 4096 and fewer than 6 frames', () => {
const w = wireManifest() as any;
expect(() => parseManifest({ ...w, bake_id: undefined }, '/')).toThrow(/bake_id/);
expect(() => parseManifest({ ...w, schema: 2 }, '/')).toThrow(/schema/);
expect(() => parseManifest({ ...w, view: { ...w.view, plane_w: 4097 } }, '/')).toThrow(/4096/);
expect(() => parseManifest({ ...w, frames: w.frames.slice(1) }, '/')).toThrow(/frames/);
expect(() => parseManifest({ ...w, view: { ...w.view, horizon_row: 'x' } }, '/')).toThrow(
/horizon_row/
);
expect(() => parseManifest(null, '/')).toThrow();
});
});
const serve = (files: Record<string, unknown>) => {
const f = vi.fn(async (url: string) =>
url in files
? { ok: true, status: 200, json: async () => files[url] }
: { ok: false, status: 404, json: async () => ({}) }
);
vi.stubGlobal('fetch', f);
return f;
};
const files = () => ({
'/b/public/ridge/index.json': wireIndex,
'/b/public/ridge/vienna-abc123abc123/manifest.json': wireManifest(),
'/b/public/ridge/graz-def456def456/manifest.json': wireManifest({
place: { ...(wireManifest() as any).place, id: 'graz', name: 'Graz' },
}),
});
describe('loadManifest', () => {
afterEach(() => {
forgetManifests();
vi.unstubAllGlobals();
});
it('reads the index then the default place, with its directory', async () => {
const f = serve(files());
const m = await loadManifest('/b/');
expect(m.view.place.id).toBe('vienna');
expect(m.dir).toBe('/b/public/ridge/vienna-abc123abc123/');
expect(f.mock.calls.map((c) => c[0])).toEqual([
'/b/public/ridge/index.json',
'/b/public/ridge/vienna-abc123abc123/manifest.json',
]);
});
it('revalidates the index, the one mutable pointer, and leaves the content-addressed manifest to the cache', async () => {
const f = serve(files());
await loadManifest('/b/');
expect(f.mock.calls).toEqual([
['/b/public/ridge/index.json', { cache: 'no-cache' }],
['/b/public/ridge/vienna-abc123abc123/manifest.json', undefined],
]);
});
it('place picks the index entry; an unknown id falls back to the default', async () => {
serve(files());
expect((await loadManifest('/b/', 'graz')).view.place.id).toBe('graz');
expect((await loadManifest('/b/', 'nowhere')).view.place.id).toBe('vienna');
});
it('is memoised per URL and place', async () => {
const f = serve(files());
await loadManifest('/b/');
await loadManifest('/b/');
expect(f).toHaveBeenCalledTimes(2);
});
it('rejects on a 404 and does not remember the failure', async () => {
serve({});
await expect(loadManifest('/b/')).rejects.toThrow(/404/);
serve(files());
await expect(loadManifest('/b/')).resolves.toBeTruthy();
});
});

View file

@ -0,0 +1,150 @@
// The ridge backdrop's one JSON boundary: a static asset between the offline Python baker (scripts/ridge-bake/export.py, whose SCHEMA is the
// contract) and the page; no daemon state. Main-chunk safe: types, a parser and fetch only. Wire names are snake_case; the parsed manifest is
// camelCase and its `view` is the View (`view.ts`) the sky and the planes share.
import type { View } from './view';
export const FRAME_KEYS = ['dawn', 'morning', 'noon', 'golden', 'dusk', 'night'] as const;
export type FrameKey = typeof FRAME_KEYS[number];
export const PLANE_NAMES = ['far', 'mid', 'near'] as const;
export type PlaneName = typeof PLANE_NAMES[number];
export const PLANE_W_MAX = 4096;
export interface Frame {
key: FrameKey;
/** The sun this frame was lit by: degrees. */
sun: { az: number; el: number };
/** File names inside the manifest's directory. */
planes: Record<PlaneName, string>;
}
export interface RidgeManifest {
bakeId: string;
/** Absolute-or-rooted URL of the bake's directory, with a trailing slash. */
dir: string;
/** Display name of the place. */
name: string;
view: View;
/** In `FRAME_KEYS` order. */
frames: Frame[];
attribution: { creditLine: string; licence: string; demSource: string; url: string };
}
export interface RidgeIndex {
default: string;
places: Record<string, string>;
}
type Rec = Record<string, unknown>;
const fail = (path: string, why: string): never => {
throw new Error(`ridge manifest ${path}: ${why}`);
};
const rec = (x: unknown, path: string): Rec =>
typeof x === 'object' && x !== null && !Array.isArray(x)
? (x as Rec)
: fail(path, 'not an object');
const num = (x: unknown, path: string): number =>
typeof x === 'number' && Number.isFinite(x) ? x : fail(path, 'not a number');
const str = (x: unknown, path: string): string =>
typeof x === 'string' ? x : fail(path, 'not a string');
export function parseIndex(raw: unknown): RidgeIndex {
const r = rec(raw, 'index');
if (r.schema !== 1) fail('index.schema', 'must be 1');
const places = rec(r.places, 'index.places');
const out = Object.fromEntries(
Object.entries(places).map(([k, v]) => [k, str(v, `index.places.${k}`)])
);
return { default: str(r.default, 'index.default'), places: out };
}
/** `dir` is the URL of the directory the manifest was read from; the planes are read relative to it. */
export function parseManifest(raw: unknown, dir: string): RidgeManifest {
const r = rec(raw, 'manifest');
if (r.schema !== 1) fail('schema', 'must be 1');
const p = rec(r.place, 'place');
const v = rec(r.view, 'view');
const planeW = num(v.plane_w, 'view.plane_w');
if (planeW > PLANE_W_MAX) fail('view.plane_w', `above ${PLANE_W_MAX}`);
if (!Array.isArray(r.frames) || r.frames.length < FRAME_KEYS.length)
fail('frames', `fewer than ${FRAME_KEYS.length}`);
const frames = FRAME_KEYS.map((key) => {
const f = (r.frames as unknown[])
.map((x, i) => rec(x, `frames[${i}]`))
.find((x) => x.key === key);
if (!f) return fail('frames', `no ${key} frame`);
const sun = rec(f.sun, `frames.${key}.sun`);
const planes = rec(f.planes, `frames.${key}.planes`);
const file = (n: PlaneName) =>
str(rec(planes[n], `frames.${key}.planes.${n}`).file, `frames.${key}.planes.${n}.file`);
return {
key,
sun: { az: num(sun.az, `frames.${key}.sun.az`), el: num(sun.el, `frames.${key}.sun.el`) },
planes: { far: file('far'), mid: file('mid'), near: file('near') },
};
});
const a = rec(r.attribution, 'attribution');
const id = str(p.id, 'place.id');
return {
bakeId: str(r.bake_id, 'bake_id'),
dir,
name: str(p.name, 'place.name'),
view: {
place: {
id,
lat: num(p.lat, 'place.lat'),
lon: num(p.lon, 'place.lon'),
altitudeKm: num(p.altitude_km, 'place.altitude_km'),
},
lookAzDeg: num(v.look_az_deg, 'view.look_az_deg'),
windowDeg: num(v.window_deg, 'view.window_deg'),
planeW,
planeH: num(v.plane_h, 'view.plane_h'),
pxPerDegH: num(v.px_per_deg_h, 'view.px_per_deg_h'),
horizonRow: num(v.horizon_row, 'view.horizon_row'),
},
frames,
attribution: {
creditLine: str(a.credit_line, 'attribution.credit_line'),
licence: str(a.licence, 'attribution.licence'),
demSource: str(a.dem_source, 'attribution.dem_source'),
url: str(a.url, 'attribution.url'),
},
};
}
async function getJson(url: string, init?: RequestInit): Promise<unknown> {
const res = await fetch(url, init);
if (!res.ok) throw new Error(`ridge: ${url} answered ${res.status}`);
return res.json();
}
const memo = new Map<string, Promise<RidgeManifest>>();
/**
* The manifest of a baked place: `${baseUrl}public/ridge/index.json` names each place's directory, then `<dir>/manifest.json` is read.
* `placeId` picks the entry (unknown or absent: the index's default). Memoised per index URL and place; a failure is not remembered.
* Rejects on any failure: the caller degrades to the sky-only scene.
*/
export function loadManifest(baseUrl: string, placeId?: string): Promise<RidgeManifest> {
const root = `${baseUrl}public/ridge/`;
const key = `${root}|${placeId ?? ''}`;
const hit = memo.get(key);
if (hit) return hit;
const p = (async () => {
const index = parseIndex(
// The index is the one mutable pointer (a rebake deletes the old directory it named); the manifest and planes are content-addressed.
await getJson(`${root}index.json`, { cache: 'no-cache' })
);
const id = placeId !== undefined && placeId in index.places ? placeId : index.default;
const name = index.places[id];
if (name === undefined) throw new Error(`ridge: index has no place ${id}`);
const dir = `${root}${name}/`;
return parseManifest(await getJson(`${dir}manifest.json`), dir);
})();
memo.set(key, p);
p.catch(() => memo.delete(key));
return p;
}
/** For tests: forget every memoised manifest. */
export const forgetManifests = (): void => memo.clear();

View file

@ -1,7 +1,12 @@
import { describe, expect, it } from 'vitest';
import { FALLBACK_VIEW } from './view';
import { ELSEWHERE_VIEW } from './manifest.fixture';
import { sunForState } from './sunAt';
import { makePhysicalSky, paintSky } from './physicalSky';
import { skyBackdrop } from '../skyState';
import type { FixedSkyState } from '../skyBand';
import { windowFor } from './view';
import type { PhysicalSky } from './physicalSky';
// The Aqua method on tonemapped pixels: paint the sky the way the host canvas does and measure the WCAG luminance of every 8-bit
// texel, against the bounds the page text was checked against (`skyBackdrop`, 4.5:1). Over the weather, the day of the year, and
@ -13,6 +18,13 @@ const lin = (v: number): number => {
const luma = (d: Uint8ClampedArray, i: number): number =>
0.2126 * lin(d[i]) + 0.7152 * lin(d[i + 1]) + 0.0722 * lin(d[i + 2]);
const camera = (sky: PhysicalSky, w: number, h: number) => windowFor(sky.view, w, h).camera;
/** The camera of the viewport turned to face the sun, so the disc's colour and floor are measured wherever the sun is. */
const facingSun = (sky: PhysicalSky, w: number, h: number) => ({
...camera(sky, w, h),
azimuthDeg: sky.sun.azimuth,
});
const DAYS = ['2026-03-20', '2026-06-21', '2026-09-23', '2026-12-21'];
const WEATHER = [0, 0.3, 0.55, 0.75, 1];
const VIEWPORTS: [number, number][] = [
@ -31,9 +43,9 @@ describe.each(['day', 'dusk', 'night'] as FixedSkyState[])(
let hi = -1;
for (const day of DAYS) {
for (const w of WEATHER) {
const sky = makePhysicalSky(state, new Date(`${day}T10:40:00Z`), w);
const sky = makePhysicalSky(state, new Date(`${day}T10:40:00Z`), w, FALLBACK_VIEW);
for (const [vw, vh] of VIEWPORTS) {
const px = paintSky(sky, vw, vh);
const px = paintSky(sky, vw, vh, camera(sky, vw, vh));
for (let i = 0; i < px.w * px.h * 4; i += 4) {
const l = luma(px.data, i);
lo = Math.min(lo, l);
@ -51,8 +63,12 @@ describe.each(['day', 'dusk', 'night'] as FixedSkyState[])(
describe('weather in the sky', () => {
it('overcast is cloud: less blue and less contrast than clear air at the same sun', () => {
const at = new Date('2026-06-21T10:40:00Z');
const clear = paintSky(makePhysicalSky('day', at, 0), 640, 360);
const grey = paintSky(makePhysicalSky('day', at, 0.7), 640, 360);
const paint = (w: number) => {
const sky = makePhysicalSky('day', at, w, FALLBACK_VIEW);
return paintSky(sky, 640, 360, camera(sky, 640, 360));
};
const clear = paint(0);
const grey = paint(0.7);
const spread = (p: typeof clear) => {
let lo = 2;
let hi = -1;
@ -79,11 +95,8 @@ describe('the sun disc', () => {
let drawn = 0;
for (const state of ['day', 'dusk'] as FixedSkyState[]) {
for (let m = 0; m < 24 * 60; m += 10) {
const d = paintSky(
makePhysicalSky(state, new Date(Date.UTC(2026, 10, 11, 0, m)), 0),
1280,
720
).sunDisc;
const sky = makePhysicalSky(state, new Date(Date.UTC(2026, 10, 11, 0, m)), 0, FALLBACK_VIEW);
const d = paintSky(sky, 1280, 720, facingSun(sky, 1280, 720)).sunDisc;
if (!d) continue;
drawn += 1;
expect(
@ -93,24 +106,46 @@ describe('the sun disc', () => {
}
expect(drawn).toBeGreaterThan(0);
});
const noon = new Date('2026-11-11T10:40:00Z'); // 21 degrees: in June the noon sun is above the top of the image
const disc = (sky: PhysicalSky) => paintSky(sky, 1280, 720, facingSun(sky, 1280, 720));
const noon = new Date('2026-12-21T11:00:00Z'); // about 18 degrees: higher suns are above the top of the window
it('is drawn by the host, in a clear sky by day, white with the sun high', () => {
const d = paintSky(makePhysicalSky('day', noon, 0), 1280, 720).sunDisc;
const sky = makePhysicalSky('day', noon, 0, FALLBACK_VIEW);
const d = disc(sky).sunDisc;
expect(d).toBeDefined();
expect(d!.x).toBeGreaterThan(0);
expect(d!.x).toBeLessThan(1);
expect(d!.y).toBeGreaterThan(0);
expect(d!.y).toBeLessThan(0.78); // above the horizon line
expect(d!.y).toBeLessThan(camera(sky, 1280, 720).horizonAt); // above the horizon line
expect(d!.alpha).toBe(1);
expect(Math.min(...d!.rgb)).toBeGreaterThan(150); // a warm white at 21 degrees
});
it('is hidden by overcast and absent at night', () => {
expect(paintSky(makePhysicalSky('day', noon, 0.7), 1280, 720).sunDisc).toBeUndefined();
expect(paintSky(makePhysicalSky('night', noon, 0), 1280, 720).sunDisc).toBeUndefined();
expect(disc(makePhysicalSky('day', noon, 0.7, FALLBACK_VIEW)).sunDisc).toBeUndefined();
expect(disc(makePhysicalSky('night', noon, 0, FALLBACK_VIEW)).sunDisc).toBeUndefined();
});
it('is orange-tinted low in the sky', () => {
const d = paintSky(makePhysicalSky('dusk', noon, 0), 1280, 720).sunDisc;
const d = disc(makePhysicalSky('dusk', noon, 0, FALLBACK_VIEW)).sunDisc;
expect(d).toBeDefined();
expect(d!.rgb[0]).toBeGreaterThan(d!.rgb[2]);
});
});
describe('the sky is made from the View it is given', () => {
const NOW = new Date('2026-06-21T10:40:00Z');
const sameSpotOtherCamera = { ...FALLBACK_VIEW, lookAzDeg: 30, windowDeg: 100, horizonRow: 200 };
it("lights the sky from the sun over the View's place, at its altitude", () => {
const sky = makePhysicalSky('day', NOW, 0, ELSEWHERE_VIEW);
expect(sky.view).toBe(ELSEWHERE_VIEW);
expect(sky.sun).toEqual(sunForState(ELSEWHERE_VIEW.place, 'day', NOW));
expect(sky.sun).not.toEqual(sunForState(FALLBACK_VIEW.place, 'day', NOW));
expect(sky.atm.observerKm).toBe(ELSEWHERE_VIEW.place.altitudeKm);
});
it("fits and samples the stops through the View's camera: the same place and sun with another look is another gradient", () => {
const a = makePhysicalSky('day', NOW, 0, FALLBACK_VIEW);
const b = makePhysicalSky('day', NOW, 0, sameSpotOtherCamera);
expect(b.sun).toEqual(a.sun);
expect(b.stops).not.toEqual(a.stops);
});
});

View file

@ -1,29 +1,25 @@
// The ridge scene's sky: Vienna's sun on the real clock, the atmosphere baked from it, a camera, and the display that makes it
// The ridge scene's sky: the View's place's sun on the real clock, the atmosphere baked from it, a camera, and the display that makes it
// pixels. `makePhysicalSky` does the one expensive thing (the LUT bake, tens of ms) and fits the display from a fixed 16:9
// grid, so resizing the window repaints the same sky. `paintSky` is per-texel lookups only.
import type { FixedSkyState } from '../skyBand';
import { bakeAtmosphere } from './earthAtmosphere';
import type { Atmosphere } from './earthAtmosphere';
import {
domeSize,
HORIZON_AT,
renderDome,
SUN_RADIUS,
sunOnScreen,
viewFor,
VFOV_DEG,
} from './skyDome';
import type { Dome, DomeView } from './skyDome';
import { domeSize, renderDome, SUN_RADIUS, sunOnScreen } from './skyDome';
import type { Dome } from './skyDome';
import type { Srgb } from './skyColour';
import { display8, fitDisplay, holdBudget, MARGIN } from './toneMap';
import type { Display, Fit } from './toneMap';
import { sunForState, VIENNA } from './vienna';
import type { SunSample } from './vienna';
import { sunForState } from './sunAt';
import type { SunSample } from './sunAt';
import { windowFor } from './view';
import type { Camera, View } from './view';
import { weatherOf } from './weather';
import type { Weather } from './weather';
export interface PhysicalSky {
state: FixedSkyState;
/** Where the observer stands and where the planes look: the camera of any viewport is `windowFor(view, w, h).camera`. */
view: View;
sun: SunSample;
weather: Weather;
atm: Atmosphere;
@ -35,7 +31,7 @@ export interface PhysicalSky {
bakeMs: number;
}
const CANON = { w: 64, h: 36, aspect: 16 / 9 };
const CANON = { w: 64, h: 36 };
const clock = (): number => (typeof performance !== 'undefined' ? performance.now() : Date.now());
@ -56,26 +52,30 @@ export function rowMean(dome: Dome, display: Display, row: number): Srgb {
export function makePhysicalSky(
state: FixedSkyState,
now: Date,
weatherValue: number
weatherValue: number,
view: View
): PhysicalSky {
const t0 = clock();
const sun = sunForState(state, now);
const sun = sunForState(view.place, state, now);
const weather = weatherOf(weatherValue);
const atm = bakeAtmosphere({
sunElevationDeg: sun.elevation,
mieScale: weather.mieScale,
observerKm: VIENNA.altitudeKm,
observerKm: view.place.altitudeKm,
});
const dome = renderDome(atm, sun, viewFor(sun, CANON.aspect), CANON.w, CANON.h, weather.cover);
// A 16:9 viewport: the camera's fov and horizon fraction do not depend on its size, only on its shape.
const { camera } = windowFor(view, CANON.w, CANON.h);
const dome = renderDome(atm, sun, camera, CANON.w, CANON.h, weather.cover);
const fit = fitDisplay(state, dome.hdr, dome.meanLuma);
const at = (f: number) => rowMean(dome, fit.display, Math.round(f * CANON.h));
return {
state,
view,
sun,
weather,
atm,
fit,
stops: { top: at(0.02), mid: at(0.5), horizon: at(HORIZON_AT) },
stops: { top: at(0.02), mid: at(0.5), horizon: at(camera.horizonAt) },
bakeMs: clock() - t0,
};
}
@ -86,16 +86,15 @@ export interface SkyPixels {
/** RGBA, 8 bit. */
data: Uint8ClampedArray;
dome: Dome;
view: DomeView;
camera: Camera;
/** The sun's disc for the host to draw on the upscaled canvas (it is smaller than a texel): place as fractions of the image, colour, radius in fractions of the height, opacity after the cloud. */
sunDisc?: { x: number; y: number; rgb: Srgb; radius: number; alpha: number };
}
/** The sky for a viewport, as a small RGBA grid the host canvas upscales smoothly. */
export function paintSky(p: PhysicalSky, cssW: number, cssH: number): SkyPixels {
/** The sky for a viewport seen through `camera` (`windowFor(p.view, cssW, cssH).camera`), as a small RGBA grid the host canvas upscales smoothly. */
export function paintSky(p: PhysicalSky, cssW: number, cssH: number, camera: Camera): SkyPixels {
const { w, h } = domeSize(cssW, cssH);
const view = viewFor(p.sun, cssW / cssH);
const dome = renderDome(p.atm, p.sun, view, w, h, p.weather.cover);
const dome = renderDome(p.atm, p.sun, camera, w, h, p.weather.cover);
const data = new Uint8ClampedArray(w * h * 4);
const out = [0, 0, 0];
const display = holdBudget(p.state, dome.hdr, p.fit.display);
@ -106,7 +105,7 @@ export function paintSky(p: PhysicalSky, cssW: number, cssH: number): SkyPixels
data[i * 4 + 2] = out[2];
data[i * 4 + 3] = 255;
}
return { w, h, data, dome, view, sunDisc: sunDisc(p, view) };
return { w, h, data, dome, camera, sunDisc: sunDisc(p, camera) };
}
/** The luma the disc must keep: the day/dusk floor of toneMap's budget. */
@ -120,8 +119,8 @@ const smooth01 = (a: number, b: number, v: number): number => {
* The sun's disc: always brighter than the display's white, so what is left of it is its colour, the air's transmittance to the sun
* normalised to its strongest channel (white high up, orange near the horizon). Thick cloud hides it.
*/
function sunDisc(p: PhysicalSky, view: DomeView): SkyPixels['sunDisc'] {
const at = sunOnScreen(p.sun, view);
function sunDisc(p: PhysicalSky, camera: Camera): SkyPixels['sunDisc'] {
const at = sunOnScreen(p.sun, camera);
const alpha = 1 - smooth01(0.1, 0.6, p.weather.cover);
if (!at || alpha <= 0) return undefined;
const trans = [0, 0, 0];
@ -138,7 +137,7 @@ function sunDisc(p: PhysicalSky, view: DomeView): SkyPixels['sunDisc'] {
return {
...at,
rgb: rgb as unknown as Srgb,
radius: (SUN_RADIUS * 180) / Math.PI / VFOV_DEG,
radius: (SUN_RADIUS * 180) / Math.PI / camera.vfovDeg,
alpha,
};
}

View file

@ -0,0 +1,229 @@
// The committed baked set under public/ridge: what the baker wrote is what ships, within the budget the baker itself refuses on
// (export.py PAIR_BYTES_MAX, DECODE_BYTES_MAX). Reads with node fs. The gate reads each plane's own bytes, not the manifest's claims about
// them: the WebP container is walked and its stated dimensions (VP8X canvas, VP8 / VP8L frame header) must equal the manifest's w and h,
// which the decode budget is computed from; and the bake id is re-derived from the plane bytes, as the baker derives it, so any
// corruption of a plane, same length or not, fails. No pixel decoder is in the tree; the baker's own check decodes pixels.
import { createHash } from 'node:crypto';
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
import { join } from 'node:path';
import { describe, expect, it } from 'vitest';
import { FRAME_KEYS, PLANE_NAMES, parseIndex, parseManifest } from './manifest';
/** The same two constants as scripts/ridge-bake/export.py: two adjacent frames, fetched and decoded. */
export const PAIR_BYTES_MAX = 800_000;
export const DECODE_BYTES_MAX = 20_000_000;
interface WirePlane {
file: string;
bytes: number;
w: number;
h: number;
}
interface WireFrame {
key: string;
planes: Record<string, WirePlane>;
luma: { min: number; max: number };
}
export interface WireManifest {
bake_id: string;
frames: WireFrame[];
}
const planes = (f: WireFrame): WirePlane[] => PLANE_NAMES.map((n) => f.planes[n]);
/** Why a manifest breaks the size gate, or undefined. Frames are taken in FRAME_KEYS order; the pairs are neighbours, and night meets dawn. */
export function sizeGate(m: WireManifest): string | undefined {
const byKey = FRAME_KEYS.map((k) => m.frames.find((f) => f.key === k) as WireFrame);
for (let i = 0; i < byKey.length; i += 1) {
const pair = [byKey[i], byKey[(i + 1) % byKey.length]];
const ps = pair.flatMap(planes);
const fetched = ps.reduce((a, p) => a + p.bytes, 0);
const decoded = ps.reduce((a, p) => a + p.w * p.h * 4, 0);
if (fetched > PAIR_BYTES_MAX) return `${pair[0].key}+${pair[1].key} fetches ${fetched}`;
if (decoded > DECODE_BYTES_MAX) return `${pair[0].key}+${pair[1].key} decodes ${decoded}`;
}
return undefined;
}
/**
* Why a buffer is not a well-formed WebP of `w` x `h`, or undefined. Checks the RIFF envelope (its size is the file's), that the chunks tile
* the file exactly, and the dimensions the container and the bitstream header each state.
*/
export function webpFault(b: Uint8Array, w: number, h: number): string | undefined {
const u32 = (o: number) => (b[o] | (b[o + 1] << 8) | (b[o + 2] << 16) | (b[o + 3] << 24)) >>> 0;
const u24 = (o: number) => b[o] | (b[o + 1] << 8) | (b[o + 2] << 16);
const tag = (o: number) => String.fromCharCode(b[o], b[o + 1], b[o + 2], b[o + 3]);
if (b.length < 20 || tag(0) !== 'RIFF' || tag(8) !== 'WEBP') return 'not a RIFF WEBP';
if (u32(4) + 8 !== b.length) return `RIFF size ${u32(4)} does not fit a ${b.length} byte file`;
const dims: string[] = [];
let seen = 0;
let o = 12;
while (o < b.length) {
if (o + 8 > b.length) return 'a chunk header runs past the file';
const id = tag(o);
const len = u32(o + 4);
const body = o + 8;
if (body + len > b.length) return `chunk ${id} runs past the file`;
if (id === 'VP8X') dims.push(`${u24(body + 4) + 1}x${u24(body + 7) + 1}`);
if (id === 'VP8 ') {
if (b[body + 3] !== 0x9d || b[body + 4] !== 0x01 || b[body + 5] !== 0x2a)
return 'VP8 start code missing';
dims.push(
`${(b[body + 6] | (b[body + 7] << 8)) & 0x3fff}x${
(b[body + 8] | (b[body + 9] << 8)) & 0x3fff
}`
);
seen += 1;
}
if (id === 'VP8L') {
if (b[body] !== 0x2f) return 'VP8L signature missing';
const bits = u32(body + 1);
dims.push(`${(bits & 0x3fff) + 1}x${((bits >>> 14) & 0x3fff) + 1}`);
seen += 1;
}
o = body + len + (len & 1);
}
if (o !== b.length) return 'the chunks do not tile the file';
if (seen !== 1) return `${seen} bitstream chunks`;
const bad = dims.find((d) => d !== `${w}x${h}`);
return bad ? `states ${bad}, the manifest ${w}x${h}` : undefined;
}
/** The baker's bake id (export.py `bake_id`): first 12 hex of sha256 over the plane bytes, frames in FRAME_KEYS order, planes far, mid, near. */
export function bakeIdOf(planeBytes: (frame: string, plane: string) => Uint8Array): string {
const h = createHash('sha256');
FRAME_KEYS.forEach((k) => PLANE_NAMES.forEach((n) => h.update(planeBytes(k, n))));
return h.digest('hex').slice(0, 12);
}
const ROOT = join(process.cwd(), 'public', 'ridge');
const readJson = (p: string): unknown => JSON.parse(readFileSync(p, 'utf8'));
const index = parseIndex(readJson(join(ROOT, 'index.json')));
const dirs = Object.entries(index.places);
describe('the committed ridge assets', () => {
it('ship at least one place, and the index default is one of them', () => {
expect(dirs.length).toBeGreaterThan(0);
expect(index.places[index.default]).toBeDefined();
});
dirs.forEach(([id, dir]) => {
describe(`${id} (${dir})`, () => {
const raw = readJson(join(ROOT, dir, 'manifest.json')) as WireManifest;
it('parses, and its directory ends in its bake id', () => {
expect(() => parseManifest(raw, `${dir}/`)).not.toThrow();
expect(dir.endsWith(`-${raw.bake_id}`)).toBe(true);
});
it('has every plane file, at the length the manifest states', () => {
raw.frames.flatMap(planes).forEach((p) => {
expect(statSync(join(ROOT, dir, p.file)).size, p.file).toBe(p.bytes);
});
});
it('decodes every plane as the WebP of the size the manifest states', () => {
raw.frames.flatMap(planes).forEach((p) => {
expect(
webpFault(readFileSync(join(ROOT, dir, p.file)), p.w, p.h),
p.file
).toBeUndefined();
});
});
it('holds the bytes its bake id was made from', () => {
const bytes = (frame: string, plane: string) =>
readFileSync(
join(
ROOT,
dir,
(raw.frames.find((f) => f.key === frame) as WireFrame).planes[plane].file
)
);
expect(bakeIdOf(bytes)).toBe(raw.bake_id);
});
it('keeps every adjacent pair of frames inside the fetch and decode budget', () => {
expect(sizeGate(raw)).toBeUndefined();
});
it('records a finite luma for every frame', () => {
expect(raw.frames).toHaveLength(FRAME_KEYS.length);
raw.frames.forEach((f) => {
expect(Number.isFinite(f.luma.min) && Number.isFinite(f.luma.max), f.key).toBe(true);
expect(f.luma.min, f.key).toBeLessThanOrEqual(f.luma.max);
});
});
});
});
it('holds no file a manifest does not name', () => {
const named = new Set<string>(['index.json']);
dirs.forEach(([, dir]) => {
named.add(`${dir}/manifest.json`);
(readJson(join(ROOT, dir, 'manifest.json')) as WireManifest).frames
.flatMap(planes)
.forEach((p) => named.add(`${dir}/${p.file}`));
});
const onDisk = readdirSync(ROOT, { recursive: true, withFileTypes: true })
.filter((e) => e.isFile())
.map((e) => join(e.parentPath, e.name).slice(ROOT.length + 1));
expect(onDisk.filter((f) => !named.has(f))).toEqual([]);
expect([...named].filter((f) => !existsSync(join(ROOT, f)))).toEqual([]);
});
});
describe('the plane gates refuse a plane that is not what the manifest says', () => {
const real = readFileSync(join(ROOT, Object.values(index.places)[0], 'noon-mid.webp'));
const manifest = readJson(
join(ROOT, Object.values(index.places)[0], 'manifest.json')
) as WireManifest;
const { w, h } = manifest.frames[2].planes.mid;
const id = (b: Uint8Array) => bakeIdOf(() => b);
it('accepts the real plane at its size', () => {
expect(webpFault(real, w, h)).toBeUndefined();
});
it("refuses a manifest that lies about the plane's size, and a file cut short or padded", () => {
expect(webpFault(real, w, h + 1)).toMatch(/states/);
expect(webpFault(real.subarray(0, real.length - 1), w, h)).toBeDefined();
expect(webpFault(Buffer.concat([real, Buffer.from([0])]), w, h)).toBeDefined();
});
it('refuses a frame header whose dimensions are changed, and bytes that are not a WebP', () => {
const vp8 = Buffer.from(real);
const at = vp8.indexOf('VP8 ') + 8 + 6;
vp8[at] ^= 0x01;
expect(webpFault(vp8, w, h)).toMatch(/states/);
expect(webpFault(Buffer.from('not a webp, but long enough to try'), w, h)).toMatch(/RIFF/);
});
it('re-derives a different bake id from a plane with the same length and 8 bytes overwritten', () => {
const bad = Buffer.from(real);
bad.fill(0xab, 100, 108);
expect(bad.length).toBe(real.length);
expect(id(bad)).not.toBe(id(real));
});
});
describe('sizeGate', () => {
const plane = (bytes: number): WirePlane => ({ file: 'x.webp', bytes, w: 1280, h: 600 });
const frame = (key: string, bytes: number): WireFrame => ({
key,
planes: { far: plane(bytes), mid: plane(bytes), near: plane(bytes) },
luma: { min: 0, max: 1 },
});
const set = (bytes: number): WireManifest => ({
bake_id: 'x',
frames: FRAME_KEYS.map((k) => frame(k, bytes)),
});
it('passes a pair at the budget and fails a 900 KB pair', () => {
expect(sizeGate(set(133_333))).toBeUndefined();
expect(sizeGate(set(150_000))).toMatch(/fetches 900000/);
});
it('fails a pair that decodes past the budget', () => {
const m = set(1);
m.frames[2].planes.far = { file: 'x.webp', bytes: 1, w: 4096, h: 1000 };
expect(sizeGate(m)).toMatch(/decodes/);
});
});

View file

@ -1,404 +0,0 @@
import { describe, expect, it } from 'vitest';
import { BakeParams, bakeSlice, faceLight, layerPaint, sunDirection } from './ridgeBake';
import { SKY_BAND } from '../skyBand';
import { ridgeSkyFor } from '../skyForState';
import { LACUNARITY, LAYERS, profile } from './ridgeWorld';
import { skyColours, SkyColours } from './skyColour';
const SKY: SkyColours = { ...skyColours(), sunAzimuth: 0.3 }; // sun toward +x
const PERIOD = 400;
const VIEW_H = 800;
const BAND = 480;
const params = (
li: number,
px0: number,
cols: number,
over: Partial<BakeParams> = {}
): BakeParams => ({
spec: LAYERS[li],
sky: SKY,
period: PERIOD,
scale: 1,
viewH: VIEW_H,
rows: BAND,
bandH: BAND,
px0,
cols,
...over,
});
function bake(p: BakeParams, slices: number[] = [p.cols]): Uint8ClampedArray {
const out = new Uint8ClampedArray(p.cols * p.rows * 4);
const paint = layerPaint(p.spec, p.sky);
let c = 0;
for (const n of slices) {
bakeSlice(out, p, paint, c, Math.min(p.cols, c + n));
c += n;
}
return out;
}
const lum = (d: Uint8ClampedArray, cols: number, x: number, y: number) => {
const o = (y * cols + x) * 4;
return 0.2126 * d[o] + 0.7152 * d[o + 1] + 0.0722 * d[o + 2];
};
/** First row of column x whose alpha is full. */
const solidRow = (d: Uint8ClampedArray, cols: number, x: number) => {
let y = 0;
while (y < BAND && d[(y * cols + x) * 4 + 3] < 255) y += 1;
return y;
};
describe('the field is a function of world-x', () => {
it('is deterministic, and defined and bounded at any world-x', () => {
for (const spec of LAYERS) {
for (const t of [-40.3, 0, 0.5, 17.77, 5000.123]) {
expect(profile(spec, t)).toBe(profile(spec, t));
expect(profile(spec, t)).toBeGreaterThanOrEqual(0);
expect(profile(spec, t)).toBeLessThanOrEqual(1);
}
}
});
it('has no repeat at the old period (1 viewport) or its multiples: the profile differs from itself shifted', () => {
for (const spec of LAYERS) {
for (const lag of [1, 2, 3, 0.5]) {
let diff = 0;
for (let i = 0; i < 400; i += 1)
diff += Math.abs(profile(spec, i / 100) - profile(spec, i / 100 + lag));
expect(diff / 400).toBeGreaterThan(0.04);
}
}
});
it('the same world column bakes to the same pixels whichever chunk or slice paints it', () => {
for (const li of [0, 3]) {
const whole = bake(params(li, 1000, 200));
const sliced = bake(params(li, 1000, 200), [37, 64, 1000]);
expect(Array.from(sliced)).toEqual(Array.from(whole));
// A chunk starting 120 columns later sees the same pixels for the columns it shares.
const shifted = bake(params(li, 1120, 80));
for (const x of [0, 17, 79]) {
for (const y of [0, 100, 300, BAND - 1]) {
const a = (y * 200 + 120 + x) * 4;
const b = (y * 80 + x) * 4;
expect(Array.from(shifted.slice(b, b + 4))).toEqual(Array.from(whole.slice(a, a + 4)));
}
}
}
});
it('is continuous across a chunk seam: the crest steps no more at the seam than elsewhere', () => {
const chunk = Math.round(PERIOD * 1.2);
for (const li of [0, 1, 2, 3]) {
const p = params(li, chunk - 40, 80);
const d = bake(p);
let worst = 0;
for (let x = 1; x < 80; x += 1)
worst = Math.max(worst, Math.abs(solidRow(d, 80, x) - solidRow(d, 80, x - 1)));
const seam = Math.abs(solidRow(d, 80, 40) - solidRow(d, 80, 39));
expect(seam).toBeLessThanOrEqual(Math.max(worst, 1));
// and the seam columns equal a chunk baked from the other side
const other = bake(params(li, chunk - 40, 80, { cols: 80 }));
expect(Array.from(other)).toEqual(Array.from(d));
}
});
});
describe('shading from the slope normal', () => {
const sun = sunDirection(SKY);
it('the sun is toward +x for azimuth 0.3', () => {
expect(sun[0]).toBeGreaterThan(0.5);
});
it('a face looking at the sun is lit more than one looking away', () => {
// slope < 0: the crest falls toward +x, so the face looks at +x, at the sun.
for (const s of [0.2, 0.6, 1.5]) expect(faceLight(-s, sun)).toBeGreaterThan(faceLight(s, sun));
expect(faceLight(1.5, sun)).toBeLessThan(0.2);
expect(faceLight(-1.5, sun)).toBeGreaterThan(0.6);
});
it('in the baked pixels the lit side is brighter than the shadow side', () => {
for (const li of [0, 1, 2]) {
const cols = 900;
const p = params(li, 0, cols);
const d = bake(p);
const slope = (x: number) => (solidRow(d, cols, x - 1) - solidRow(d, cols, x + 1)) / 2; // rows up per column
const litX: number[] = [];
const shadeX: number[] = [];
for (let x = 2; x < cols - 2; x += 1) {
if (slope(x) < -0.8) litX.push(x);
if (slope(x) > 0.8) shadeX.push(x);
}
expect(litX.length).toBeGreaterThan(5);
expect(shadeX.length).toBeGreaterThan(5);
const mean = (xs: number[]) =>
xs.reduce((a, x) => a + lum(d, cols, x, solidRow(d, cols, x) + 12), 0) / xs.length;
expect(mean(litX)).toBeGreaterThan(mean(shadeX));
}
});
it('a lit crest rim: the crest pixels are brighter than the face a little below, on both sides', () => {
for (const li of [0, 1, 2, 3]) {
const cols = 600;
const d = bake(params(li, 300, cols));
let rimmed = 0;
let n = 0;
for (let x = 3; x < cols - 3; x += 1) {
const y = solidRow(d, cols, x);
if (y + 40 >= BAND) continue;
n += 1;
if (lum(d, cols, x, y + 1) > lum(d, cols, x, y + 30)) rimmed += 1;
}
expect(n).toBeGreaterThan(100);
expect(rimmed / n).toBeGreaterThan(0.8);
}
});
it('the foot of the face is clearly darker than its top, on every layer: volume from the depth below the crest', () => {
// Without the depth shading the bottom row is 0.93 to 0.98 of the luminance 12 rows under the crest; with it, 0.73 to 0.82.
for (const li of [0, 1, 2, 3]) {
const cols = 900;
const d = bake(params(li, 0, cols, { spec: { ...LAYERS[li], mist: 0, gully: 0 } }));
let ratio = 0;
let n = 0;
for (let x = 0; x < cols; x += 1) {
const y = solidRow(d, cols, x);
if (y + 90 >= BAND) continue;
n += 1;
ratio += lum(d, cols, x, BAND - 1) / Math.max(1, lum(d, cols, x, y + 12));
}
expect(n).toBeGreaterThan(30);
expect(ratio / n).toBeLessThan(0.88);
}
});
it('the valley mist still pulls the foot toward the haze colour', () => {
const cols = 300;
const p = params(0, 0, cols);
const misty = bake(p);
const clear = bake(params(0, 0, cols, { spec: { ...LAYERS[0], mist: 0 } }));
const haze = layerPaint(p.spec, p.sky).haze;
const dist = (d: Uint8ClampedArray, x: number, y: number) => {
const o = (y * cols + x) * 4;
return Math.hypot(d[o] - haze[0], d[o + 1] - haze[1], d[o + 2] - haze[2]);
};
let nearer = 0;
let n = 0;
for (let x = 0; x < cols; x += 1) {
const y = solidRow(misty, cols, x);
if (y + 200 >= BAND) continue;
n += 1;
if (dist(misty, x, y + 200) < dist(clear, x, y + 200)) nearer += 1;
}
expect(n).toBeGreaterThan(50);
expect(nearer / n).toBeGreaterThan(0.9);
});
it('erosion streaks run down the face: brightness varies across columns at one depth, and not without them', () => {
const cols = 500;
const spread = (gully: number) => {
const d = bake(params(2, 0, cols, { spec: { ...LAYERS[2], gully } }));
// The same depth below the crest in every column, in the lit-independent part (gullies darken, lit is column-wise).
const v: number[] = [];
for (let x = 0; x < cols; x += 1) {
const y = solidRow(d, cols, x);
if (y + 90 < BAND) v.push(lum(d, cols, x, y + 80) / Math.max(1, lum(d, cols, x, y + 8)));
}
const m = v.reduce((a, b) => a + b, 0) / v.length;
return v.reduce((a, b) => a + (b - m) ** 2, 0) / v.length;
};
expect(spread(LAYERS[2].gully)).toBeGreaterThan(spread(0) * 1.2);
});
});
describe('bake cost', () => {
it('is recorded, not asserted: one chunk of every layer at 1280 x 720', () => {
const period = 1280;
const rows = Math.round(720 * 0.6);
const ms = LAYERS.map((spec) => {
const p: BakeParams = {
spec,
sky: SKY,
period,
scale: 1,
viewH: 720,
rows,
bandH: rows,
px0: 0,
cols: Math.round(period * 1.2),
};
const t0 = performance.now();
bake(p);
return performance.now() - t0;
});
expect(ms.every((v) => Number.isFinite(v) && v >= 0)).toBe(true);
console.info(
`ridge bake ms per layer (node, 1 chunk, 1280x720): ${ms.map((v) => v.toFixed(1)).join(' ')}`
);
});
});
describe('the ridged multifractal core, each rule on its own', () => {
const one = { ...LAYERS[0], octaves: 1, warp: 0, crag: 1 };
it('folds each octave at its crest: a creased, sharp ridge line, not a smooth swell', () => {
// Largest second difference over a fine scan. A smooth curve of this size stays in the hundreds;
// a fold leaves a crease where the noise crosses its midpoint.
const h = 1e-3;
let worst = 0;
for (let i = 1; i < 20000; i += 1) {
const t = i * h;
worst = Math.max(
worst,
Math.abs(profile(one, t + h) - 2 * profile(one, t) + profile(one, t - h)) / (h * h)
);
}
expect(worst).toBeGreaterThan(2000);
});
it('domain-warps: the same layer without its warp is a measurably different line', () => {
const spec = LAYERS[1];
let diff = 0;
for (let i = 0; i < 2000; i += 1)
diff += Math.abs(profile(spec, i / 100) - profile({ ...spec, warp: 0 }, i / 100));
expect(diff / 2000).toBeGreaterThan(0.02);
});
it('weights each octave by the previous one: where the first octave is low, the later ones add almost nothing', () => {
const lo = { ...LAYERS[1], octaves: 1, warp: 0 };
const hi = { ...LAYERS[1], octaves: 5, warp: 0 };
let valley = 0;
let nv = 0;
let peak = 0;
let np = 0;
for (let i = 0; i < 10000; i += 1) {
const a = profile(lo, i / 500);
const d = Math.abs(profile(hi, i / 500) - a);
if (a < 0.12) {
valley += d;
nv += 1;
} else if (a > 0.35) {
peak += d;
np += 1;
}
}
expect(nv).toBeGreaterThan(200);
expect(np).toBeGreaterThan(200);
expect(valley / nv).toBeLessThan(0.25 * (peak / np));
});
it('octave lattices do not line up: the lacunarity is not a power of two', () => {
expect(Number.isInteger(Math.log2(LACUNARITY))).toBe(false);
expect(Number.isInteger(LACUNARITY)).toBe(false);
});
it('distance flattens relief: contrast falls from the nearest layer to the farthest', () => {
const c = LAYERS.map((spec) => layerPaint(spec, SKY).contrast);
expect(c[LAYERS.length - 1]).toBe(1);
for (let i = 1; i < c.length; i += 1) expect(c[i]).toBeGreaterThan(c[i - 1]);
// and it shows in the pixels: the lit/shadow difference is smaller on the farthest layer than the nearest
const gap = (li: number) => {
const cols = 700;
const d = bake(params(li, 0, cols));
const slope = (x: number) => (solidRow(d, cols, x - 1) - solidRow(d, cols, x + 1)) / 2;
const avg = (pick: (s: number) => boolean) => {
const xs = Array.from({ length: cols - 4 }, (_, k) => k + 2).filter((x) => pick(slope(x)));
return (
xs.reduce((a, x) => a + lum(d, cols, x, solidRow(d, cols, x) + 12), 0) /
Math.max(1, xs.length)
);
};
return (
Math.abs(avg((s) => s < -0.8) - avg((s) => s > 0.8)) /
Math.max(
1,
avg(() => true)
)
);
};
expect(gap(0)).toBeLessThan(gap(3));
});
});
describe('the state band: the theme ink clears 4.5:1 on a ridge as on bare sky', () => {
const linear = (v: number) => {
const c = v / 255;
return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
};
const luma = (d: Uint8ClampedArray, o: number) =>
0.2126 * linear(d[o]) + 0.7152 * linear(d[o + 1]) + 0.0722 * linear(d[o + 2]);
/** Lowest and highest luminance over the solid pixels of every layer. */
const range = (band: typeof SKY_BAND[keyof typeof SKY_BAND] | undefined) => {
let lo = 1;
let hi = 0;
for (const li of [0, 1, 2, 3]) {
const p = params(li, 0, 700, { sky: { ...SKY, band } });
const d = bake(p);
for (let o = 0; o < d.length; o += 4) {
if (d[o + 3] < 255) continue;
const l = luma(d, o);
lo = Math.min(lo, l);
hi = Math.max(hi, l);
}
}
return { lo, hi };
};
it("day and dusk: no ridge pixel is darker than the text floor (Day's), on every layer", () => {
for (const band of [SKY_BAND.day, SKY_BAND.dusk]) {
expect(range(band).lo).toBeGreaterThanOrEqual(SKY_BAND.day.floor);
}
});
it('night: no ridge pixel is lighter than the ceiling', () => {
expect(range(SKY_BAND.night).hi).toBeLessThanOrEqual(SKY_BAND.night.ceiling);
});
it('is the identity with no band, so nothing else changes', () => {
expect(layerPaint(LAYERS[2], SKY).shift).toBeNull();
expect(range(undefined).lo).toBeLessThan(SKY_BAND.day.floor);
});
it('keeps the shading: the foot is still darker than the top inside the band', () => {
// Inside the band the face's range is compressed, not removed: dusk, no mist or gullies, bottom row over 12 rows under the crest.
const sky = { ...SKY, band: SKY_BAND.dusk };
for (const li of [0, 1, 2, 3]) {
const cols = 900;
const d = bake(params(li, 0, cols, { sky, spec: { ...LAYERS[li], mist: 0, gully: 0 } }));
let ratio = 0;
let n = 0;
for (let x = 0; x < cols; x += 1) {
const y = solidRow(d, cols, x);
if (y + 90 >= BAND) continue;
n += 1;
ratio += luma(d, ((BAND - 1) * cols + x) * 4) / luma(d, ((y + 12) * cols + x) * 4);
}
expect(n).toBeGreaterThan(30);
// Measured 0.84, 0.85, 0.91, 0.97 far to near: the nearest ground is near black, so its range is small in absolute terms.
expect(ratio / n).toBeLessThan(li < 3 ? 0.95 : 1);
}
});
it('the real states, over a sweep of the sun: no pixel overflows the encoding (an overflow reads as a stray black or yellow channel)', () => {
for (const state of ['day', 'dusk', 'night'] as const) {
for (const sunAzimuth of [0, 0.1, 0.25, 0.3, 0.5, 0.75, 0.9]) {
const sky = { ...ridgeSkyFor(state), sunAzimuth };
for (const li of [0, 1, 2, 3]) {
const d = bake(params(li, 1000, 400, { sky }));
let stray = 0;
for (let o = 0; o < d.length; o += 4) {
if (d[o + 3] < 255) continue;
// A channel far below both others is the signature of a lookup that read past its table.
const lo = Math.min(d[o], d[o + 1], d[o + 2]);
const mid = d[o] + d[o + 1] + d[o + 2] - lo - Math.max(d[o], d[o + 1], d[o + 2]);
if (mid - lo > 120) stray += 1;
}
expect(stray).toBe(0);
}
}
}
});
});

View file

@ -1,221 +0,0 @@
/* eslint-disable no-param-reassign -- `bakeSlice` fills the caller's pixel buffer */
// The pixels of a ridge chunk, as a pure function: no DOM, no canvas. Each column is lit from the
// slope of the heightfield's crest line against the sun; the face under it takes a light-to-haze
// gradient, erosion streaks and a lit rim on the crest. The depth treatment (colour, haze, contrast
// by distance) is ridgeWorld's `layerColour`, `hazeColour` and `transmittance`, used here and not redone.
import {
gullyAt,
HorizonView,
LayerSpec,
layerProfile,
hazeColour,
layerColour,
transmittance,
} from './ridgeWorld';
import type { SkyBand } from '../skyBand';
import { LUMA, SKY_BAND } from '../skyBand';
import type { SkyColours, Srgb } from './skyColour';
export type Vec3 = readonly [number, number, number];
/** Unit vector toward the sun: x across the screen (from the azimuth), y up, z toward the viewer. */
export function sunDirection(sky: SkyColours): Vec3 {
const az = sky.sunAzimuth ?? 0.3;
const x = 0.9 * Math.sin(2 * Math.PI * az);
const l = Math.hypot(x, 0.35, 0.45);
return [x / l, 0.35 / l, 0.45 / l];
}
/** How steeply the crest's slope tilts the face's normal sideways. */
const TILT = 1.2;
/**
* Sun light on the face under a crest whose silhouette has `slope` (rows per column, positive when the
* crest rises toward +x, so the face looks toward -x). 0 in full shadow .. 1 head-on to the sun.
*/
export function faceLight(slope: number, sun: Vec3): number {
const nx = -slope * TILT;
const inv = 1 / Math.hypot(nx, 0.55, 0.85);
const d = (nx * sun[0] + 0.55 * sun[1] + 0.85 * sun[2]) * inv;
return Math.min(1, Math.max(0, (d - 0.2) / 0.7));
}
export interface BakeParams {
spec: LayerSpec;
sky: SkyColours;
/** Pixels in one viewport width: the unit of world-x. */
period: number;
/** CSS px to canvas px. */
scale: number;
/** Viewport height in canvas px. */
viewH: number;
/** Rows of the pixel buffer (the band plus any padding below it). */
rows: number;
/** Row count of the visible band; the crest is measured from the top of it. */
bandH: number;
/** Where world-x is a compass azimuth, for the horizon layer (`LayerSpec.horizon`); absent: every layer is noise. */
horizonView?: HorizonView;
/** World pixel of the buffer's column 0 (may be fractional-free negative: a chunk's left minus its padding). */
px0: number;
/** Columns of the buffer. */
cols: number;
}
const clamp01 = (v: number): number => Math.min(1, Math.max(0, v));
const smoothstep = (a: number, b: number, v: number): number => {
const t = clamp01((v - a) / (b - a));
return t * t * (3 - 2 * t);
};
const SUN_WHITE: Srgb = [255, 238, 205];
/** Everything `bakeSlice` needs per layer, computed once. */
export interface LayerPaint {
/** The state's band as a shift of linear light (raises the floor, lowers the ceiling); null when the sky has none. */
shift: BandShift | null;
base: Srgb;
haze: Srgb;
warm: Srgb;
contrast: number;
sun: Vec3;
/** How much the weather's mist multiplies the layer's own (`weather.ts`): 1 in clear air. */
mistK: number;
}
const decode = (v: number): number => {
const s = v / 255;
return s <= 0.04045 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;
};
const DECODE = Float32Array.from({ length: 256 }, (_, v) => decode(v));
const ENCODE_STEPS = 4095;
const ENCODE = Uint8ClampedArray.from({ length: ENCODE_STEPS + 1 }, (_, i) => {
const l = i / ENCODE_STEPS;
return 255 * (l <= 0.0031308 ? 12.92 * l : 1.055 * l ** (1 / 2.4) - 0.055);
});
/** What the band is held clear of at its edges: the 8-bit rounding and the blur and in-scatter the chunk takes after the bake. */
const BAND_MARGIN = 0.012;
/** Linear light out = in * `scale` + `add[channel]`. */
export interface BandShift {
add: readonly [number, number, number];
scale: number;
}
/**
* The state's band (skyBand.ts) as a shift of linear light, solved once per layer. Adding `add` (the zenith colour, so
* the ridge reads as shadowed by the sky above, a hue apart from the horizon behind it) raises every pixel's
* luminance by exactly the floor, so the face keeps its whole gradient and the darkest pixel is on the floor; scaling by
* `scale` puts the lightest pixel on the ceiling. Smooth in the pixel's own luminance: no cut and no region.
* The floor is the text bound (Day's), not Dusk's higher link bound: the link ink is not set on ridges, and Dusk's
* floor washes the faces to a flat pale slab.
*/
export function bandShift(band: SkyBand | undefined, lift: Srgb): BandShift | null {
if (!band) return null;
const floor = Math.min(band.floor, SKY_BAND.day.floor);
const scale = band.ceiling < 1 ? band.ceiling - BAND_MARGIN / 3 : 1;
if (floor <= 0 && scale === 1) return null;
const lin = lift.map(decode);
const k =
floor > 0
? (floor + BAND_MARGIN) / (LUMA[0] * lin[0] + LUMA[1] * lin[1] + LUMA[2] * lin[2])
: 0;
return { add: [lin[0] * k, lin[1] * k, lin[2] * k], scale };
}
export function layerPaint(spec: LayerSpec, sky: SkyColours): LayerPaint {
const warm: Srgb = [
(sky.horizon[0] + SUN_WHITE[0]) / 2,
(sky.horizon[1] + SUN_WHITE[1]) / 2,
(sky.horizon[2] + SUN_WHITE[2]) / 2,
];
return {
shift: bandShift(sky.band, sky.top),
base: layerColour(spec, sky),
haze: hazeColour(sky),
warm,
// Distance flattens relief: the same transmittance that thins the colour.
contrast: 0.35 + 0.65 * transmittance(spec),
sun: sunDirection(sky),
mistK: 1 + 2.5 * (sky.physical?.weather.mist ?? 0),
};
}
/** Crest row (float, from the top of the band) of every buffer column from c0 - 1 to c1 (inclusive). */
function crestRows(p: BakeParams, c0: number, c1: number): Float64Array {
const out = new Float64Array(c1 - c0 + 2);
const amp = p.spec.relief * p.viewH;
const lift = p.spec.base * p.viewH;
for (let c = c0 - 1; c <= c1; c += 1) {
const top = Math.min(
p.bandH,
lift + amp * layerProfile(p.spec, (p.px0 + c) / p.period, p.horizonView)
);
out[c - c0 + 1] = p.bandH - top;
}
return out;
}
/**
* Paint columns [c0, c1) of the chunk into `out` (RGBA, `p.cols` wide, `p.rows` tall, zeroed). One
* call is a slice of a chunk, so a caller can keep each task short. Seams are exact: a column's pixels
* depend only on its world pixel, never on where the slice or chunk begins.
*/
export function bakeSlice(
out: Uint8ClampedArray,
p: BakeParams,
paint: LayerPaint,
c0: number,
c1: number
): void {
const { spec } = p;
const crest = crestRows(p, c0, c1);
const faceH = 0.45 * p.viewH;
const rim = 2.2 * p.scale;
const glow = 9 * p.scale;
const { base, haze, warm, contrast, sun, shift, mistK } = paint;
for (let c = c0; c < c1; c += 1) {
const cy = crest[c - c0 + 1];
const slope = (crest[c - c0 + 2] - crest[c - c0]) * -0.5; // rows up per column
const lit = faceLight(slope, sun);
const steep = 0.45 + 0.55 * clamp01(Math.abs(slope) * 0.8);
const gl = gullyAt(spec, (p.px0 + c) / p.scale) * steep;
const y0 = Math.max(0, Math.floor(cy));
for (let y = y0; y < p.rows; y += 1) {
const d = y + 0.5 - cy;
const dn = clamp01(d / faceH);
const cover = clamp01(y + 1 - cy);
const gully = spec.gully * gl * smoothstep(0, 0.2, dn);
// Shade the ground colour by the sun; the lit warm light fades down the face and in the gullies.
// Volume from the depth below the crest: the face darkens toward its foot (before the mist lifts it
// toward the haze), and the sun catches its top. A few multiplies per pixel, in the bake only.
const foot = 1 - 0.4 * contrast * smoothstep(0.05, 0.9, dn);
const top = 1 - smoothstep(0, 0.3, dn);
const shade = (0.7 + 0.3 * lit * contrast + 0.3 * (1 - contrast)) * (1 - 0.5 * gully) * foot;
const warmK =
0.22 * contrast * lit * lit * (1 - 0.7 * dn) * (1 - 0.6 * gully) +
0.04 * contrast * lit * top;
// A hard 2 px lip on the crest and a soft glow under it.
const rimK =
(clamp01(1 - d / rim) * 0.4 + (d < glow * 5 ? Math.exp(-d / glow) * 0.08 : 0)) *
contrast *
(0.3 + 0.7 * lit);
const mist = Math.min(0.9, spec.mist * mistK) * smoothstep(0, 1, dn);
const o = (y * p.cols + c) * 4;
for (let i = 0; i < 3; i += 1) {
let v = base[i] * shade + warm[i] * (warmK + rimK);
v += (haze[i] - v) * mist;
out[o + i] = shift
? ENCODE[
Math.min(
ENCODE_STEPS,
((DECODE[v >= 255 ? 255 : v <= 0 ? 0 : Math.round(v)] * shift.scale +
shift.add[i]) *
ENCODE_STEPS) |
0
)
]
: v;
}
out[o + 3] = cover * 255;
}
}
}

View file

@ -1,236 +0,0 @@
// Every number of the ridge scene, and the pure functions that turn them into silhouettes. No DOM.
// A ridge is a function of world-x (t, in viewport widths): ridged multifractal noise, domain-warped,
// one function for every layer with the parameters below. Nothing in it repeats.
import { horizonShape } from './horizon';
import { valueNoise } from './noise';
import type { Srgb, SkyColours } from './skyColour';
export interface LayerSpec {
/** 0 farthest. Also the noise row, so layers never share a profile. */
index: number;
/** Lattice cells per viewport width for the first octave; each octave multiplies it by LACUNARITY. */
baseCells: number;
/** Peak-to-valley height of the silhouette, as a fraction of the viewport height. */
relief: number;
/** 0 rounded hills .. 1 sharp crags: how tightly each octave is folded at its crest. */
crag: number;
/** Ridge top at its lowest point, fraction of the viewport height up from the bottom. */
base: number;
/** 0 = the layer is the horizon haze, 1 = the nearest ground colour. */
depth: number;
/** Seconds for the layer to drift one viewport width. Farther layers are slower. */
seconds: number;
/** Octaves of the ridged multifractal; the bake cost is linear in it. */
octaves: number;
/** Domain warp, in cells of the first octave. */
warp: number;
/** 0..1 depth of the erosion streaks down the face. */
gully: number;
/** Width of one gully, CSS px. */
gullyPx: number;
/** 0..1 how far the foot of the face dissolves into the valley mist. */
mist: number;
/**
* The layer is the real horizon of the viewpoint (`horizon.ts`), not noise: world-x is compass azimuth across the viewport
* (`HorizonView`) and it does not drift, so it stays under the sun it is lit by. Its `relief` is the ring's own range.
*/
horizon?: boolean;
}
/** Where the camera looks and how wide it sees, to read world-x as an azimuth: the sky's own view (`skyDome.ts viewFor`). */
export interface HorizonView {
/** Compass azimuth of the viewport's centre, degrees clockwise from north. */
azimuthDeg: number;
hfovDeg: number;
}
/** The static horizon layer's track sits this many viewport widths left of the screen, so the camera's sway never shows its edge. */
export const HORIZON_TRACK_SHIFT = 0.06;
/** Four layers: one function, four parameter rows. */
export const LAYERS: readonly LayerSpec[] = [
{
index: 0,
baseCells: 2.2,
relief: 0.16,
crag: 0.85,
base: 0.3,
depth: 0.12,
seconds: Infinity,
octaves: 5,
warp: 0.6,
gully: 0.3,
gullyPx: 16,
mist: 0.35,
horizon: true,
},
{
index: 1,
baseCells: 3.0,
relief: 0.2,
crag: 0.7,
base: 0.26,
depth: 0.38,
seconds: 300,
octaves: 5,
warp: 0.55,
gully: 0.5,
gullyPx: 20,
mist: 0.3,
},
{
index: 2,
baseCells: 3.8,
relief: 0.16,
crag: 0.5,
base: 0.17,
depth: 0.66,
seconds: 210,
octaves: 5,
warp: 0.5,
gully: 0.6,
gullyPx: 24,
mist: 0.22,
},
{
index: 3,
baseCells: 4.6,
relief: 0.12,
crag: 0.3,
base: 0.08,
depth: 1,
seconds: 150,
octaves: 4,
warp: 0.45,
gully: 0.7,
gullyPx: 30,
mist: 0.16,
},
];
/** Not an integer power of two, so no octave's lattice lines up with another's. */
export const LACUNARITY = 2.07;
/** Each octave is weighted by the previous octave's folded signal, times this, clamped to 1. */
export const WEIGHT_GAIN = 2;
const AMP_GAIN = 0.5;
/** Calibration: the weighted sum's typical top is about 1 / this. */
const PEAK_NORM = 1.25;
/** Silhouette height in [0, 1] at world-x `t` (viewport widths); defined for every real t. */
export function profile(spec: LayerSpec, t: number): number {
const row = spec.index * 101;
const wt =
t + (spec.warp * (2 * valueNoise(t * spec.baseCells * 0.7, row + 97) - 1)) / spec.baseCells;
const e = 0.03 + (1 - spec.crag) * 0.4;
const top = Math.sqrt(1 + e * e);
let sum = 0;
let norm = 0;
let amp = 1;
let f = spec.baseCells;
let w = 1;
for (let k = 0; k < spec.octaves; k += 1) {
const v = 2 * valueNoise(wt * f, row + k * 13) - 1;
// Fold: 1 at the zero crossing of the noise, 0 at its extremes; `e` rounds the crease.
let sig = 1 - Math.sqrt(v * v + e * e) / top;
sig *= sig;
sig *= w;
w = Math.min(1, sig * WEIGHT_GAIN);
sum += sig * amp;
norm += amp;
amp *= AMP_GAIN;
f *= LACUNARITY;
}
// A soft swell under the folds, so the valleys between sharp crests are not flat floors.
const swell = valueNoise(wt * spec.baseCells * 0.6, row + 211);
return Math.min(1, 0.85 * (sum / norm) * PEAK_NORM + 0.15 * swell);
}
/**
* Silhouette height in [0, 1] of a layer at world-x `t` (viewport widths). The horizon layer reads the baked horizon at the azimuth `t`
* is under `view`; every other layer, or a horizon layer with no view, is the noise `profile`.
*/
export function layerProfile(spec: LayerSpec, t: number, view?: HorizonView): number {
if (!spec.horizon || !view) return profile(spec, t);
return horizonShape(view.azimuthDeg + (t - HORIZON_TRACK_SHIFT - 0.5) * view.hfovDeg);
}
/** Erosion streak strength in [0, 1] at world-x `wxCss` (CSS px): a folded noise at gully width. */
export function gullyAt(spec: LayerSpec, wxCss: number): number {
const g = 1 - Math.abs(2 * valueNoise(wxCss / spec.gullyPx, spec.index * 131 + 5) - 1);
return g * g;
}
/** The distance haze the far layers dissolve into: between the mid sky and the horizon, not the horizon glow itself. */
export function hazeColour(sky: SkyColours): Srgb {
const m = (a: number, b: number) => Math.round(a + (b - a) * 0.4);
return [
m(sky.mid[0], sky.horizon[0]),
m(sky.mid[1], sky.horizon[1]),
m(sky.mid[2], sky.horizon[2]),
];
}
/**
* Aerial perspective, applied once at bake time. A layer at distance d (0 nearest, 1 farthest) is seen
* through Beer-Lambert transmittance T = exp(-extinction * d): it shows T of its own colour and 1 - T of
* the haze, then loses `desaturate * (1 - T)` of its chroma, is softened by up to `blurPx` (canvas
* pixels at scale 1) and takes a faint warm in-scatter of at most `scatter` on the sun side.
*/
export const AERIAL = { extinction: 1.6, desaturate: 0.5, blurPx: 2, scatter: 0.16 };
/** Distance of a layer from the viewer, 0 (nearest) .. 1 (farthest). */
export const layerDistance = (spec: LayerSpec): number => 1 - spec.depth;
export const transmittance = (spec: LayerSpec): number =>
Math.exp(-AERIAL.extinction * layerDistance(spec));
/** Layer colour as seen: its own ground colour thinned by the haze between it and the eye. */
export function layerColour(spec: LayerSpec, sky: SkyColours): Srgb {
const h = hazeColour(sky);
const t = transmittance(spec);
const seen = (i: number) => sky.ground[i] * t + h[i] * (1 - t);
const luma = 0.2126 * seen(0) + 0.7152 * seen(1) + 0.0722 * seen(2);
const keep = 1 - AERIAL.desaturate * (1 - t);
const out = (i: number) => Math.round(luma + (seen(i) - luma) * keep);
return [out(0), out(1), out(2)];
}
/** Edge softening for a layer, in canvas pixels at scale 1; 0 for the nearest. */
export const edgeBlur = (spec: LayerSpec): number => AERIAL.blurPx * (1 - transmittance(spec));
/**
* Alpha of the warm sun-side in-scatter at world-x `t` (viewport widths). Zero when the sky has no
* sun; grows with distance. The lobe is the product of two cosines with incommensurate wavelengths
* (1.9 and 1.9 * golden ratio), so it is not periodic; its peaks are where the sun's azimuth puts them.
*/
export function scatterAlpha(spec: LayerSpec, sky: SkyColours, t: number): number {
if (sky.sunAzimuth === undefined) return 0;
const a = 0.5 + 0.5 * Math.cos(2 * Math.PI * (t / 1.9 - sky.sunAzimuth));
const b = 0.75 + 0.25 * Math.cos((2 * Math.PI * t) / (1.9 * 1.618034));
return AERIAL.scatter * (1 - transmittance(spec)) * a * a * b;
}
/** Chunk width as a multiple of the viewport width: room for the yaw sway on top of the viewport. */
export const CHUNK_OVER = 1.2;
/**
* Integer pixel sizes for a viewport. `period` is one viewport width, the unit of world-x; a chunk
* is `chunk` pixels of world, `height` the band a layer is painted in.
*/
export function layerSize(cssW: number, cssH: number, scale: number) {
const period = Math.round(cssW * scale);
const band = Math.round(cssH * scale * 0.6);
return { period, chunk: Math.round(period * CHUNK_OVER), height: band };
}
/** Ridge-top y (canvas pixels, from the top of the layer's band) at world-x `t` (viewport widths). */
export function ridgeY(
spec: LayerSpec,
t: number,
bandH: number,
viewH: number,
view?: HorizonView
): number {
const topFromBottom = spec.base * viewH + spec.relief * viewH * layerProfile(spec, t, view);
return bandH - Math.min(bandH, topFromBottom);
}

View file

@ -1,10 +1,8 @@
// The one place the sky's colours come from. Void's `src/sky/sun.js` (`light({ time, lat, lon,
// cloud_cover, precipitation })`, PR #20) is the intended source; until it lands this returns a
// fixed twilight: lavender zenith, rose mid-sky, orange horizon. These are the raw stops: the scene draws them
// shaped into the dusk band of ../skyBand.ts (`ridgeSkyFor`, skyForState.ts), whose floor the link of Noonday Sky needs. Replace the body, keep the signature: the scene calls it once, at startup,
// and bakes the result.
// fixed twilight: lavender zenith, rose mid-sky, orange horizon. Replace the body, keep the signature: the scene calls it once, at startup,
// and paints the result.
import type { SkyBand } from '../skyBand';
import type { PhysicalSky } from './physicalSky';
export type Srgb = readonly [number, number, number];
@ -14,14 +12,8 @@ export interface SkyColours {
top: Srgb;
/** Mid-sky band. */
mid: Srgb;
/** Colour at the horizon, which the far ridges fade into. */
/** Colour at the horizon. */
horizon: Srgb;
/** Nearest ridge: the ground colour the farther layers mix away from. */
ground: Srgb;
/** Where the sun sits across one drift period, 0..1; the layers pick up a faint warm in-scatter there. Omit for none. */
sunAzimuth?: number;
/** The state's luminance band (skyBand.ts): the layers are shaped into it as the sky is, so the theme ink clears 4.5:1 on a ridge as on bare sky. Omit for none. */
band?: SkyBand;
/** The physical sky this palette was read from; when present the host canvas is painted from it, not from the three stops. */
physical?: PhysicalSky;
}
@ -31,7 +23,5 @@ export function skyColours(_now: Date = new Date()): SkyColours {
top: [170, 145, 220],
mid: [225, 130, 165],
horizon: [240, 150, 80],
ground: [7, 17, 26],
sunAzimuth: 0.3,
};
}

View file

@ -2,29 +2,13 @@
// LUT (`earthAtmosphere.ts`), and nothing else; one lookup per texel, no march. The result is linear HDR
// radiance per unit of sun irradiance (a `Float32Array`); `toneMap.ts` makes it pixels.
import type { Atmosphere } from './earthAtmosphere';
import type { SunSample } from './vienna';
import type { SunSample } from './sunAt';
import type { Camera } from './view';
import { cloudAmount, cloudRadiance } from './clouds';
const RAD = Math.PI / 180;
/** The sun's angular radius in radians (Hillaire's/Bruneton's `sun_angular_radius`). */
export const SUN_RADIUS = 0.004675;
/** Vertical field of view of the camera, degrees, and where the horizon sits down the image: the ridge scene's sky stops. */
export const VFOV_DEG = 50;
export const HORIZON_AT = 0.78;
export interface DomeView {
/** Compass azimuth the camera faces, degrees clockwise from north. */
azimuthDeg: number;
/** Width over height of the viewport. */
aspect: number;
}
/** Where the camera looks: toward the sun's side while it is up, so the sun sits right of centre; south in the dark. */
export const viewFor = (sun: SunSample, aspect: number): DomeView => ({
azimuthDeg: sun.elevation > -6 ? sun.azimuth - 24 : 180,
aspect,
});
export interface Dome {
w: number;
h: number;
@ -46,18 +30,12 @@ export const domeSize = (cssW: number, cssH: number): { w: number; h: number } =
export function renderDome(
atm: Atmosphere,
sun: SunSample,
view: DomeView,
camera: Camera,
w: number,
h: number,
cover = 0
): Dome {
const hdr = new Float32Array(w * h * 3);
const tanHalf = Math.tan((VFOV_DEG * RAD) / 2);
const fa = view.azimuthDeg * RAD;
const fx = Math.sin(fa);
const fy = Math.cos(fa);
const rx = Math.cos(fa);
const ry = -Math.sin(fa);
const sunAz = sun.azimuth * RAD;
const sunEl = sun.elevation * RAD;
const sz = Math.sin(sunEl);
@ -72,22 +50,19 @@ export function renderDome(
}
let logSum = 0;
for (let j = 0; j < h; j += 1) {
const v = (HORIZON_AT - (j + 0.5) / h) * 2 * tanHalf;
const el = (camera.horizonAt - (j + 0.5) / h) * camera.vfovDeg * RAD;
const vz = Math.sin(el);
const ground = Math.cos(el);
for (let i = 0; i < w; i += 1) {
const u = ((i + 0.5) / w - 0.5) * view.aspect * 2 * tanHalf;
const dx = fx + rx * u;
const dy = fy + ry * u;
const dz = v;
const n = Math.hypot(dx, dy, dz);
const vz = dz / n;
const cosFromSun = Math.cos(Math.atan2(dx, dy) - sunAz);
const az = (camera.azimuthDeg + ((i + 0.5) / w - 0.5) * camera.hfovDeg) * RAD;
const cosFromSun = Math.cos(az - sunAz);
atm.radiance(vz, cosFromSun, rgb);
let r = rgb[0];
let g = rgb[1];
let b = rgb[2];
let lum = 0.2126 * rgb[0] + 0.7152 * rgb[1] + 0.0722 * rgb[2];
if (cover > 0) {
const c = cloudAmount(dx / n, dy / n, vz, cover);
const c = cloudAmount(ground * Math.sin(az), ground * Math.cos(az), vz, cover);
lum += (0.2126 * cloud[0] + 0.7152 * cloud[1] + 0.0722 * cloud[2] - lum) * c;
r += (cloud[0] - r) * c;
g += (cloud[1] - g) * c;
@ -103,20 +78,11 @@ export function renderDome(
return { w, h, hdr, meanLuma: Math.exp(logSum / (w * h)) };
}
/** Where the sun is on the image, as fractions of its width and height (y down), or null when it is behind the camera or below the horizon. */
export function sunOnScreen(sun: SunSample, view: DomeView): { x: number; y: number } | null {
/** Where the sun is on the image (the camera's angle-linear mapping, `view.ts`), as fractions of its width and height (y down), or null when it is behind the camera or below the horizon. */
export function sunOnScreen(sun: SunSample, camera: Camera): { x: number; y: number } | null {
if (sun.elevation < -0.3) return null;
const tanHalf = Math.tan((VFOV_DEG * RAD) / 2);
const fa = view.azimuthDeg * RAD;
const az = sun.azimuth * RAD;
const el = sun.elevation * RAD;
const sx = Math.cos(el) * Math.sin(az);
const sy = Math.cos(el) * Math.cos(az);
const along = sx * Math.sin(fa) + sy * Math.cos(fa);
if (along <= 0.05) return null;
const u = (sx * Math.cos(fa) - sy * Math.sin(fa)) / along;
const v = Math.sin(el) / along;
const x = u / (view.aspect * 2 * tanHalf) + 0.5;
const y = HORIZON_AT - v / (2 * tanHalf);
const dAz = ((sun.azimuth - camera.azimuthDeg + 540) % 360) - 180;
const x = 0.5 + dAz / camera.hfovDeg;
const y = camera.horizonAt - sun.elevation / camera.vfovDeg;
return x < -0.05 || x > 1.05 || y < -0.05 || y > 1.05 ? null : { x, y };
}

View file

@ -0,0 +1,74 @@
import { describe, expect, it } from 'vitest';
import { solarNoon, STATE_ELEVATION, sunAt, sunForState } from './sunAt';
import { FALLBACK_VIEW } from './view';
const P = FALLBACK_VIEW.place;
const at = (iso: string) => new Date(iso);
describe('the sun over Vienna', () => {
it('the Vienna place reproduces the sun the old Vienna constant gave, exactly, at three dates', () => {
// Values taken from the old constant's sunPosition before it was replaced.
const pinned: [string, number, number][] = [
['2026-03-20T06:30:00Z', 14.339283830037077, 106.82531633377323],
['2026-06-21T10:40:00Z', 65.0288142821217, 171.0604046707017],
['2026-12-21T15:20:00Z', -3.19313392852625, 237.64629073006313],
];
for (const [iso, el, az] of pinned) {
const s = sunAt(P, at(iso));
expect(s.elevation).toBe(el);
expect(s.azimuth).toBe(az);
}
});
it('stands at 90 - lat + declination at solar noon: the solstices and equinox', () => {
const june = sunAt(P, solarNoon(P, at('2026-06-21T12:00:00Z')));
expect(june.elevation).toBeCloseTo(90 - P.lat + 23.44, 0);
const december = sunAt(P, solarNoon(P, at('2026-12-21T12:00:00Z')));
expect(december.elevation).toBeCloseTo(90 - P.lat - 23.44, 0);
const march = sunAt(P, solarNoon(P, at('2026-03-20T12:00:00Z')));
expect(march.elevation).toBeCloseTo(90 - P.lat, 0);
});
it('is due south at solar noon, east in the morning and west in the afternoon', () => {
const noon = solarNoon(P, at('2026-09-23T12:00:00Z'));
expect(Math.abs(sunAt(P, noon).azimuth - 180)).toBeLessThan(2); // solarNoon is found to 5 minutes, which is 1.25 degrees of azimuth
expect(sunAt(P, new Date(noon.getTime() - 3 * 3600_000)).azimuth).toBeLessThan(150);
expect(sunAt(P, new Date(noon.getTime() + 3 * 3600_000)).azimuth).toBeGreaterThan(210);
});
it('solar noon at 16.35 E falls near 10:40 UTC in mid November', () => {
// 16.3534 E is 65.4 minutes ahead of Greenwich; the equation of time in mid November is +15 minutes (sun early): 12:00 - 65.5 - 15 = 10:40 UTC.
const n = solarNoon(P, at('2026-11-11T12:00:00Z'));
const minutes = n.getUTCHours() * 60 + n.getUTCMinutes();
expect(minutes).toBeGreaterThan(10 * 60 + 30);
expect(minutes).toBeLessThan(10 * 60 + 50);
});
it('has a short day in December and a long one in June: published 8 h 4 min and 16 h 0 min with refraction, which adds about 6 minutes each end the ephemeris leaves out', () => {
const daylight = (day: string) => {
let n = 0;
for (let m = 0; m < 24 * 60; m += 1)
if (sunAt(P, new Date(Date.parse(day) + m * 60_000)).elevation > 0) n += 1;
return n;
};
expect(Math.abs(daylight('2026-12-21T00:00:00Z') - (8 * 60 + 4))).toBeLessThan(15);
expect(Math.abs(daylight('2026-06-21T00:00:00Z') - (16 * 60 + 0))).toBeLessThan(15);
});
it('sunForState: the real sun when it is in the state, else a moment of today that is', () => {
const noon = solarNoon(P, at('2026-11-11T12:00:00Z'));
expect(sunForState(P, 'day', noon).time).toEqual(noon);
const midnight = new Date(noon.getTime() + 12 * 3600_000);
for (const state of ['day', 'dusk', 'night'] as const) {
for (const now of [noon, midnight, at('2026-11-11T15:30:00Z')]) {
const s = sunForState(P, state, now);
const [lo, hi] = STATE_ELEVATION[state];
expect(s.elevation).toBeGreaterThanOrEqual(lo - 0.01);
expect(s.elevation).toBeLessThanOrEqual(hi + 0.01);
}
}
expect(sunForState(P, 'dusk', noon).elevation).toBeCloseTo(0, 1);
expect(sunForState(P, 'dusk', noon).azimuth).toBeGreaterThan(180); // sets in the west half
});
});

View file

@ -1,11 +1,10 @@
// The sun over the server's viewpoint in Vienna (48.1929826 N, 16.3534237 E, 183 m: Meta's location, the Copernicus DSM height there) at a real moment: one ephemeris, `sunPosition` of src/sky/sun.js (the
// The sun over a Place (`view.ts`) at a real moment: one ephemeris, `sunPosition` of src/sky/sun.js (the
// low-precision NOAA/Meeus series, a hundredth of a degree for 1900..2100; no refraction), and the choice of a moment for each
// fixed sky state. The ridge sky is the physical atmosphere (`earthAtmosphere.ts`) lit from this sun.
import { sunPosition } from '../../../../sky/sun.js';
import { DAY_FROM } from '../skyState';
import type { FixedSkyState } from '../skyBand';
export const VIENNA = { lat: 48.1929826, lon: 16.3534237, altitudeKm: 0.183 } as const;
import type { Place } from './view';
export interface SunSample {
/** Degrees above the horizon, no refraction. */
@ -16,8 +15,8 @@ export interface SunSample {
time: Date;
}
export function viennaSun(time: Date): SunSample {
const { elevation, azimuth } = sunPosition(time, VIENNA.lat, VIENNA.lon) as {
export function sunAt(place: Place, time: Date): SunSample {
const { elevation, azimuth } = sunPosition(time, place.lat, place.lon) as {
elevation: number;
azimuth: number;
};
@ -36,13 +35,13 @@ export const STATE_ELEVATION: Record<FixedSkyState, readonly [number, number]> =
const startOfUtcDay = (t: Date): number =>
Math.floor(t.getTime() / (24 * 60 * MIN)) * 24 * 60 * MIN;
/** The moment of the day (UTC day of `t`, every 5 minutes) the sun is highest over Vienna: solar noon to the step. */
export function solarNoon(t: Date): Date {
/** The moment of the day (UTC day of `t`, every 5 minutes) the sun is highest over the place: solar noon to the step. */
export function solarNoon(place: Place, t: Date): Date {
const d0 = startOfUtcDay(t);
let best = d0;
let bestEl = -90;
for (let m = 0; m < 24 * 60; m += 5) {
const el = viennaSun(new Date(d0 + m * MIN)).elevation;
const el = sunAt(place, new Date(d0 + m * MIN)).elevation;
if (el > bestEl) {
bestEl = el;
best = d0 + m * MIN;
@ -52,24 +51,24 @@ export function solarNoon(t: Date): Date {
}
/**
* The sun a state draws. The real sun over Vienna now when it is in the state's range (so 'auto' is the real sky); otherwise the
* The sun a state draws. The real sun over the place now when it is in the state's range (so 'auto' is the real sky); otherwise the
* sun of today at a moment that is: noon for Day, the evening sunset for Dusk, the opposite of noon for Night. A picker that
* forces Day at night shows a noon sky, as a person looking at a noon photograph would.
*/
export function sunForState(state: FixedSkyState, now: Date): SunSample {
const real = viennaSun(now);
export function sunForState(place: Place, state: FixedSkyState, now: Date): SunSample {
const real = sunAt(place, now);
const [lo, hi] = STATE_ELEVATION[state];
if (real.elevation >= lo && real.elevation <= hi) return real;
const noon = solarNoon(now);
if (state === 'day') return viennaSun(noon);
if (state === 'night') return viennaSun(new Date(noon.getTime() + 12 * 60 * MIN));
const noon = solarNoon(place, now);
if (state === 'day') return sunAt(place, noon);
if (state === 'night') return sunAt(place, new Date(noon.getTime() + 12 * 60 * MIN));
// Dusk: bisect the evening crossing of the horizon between noon and the opposite moment.
let a = noon.getTime();
let b = noon.getTime() + 12 * 60 * MIN;
for (let i = 0; i < 30; i += 1) {
const m = (a + b) / 2;
if (viennaSun(new Date(m)).elevation > 0) a = m;
if (sunAt(place, new Date(m)).elevation > 0) a = m;
else b = m;
}
return viennaSun(new Date((a + b) / 2));
return sunAt(place, new Date((a + b) / 2));
}

View file

@ -75,6 +75,9 @@ export const BUDGET: Record<FixedSkyState, Budget> = {
night: { floor: 0, ceiling: 0.047 - MARGIN, key: 0.03, min: 8, max: 3000 },
};
/** The luma one 8-bit code moves at the day floor (about 0.0037): a lift step smaller than this can leave the encoded luma where it was, so the floor search never steps less. */
const CODE_STEP = 0.0045;
/** The display transform of a sky: exposure, then ACES, then a black lift (veiling glare) in display-linear. */
export interface Display {
exposure: number;
@ -157,7 +160,7 @@ export function fitDisplay(state: FixedSkyState, hdr: Float32Array, meanLuma: nu
lift = (b.floor - r.lo) / (1 - r.lo);
let probe = range({ exposure, lift });
for (let k = 0; k < 6 && probe.lo < b.floor; k += 1) {
lift += (b.floor - probe.lo) * 1.05;
lift += Math.max((b.floor - probe.lo) * 1.05, CODE_STEP);
probe = range({ exposure, lift });
}
r = probe;

View file

@ -1,57 +0,0 @@
import { describe, expect, it } from 'vitest';
import { solarNoon, STATE_ELEVATION, sunForState, VIENNA, viennaSun } from './vienna';
const at = (iso: string) => new Date(iso);
describe('the sun over Vienna', () => {
it('stands at 90 - lat + declination at solar noon: the solstices and equinox', () => {
const june = viennaSun(solarNoon(at('2026-06-21T12:00:00Z')));
expect(june.elevation).toBeCloseTo(90 - VIENNA.lat + 23.44, 0);
const december = viennaSun(solarNoon(at('2026-12-21T12:00:00Z')));
expect(december.elevation).toBeCloseTo(90 - VIENNA.lat - 23.44, 0);
const march = viennaSun(solarNoon(at('2026-03-20T12:00:00Z')));
expect(march.elevation).toBeCloseTo(90 - VIENNA.lat, 0);
});
it('is due south at solar noon, east in the morning and west in the afternoon', () => {
const noon = solarNoon(at('2026-09-23T12:00:00Z'));
expect(Math.abs(viennaSun(noon).azimuth - 180)).toBeLessThan(2); // solarNoon is found to 5 minutes, which is 1.25 degrees of azimuth
expect(viennaSun(new Date(noon.getTime() - 3 * 3600_000)).azimuth).toBeLessThan(150);
expect(viennaSun(new Date(noon.getTime() + 3 * 3600_000)).azimuth).toBeGreaterThan(210);
});
it('solar noon at 16.35 E falls near 10:40 UTC in mid November', () => {
// 16.3534 E is 65.4 minutes ahead of Greenwich; the equation of time in mid November is +15 minutes (sun early): 12:00 - 65.5 - 15 = 10:40 UTC.
const n = solarNoon(at('2026-11-11T12:00:00Z'));
const minutes = n.getUTCHours() * 60 + n.getUTCMinutes();
expect(minutes).toBeGreaterThan(10 * 60 + 30);
expect(minutes).toBeLessThan(10 * 60 + 50);
});
it('has a short day in December and a long one in June: published 8 h 4 min and 16 h 0 min with refraction, which adds about 6 minutes each end the ephemeris leaves out', () => {
const daylight = (day: string) => {
let n = 0;
for (let m = 0; m < 24 * 60; m += 1)
if (viennaSun(new Date(Date.parse(day) + m * 60_000)).elevation > 0) n += 1;
return n;
};
expect(Math.abs(daylight('2026-12-21T00:00:00Z') - (8 * 60 + 4))).toBeLessThan(15);
expect(Math.abs(daylight('2026-06-21T00:00:00Z') - (16 * 60 + 0))).toBeLessThan(15);
});
it('sunForState: the real sun when it is in the state, else a moment of today that is', () => {
const noon = solarNoon(at('2026-11-11T12:00:00Z'));
expect(sunForState('day', noon).time).toEqual(noon);
const midnight = new Date(noon.getTime() + 12 * 3600_000);
for (const state of ['day', 'dusk', 'night'] as const) {
for (const now of [noon, midnight, at('2026-11-11T15:30:00Z')]) {
const s = sunForState(state, now);
const [lo, hi] = STATE_ELEVATION[state];
expect(s.elevation).toBeGreaterThanOrEqual(lo - 0.01);
expect(s.elevation).toBeLessThanOrEqual(hi + 0.01);
}
}
expect(sunForState('dusk', noon).elevation).toBeCloseTo(0, 1);
expect(sunForState('dusk', noon).azimuth).toBeGreaterThan(180); // sets in the west half
});
});

View file

@ -1,306 +0,0 @@
{
"viewpoint": { "lat": 48.1929826, "lon": 16.3534237, "groundM": 183.3 },
"stepDeg": 0.5,
"radiusKm": 60.0,
"minKm": 1.0,
"curvature": "drop d^2/2R*(1-0.13), R=6371000 m",
"source": {
"dataset": "Copernicus DEM GLO-30 (COP-DEM_GLO-30-DGED), a DSM, EGM2008 heights, 1 arc-second grid",
"tiles": [
"N47_00_E015_00",
"N47_00_E016_00",
"N47_00_E017_00",
"N48_00_E015_00",
"N48_00_E016_00",
"N48_00_E017_00",
"N49_00_E015_00",
"N49_00_E016_00",
"N49_00_E017_00"
],
"tileBytesEach": "about 42-44 MB (Cloud Optimized GeoTIFF); nine tiles, about 380 MB, read once, not committed",
"url": "https://copernicus-dem-30m.s3.amazonaws.com/ (AWS Open Data, eu-central-1, no account)",
"licence": "Copernicus DEM licence: free of charge for any use incl. commercial, attribution required, no warranty (https://spacedata.copernicus.eu/collections/copernicus-digital-elevation-model, Licencing)",
"attribution": "Contains modified Copernicus data: DEM \u00a9 DLR e.V. 2010-2014 and \u00a9 Airbus Defence and Space GmbH 2014-2018 provided under COPERNICUS by the European Union and ESA; all rights reserved.",
"bakedBy": "scripts/bake-horizon.py"
},
"elevationDeg": [
1.608, 1.522, 1.467, 1.452, 1.433, 1.426, 1.497, 1.6, 1.597, 1.564, 1.532, 1.495, 1.467, 1.544,
1.62, 1.655, 1.707, 1.775, 1.803, 1.683, 1.532, 1.394, 1.263, 1.297, 1.358, 1.412, 1.433, 1.339,
1.264, 1.269, 1.195, 1.142, 1.13, 1.22, 1.401, 1.368, 1.326, 1.294, 1.378, 1.46, 1.488, 1.516,
1.549, 1.598, 1.647, 1.698, 1.769, 1.816, 1.801, 1.783, 1.695, 1.619, 1.586, 1.547, 1.442,
1.335, 1.282, 1.286, 1.335, 1.421, 1.449, 1.437, 1.313, 1.268, 1.164, 1.14, 1.098, 1.06, 0.984,
0.938, 0.91, 0.877, 0.884, 0.872, 0.943, 0.974, 0.919, 0.873, 0.861, 0.839, 0.731, 0.673, 0.56,
0.494, 0.527, 0.601, 0.566, 0.584, 0.579, 0.643, 0.666, 0.741, 0.851, 0.711, 0.703, 0.573, 0.54,
0.488, 0.433, 0.452, 0.467, 0.483, 0.475, 0.517, 0.419, 0.428, 0.457, 0.489, 0.522, 0.482, 0.5,
0.527, 0.528, 0.471, 0.458, 0.487, 0.521, 0.578, 0.553, 0.419, 0.301, 0.324, 0.425, 0.468,
0.512, 0.558, 0.575, 0.521, 0.477, 0.444, 0.372, 0.297, 0.365, 0.446, 0.486, 0.523, 0.532,
0.516, 0.516, 0.531, 0.52, 0.546, 0.565, 0.535, 0.527, 0.508, 0.516, 0.37, 0.359, 0.33, 0.375,
0.454, 0.546, 0.496, 0.449, 0.425, 0.447, 0.398, 0.388, 0.439, 0.476, 0.496, 0.517, 0.541,
0.494, 0.439, 0.383, 0.411, 0.502, 0.621, 0.578, 0.548, 0.568, 0.642, 0.713, 0.708, 0.654,
0.704, 0.741, 0.709, 0.677, 0.661, 0.66, 0.666, 0.662, 0.651, 0.699, 0.727, 0.746, 0.809, 0.788,
0.799, 0.793, 0.702, 0.734, 0.79, 0.754, 0.704, 0.616, 0.598, 0.708, 0.719, 0.706, 0.704, 0.726,
0.826, 0.849, 0.85, 0.813, 0.853, 0.926, 0.971, 0.918, 1.003, 1.0, 0.927, 0.985, 1.031, 1.043,
1.067, 1.13, 1.022, 1.02, 1.043, 1.101, 1.144, 1.099, 1.082, 1.064, 1.2, 1.238, 1.16, 1.084,
1.202, 1.326, 1.39, 1.429, 1.405, 1.332, 1.358, 1.361, 1.357, 1.354, 1.335, 1.319, 1.376, 1.309,
1.289, 1.257, 1.283, 1.237, 1.296, 1.353, 1.259, 1.314, 1.236, 1.307, 1.344, 1.36, 1.251, 1.292,
1.308, 1.26, 1.288, 1.365, 1.391, 1.414, 1.32, 1.299, 1.35, 1.288, 1.361, 1.303, 1.268, 1.29,
1.305, 1.326, 1.344, 1.328, 1.32, 1.358, 1.375, 1.443, 1.477, 1.492, 1.525, 1.513, 1.422, 1.314,
1.294, 1.337, 1.414, 1.343, 1.397, 1.431, 1.409, 1.378, 1.423, 1.438, 1.343, 1.257, 1.208,
1.209, 1.233, 1.251, 1.267, 1.274, 1.248, 1.254, 1.297, 1.34, 1.359, 1.34, 1.199, 1.162, 1.255,
1.292, 1.29, 1.24, 1.222, 1.24, 1.271, 1.264, 1.317, 1.256, 1.26, 1.321, 1.301, 1.266, 1.268,
1.308, 1.357, 1.474, 1.419, 1.438, 1.393, 1.354, 1.343, 1.354, 1.333, 1.321, 1.359, 1.437, 1.47,
1.395, 1.413, 1.446, 1.466, 1.434, 1.446, 1.458, 1.419, 1.522, 1.502, 1.506, 1.512, 1.528,
1.526, 1.485, 1.427, 1.508, 1.5, 1.48, 1.534, 1.514, 1.449, 1.449, 1.386, 1.368, 1.388, 1.404,
1.374, 1.374, 1.352, 1.3, 1.347, 1.332, 1.265, 1.235, 1.39, 1.285, 1.226, 1.233, 1.235, 1.211,
1.164, 1.183, 1.217, 1.143, 1.128, 1.117, 1.112, 1.086, 1.071, 1.118, 1.103, 1.002, 0.99, 0.995,
1.025, 0.998, 1.024, 0.971, 1.045, 1.09, 1.11, 1.108, 1.081, 1.171, 1.308, 1.446, 1.482, 1.484,
1.494, 1.469, 1.472, 1.479, 1.493, 1.467, 1.388, 1.293, 1.203, 1.104, 1.145, 1.12, 1.028, 1.004,
0.982, 0.968, 0.933, 0.97, 0.986, 1.035, 1.12, 1.227, 1.281, 1.316, 1.312, 1.273, 1.187, 1.205,
1.197, 1.199, 1.176, 1.177, 1.142, 1.19, 1.377, 1.369, 1.322, 1.295, 1.385, 1.493, 1.576, 1.679,
1.769, 1.622, 1.711, 1.641, 1.517, 1.582, 1.525, 1.39, 1.389, 1.343, 1.331, 1.369, 1.343, 1.257,
1.212, 1.134, 1.039, 1.03, 1.075, 1.027, 1.059, 1.112, 1.132, 1.14, 1.16, 1.167, 1.178, 1.179,
1.192, 1.214, 1.245, 1.289, 1.286, 1.279, 1.259, 1.256, 1.202, 1.138, 1.159, 1.116, 1.12, 1.102,
0.999, 1.042, 1.051, 1.09, 1.143, 1.244, 1.269, 1.296, 1.353, 1.412, 1.579, 1.686, 1.814, 1.857,
1.773, 1.731, 1.748, 1.742, 1.668, 1.594, 1.628, 1.692, 1.76, 1.822, 1.863, 1.904, 1.895, 1.849,
1.786, 1.778, 1.731, 1.666, 1.633, 1.561, 1.526, 1.662, 1.747, 1.75, 1.704, 1.631, 1.563, 1.473,
1.501, 1.488, 1.464, 1.441, 1.386, 1.31, 1.298, 1.293, 1.306, 1.371, 1.42, 1.416, 1.417, 1.418,
1.409, 1.399, 1.408, 1.431, 1.434, 1.437, 1.509, 1.544, 1.58, 1.674, 1.528, 1.534, 1.517, 1.497,
1.525, 1.546, 1.562, 1.58, 1.55, 1.634, 1.614, 1.66, 1.643, 1.663, 1.68, 1.829, 1.99, 2.058,
2.014, 1.911, 2.025, 2.023, 2.036, 2.031, 2.009, 2.012, 2.02, 2.109, 2.107, 2.12, 2.171, 2.179,
2.125, 2.084, 2.09, 2.129, 2.183, 2.16, 2.172, 2.239, 2.237, 2.218, 2.217, 2.137, 2.169, 2.203,
2.233, 2.262, 2.272, 2.21, 2.133, 1.955, 1.909, 1.97, 2.138, 2.187, 2.229, 2.278, 2.191, 2.167,
2.213, 2.327, 2.409, 2.31, 2.242, 2.205, 2.152, 2.154, 2.258, 2.217, 2.126, 2.054, 1.97, 1.942,
2.022, 1.894, 1.886, 1.921, 1.897, 1.936, 1.964, 1.978, 1.961, 1.927, 1.879, 1.904, 1.96, 1.9,
1.922, 2.023, 2.061, 2.115, 1.99, 1.805, 1.736, 1.776, 1.841, 1.863, 1.934, 1.942, 1.962, 2.043,
2.082, 2.082, 2.092, 2.062, 1.957, 1.794, 1.821, 1.913, 2.009, 2.066, 2.105, 2.036, 1.964,
1.984, 2.005, 2.036, 2.072, 2.094, 2.022, 2.153, 2.177, 2.146, 2.125, 2.061, 2.004, 1.955,
1.902, 1.84, 1.767, 1.7, 1.696, 1.718, 1.725, 1.709, 1.745, 1.805, 1.836, 1.939, 2.053, 2.14,
2.139, 2.142, 2.035, 1.902, 1.779, 1.667, 1.564, 1.57, 1.581, 1.628, 1.693, 1.739, 1.738, 1.738,
1.675
],
"distanceKm": [
1.0, 1.0, 1.06, 1.06, 1.06, 1.06, 1.06, 1.03, 1.0, 1.0, 1.0, 1.0, 1.09, 1.09, 1.0, 1.09, 1.09,
1.09, 1.09, 1.09, 1.09, 1.09, 1.09, 1.12, 1.12, 1.12, 1.12, 1.12, 1.12, 1.09, 1.09, 1.09, 1.09,
1.0, 1.0, 1.0, 1.0, 1.03, 1.06, 1.06, 1.03, 1.03, 1.03, 1.03, 1.03, 1.03, 1.0, 1.0, 1.0, 1.0,
1.0, 1.03, 1.03, 1.03, 1.03, 1.03, 1.0, 1.0, 1.06, 1.06, 1.06, 1.06, 1.06, 1.12, 1.0, 1.0, 1.24,
1.24, 1.21, 1.21, 1.3, 1.3, 1.27, 1.3, 1.33, 1.33, 1.33, 1.33, 1.27, 1.24, 1.27, 1.27, 1.27,
1.03, 1.03, 1.42, 1.84, 1.84, 1.81, 1.03, 1.72, 1.63, 1.63, 1.63, 1.33, 1.33, 1.06, 1.06, 1.72,
1.72, 1.69, 1.69, 1.69, 1.69, 1.69, 1.81, 1.81, 1.75, 1.51, 1.75, 1.75, 1.75, 1.75, 1.75, 1.72,
1.72, 1.72, 1.72, 1.72, 1.72, 1.72, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.42,
1.42, 1.24, 1.3, 1.3, 1.3, 1.3, 1.3, 1.3, 1.3, 1.27, 1.03, 1.36, 1.36, 1.36, 1.36, 1.36, 1.45,
1.45, 1.09, 1.12, 1.12, 1.54, 1.57, 1.48, 1.48, 1.48, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.18, 1.18, 1.21, 1.21, 1.21, 1.12, 1.12, 1.12, 1.12, 1.12, 1.15, 1.15, 1.15, 1.15, 1.18,
1.15, 1.15, 1.24, 1.24, 1.27, 1.27, 1.24, 1.24, 1.24, 1.27, 1.27, 1.27, 1.9, 1.9, 1.93, 1.93,
1.93, 1.99, 1.99, 1.99, 1.99, 1.9, 2.02, 2.02, 2.02, 2.05, 2.02, 1.99, 1.99, 1.99, 1.48, 1.21,
1.21, 1.57, 1.69, 1.69, 1.66, 1.39, 1.39, 1.39, 1.54, 1.6, 1.54, 1.54, 1.54, 1.48, 1.45, 1.21,
1.21, 1.21, 1.27, 1.39, 1.39, 1.36, 1.39, 1.39, 1.39, 1.33, 1.33, 1.33, 1.33, 1.33, 1.48, 1.45,
1.39, 1.39, 1.18, 1.18, 1.18, 1.48, 1.48, 1.27, 1.24, 1.27, 1.36, 1.36, 1.39, 1.33, 1.3, 1.3,
1.3, 1.3, 1.3, 1.3, 1.33, 1.0, 1.42, 1.42, 1.27, 1.27, 1.27, 1.27, 1.03, 1.03, 1.03, 1.03, 1.03,
1.03, 1.03, 1.03, 1.06, 1.06, 1.03, 1.03, 1.03, 1.36, 1.06, 1.06, 1.06, 1.06, 1.09, 1.09, 1.18,
1.21, 1.09, 1.09, 1.09, 1.09, 1.09, 1.09, 1.09, 1.09, 1.09, 1.09, 1.15, 1.15, 1.36, 1.36, 1.36,
1.36, 1.33, 1.33, 3.13, 1.21, 1.21, 3.34, 3.31, 2.83, 2.65, 2.65, 1.3, 2.29, 1.3, 1.15, 1.15,
1.15, 2.29, 2.68, 2.41, 2.23, 2.23, 2.2, 1.12, 2.23, 2.17, 2.17, 2.17, 1.84, 2.74, 1.87, 1.87,
2.95, 2.2, 2.17, 2.17, 2.35, 2.35, 2.23, 2.23, 2.11, 2.14, 2.08, 2.2, 2.2, 2.2, 2.2, 2.2, 2.2,
2.41, 2.41, 2.38, 2.41, 2.41, 2.44, 2.44, 2.44, 1.99, 2.08, 2.08, 2.08, 2.08, 2.05, 2.05, 2.5,
2.38, 2.47, 2.29, 2.23, 2.17, 1.69, 1.69, 2.38, 2.47, 2.38, 2.17, 2.71, 2.71, 2.2, 2.47, 2.41,
2.41, 2.32, 2.38, 2.38, 2.08, 2.5, 2.11, 2.11, 1.75, 1.75, 1.48, 1.81, 1.81, 1.78, 1.78, 1.87,
17.2, 17.2, 17.26, 17.32, 17.44, 18.1, 17.56, 17.5, 17.38, 17.5, 17.62, 17.68, 17.74, 17.8,
14.86, 14.92, 14.98, 2.98, 26.38, 26.38, 26.38, 26.62, 26.14, 26.02, 26.02, 26.38, 26.26, 26.38,
26.38, 26.26, 26.26, 26.26, 14.98, 14.98, 14.98, 14.44, 13.12, 13.18, 13.66, 13.72, 13.72,
13.84, 13.84, 13.84, 13.72, 12.52, 12.58, 12.58, 12.28, 12.22, 12.28, 16.54, 14.02, 15.04,
16.18, 16.24, 17.08, 17.26, 17.44, 17.5, 17.44, 17.32, 17.44, 17.02, 13.96, 14.14, 9.22, 9.16,
9.1, 9.22, 9.28, 9.28, 9.22, 9.16, 9.16, 9.34, 8.68, 8.68, 8.62, 8.74, 8.86, 8.92, 8.86, 8.92,
8.92, 11.74, 14.08, 11.98, 12.22, 12.04, 34.3, 10.36, 10.06, 9.64, 9.58, 9.58, 9.7, 9.7, 10.18,
10.18, 10.18, 10.24, 10.06, 10.48, 9.94, 9.82, 9.76, 9.7, 8.02, 8.14, 8.14, 8.14, 8.08, 8.56,
8.38, 8.38, 8.32, 8.38, 8.02, 8.02, 8.02, 8.14, 8.2, 7.9, 7.9, 7.84, 7.78, 7.66, 7.72, 7.72,
7.72, 1.0, 1.0, 1.0, 1.0, 1.0, 1.45, 1.21, 1.21, 1.3, 1.15, 1.15, 1.15, 1.15, 1.15, 1.15, 1.15,
1.33, 1.33, 1.33, 1.48, 1.3, 1.3, 1.3, 1.42, 1.09, 2.02, 1.81, 2.59, 2.59, 2.53, 1.21, 1.75,
1.69, 1.66, 2.38, 1.57, 1.57, 1.57, 7.18, 7.18, 7.24, 7.24, 7.18, 7.12, 1.0, 1.0, 1.0, 1.0, 1.0,
7.36, 7.3, 7.3, 7.18, 7.24, 7.06, 7.06, 7.06, 7.06, 7.0, 7.12, 7.12, 7.12, 7.0, 7.0, 7.0, 7.0,
7.24, 7.24, 7.06, 7.18, 7.0, 7.06, 7.0, 7.0, 6.94, 7.0, 7.24, 1.03, 1.03, 1.03, 1.0, 1.0, 1.0,
1.06, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.03, 1.03, 1.03, 1.03, 1.03, 1.03, 1.27, 1.27, 1.27,
1.24, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.09, 1.09, 1.06, 1.06, 1.06, 1.06, 1.06,
1.09, 1.09, 9.64, 9.58, 9.64, 9.64, 9.7, 9.7, 9.7, 9.7, 9.7, 9.64, 9.64, 9.64, 9.7, 1.03, 1.03,
1.03, 1.03, 1.03, 1.03, 9.52, 1.0, 1.0, 9.4, 8.68, 8.68, 8.68, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 9.52, 9.46, 9.46, 9.46, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0
],
"peakHeightM": [
211.4, 209.9, 210.5, 210.2, 209.9, 209.7, 211.0, 212.1, 211.2, 210.6, 210.1, 209.4, 211.3,
212.7, 211.6, 214.8, 215.8, 217.1, 217.7, 215.4, 212.5, 209.9, 207.4, 208.7, 209.9, 211.0,
211.4, 209.5, 208.1, 207.5, 206.1, 205.1, 204.8, 204.6, 207.8, 207.2, 206.5, 206.6, 208.8,
210.4, 210.1, 210.6, 211.2, 212.1, 213.0, 213.9, 214.2, 215.0, 214.8, 214.5, 212.9, 212.5,
211.9, 211.2, 209.3, 207.3, 205.7, 205.8, 208.1, 209.6, 210.2, 209.9, 207.6, 208.2, 203.7,
203.2, 207.2, 206.3, 204.2, 203.2, 204.0, 203.3, 203.0, 203.2, 205.3, 206.0, 204.7, 203.7,
202.5, 201.5, 199.6, 198.3, 195.8, 192.2, 192.8, 198.3, 201.7, 202.3, 201.8, 194.9, 203.5,
204.5, 207.7, 203.7, 199.7, 196.7, 193.3, 192.4, 196.5, 197.1, 197.2, 197.7, 197.5, 198.7,
195.8, 197.0, 197.9, 198.4, 197.2, 198.2, 198.7, 199.6, 199.6, 197.9, 197.2, 198.1, 199.1,
200.8, 200.1, 196.0, 192.5, 189.0, 190.8, 191.5, 192.3, 193.1, 193.4, 192.4, 191.7, 191.1,
189.8, 190.8, 192.5, 193.0, 194.4, 195.2, 195.5, 195.1, 195.1, 195.4, 195.2, 195.5, 193.5,
196.1, 195.9, 195.5, 195.6, 192.2, 192.5, 191.8, 190.5, 192.2, 194.0, 196.8, 195.7, 194.4,
195.0, 193.7, 190.1, 191.0, 191.7, 192.0, 192.4, 192.8, 192.0, 191.0, 190.0, 191.8, 193.7,
196.5, 195.6, 194.9, 194.5, 195.9, 197.3, 197.2, 196.1, 197.5, 198.2, 197.6, 196.9, 197.0,
196.6, 196.7, 197.7, 197.5, 198.9, 199.5, 199.5, 200.9, 200.4, 201.1, 201.0, 198.9, 207.9,
209.7, 208.9, 207.2, 204.3, 204.3, 208.1, 208.5, 208.1, 206.9, 209.1, 212.7, 213.5, 214.0,
212.2, 213.2, 215.7, 217.3, 207.1, 204.6, 204.5, 208.8, 212.5, 213.9, 213.7, 209.3, 210.8,
208.2, 210.9, 212.6, 213.0, 214.2, 213.0, 211.4, 210.3, 208.7, 209.5, 207.9, 207.4, 212.6,
215.6, 216.4, 218.1, 217.5, 215.7, 214.9, 215.0, 214.9, 214.8, 214.4, 217.5, 218.3, 215.2,
214.7, 209.3, 209.8, 208.9, 216.9, 218.4, 211.3, 211.8, 210.8, 214.4, 215.3, 216.4, 212.4,
212.7, 213.1, 212.0, 212.6, 214.4, 215.0, 216.2, 206.4, 215.6, 216.9, 211.9, 213.6, 212.3,
211.5, 206.5, 206.8, 207.2, 207.5, 207.2, 207.1, 207.8, 208.1, 210.0, 210.7, 210.2, 210.8,
210.5, 217.2, 207.7, 207.3, 208.1, 209.5, 208.9, 209.9, 212.8, 213.1, 209.6, 210.4, 210.7,
208.9, 207.3, 206.3, 206.4, 206.8, 207.2, 207.5, 208.9, 208.4, 213.2, 214.2, 215.2, 215.7,
214.5, 211.2, 247.4, 209.9, 210.7, 259.2, 255.7, 244.2, 241.1, 242.6, 212.1, 236.3, 211.9,
208.7, 209.9, 209.5, 234.2, 243.1, 238.7, 236.5, 241.0, 238.1, 211.5, 237.8, 234.9, 234.5,
234.9, 226.3, 246.9, 227.9, 230.4, 259.6, 237.2, 237.1, 238.4, 243.8, 242.5, 239.9, 240.4,
235.8, 240.4, 238.1, 241.4, 241.7, 242.3, 242.2, 240.6, 238.4, 247.1, 246.8, 245.2, 248.2,
247.4, 245.4, 245.4, 242.7, 231.1, 234.0, 234.5, 233.4, 233.5, 231.9, 230.1, 242.5, 239.0,
238.2, 233.0, 237.7, 232.3, 219.6, 219.9, 235.0, 235.9, 232.0, 228.4, 241.4, 237.8, 226.9,
231.8, 230.5, 229.4, 227.0, 230.1, 229.5, 219.9, 226.9, 220.2, 221.3, 214.0, 214.8, 208.5,
216.5, 217.9, 218.0, 217.9, 218.8, 554.9, 596.3, 639.3, 651.8, 655.9, 677.7, 654.8, 653.7,
652.5, 660.3, 655.7, 632.9, 605.1, 578.6, 484.8, 496.7, 491.5, 237.4, 693.3, 682.9, 676.3,
665.1, 672.6, 677.4, 699.5, 746.5, 793.0, 820.8, 836.9, 831.7, 813.8, 774.3, 513.7, 511.5,
512.0, 493.9, 464.6, 457.9, 479.7, 526.0, 524.0, 515.7, 509.3, 531.0, 553.8, 538.4, 562.8,
582.7, 541.4, 558.5, 545.3, 640.0, 583.8, 599.0, 593.8, 595.2, 603.6, 604.7, 620.9, 614.5,
586.6, 570.3, 549.1, 511.8, 447.5, 462.2, 354.3, 358.3, 365.5, 371.3, 373.8, 377.0, 376.8,
377.3, 377.6, 383.6, 372.4, 377.1, 382.3, 384.6, 386.4, 384.8, 382.9, 375.9, 365.9, 430.3,
471.0, 427.2, 428.5, 403.2, 887.7, 380.7, 381.5, 382.0, 397.5, 401.8, 409.1, 418.8, 441.2,
470.9, 490.1, 514.8, 516.4, 515.3, 490.4, 489.5, 486.6, 472.2, 410.8, 419.1, 428.3, 437.9,
444.8, 466.6, 466.6, 465.3, 456.6, 449.4, 436.6, 430.0, 421.0, 419.8, 411.4, 398.0, 416.8,
426.6, 425.1, 415.1, 407.2, 398.0, 385.9, 209.6, 209.3, 208.9, 208.5, 207.5, 216.6, 210.8,
210.7, 213.0, 210.9, 211.9, 211.8, 211.8, 211.8, 211.6, 211.4, 216.1, 216.6, 216.7, 220.6,
217.6, 218.4, 219.2, 224.9, 212.4, 237.7, 231.4, 251.4, 252.7, 252.0, 216.4, 231.7, 229.2,
230.8, 250.7, 228.9, 228.5, 229.0, 397.4, 416.0, 438.4, 447.0, 439.3, 424.3, 218.7, 218.7,
218.9, 218.8, 218.4, 445.6, 444.4, 455.8, 451.0, 454.8, 454.3, 455.3, 448.6, 443.6, 442.1,
451.4, 458.2, 455.3, 452.1, 460.3, 460.0, 457.7, 467.1, 457.0, 454.1, 463.0, 459.5, 465.5,
464.3, 456.8, 445.0, 425.5, 428.2, 218.8, 221.8, 222.7, 222.3, 223.1, 221.6, 223.5, 222.0,
224.0, 225.4, 223.7, 222.5, 221.8, 220.9, 222.1, 224.0, 223.2, 221.6, 220.3, 218.8, 226.4,
228.2, 225.4, 224.2, 216.9, 216.5, 217.1, 217.6, 217.9, 217.6, 217.0, 216.1, 216.6, 220.6,
219.5, 218.9, 220.8, 221.5, 222.5, 220.2, 217.7, 216.4, 488.4, 497.5, 503.2, 515.2, 518.5,
522.0, 535.7, 542.3, 542.3, 541.8, 536.7, 518.9, 493.5, 216.1, 217.8, 219.5, 220.5, 221.2,
220.0, 516.0, 218.0, 218.4, 523.4, 502.5, 505.8, 494.9, 220.9, 221.3, 220.8, 220.4, 219.3,
218.3, 217.5, 216.5, 215.5, 214.2, 213.0, 212.9, 213.3, 213.5, 473.5, 477.6, 487.5, 492.6,
217.2, 219.2, 220.7, 220.7, 220.7, 218.9, 216.5, 214.4, 212.5, 210.6, 210.7, 210.9, 211.8,
212.9, 213.7, 213.7, 213.7, 212.6
],
"slopeEast": [
0.002, -0.001, 0.056, 0.043, 0.022, 0.002, 0.007, -0.005, 0.014, 0.012, 0.002, -0.013, 0.046,
0.027, -0.083, -0.005, -0.017, -0.01, -0.007, -0.03, -0.051, -0.056, -0.058, -0.088, -0.109,
-0.099, -0.085, -0.079, -0.077, 0.008, 0.005, 0.006, -0.01, 0.062, 0.068, 0.071, 0.074, 0.084,
0.019, -0.001, 0.023, 0.02, 0.021, 0.024, 0.025, 0.031, 0.054, 0.031, 0.003, -0.022, -0.034,
-0.054, -0.055, -0.049, -0.047, -0.045, -0.038, -0.041, -0.051, -0.054, -0.073, -0.086, -0.104,
-0.05, -0.085, -0.083, 0.125, 0.082, 0.049, 0.018, 0.064, 0.034, -0.013, -0.004, -0.06, -0.087,
-0.128, -0.166, -0.129, -0.038, -0.154, -0.158, -0.163, -0.116, -0.12, 0.088, 0.055, 0.048,
0.095, 0.036, 0.154, 0.016, -0.009, -0.018, -0.079, -0.121, -0.161, -0.174, 0.003, 0.037, 0.108,
0.103, 0.1, 0.106, 0.101, -0.064, -0.045, 0.013, 0.149, 0.083, 0.078, 0.071, 0.039, -0.062,
-0.065, -0.118, -0.105, -0.072, -0.045, -0.025, -0.001, -0.008, -0.007, -0.02, -0.037, -0.058,
-0.078, -0.075, -0.069, -0.063, -0.054, -0.008, 0.003, 0.123, 0.036, 0.015, -0.005, 0.017,
0.049, 0.086, 0.068, 0.013, 0.032, -0.024, -0.01, 0.005, 0.009, -0.005, -0.004, 0.021, 0.062,
-0.012, -0.014, -0.013, -0.031, 0.02, -0.004, -0.021, -0.005, -0.018, -0.026, -0.032, -0.038,
-0.046, -0.055, -0.063, -0.067, -0.007, 0.009, -0.048, -0.051, -0.051, -0.004, -0.015, -0.025,
-0.018, 0.002, -0.002, 0.005, 0.013, 0.02, 0.049, 0.013, 0.002, -0.023, -0.009, -0.075, -0.065,
0.018, 0.02, 0.024, -0.023, -0.022, -0.049, 0.01, 0.056, -0.003, -0.001, 0.008, -0.048, -0.05,
-0.03, -0.015, 0.002, -0.045, -0.077, -0.052, -0.113, -0.035, 0.052, 0.029, -0.006, -0.008,
0.119, 0.111, 0.049, 0.019, -0.001, -0.008, 0.003, 0.025, 0.028, 0.004, -0.059, 0.008, 0.005,
-0.0, 0.055, 0.03, -0.044, -0.024, -0.002, -0.029, -0.015, -0.028, -0.04, -0.079, -0.084,
-0.073, 0.118, 0.104, 0.095, 0.098, 0.122, -0.042, 0.014, 0.108, 0.117, -0.077, -0.066, -0.052,
0.006, 0.018, -0.026, -0.008, 0.016, -0.019, -0.02, 0.006, 0.033, 0.056, 0.035, 0.017, 0.002,
0.011, 0.009, -0.015, 0.039, -0.016, -0.019, 0.073, 0.06, 0.028, 0.012, -0.052, -0.037, -0.027,
-0.026, -0.025, -0.027, -0.034, -0.033, -0.112, -0.098, -0.018, -0.005, 0.014, -0.002, -0.012,
-0.021, -0.041, -0.06, -0.073, -0.068, -0.007, 0.023, -0.002, 0.014, 0.031, 0.042, 0.045, 0.05,
0.06, 0.075, 0.066, 0.054, 0.029, 0.03, -0.066, -0.045, -0.033, -0.035, 0.1, 0.072, 0.022,
-0.017, 0.002, 0.006, 0.105, 0.007, -0.015, 0.014, -0.019, -0.045, 0.021, -0.027, -0.032,
-0.043, 0.023, -0.048, 0.051, -0.072, -0.099, -0.013, 0.016, 0.062, 0.05, 0.066, 0.041, 0.022,
-0.031, -0.028, -0.003, 0.047, -0.019, -0.007, 0.014, -0.047, 0.019, -0.023, -0.023, 0.008,
-0.064, 0.049, -0.035, -0.026, 0.01, 0.043, 0.057, 0.094, -0.076, -0.07, 0.043, 0.029, 0.051,
0.001, 0.036, 0.056, -0.016, -0.042, -0.015, -0.0, 0.037, 0.052, 0.089, 0.032, 0.021, 0.06,
-0.018, 0.018, 0.002, 0.036, 0.058, -0.043, 0.005, -0.0, -0.004, -0.084, -0.088, 0.055, -0.004,
0.02, 0.027, 0.018, -0.003, -0.013, -0.01, -0.005, 0.032, 0.037, -0.085, -0.114, 0.016, -0.076,
-0.081, 0.043, 0.029, 0.014, -0.247, -0.334, -0.183, -0.03, -0.023, 0.027, 0.067, 0.018, -0.17,
0.04, 0.076, 0.16, 0.142, 0.214, -0.239, -0.036, 0.236, 0.006, -0.075, -0.015, -0.134, 0.191,
-0.098, -0.235, -0.094, -0.348, -0.156, -0.126, 0.06, -0.008, 0.039, 0.184, 0.048, 0.037, 0.035,
-0.057, -0.06, 0.062, -0.295, -0.071, -0.175, 0.046, -0.045, -0.173, -0.173, -0.161, -0.148,
0.042, -0.034, -0.145, 0.247, 0.002, -0.209, 0.028, -0.129, -0.037, 0.154, -0.069, -0.023,
0.149, 0.119, -0.146, 0.175, 0.112, -0.005, -0.029, -0.11, -0.064, -0.06, -0.018, -0.04, 0.03,
-0.087, -0.043, -0.015, -0.031, -0.13, -0.068, -0.06, 0.001, -0.021, -0.0, -0.013, 0.032, 0.012,
-0.025, 0.013, 0.081, 0.008, -0.101, 0.006, -0.063, -0.072, -0.034, -0.109, -0.03, -0.058,
-0.068, -0.174, -0.1, -0.092, -0.088, -0.053, 0.072, 0.007, -0.043, -0.029, -0.004, -0.033,
-0.07, -0.071, -0.07, -0.077, -0.07, -0.021, -0.02, -0.061, -0.004, -0.114, -0.02, -0.023,
0.032, 0.05, -0.037, -0.005, -0.02, -0.109, -0.063, 0.014, -0.015, -0.037, 0.007, 0.019, 0.035,
0.051, 0.064, -0.018, -0.034, -0.025, 0.038, -0.074, -0.071, -0.068, -0.065, -0.061, -0.056,
-0.05, -0.038, -0.032, -0.02, 0.035, -0.023, -0.034, -0.042, -0.009, -0.053, -0.061, -0.048,
0.032, 0.055, -0.052, 0.019, 0.04, -0.026, 0.007, -0.033, -0.04, -0.044, -0.047, 0.134, -0.002,
0.095, 0.061, -0.082, -0.127, 0.039, 0.057, 0.077, 0.092, 0.081, 0.007, -0.041, 0.057, -0.038,
0.02, 0.004, -0.126, -0.118, -0.042, -0.061, -0.009, -0.034, -0.028, -0.016, 0.066, -0.008,
-0.033, -0.062, -0.168, 0.054, 0.058, -0.007, 0.007, -0.054, -0.042, -0.257, -0.152, 0.009,
0.029, 0.032, 0.036, -0.033, -0.046, -0.05, 0.018, -0.059, -0.065, -0.078, -0.093, -0.109,
-0.126, -0.136, -0.072, -0.057, -0.05, -0.039, -0.032, -0.029, -0.024, -0.055, -0.05, -0.057,
0.019, 0.031, 0.057, 0.078, 0.091, 0.084, 0.071, 0.05, 0.033, 0.066, 0.024, -0.039, -0.043,
-0.062, -0.086, -0.078, -0.035, -0.041, 0.114, -0.004, 0.08, -0.043, 0.008, 0.035, 0.117, 0.075,
0.091, -0.088, -0.074, -0.157, -0.187, 0.023, 0.023, 0.021, 0.021, 0.017, 0.002, -0.01, 0.054,
0.037, 0.016, 0.058, -0.096, -0.292, 0.009, 0.007, -0.003, -0.011, -0.009, -0.005, -0.017,
-0.031, -0.04, -0.029, -0.019, 0.007, 0.036, 0.064, 0.06, 0.091, 0.084, 0.021, 0.044, 0.024,
0.007, 0.007, 0.006, -0.001, -0.004, -0.011, -0.026, -0.037, -0.048, -0.06, -0.057, -0.054,
-0.051, -0.031, -0.014, -0.007
],
"slopeNorth": [
-0.005, 0.005, -0.003, 0.002, -0.006, -0.016, -0.044, -0.027, -0.024, -0.026, -0.026, -0.026,
-0.05, -0.054, 0.02, -0.042, -0.033, -0.022, -0.009, 0.008, 0.025, 0.032, 0.037, 0.003, 0.01,
0.01, 0.013, 0.017, 0.026, 0.059, 0.018, -0.02, -0.039, 0.024, 0.024, 0.008, -0.005, -0.003,
-0.06, -0.088, -0.024, -0.033, -0.037, -0.039, -0.046, -0.052, -0.026, -0.019, -0.023, -0.025,
-0.027, -0.106, -0.113, -0.118, -0.111, -0.103, -0.016, -0.016, -0.129, -0.122, -0.096, -0.075,
-0.046, -0.071, 0.056, 0.061, -0.108, -0.077, 0.028, 0.067, -0.029, -0.009, 0.048, -0.009,
-0.029, -0.018, 0.006, 0.025, 0.144, 0.19, 0.17, 0.167, 0.149, -0.078, -0.083, -0.0, -0.081,
-0.04, -0.001, -0.033, -0.082, -0.031, 0.061, 0.14, 0.078, 0.132, 0.104, 0.127, -0.011, -0.01,
0.071, 0.129, 0.113, 0.103, 0.13, -0.038, -0.037, 0.044, 0.052, 0.016, 0.01, -0.01, -0.02,
-0.014, 0.06, 0.086, 0.082, 0.067, 0.049, 0.051, 0.058, -0.08, -0.095, -0.099, -0.098, -0.093,
-0.064, -0.024, 0.007, 0.03, 0.041, -0.085, -0.064, -0.021, -0.069, -0.063, -0.054, -0.027,
0.013, 0.058, 0.082, 0.091, 0.065, 0.04, 0.085, 0.117, 0.139, 0.134, 0.059, 0.047, 0.008, 0.002,
0.005, 0.01, 0.034, 0.039, 0.068, 0.077, -0.041, -0.017, -0.011, -0.017, -0.021, -0.021, -0.003,
0.016, 0.034, -0.057, -0.046, -0.067, -0.062, -0.059, -0.032, -0.028, -0.023, -0.01, -0.01,
-0.033, -0.028, -0.004, 0.019, -0.01, 0.056, 0.067, -0.061, -0.049, -0.108, -0.079, -0.0, 0.018,
0.024, 0.053, 0.08, 0.085, -0.008, 0.015, 0.016, 0.027, 0.048, -0.05, -0.038, -0.058, -0.086,
-0.001, -0.061, -0.038, -0.014, 0.027, 0.062, -0.036, -0.007, 0.046, -0.023, -0.001, 0.01,
-0.073, 0.053, 0.073, -0.02, 0.004, -0.001, -0.014, -0.069, -0.016, -0.041, 0.001, 0.021,
-0.017, -0.019, -0.115, -0.11, -0.083, -0.119, -0.097, -0.076, -0.101, -0.069, -0.054, -0.022,
-0.045, -0.028, -0.016, -0.008, 0.019, -0.037, 0.005, 0.007, 0.01, -0.092, -0.096, -0.079,
0.074, 0.07, -0.076, -0.025, -0.075, -0.039, 0.003, -0.006, 0.013, -0.03, -0.064, -0.079,
-0.076, -0.056, -0.026, 0.042, -0.084, 0.038, 0.055, 0.002, 0.033, 0.01, -0.003, 0.001, -0.02,
-0.041, -0.057, -0.056, -0.053, -0.045, -0.037, 0.043, 0.037, -0.023, -0.013, -0.012, 0.063,
0.029, 0.024, 0.012, -0.007, 0.01, 0.006, -0.057, 0.007, -0.017, 0.02, 0.061, 0.058, 0.039,
0.021, 0.001, -0.02, -0.025, -0.024, 0.039, 0.052, 0.03, 0.03, 0.028, 0.035, 0.003, 0.005,
0.027, -0.048, -0.059, 0.005, -0.037, -0.029, -0.058, -0.066, -0.009, -0.02, 0.014, -0.009,
-0.015, -0.01, -0.045, -0.036, -0.085, -0.017, 0.011, -0.052, 0.048, -0.025, -0.024, -0.035,
-0.031, -0.05, -0.058, -0.013, -0.05, -0.053, -0.058, -0.033, -0.017, 0.001, -0.025, -0.066,
-0.023, -0.014, -0.011, -0.059, -0.019, -0.009, 0.005, -0.007, -0.034, -0.051, 0.002, 0.036,
0.0, 0.007, -0.022, -0.028, -0.046, -0.051, -0.018, -0.021, -0.013, 0.012, -0.005, -0.035,
-0.036, 0.011, -0.065, 0.025, 0.006, 0.018, -0.103, 0.005, -0.007, -0.025, -0.061, -0.044,
-0.049, -0.008, 0.004, -0.043, 0.022, -0.062, -0.019, -0.055, 0.001, -0.01, -0.057, -0.005,
-0.051, -0.064, -0.023, 0.029, -0.044, 0.079, 0.066, -0.049, -0.043, 0.043, 0.037, 0.009, -0.04,
-0.175, -0.046, -0.025, -0.055, -0.045, 0.01, -0.078, -0.036, -0.127, -0.126, -0.145, 0.102,
0.094, -0.123, 0.055, -0.109, 0.133, -0.142, -0.009, -0.085, 0.067, 0.024, 0.056, -0.06, 0.156,
0.025, -0.068, 0.018, -0.106, 0.077, 0.019, -0.02, -0.043, 0.021, -0.021, 0.053, -0.023, -0.091,
-0.044, 0.016, 0.174, 0.024, 0.172, 0.213, -0.05, -0.021, -0.051, -0.255, 0.041, 0.076, -0.168,
0.031, -0.153, -0.058, -0.022, 0.063, -0.084, -0.122, -0.153, -0.157, -0.171, 0.014, 0.054,
0.036, 0.084, 0.009, 0.019, 0.018, -0.012, 0.013, 0.008, 0.011, 0.061, 0.061, 0.057, -0.013,
-0.036, -0.024, -0.045, -0.046, -0.101, -0.156, -0.066, -0.239, -0.1, -0.077, -0.243, -0.08,
0.073, 0.039, 0.179, 0.106, 0.005, 0.086, 0.044, 0.321, 0.232, 0.195, 0.145, -0.074, -0.069,
-0.057, 0.021, -0.1, -0.165, 0.063, 0.139, 0.12, 0.091, 0.134, 0.082, 0.064, -0.056, -0.118,
-0.146, -0.004, -0.123, -0.142, -0.092, -0.121, 0.172, 0.209, 0.083, -0.15, -0.088, -0.128,
-0.152, -0.196, 0.004, -0.017, -0.027, -0.037, -0.042, 0.021, 0.014, 0.009, 0.003, 0.082, 0.074,
0.061, 0.048, 0.031, 0.013, -0.006, 0.081, 0.074, 0.055, -0.036, 0.041, 0.024, 0.006, -0.068,
-0.022, 0.073, 0.021, 0.055, 0.009, 0.022, 0.079, -0.019, 0.052, 0.08, 0.02, 0.073, 0.04, 0.01,
0.224, 0.291, 0.218, -0.011, -0.137, -0.234, 0.091, 0.094, 0.095, 0.093, 0.062, 0.02, 0.061,
0.003, 0.044, -0.063, 0.145, -0.012, -0.046, -0.011, 0.075, 0.117, 0.046, -0.033, 0.108, 0.084,
-0.011, -0.094, -0.024, -0.13, 0.075, 0.1, 0.124, -0.018, -0.058, -0.136, -0.149, -0.227,
-0.022, 0.105, 0.119, 0.113, 0.09, 0.076, 0.045, -0.045, 0.032, 0.029, 0.034, 0.051, 0.055,
0.044, 0.028, -0.072, -0.095, -0.088, -0.06, -0.032, -0.005, 0.003, -0.016, -0.008, 0.039,
0.085, 0.076, 0.061, 0.046, 0.035, 0.031, 0.022, 0.018, 0.014, 0.006, 0.014, 0.009, -0.002,
-0.002, 0.003, -0.003, -0.024, -0.018, 0.068, 0.212, 0.049, 0.187, 0.06, 0.024, 0.003, -0.084,
-0.173, 0.03, -0.138, -0.17, -0.157, 0.006, -0.007, -0.026, -0.034, -0.038, -0.045, 0.001,
-0.075, -0.067, 0.237, 0.215, 0.109, 0.055, -0.068, -0.066, -0.059, -0.054, -0.059, -0.062,
-0.065, -0.07, -0.072, -0.064, -0.054, -0.045, -0.035, -0.022, 0.059, 0.161, 0.071, 0.021,
-0.021, -0.027, -0.033, -0.039, -0.044, -0.038, -0.029, -0.034, -0.057, -0.078, -0.075, -0.071,
-0.076, -0.085, -0.083, -0.057, -0.03, -0.018
]
}

View file

@ -0,0 +1,60 @@
import { describe, expect, it } from 'vitest';
import { sunOnScreen } from './skyDome';
import { DRIFT_FRAC, FALLBACK_VIEW, windowFor } from './view';
import type { View } from './view';
const VIEWPORTS: [number, number][] = [
[390, 844],
[1920, 1080],
[3440, 1440],
];
describe('windowFor', () => {
it.each(VIEWPORTS)('%i x %i: the planes cover the viewport, the camera sees only baked sky, the horizon is the plane\'s', (w, h) => {
const v = FALLBACK_VIEW;
const win = windowFor(v, w, h);
expect(win.driftPx).toBeGreaterThanOrEqual(0);
expect(win.plane.w - 2 * win.driftPx).toBeCloseTo(w, 6);
expect(win.plane.h).toBeLessThanOrEqual(h * 4); // sanity: scale is not runaway
// Angle seen never exceeds the baked window minus the drift margin on both sides.
expect(win.camera.hfovDeg).toBeLessThanOrEqual(v.windowDeg / (1 + 2 * DRIFT_FRAC) + 1e-9);
// The plane is anchored to the bottom; its horizon row lands at the camera's horizon fraction of the viewport.
const horizonY = h - win.plane.bottom - win.plane.h + win.plane.scale * v.horizonRow;
expect(Math.abs(horizonY - win.camera.horizonAt * h)).toBeLessThan(0.5);
expect(win.camera.azimuthDeg).toBe(v.lookAzDeg);
expect(win.camera.vfovDeg / win.camera.hfovDeg).toBeCloseTo(h / w, 9); // one scale on both axes
});
it('is shaped by the View alone: another window, plane and horizon give another camera', () => {
const wide: View = { ...FALLBACK_VIEW, lookAzDeg: 10, windowDeg: 144, planeW: 2560, horizonRow: 200 };
const a = windowFor(FALLBACK_VIEW, 1920, 1080).camera;
const b = windowFor(wide, 1920, 1080).camera;
expect(b.azimuthDeg).toBe(10);
expect(b.horizonAt).not.toBeCloseTo(a.horizonAt, 2);
});
it('registration: a sun 10 degrees right of the look azimuth sits where the plane puts that azimuth', () => {
for (const [w, h] of VIEWPORTS) {
const v = FALLBACK_VIEW;
const win = windowFor(v, w, h);
const sun = { elevation: 12, azimuth: v.lookAzDeg + 10, time: new Date(0) };
const at = sunOnScreen(sun, win.camera)!;
const planeX = w / 2 + 10 * v.pxPerDegH * win.plane.scale; // plane centred on lookAz
expect(Math.abs(at.x * w - planeX)).toBeLessThan(0.5);
// and its height above the ridge's horizon is 12 degrees of plane pixels
const planeY = win.camera.horizonAt * h - 12 * v.pxPerDegH * win.plane.scale;
expect(Math.abs(at.y * h - planeY)).toBeLessThan(0.5);
}
});
it('the sun is seen across north: azimuths wrap', () => {
const v: View = { ...FALLBACK_VIEW, lookAzDeg: 355 };
const win = windowFor(v, 1920, 1080);
const at = sunOnScreen({ elevation: 10, azimuth: 5, time: new Date(0) }, win.camera)!;
expect(Math.abs(at.x * 1920 - (960 + 10 * v.pxPerDegH * win.plane.scale))).toBeLessThan(0.5);
});
it('the sun does not turn the camera: the same camera for any sun', () => {
expect(windowFor(FALLBACK_VIEW, 1280, 720).camera.azimuthDeg).toBe(FALLBACK_VIEW.lookAzDeg);
});
});

View file

@ -0,0 +1,83 @@
// The one View: where the sky's camera stands and looks, and where the ridge planes sit against it. A baked place's manifest
// (`view` and `place`, manifest v1) is a View; a sky with no planes uses FALLBACK_VIEW. `windowFor` is the single owner of the
// registration between sky and ridge: one View and a viewport in, the sky Camera and the plane's placement out. Nothing else
// chooses a field of view, a horizon row or an azimuth.
/** Where the observer stands. `id` names the baked place (it keys a rebuild); `altitudeKm` is the ground height, eye height excluded. */
export interface Place {
id: string;
lat: number;
lon: number;
altitudeKm: number;
}
/** The manifest's `view`: the baked window of the sky the planes cover. Angles are degrees, planes are in plane pixels. */
export interface ManifestView {
/** Compass azimuth at the centre of the planes, degrees clockwise from north. */
lookAzDeg: number;
/** Horizontal angle the planes span. */
windowDeg: number;
planeW: number;
planeH: number;
pxPerDegH: number;
/** Row of the horizon in the plane, from its top. */
horizonRow: number;
}
export interface View extends ManifestView {
place: Place;
}
/**
* The camera of the sky. Angle-linear, as the planes are (`pxPerDegH` is constant across a plane): a pixel's azimuth is
* `azimuthDeg + (x - 0.5) * hfovDeg` and its elevation is `(horizonAt - y) * vfovDeg`, x and y as fractions of the viewport.
*/
export interface Camera {
/** Compass azimuth at the centre column. */
azimuthDeg: number;
hfovDeg: number;
vfovDeg: number;
/** Where the horizon sits down the viewport, as a fraction of its height (0 top). */
horizonAt: number;
}
/** The sky of a scene with no planes (a failed load): Vienna facing south-west, with the numbers of the six-key Vienna bake. */
export const FALLBACK_VIEW: View = {
place: { id: 'vienna', lat: 48.1929826, lon: 16.3534237, altitudeKm: 0.183 },
lookAzDeg: 214,
windowDeg: 72,
planeW: 1280,
planeH: 600,
pxPerDegH: 1280 / 72,
horizonRow: 314.5,
};
/** The share of the viewport height the plane's height is at least scaled to, and the slack each side of the viewport kept for drift. Authored, tunable by eye. */
export const RIDGE_BAND_FRAC = 0.6;
export const DRIFT_FRAC = 0.05;
export interface Window {
camera: Camera;
/** The planes in viewport pixels: scaled size, anchored to the viewport's bottom edge and centred on `lookAzDeg`. */
plane: { scale: number; w: number; h: number; bottom: number };
/** How far the plane may slide sideways from centre and still cover the viewport. */
driftPx: number;
}
export function windowFor(view: View, viewportW: number, viewportH: number): Window {
const scale = Math.max(
(RIDGE_BAND_FRAC * viewportH) / view.planeH,
(viewportW * (1 + 2 * DRIFT_FRAC)) / view.planeW
);
const hfovDeg = viewportW / (scale * view.pxPerDegH);
return {
camera: {
azimuthDeg: view.lookAzDeg,
hfovDeg,
vfovDeg: (hfovDeg * viewportH) / viewportW,
horizonAt: (viewportH - scale * (view.planeH - view.horizonRow)) / viewportH,
},
plane: { scale, w: scale * view.planeW, h: scale * view.planeH, bottom: 0 },
driftPx: (scale * view.planeW - viewportW) / 2,
};
}

View file

@ -1,5 +1,7 @@
import { describe, expect, it } from 'vitest';
import { viennaSun } from './vienna';
import { sunAt } from './sunAt';
import { FALLBACK_VIEW } from './view';
import { ELSEWHERE_VIEW } from './manifest.fixture';
import {
CLEAR_SKY,
currentWeather,
@ -85,8 +87,8 @@ describe('weather from the file', () => {
expect(weatherFromFile(file({ glow_wm2: 0, humidity_pct: 100 }), -20)).toBe(0.75);
});
it('the 2026-11-10 midday fixture, GLOW 56-89 W/m2 under an overcast sky, is full cloud; the clear midday of 2026-10-15, 546-569, is none', () => {
const nov = viennaSun(new Date('2026-11-10T10:40:00Z')).elevation;
const oct = viennaSun(new Date('2026-10-15T10:50:00Z')).elevation;
const nov = sunAt(FALLBACK_VIEW.place, new Date('2026-11-10T10:40:00Z')).elevation;
const oct = sunAt(FALLBACK_VIEW.place, new Date('2026-10-15T10:50:00Z')).elevation;
for (const g of [56, 72, 89]) expect(cloudFromGlow(g, nov)).toBeGreaterThan(0.95);
for (const g of [546, 569]) expect(cloudFromGlow(g, oct)).toBe(0);
expect(weatherFromFile(file({ glow_wm2: 72, sunshine_s_10min: 0 }), nov)).toBeGreaterThan(0.65);
@ -120,17 +122,18 @@ describe('weather from the file', () => {
describe('the weather now', () => {
it('uses the file while it is fresh', () => {
expect(currentWeather(file({ glow_wm2: 0, humidity_pct: 95 }), NOW)).toBeCloseTo(0.7, 2);
expect(currentWeather(file({ glow_wm2: 0, humidity_pct: 95 }), FALLBACK_VIEW.place, NOW)).toBeCloseTo(0.7, 2);
});
it('falls back to a clear sky when the file was fetched more than 30 minutes ago', () => {
const f = file({ humidity_pct: 100, fetched_at: '2026-11-11T10:19:00Z' });
expect(NOW.getTime() - Date.parse(f.fetched_at)).toBeGreaterThan(STALE_MS);
expect(currentWeather(f, NOW)).toBe(CLEAR_SKY);
expect(currentWeather(f, FALLBACK_VIEW.place, NOW)).toBe(CLEAR_SKY);
});
it('the 30 minute mark itself is still fresh', () => {
expect(
currentWeather(
file({ glow_wm2: 0, humidity_pct: 100, fetched_at: '2026-11-11T10:20:00Z' }),
FALLBACK_VIEW.place,
NOW
)
).toBe(0.75);
@ -138,19 +141,34 @@ describe('the weather now', () => {
it('falls back to a clear sky when the observation is more than 40 minutes old, though the file is fresh', () => {
const f = file({ precip_mm_10min: 2, observed_at: '2026-11-11T10:09:00Z' });
expect(NOW.getTime() - Date.parse(f.observed_at)).toBeGreaterThan(OBSERVATION_STALE_MS);
expect(currentWeather(f, NOW)).toBe(CLEAR_SKY);
expect(currentWeather(f, FALLBACK_VIEW.place, NOW)).toBe(CLEAR_SKY);
expect(
currentWeather(file({ precip_mm_10min: 2, observed_at: '2026-11-11T10:11:00Z' }), NOW)
currentWeather(file({ precip_mm_10min: 2, observed_at: '2026-11-11T10:11:00Z' }), FALLBACK_VIEW.place, NOW)
).toBeGreaterThan(0.8);
});
it('falls back to a clear sky with no file, and with a fetch time from the future', () => {
expect(currentWeather(null, NOW)).toBe(CLEAR_SKY);
expect(currentWeather(null, FALLBACK_VIEW.place, NOW)).toBe(CLEAR_SKY);
expect(
currentWeather(file({ precip_mm_10min: 2, fetched_at: '2026-11-12T10:40:00Z' }), NOW)
currentWeather(file({ precip_mm_10min: 2, fetched_at: '2026-11-12T10:40:00Z' }), FALLBACK_VIEW.place, NOW)
).toBe(CLEAR_SKY);
});
});
describe('the sun the weather is read against is the place it is given', () => {
it("sets radiation against the sun over that place: by day in Vienna it reads cloud, at the other side of the world, in the night, it reads the humidity", () => {
const f = file({ glow_wm2: 100, sunshine_s_10min: null, humidity_pct: 60 });
const sunEl = (place: typeof FALLBACK_VIEW.place) =>
sunAt(place, new Date(Date.parse(f.observed_at) - 5 * 60_000)).elevation;
expect(sunEl(FALLBACK_VIEW.place)).toBeGreaterThan(20);
expect(sunEl(ELSEWHERE_VIEW.place)).toBeLessThan(0);
const vienna = currentWeather(f, FALLBACK_VIEW.place, NOW);
const elsewhere = currentWeather(f, ELSEWHERE_VIEW.place, NOW);
expect(vienna).toBe(weatherFromFile(f, sunEl(FALLBACK_VIEW.place)));
expect(elsewhere).toBe(weatherFromFile(f, sunEl(ELSEWHERE_VIEW.place)));
expect(vienna).not.toBe(elsewhere);
});
});
describe('bad instrument values', () => {
it('a negative sunshine is no reading, not full cloud', () => {
expect(cloudEstimate(file({ glow_wm2: null, sunshine_s_10min: -5 }), 21)).toBeNull();
@ -164,7 +182,7 @@ describe('bad instrument values', () => {
sunshine_s_10min: null,
humidity_pct: 50,
});
const at = (m: number) => currentWeather(f, new Date(Date.parse(f.observed_at) + m * 60_000));
const at = (m: number) => currentWeather(f, FALLBACK_VIEW.place, new Date(Date.parse(f.observed_at) + m * 60_000));
expect(at(0)).toBe(at(29));
});
});
@ -193,7 +211,7 @@ describe('reading weather.json', () => {
sunshine_s_10min: null,
glow_wm2: null,
});
expect(currentWeather(gap, new Date('2026-11-11T22:50:00Z'))).toBe(CLEAR_SKY);
expect(currentWeather(gap, FALLBACK_VIEW.place, new Date('2026-11-11T22:50:00Z'))).toBe(CLEAR_SKY);
});
it("is not another station's file", () => {
expect(parseWeatherFile({ ...file(), station: 11036 })).toBeNull();

View file

@ -6,7 +6,8 @@
// 600 the direct beam was strong enough to burn the card); with the sun low or down it is a ramp of the humidity. A file that is
// missing, unreadable, malformed or stale (below) is no observation, and no observation is a clear sky.
import { WEATHER_CREDIT } from './credits';
import { viennaSun } from './vienna';
import { sunAt } from './sunAt';
import type { Place } from './view';
/** Reader rule of docs/weather-feed.md: the file is stale 30 minutes after it was fetched or 40 after the observation it holds (the station reports every 10). */
export const STALE_MS = 30 * 60_000;
@ -104,8 +105,8 @@ export function weatherFromFile(f: WeatherFile, sunEl: number): number {
return Math.max(0.7 * cloud, mist);
}
/** The weather number now: the file's, or a clear sky when there is none or it is stale (see STALE_MS) or from the future. */
export function currentWeather(f: WeatherFile | null, now: Date = new Date()): number {
/** The weather number now, its sun taken at `place` (the View's): the file's, or a clear sky when there is none or it is stale (see STALE_MS) or from the future. */
export function currentWeather(f: WeatherFile | null, place: Place, now: Date = new Date()): number {
if (!f) return CLEAR_SKY;
const fetched = now.getTime() - Date.parse(f.fetched_at);
const observed = now.getTime() - Date.parse(f.observed_at);
@ -119,7 +120,7 @@ export function currentWeather(f: WeatherFile | null, now: Date = new Date()): n
)
return CLEAR_SKY;
// GLOW and SO are means of the ten minutes ending at observed_at: the sun of their midpoint is the one to set them against.
return weatherFromFile(f, viennaSun(new Date(Date.parse(f.observed_at) - 5 * 60_000)).elevation);
return weatherFromFile(f, sunAt(place, new Date(Date.parse(f.observed_at) - 5 * 60_000)).elevation);
}
/** Fetch and parse `weather.json`; null on any failure. */

View file

@ -1,8 +1,9 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { FALLBACK_VIEW } from './ridge/view';
import { ELSEWHERE_VIEW } from './ridge/manifest.fixture';
import { atStandardTime } from './standardTime.testkit';
import { baked } from '../../../sky/baked';
import { AA, luminance } from '../../../sky/skyTokens.js';
import { skyColours } from './ridge/skyColour';
import { chooseSky, skyColour } from './terrain/terrainSky';
import {
DUSK_SUN_ELEVATION,
@ -10,6 +11,7 @@ import {
ridgeDrawn,
ridgeSkyFor,
ridgeSkyKey,
loadWeather,
terrainDrawn,
terrainSkyFor,
} from './skyForState';
@ -78,10 +80,9 @@ describe('the bands the text ink needs', () => {
});
describe('ridgeSkyFor', () => {
it('carries the state band, which the layers are shaped into, and the physical sky of that state', () => {
it('carries the physical sky of the state', () => {
for (const state of STATES) {
const sky = ridgeSkyFor(state, 0.3);
expect(sky.band).toEqual(SKY_BAND[state]);
const sky = ridgeSkyFor(state, FALLBACK_VIEW, 0.3);
expect(sky.physical?.state).toBe(state);
expect(sky.physical?.stops).toEqual({ top: sky.top, mid: sky.mid, horizon: sky.horizon });
}
@ -92,32 +93,42 @@ describe('ridgeSkyFor', () => {
s.top,
s.mid,
s.horizon,
s.band,
s.physical?.sun,
];
setSkyClock(() => atStandardTime(12, 0));
expect(key(ridgeSkyFor('auto', 0.3))).toEqual(key(ridgeSkyFor('day', 0.3)));
expect(key(ridgeSkyFor('auto', FALLBACK_VIEW, 0.3))).toEqual(key(ridgeSkyFor('day', FALLBACK_VIEW, 0.3)));
setSkyClock(() => atStandardTime(0, 0));
expect(key(ridgeSkyFor('auto', 0.3))).toEqual(key(ridgeSkyFor('night', 0.3)));
expect(key(ridgeSkyFor('auto', FALLBACK_VIEW, 0.3))).toEqual(key(ridgeSkyFor('night', FALLBACK_VIEW, 0.3)));
setSkyClock(() => atStandardTime(18, 10));
expect(key(ridgeSkyFor('auto', 0.3))).toEqual(key(ridgeSkyFor('dusk', 0.3)));
expect(key(ridgeSkyFor('auto', FALLBACK_VIEW, 0.3))).toEqual(key(ridgeSkyFor('dusk', FALLBACK_VIEW, 0.3)));
});
it("is lit from the real sun over Vienna: a forced state's sun is inside the state, the clear noon sky is blue", () => {
const day = ridgeSkyFor('day', 0);
const day = ridgeSkyFor('day', FALLBACK_VIEW, 0);
expect(day.physical?.sun.elevation).toBeGreaterThanOrEqual(DAY_FROM);
expect(day.top[2]).toBeGreaterThan(day.top[0]);
const dusk = ridgeSkyFor('dusk', 0);
const dusk = ridgeSkyFor('dusk', FALLBACK_VIEW, 0);
expect(Math.abs(dusk.physical?.sun.elevation ?? 99)).toBeLessThanOrEqual(DAY_FROM);
expect(ridgeSkyFor('night', 0).physical?.sun.elevation).toBeLessThanOrEqual(-DAY_FROM);
expect(day.ground).toEqual(skyColours().ground);
expect(ridgeSkyFor('night', FALLBACK_VIEW, 0).physical?.sun.elevation).toBeLessThanOrEqual(-DAY_FROM);
});
it("is made over the View's place: the sun and the observer are the View's", () => {
const sky = ridgeSkyFor('day', ELSEWHERE_VIEW, 0);
expect(sky.physical?.view).toBe(ELSEWHERE_VIEW);
expect(sky.physical?.atm.observerKm).toBe(ELSEWHERE_VIEW.place.altitudeKm);
});
it("keys a rebuild by the View's place as well: another place is another key", () => {
const other = { ...FALLBACK_VIEW, place: { ...FALLBACK_VIEW.place, id: 'graz' } };
expect(ridgeSkyKey('day', FALLBACK_VIEW, 0.3)).not.toBe(ridgeSkyKey('day', other, 0.3));
expect(ridgeSkyKey('day', other, 0.3)).toContain('graz');
});
it('keys a rebuild by state and weather, not by the moving sun', () => {
setSkyClock(() => atStandardTime(12, 0));
expect(ridgeSkyKey('auto', 0.3)).toBe(ridgeSkyKey('day', 0.3));
expect(ridgeSkyKey('day', 0.3)).not.toBe(ridgeSkyKey('day', 0.8));
expect(ridgeSkyKey('day', 0.3)).not.toBe(ridgeSkyKey('night', 0.3));
expect(ridgeSkyKey('auto', FALLBACK_VIEW, 0.3)).toBe(ridgeSkyKey('day', FALLBACK_VIEW, 0.3));
expect(ridgeSkyKey('day', FALLBACK_VIEW, 0.3)).not.toBe(ridgeSkyKey('day', FALLBACK_VIEW, 0.8));
expect(ridgeSkyKey('day', FALLBACK_VIEW, 0.3)).not.toBe(ridgeSkyKey('night', FALLBACK_VIEW, 0.3));
});
it.each(STATES)('%s: the display is fitted to the budget its text ink needs', (state) => {
@ -125,7 +136,7 @@ describe('ridgeSkyFor', () => {
const r = ridgeDrawn(state);
expect(r.darkest).toBeGreaterThanOrEqual(floor);
expect(r.lightest).toBeLessThanOrEqual(ceiling);
const fit = ridgeSkyFor(state, 0.3).physical?.fit;
const fit = ridgeSkyFor(state, FALLBACK_VIEW, 0.3).physical?.fit;
expect(fit?.lo).toBeGreaterThanOrEqual(r.darkest - 1e-9);
expect(fit?.hi).toBeLessThanOrEqual(r.lightest + 1e-9);
});
@ -371,3 +382,17 @@ describe('skyBackdrop holds the union of what the scenes draw', () => {
expect(Math.min(l.low, r.darkest) - b.darkest).toBeLessThan(5e-4);
});
});
describe('loadWeather', () => {
it('reads the file again for another place inside the refresh window: the weather it set belongs to the sun of the place it was read for', async () => {
const fetched = vi.fn(async () => ({ ok: false, status: 404, json: async () => ({}) }));
vi.stubGlobal('fetch', fetched);
const t = new Date('2030-01-01T00:00:00Z');
await loadWeather('/w.json', FALLBACK_VIEW.place, t);
await loadWeather('/w.json', FALLBACK_VIEW.place, new Date(t.getTime() + 60_000));
expect(fetched).toHaveBeenCalledTimes(1);
await loadWeather('/w.json', ELSEWHERE_VIEW.place, new Date(t.getTime() + 120_000));
expect(fetched).toHaveBeenCalledTimes(2);
vi.unstubAllGlobals();
});
});

View file

@ -7,8 +7,8 @@ import { baked } from '../../../sky/baked';
import { chooseSky, skyColour, urlSky } from './terrain/terrainSky';
import type { SkyName } from './terrain/terrainSky';
import type { SkyColours, Srgb } from './ridge/skyColour';
import { skyColours } from './ridge/skyColour';
import { makePhysicalSky } from './ridge/physicalSky';
import type { Place, View } from './ridge/view';
import { BUDGET } from './ridge/toneMap';
import { ridgeWeather, setFedWeather } from './ridge/weather';
import { currentWeather, fetchWeatherFile } from './ridge/weatherFeed';
@ -28,39 +28,40 @@ export const DUSK_SUN_ELEVATION = 2;
*/
export const ridgeSkyKey = (
state: SkyState,
view: View,
weather: number = ridgeWeather(),
now: Date = skyNow()
): string => `ridge:${resolveSkyState(state, now)}:${weather}`;
): string => `ridge:${resolveSkyState(state, now)}:${weather}:${view.place.id}`;
const WEATHER_REFRESH_MS = 10 * 60_000;
let weatherAt = -Infinity;
let weatherPlace = '';
/**
* Read `weather.json` (the page's own, next to config.json) into the weather the ridge sky is drawn for; at most once in ten minutes.
* Never throws: a missing, stale or broken file is a clear sky (`currentWeather`).
*/
export async function loadWeather(url: string, now: Date = skyNow()): Promise<void> {
if (now.getTime() - weatherAt < WEATHER_REFRESH_MS) return;
export async function loadWeather(url: string, place: Place, now: Date = skyNow()): Promise<void> {
if (place.id === weatherPlace && now.getTime() - weatherAt < WEATHER_REFRESH_MS) return;
weatherAt = now.getTime();
setFedWeather(currentWeather(await fetchWeatherFile(url), now));
weatherPlace = place.id;
setFedWeather(currentWeather(await fetchWeatherFile(url), place, now));
}
/**
* The ridge scene's `sky` option: the physical sky over Vienna for the state ('auto': the real sun now; a forced state: the
* The ridge scene's `sky` option: the physical sky over the View's place for the state ('auto': the real sun now; a forced state: the
* sun of today at a moment inside it, `sunForState`), as a palette for the layers and stars plus the `physical` sky the host canvas
* is painted from. The atmosphere LUTs are baked here, once per scene start (tens of ms).
*/
export function ridgeSkyFor(
state: SkyState,
view: View,
weather: number = ridgeWeather(),
now: Date = skyNow()
): SkyColours {
const { ground } = skyColours();
const fixed = resolveSkyState(state, now);
const physical = makePhysicalSky(fixed, now, weather);
const physical = makePhysicalSky(fixed, now, weather, view);
return {
...physical.stops,
ground,
band: SKY_BAND[fixed],
physical,
};
}

View file

@ -48,7 +48,6 @@ export function nightColours(): SkyColours {
top: hex(g[0]),
mid: hex(g[Math.floor(g.length / 2)]),
horizon: hex(g[g.length - 1]),
ground: [0, 0, 0],
};
}

View file

@ -24,6 +24,8 @@ export type SceneStyle = 'ridge' | 'raymarch' | 'terrain';
export interface BackgroundSceneConfig {
tier?: SceneTier;
style?: SceneStyle;
/** The baked ridge place to show (a key of the ridge index); unknown or absent: the index's default. */
place?: string;
/** Let the page URL switch on the diagnostic overlays (`?probe=1`, `?tierlabel=1`) in a production build. Default: no. */
debugOverlays?: boolean;
}