scene: a failed sky-state rebuild restores the ground; an explicit sky beats the URL
Some checks failed
Build pull request / Build pull request (pull_request) Has been cancelled
Some checks failed
Build pull request / Build pull request (pull_request) Has been cancelled
The door tells the host (onInactive/onActive) when a rebuild makes no scene or makes one again. chooseSky takes an explicit sun or sky option before ?sun=, ?az= and ?sky=; only auto, which passes none, is steered by the URL. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
8c02b85796
commit
6c22d4f1dc
6 changed files with 92 additions and 6 deletions
|
|
@ -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[
|
||||
|
|
|
|||
|
|
@ -40,6 +40,8 @@ export function mountBackgroundScene(
|
|||
startBackgroundScene(config, capabilities, {
|
||||
canvas,
|
||||
onCompileFail: onFail,
|
||||
onInactive,
|
||||
onActive,
|
||||
onContextLost: () => {
|
||||
if (stale()) return;
|
||||
generation += 1;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -10,6 +10,10 @@ export type { SceneStyle, SceneTier } from './tier';
|
|||
export type StartOptions = Omit<SceneOptions, 'tier' | 'patchSource' | 'onCompileFail'> & {
|
||||
/** 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() {
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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()));
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue