diff --git a/src/app/features/scene/index.test.ts b/src/app/features/scene/index.test.ts index 7971508b..5d0e6410 100644 --- a/src/app/features/scene/index.test.ts +++ b/src/app/features/scene/index.test.ts @@ -45,14 +45,14 @@ describe('startBackgroundScene follows the sky state', () => { scene?.dispose(); }); - it('rebuilds when auto is replaced by the explicit state it already resolves to (a URL may steer only auto)', async () => { - const [a, b] = [fakeScene(), fakeScene()]; - createRidgeScene.mockReturnValueOnce(a).mockReturnValueOnce(b); + it('does not rebuild when auto is replaced by the explicit state it already resolves to', async () => { + const a = fakeScene(); + createRidgeScene.mockReturnValueOnce(a); const scene = await startBackgroundScene(config, caps, { canvas }); setSkyState('night'); await flush(); - expect(a.dispose).toHaveBeenCalledOnce(); - expect(createRidgeScene).toHaveBeenCalledTimes(2); + expect(a.dispose).not.toHaveBeenCalled(); + expect(createRidgeScene).toHaveBeenCalledOnce(); scene?.dispose(); }); @@ -146,14 +146,13 @@ describe('startBackgroundScene follows the sky state', () => { expect(first.dispose).toHaveBeenCalledOnce(); }); - it('drops a scene whose rebuild finishes after dispose', async () => { - const second = fakeScene(); - createRidgeScene.mockReturnValueOnce(fakeScene()).mockReturnValueOnce(second); + it('builds nothing for a rebuild that is disposed before it starts', async () => { + createRidgeScene.mockReturnValueOnce(fakeScene()).mockReturnValueOnce(fakeScene()); const scene = await startBackgroundScene(config, caps, { canvas }); setSkyState('day'); scene?.dispose(); await flush(); - expect(second.dispose).toHaveBeenCalledOnce(); + expect(createRidgeScene).toHaveBeenCalledOnce(); }); it('keeps only the latest of two quick changes', async () => { diff --git a/src/app/features/scene/index.ts b/src/app/features/scene/index.ts index 73ab8d5a..55966188 100644 --- a/src/app/features/scene/index.ts +++ b/src/app/features/scene/index.ts @@ -1,6 +1,6 @@ import type { Scene, SceneOptions } from './Scene'; import { BackgroundSceneConfig, Capabilities, SceneTier, resolveTier } from './tier'; -import { getResolvedSkyState, getSkyState, subscribe } from './skyState'; +import { getSkyState, subscribe } from './skyState'; import type { SkyState } from './skyState'; export type { Scene, SceneStats } from './Scene'; @@ -44,6 +44,24 @@ async function create( return createScene(options); } +/** + * What a rebuild is for: the sky options the scene would be built from. A change of choice that draws the same sky + * (Day picked while the clock says day, 'auto' to the fixed state it resolves to) is not a rebuild; the page URL steers + * only 'auto' (`terrainSkyFor`), so there the two differ and the key says so. + */ +async function skyKey( + config: BackgroundSceneConfig | undefined, + state: SkyState +): Promise { + try { + const { ridgeSkyFor, terrainSkyFor } = await import('./skyForState'); + return JSON.stringify(config?.style === 'terrain' ? terrainSkyFor(state) : ridgeSkyFor(state)); + } catch { + // The chunk failed to load: no key, so every change rebuilds (and `create` reports the failure). + return null; + } +} + /** * The client's one door to the scene. When the tier resolves to 'off' nothing is imported and no * canvas context is requested; otherwise the scene code loads on demand, so it stays out of the @@ -66,7 +84,7 @@ export async function startBackgroundScene( if (tier === 'off') return null; const build = (state: SkyState) => create(config, { ...options, tier }, state); const state = getSkyState(); - const key0 = `${state}:${getResolvedSkyState()}`; + const key0 = config?.style === 'raymarch' ? null : await skyKey(config, state); const first = await build(state); if (!first || config?.style === 'raymarch') return first; @@ -75,11 +93,11 @@ export async function startBackgroundScene( let loggedIn = false; let disposed = false; let generation = 0; - // A rebuild is for a new choice or a new resolved sky ('auto' crossing a boundary), not for a repeat of either. + // A rebuild is for a different sky ('auto' crossing a boundary, a new choice), not for a repeat of the one drawn. const rebuild = async () => { const next = getSkyState(); - const key = `${next}:${getResolvedSkyState()}`; - if (key === built) return; + const key = await skyKey(config, next); + if ((key !== null && key === built) || disposed) return; generation += 1; const mine = generation; built = key; diff --git a/src/app/features/scene/skyBand.ts b/src/app/features/scene/skyBand.ts index b6b39315..ac311081 100644 --- a/src/app/features/scene/skyBand.ts +++ b/src/app/features/scene/skyBand.ts @@ -58,12 +58,14 @@ export interface SkyBand { /** * The shaping targets, in linear luma. The floor sits 0.019 above the text bound (0.2983): the screen encoding is * gamma 2.2, which reads about 0.006 darker through the sRGB curve at this level, and 8-bit rounding with the sky - * pass's dither at its worst (one step in every channel) takes about 0.007 more. + * pass's dither at its worst (one step in every channel) takes about 0.007 more. The night ceiling is lower than the text bound + * (0.0931) by more than that: 0.077394 is the luma at which one step up reads 0.08164, the lightest the baked night sky shows + * (skyState.ts), so a sky the page URL steers at night, scaled down to the ceiling, cannot show lighter than the baked one. */ export const SKY_BAND: Record = { day: { floor: 0.317, ceiling: 1, lift: hexToLinear('#e2f4ff') }, dusk: { floor: 0.317, ceiling: 1, lift: hexToLinear('#eba0b9') }, - night: { floor: 0, ceiling: 0.085, lift: hexToLinear('#5a6a9a') }, + night: { floor: 0, ceiling: 0.077394, lift: hexToLinear('#5a6a9a') }, }; /** diff --git a/src/app/features/scene/skyForState.test.ts b/src/app/features/scene/skyForState.test.ts index 2ad3b5f6..9d1dfd2d 100644 --- a/src/app/features/scene/skyForState.test.ts +++ b/src/app/features/scene/skyForState.test.ts @@ -183,6 +183,14 @@ describe('terrainSkyFor', () => { expect(night.high).toBeLessThanOrEqual(needs('night').ceiling); }); + it('keeps a URL-steered sky at night no lighter than the Night backdrop the text was checked against', () => { + setSkyClock(() => atStandardTime(0, 0)); + ['?sun=60', '?sun=10&az=40', '?sun=2&az=250', '?sky=noonday'].forEach((q) => { + search = q; + expect(terrainDrawn('auto').lightest).toBeLessThanOrEqual(skyBackdrop('night').lightest); + }); + }); + it('lets an explicit state beat the URL, as before auto had one', () => { search = '?sun=30&az=120&sky=midnight'; expect(terrainSkyFor('day')).toEqual({ sky: 'noonday', band: SKY_BAND.day }); @@ -280,12 +288,24 @@ describe('skyBackdrop holds the union of what the scenes draw', () => { return { darkest: hunt(-1), lightest: hunt(1) }; }; + // At night a sky the page URL steers (?sun= ?sky=) is shaped to the band's ceiling, so the lightest it can show is one + // 8-bit step above the ceiling's own grey: the Night constant must cover it too, or text is checked against a sky + // lighter than the one it sits on. + const steeredNight = displayedLuminance( + [SKY_BAND.night.ceiling, SKY_BAND.night.ceiling, SKY_BAND.night.ceiling], + 'high' + ); + const computed = (state: FixedSkyState) => { const r = [ridgeDrawn(state), terrainDrawn(state)]; const ind = extremes(state); return { darkest: Math.min(...r.map((x) => x.darkest), ind.darkest), - lightest: Math.max(...r.map((x) => x.lightest), ind.lightest), + lightest: Math.max( + ...r.map((x) => x.lightest), + ind.lightest, + ...(state === 'night' ? [steeredNight] : []) + ), }; }; diff --git a/src/app/features/scene/skyState.ts b/src/app/features/scene/skyState.ts index 666e83d6..21117039 100644 --- a/src/app/features/scene/skyState.ts +++ b/src/app/features/scene/skyState.ts @@ -127,11 +127,12 @@ export interface SkyBackdrop { // 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. The ridge sky counts // over its whole range, the terrain sky over what its frozen camera reaches (TERRAIN_REACH_DEG, skyForState.ts). +// Night's lightest also covers a sky the page URL steers (shaped to the band ceiling, skyBand.ts). // Bounds what the 8-bit screen shows too (one step each way for rounding and dither), rounded outward to six places. const BACKDROPS: Record = { day: { darkest: 0.461063, lightest: 0.901108 }, dusk: { darkest: 0.30619, lightest: 0.968744 }, - night: { darkest: 0.05987, lightest: 0.081705 }, + night: { darkest: 0.05987, lightest: 0.081642 }, }; export const skyBackdrop = (state: SkyState): SkyBackdrop => BACKDROPS[resolveSkyState(state)];