Scene: a throwing ray-march createScene goes to onCompileFail #52

Closed
nibu wants to merge 1 commit from nibu/scene-compile-fail into dev AGit
2 changed files with 19 additions and 7 deletions

View file

@ -162,4 +162,19 @@ describe('startBackgroundScene follows the sky state', () => {
await flush();
expect(createScene).toHaveBeenCalledOnce();
});
it('keeps the ground and reports a ray-march scene that throws while being created', async () => {
const boom = new Error('shader link failed');
createScene.mockImplementation(() => {
throw boom;
});
const onCompileFail = vi.fn();
const scene = await startBackgroundScene({ ...config, style: 'raymarch' }, caps, {
canvas,
onCompileFail,
});
expect(scene).toBeNull();
expect(onCompileFail).toHaveBeenCalledOnce();
expect(onCompileFail).toHaveBeenCalledWith(boom);
});
});

View file

@ -21,10 +21,9 @@ async function create(
options: StartOptions & { tier: SceneTier },
state: SkyState
): Promise<Scene | null> {
let createScene: (o: SceneOptions) => Scene | null;
try {
if (config?.style === 'raymarch') createScene = (await import('./Scene')).createScene;
else if (config?.style === 'terrain') {
if (config?.style === 'raymarch') return (await import('./Scene')).createScene(options);
if (config?.style === 'terrain') {
const { createTerrainScene } = await import('./terrain/Terrain');
const sky = terrainSkyFor(state);
const scene = createTerrainScene({ ...options, ...sky });
@ -32,15 +31,13 @@ async function create(
if (scene) return scene;
const { createTerrainSkyScene } = await import('./terrain/TerrainSky2D');
return createTerrainSkyScene({ ...options, ...sky });
} else {
const { createRidgeScene } = await import('./ridge/Ridge');
return createRidgeScene({ ...options, sky: ridgeSkyFor(state) });
}
const { createRidgeScene } = await import('./ridge/Ridge');
return createRidgeScene({ ...options, sky: ridgeSkyFor(state) });
} catch (e) {
options.onCompileFail?.(e instanceof Error ? e : new Error(String(e)));
return null;
}
return createScene(options);
}
/**