diff --git a/src/app/features/scene/index.ts b/src/app/features/scene/index.ts index 3cc42486..55b00e86 100644 --- a/src/app/features/scene/index.ts +++ b/src/app/features/scene/index.ts @@ -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 { 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'); diff --git a/src/app/features/scene/skyForState.test.ts b/src/app/features/scene/skyForState.test.ts index 1e09021d..aff09b1e 100644 --- a/src/app/features/scene/skyForState.test.ts +++ b/src/app/features/scene/skyForState.test.ts @@ -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)), + }); + }); +}); diff --git a/src/app/features/scene/skyState.ts b/src/app/features/scene/skyState.ts index bc164d7a..a6d1ce8e 100644 --- a/src/app/features/scene/skyState.ts +++ b/src/app/features/scene/skyState.ts @@ -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 = { + 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(); - -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];