diff --git a/src/app/features/scene/host.test.ts b/src/app/features/scene/host.test.ts index 272f502f..b987d0cc 100644 --- a/src/app/features/scene/host.test.ts +++ b/src/app/features/scene/host.test.ts @@ -108,6 +108,20 @@ describe('mountBackgroundScene', () => { expect(scene.login).toHaveBeenCalledOnce(); }); + it('hands the door its onInactive and onActive, for a sky-state rebuild that makes no scene', async () => { + startBackgroundScene.mockResolvedValue(fakeScene()); + const onInactive = vi.fn(); + const onActive = vi.fn(); + mountBackgroundScene(config, caps, canvas, { loggedIn: false, onInactive, onActive }); + await flush(); + const given = startBackgroundScene.mock.calls[0][2]; + onActive.mockClear(); + given.onInactive(); + given.onActive(); + expect(onInactive).toHaveBeenCalledOnce(); + expect(onActive).toHaveBeenCalledOnce(); + }); + describe('a lost WebGL context', () => { const lose = () => startBackgroundScene.mock.calls[ diff --git a/src/app/features/scene/host.ts b/src/app/features/scene/host.ts index e771f696..9a31c812 100644 --- a/src/app/features/scene/host.ts +++ b/src/app/features/scene/host.ts @@ -40,6 +40,8 @@ export function mountBackgroundScene( startBackgroundScene(config, capabilities, { canvas, onCompileFail: onFail, + onInactive, + onActive, onContextLost: () => { if (stale()) return; generation += 1; diff --git a/src/app/features/scene/index.test.ts b/src/app/features/scene/index.test.ts index 5ea2cdab..684e0c67 100644 --- a/src/app/features/scene/index.test.ts +++ b/src/app/features/scene/index.test.ts @@ -77,6 +77,34 @@ describe('startBackgroundScene follows the sky state', () => { scene?.dispose(); }); + it('puts the ground back when a rebuild makes no scene, and lets it show when one returns', async () => { + const [a, c] = [fakeScene(), fakeScene()]; + createRidgeScene.mockReturnValueOnce(a).mockReturnValueOnce(null).mockReturnValueOnce(c); + const onInactive = vi.fn(); + const onActive = vi.fn(); + const scene = await startBackgroundScene(config, caps, { canvas, onInactive, onActive }); + expect(onInactive).not.toHaveBeenCalled(); + setSkyState('day'); + await flush(); + expect(onInactive).toHaveBeenCalledOnce(); + expect(onActive).not.toHaveBeenCalled(); + setSkyState('dusk'); + await flush(); + expect(onActive).toHaveBeenCalledOnce(); + expect(onInactive).toHaveBeenCalledOnce(); + scene?.dispose(); + }); + + it('does not report a dropped rebuild as inactive', async () => { + createRidgeScene.mockReturnValueOnce(fakeScene()).mockReturnValueOnce(null); + const onInactive = vi.fn(); + const scene = await startBackgroundScene(config, caps, { canvas, onInactive }); + setSkyState('day'); + scene?.dispose(); + await flush(); + expect(onInactive).not.toHaveBeenCalled(); + }); + it('removes the subscription on dispose', async () => { const first = fakeScene(); createRidgeScene.mockReturnValue(first); diff --git a/src/app/features/scene/index.ts b/src/app/features/scene/index.ts index 7dc5b4f0..2319b328 100644 --- a/src/app/features/scene/index.ts +++ b/src/app/features/scene/index.ts @@ -10,6 +10,10 @@ export type { SceneStyle, SceneTier } from './tier'; export type StartOptions = Omit & { /** A shader failed to compile, or the scene chunk failed to load. The host keeps its ground. */ onCompileFail?: (e: Error) => void; + /** A rebuild for a new sky state made no scene: the host puts the CSS ground back (as on a lost context). */ + onInactive?: () => void; + /** A rebuild made a scene again: the host lets it show. */ + onActive?: () => void; }; async function create( @@ -78,7 +82,11 @@ export async function startBackgroundScene( return; } inner = next; - if (loggedIn) inner?.login(); + if (!next) options.onInactive?.(); + else { + options.onActive?.(); + if (loggedIn) next.login(); + } }); return { login() { diff --git a/src/app/features/scene/terrain/terrainSky.test.ts b/src/app/features/scene/terrain/terrainSky.test.ts index 016ee1ce..a6dc7a7e 100644 --- a/src/app/features/scene/terrain/terrainSky.test.ts +++ b/src/app/features/scene/terrain/terrainSky.test.ts @@ -1,8 +1,9 @@ /* eslint-disable no-restricted-syntax -- tests loop over skies, normals and distances */ import { AP_DISTANCE_SCALE } from './terrainScatter'; -import { describe, expect, it } from 'vitest'; +import { afterEach, describe, expect, it, vi } from 'vitest'; import { baked } from '../../../../sky/baked'; import { + chooseSky, coldness, glowColour, hazeColour, @@ -11,6 +12,7 @@ import { shade, SKY, skyAtSun, + skyData, skyColour, skyNameFor, sunDirection, @@ -298,3 +300,30 @@ describe('ground light and haze', () => { expect(Math.abs(along)).toBeLessThan(0.4); }); }); + +describe('chooseSky: an explicit sky wins over the page URL', () => { + const at = (search: string) => vi.stubGlobal('window', { location: { search } }); + afterEach(() => { + vi.unstubAllGlobals(); + }); + + it('lets the URL steer when no option is given', () => { + at('?sun=40&az=90'); + expect(chooseSky({})).toEqual(skyAtSun(40, 90)); + at('?sky=noonday'); + expect(chooseSky({})).toEqual(skyData('noonday')); + }); + + it('prefers an explicit baked sky to ?sun= and ?sky=', () => { + at('?sun=40&az=90&sky=noonday'); + expect(chooseSky({ sky: 'midnight' })).toEqual(skyData('midnight')); + at('?sky=noonday'); + expect(chooseSky({ sky: 'midnight' })).toEqual(skyData('midnight')); + }); + + it('prefers an explicit sun to ?sun=, and takes no azimuth from ?az=', () => { + at('?sun=40&az=90&sky=noonday'); + expect(chooseSky({ sun: { elevation: 2 } })).toEqual(skyAtSun(2, undefined)); + expect(chooseSky({ sun: { elevation: 2, azimuth: 10 } })).toEqual(skyAtSun(2, 10)); + }); +}); diff --git a/src/app/features/scene/terrain/terrainSky.ts b/src/app/features/scene/terrain/terrainSky.ts index 4034fd93..f6647407 100644 --- a/src/app/features/scene/terrain/terrainSky.ts +++ b/src/app/features/scene/terrain/terrainSky.ts @@ -228,19 +228,24 @@ export function shade(d: SkyData, albedo: Vec3, normal: Vec3, distance: number, }) as unknown as Vec3; } -/** The sky to use: a sun elevation from the option or `?sun=` (with `?az=`), else the baked sky from the option, then `?sky=midnight|noonday`, then the night notion of `skyColours()`. */ +/** + * The sky to use. An explicit option wins over the page URL, so a person who picked a sky sees it: a sun + * elevation (with its azimuth, default noonday's), else a baked sky. Only without either does the URL steer: + * `?sun=` (with `?az=`), then `?sky=midnight|noonday`, then the night notion of `skyColours()`. + */ export function chooseSky(opts: { sky?: SkyName; sun?: { elevation: number; azimuth?: number }; }): SkyData { + if (opts.sun) return skyAtSun(opts.sun.elevation, opts.sun.azimuth); + if (opts.sky) return skyData(opts.sky); const q = new URLSearchParams(window.location.search); const number = (key: string) => (q.get(key)?.trim() ? Number(q.get(key)) : NaN); - const elevation = opts.sun?.elevation ?? number('sun'); + const elevation = number('sun'); if (Number.isFinite(elevation)) { - const azimuth = opts.sun?.azimuth ?? number('az'); + const azimuth = number('az'); return skyAtSun(elevation, Number.isFinite(azimuth) ? azimuth : undefined); } - if (opts.sky) return skyData(opts.sky); const name = q.get('sky'); return skyData(isSkyName(name) ? name : skyNameFor(skyColours())); }