scene: keep the terrain sky and baked tables out of the main chunk
Some checks failed
Build pull request / Build pull request (pull_request) Has been cancelled

skyBackdrop reads constants (the union of the ridge and terrain skies, rounded
outward) instead of computing it, and the door imports skyForState lazily with
the scenes. A test fails when the constants drift from the computed union.
Main entry chunk vs dev: +0.4 KB gzip, down from +51.5 KB.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Nibu 2026-11-09 22:37:06 +00:00
commit 0d26b71862
3 changed files with 46 additions and 25 deletions

View file

@ -2,7 +2,6 @@ import type { Scene, SceneOptions } from './Scene';
import { BackgroundSceneConfig, Capabilities, SceneTier, resolveTier } from './tier';
import { getSkyState, subscribe } from './skyState';
import type { SkyState } from './skyState';
import { ridgeSkyFor, terrainSkyFor } from './skyForState';
export type { Scene, SceneStats } from './Scene';
export type { SceneStyle, SceneTier } from './tier';
@ -23,6 +22,8 @@ async function create(
): Promise<Scene | null> {
let createScene: (o: SceneOptions) => Scene | null;
try {
// Loaded here, with the scenes, so the main chunk does not carry the terrain sky or the baked tables.
const { ridgeSkyFor, terrainSkyFor } = await import('./skyForState');
if (config?.style === 'raymarch') createScene = (await import('./Scene')).createScene;
else if (config?.style === 'terrain') {
const { createTerrainScene } = await import('./terrain/Terrain');

View file

@ -116,3 +116,36 @@ describe('both scenes of a state lie inside skyBackdrop', () => {
});
});
});
describe('skyBackdrop holds the union of what the scenes draw', () => {
const computed = (state: 'day' | 'dusk' | 'night') => {
const r = [ridgeDrawn(state), terrainDrawn(state)].filter((x) => x !== undefined) as {
darkest: number;
lightest: number;
}[];
return {
darkest: Math.min(...r.map((x) => x.darkest)),
lightest: Math.max(...r.map((x) => x.lightest)),
};
};
it.each(['day', 'dusk', 'night'] as const)(
'%s: the constant bounds the computed range and sits within 1e-5 of it',
(state) => {
const c = computed(state);
const b = skyBackdrop(state);
expect(b.darkest).toBeLessThanOrEqual(c.darkest);
expect(b.lightest).toBeGreaterThanOrEqual(c.lightest);
expect(c.darkest - b.darkest).toBeLessThan(1e-5);
expect(b.lightest - c.lightest).toBeLessThan(1e-5);
}
);
it('auto is the union of the three fixed states', () => {
const all = (['day', 'dusk', 'night'] as const).map(skyBackdrop);
expect(skyBackdrop('auto')).toEqual({
darkest: Math.min(...all.map((x) => x.darkest)),
lightest: Math.max(...all.map((x) => x.lightest)),
});
});
});

View file

@ -2,8 +2,6 @@
// localStorage key; whoever draws the sky (the scene, the title bar's control) reads it here, and
// `skyBackdrop` says how light or dark the sky behind the login text can be in each state, so the
// text colours can be checked against it.
import { ridgeDrawn, terrainDrawn } from './skyForState';
export type SkyState = 'auto' | 'day' | 'dusk' | 'night';
export const SKY_STATES: readonly SkyState[] = ['auto', 'day', 'dusk', 'night'];
@ -58,27 +56,16 @@ export interface SkyBackdrop {
}
// The union of every sky a scene draws for the state: the ridge sky and the terrain sky, each from
// the function its scene uses. 'auto' follows the clock (and a URL), so it bounds all three fixed
// states. Computed on first use: the terrain range samples its sky.
const union = (ranges: readonly (SkyBackdrop | undefined)[]): SkyBackdrop => {
const r = ranges.filter((x): x is SkyBackdrop => !!x);
return {
darkest: Math.min(...r.map((x) => x.darkest)),
lightest: Math.max(...r.map((x) => x.lightest)),
};
// the function its scene uses (`ridgeDrawn`, `terrainDrawn` in skyForState.ts). Held as constants so
// this module, which the main chunk loads, does not pull in the terrain sky and the baked scattering
// tables with it; skyForState.test.ts fails when they drift from the computed union.
// Rounded outward to six places. 'auto' follows the clock (and a URL), so it
// bounds all three fixed states.
const BACKDROPS: Record<SkyState, SkyBackdrop> = {
auto: { darkest: 0.016727, lightest: 0.964723 },
day: { darkest: 0.237514, lightest: 0.919226 },
dusk: { darkest: 0.016727, lightest: 0.964723 },
night: { darkest: 0.058099, lightest: 0.077467 },
};
const FIXED_STATES = ['day', 'dusk', 'night'] as const;
const cache = new Map<SkyState, SkyBackdrop>();
export function skyBackdrop(state: SkyState): SkyBackdrop {
let b = cache.get(state);
if (!b) {
b =
state === 'auto'
? union(FIXED_STATES.map(skyBackdrop))
: union([ridgeDrawn(state), terrainDrawn(state)]);
cache.set(state, b);
}
return b;
}
export const skyBackdrop = (state: SkyState): SkyBackdrop => BACKDROPS[state];