scene: frame cadence moves to frameLoop.ts so a second scene can share it
Cap, visibility pause, reduced-motion bound, still, wake on resize and context loss were inline in createScene. Moved verbatim into startFrameLoop; Scene.ts keeps only what it owns (history reset on resume, ladder step on a missed cap). No behaviour change: the scene tests pass unchanged. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
293885089a
commit
fea7eb1f0c
2 changed files with 130 additions and 73 deletions
|
|
@ -1,13 +1,12 @@
|
|||
import { fragmentSource, vertexSource, Stage } from './shaderSource';
|
||||
import { cameraPose, pullProgress, CameraPose } from './camera';
|
||||
import { BudgetLadder, Level, LEVELS } from './budget';
|
||||
import { startFrameLoop } from './frameLoop';
|
||||
import type { SceneTier } from './tier';
|
||||
import { PALETTE } from './palette';
|
||||
|
||||
/** Phone GPU budget for the whole scene, from the ground-cut doc's budget table. */
|
||||
export const GPU_BUDGET_MS = 4.0;
|
||||
const FRAME_INTERVAL_MS = 1000 / 30;
|
||||
const REDUCED_MOTION_FRAMES = 32;
|
||||
const MAX_DPR = 1.5;
|
||||
|
||||
export type PassName = 'terrain' | 'atmosphere' | 'resolve' | 'composite' | 'prepass';
|
||||
|
|
@ -379,16 +378,11 @@ export function createScene(opts: SceneOptions): Scene | null {
|
|||
resetHistory = true;
|
||||
};
|
||||
|
||||
const reducedMotion = window.matchMedia?.('(prefers-reduced-motion: reduce)');
|
||||
const start = performance.now();
|
||||
let loginAt: number | undefined;
|
||||
let frame = 0;
|
||||
let histIndex = 0;
|
||||
let prev: CameraPose | undefined;
|
||||
let raf = 0;
|
||||
let lastRendered = -Infinity;
|
||||
let framesLeft = still ? 1 : Infinity; // a still frame, or reduced motion, bounds this
|
||||
let disposed = false;
|
||||
|
||||
// Timer queries in flight: each holds one pass's query.
|
||||
const pending: Array<{ q: WebGLQuery; pass: PassName; frame: number }> = [];
|
||||
|
|
@ -456,7 +450,7 @@ export function createScene(opts: SceneOptions): Scene | null {
|
|||
const render = (now: number) => {
|
||||
ensureSize();
|
||||
const lv = level();
|
||||
const frozen = reducedMotion?.matches ?? false;
|
||||
const frozen = loop.frozen;
|
||||
const time = frozen ? 0 : (now - start) / 1000;
|
||||
let elapsed: number | undefined;
|
||||
if (loginAt !== undefined) elapsed = frozen ? 1e9 : (now - loginAt) / 1000;
|
||||
|
|
@ -554,79 +548,31 @@ export function createScene(opts: SceneOptions): Scene | null {
|
|||
pollReadback();
|
||||
};
|
||||
|
||||
const tick = (now: number) => {
|
||||
raf = 0;
|
||||
if (disposed) return;
|
||||
const due = opts.unthrottled || now - lastRendered >= FRAME_INTERVAL_MS - 2;
|
||||
if (due) {
|
||||
if (
|
||||
!opts.fixedLevel &&
|
||||
!timer &&
|
||||
lastRendered > 0 &&
|
||||
now - lastRendered > FRAME_INTERVAL_MS * 1.5
|
||||
) {
|
||||
// no timer: a missed cap counts as over budget
|
||||
if (ladder.push(GPU_BUDGET_MS * 2)) sizedFor = undefined;
|
||||
}
|
||||
lastRendered = now;
|
||||
render(now);
|
||||
framesLeft -= 1;
|
||||
}
|
||||
if (framesLeft > 0 && (opts.unthrottled || document.visibilityState === 'visible'))
|
||||
raf = requestAnimationFrame(tick);
|
||||
};
|
||||
|
||||
const wake = () => {
|
||||
if (disposed || raf) return;
|
||||
if (reducedMotion?.matches && framesLeft <= 0) return;
|
||||
resetHistory = true; // the first frame after a pause has a real elapsed dt
|
||||
raf = requestAnimationFrame(tick);
|
||||
};
|
||||
const undo: Array<() => void> = [];
|
||||
const listen = (target: EventTarget, type: string, handler: (e: Event) => void) => {
|
||||
target.addEventListener(type, handler);
|
||||
undo.push(() => target.removeEventListener(type, handler));
|
||||
};
|
||||
const stop = () => {
|
||||
disposed = true;
|
||||
if (raf) cancelAnimationFrame(raf);
|
||||
undo.forEach((f) => f());
|
||||
};
|
||||
if (!opts.unthrottled) {
|
||||
listen(document, 'visibilitychange', () => {
|
||||
if (document.visibilityState === 'visible') wake();
|
||||
});
|
||||
}
|
||||
listen(window, 'resize', () => wake());
|
||||
// A lost context takes every texture and program with it and nothing here rebuilds them, so the
|
||||
// scene ends and the host decides what to show. preventDefault: otherwise the browser never
|
||||
// restores the context, which is what we want, and a restore would find a dead scene.
|
||||
listen(canvas, 'webglcontextlost', (e) => {
|
||||
e.preventDefault();
|
||||
stop();
|
||||
opts.onContextLost?.();
|
||||
const loop = startFrameLoop({
|
||||
canvas,
|
||||
still,
|
||||
unthrottled: opts.unthrottled,
|
||||
render,
|
||||
onSlow: () => {
|
||||
// no timer: a missed cap counts as over budget
|
||||
if (!opts.fixedLevel && !timer && ladder.push(GPU_BUDGET_MS * 2)) sizedFor = undefined;
|
||||
},
|
||||
onResume: () => {
|
||||
resetHistory = true; // the first frame after a pause has a real elapsed dt
|
||||
},
|
||||
onContextLost: opts.onContextLost,
|
||||
});
|
||||
if (reducedMotion?.matches && !still) framesLeft = REDUCED_MOTION_FRAMES;
|
||||
// The preference can flip while the scene lives: freeze to a bounded run, or thaw to a free one.
|
||||
if (reducedMotion && !still) {
|
||||
listen(reducedMotion, 'change', () => {
|
||||
framesLeft = reducedMotion.matches ? REDUCED_MOTION_FRAMES : Infinity;
|
||||
wake();
|
||||
});
|
||||
}
|
||||
wake();
|
||||
|
||||
return {
|
||||
startFormat: halfStart ? 'RGBA16F' : 'RGBA32F',
|
||||
login() {
|
||||
if (still || disposed) return; // a still has no pull-back
|
||||
if (still || loop.disposed) return; // a still has no pull-back
|
||||
loginAt = performance.now();
|
||||
if (reducedMotion?.matches) framesLeft = REDUCED_MOTION_FRAMES;
|
||||
wake();
|
||||
loop.restart();
|
||||
},
|
||||
dispose() {
|
||||
if (disposed) return;
|
||||
stop();
|
||||
if (loop.disposed) return;
|
||||
loop.stop();
|
||||
freeTargets();
|
||||
passes.forEach((p) => gl.deleteProgram(programs[p]));
|
||||
discardReadback();
|
||||
|
|
|
|||
111
src/app/features/scene/frameLoop.ts
Normal file
111
src/app/features/scene/frameLoop.ts
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
// The frame cadence both scenes share: a 30 fps cap, a pause while the tab is hidden, a bounded run
|
||||
// under reduced motion, one frame for a still, and an end for good when the context is lost.
|
||||
|
||||
/** Cap: a scene draws at most this often. */
|
||||
export const FRAME_INTERVAL_MS = 1000 / 30;
|
||||
/** Frames a reduced-motion scene draws after it starts or after login(), then stops. */
|
||||
const REDUCED_MOTION_FRAMES = 32;
|
||||
|
||||
export interface FrameLoopOptions {
|
||||
canvas: HTMLCanvasElement;
|
||||
/** Draw one frame and stop. */
|
||||
still: boolean;
|
||||
/** Ignore the cap and the visibility pause (measurement). */
|
||||
unthrottled?: boolean;
|
||||
render(now: number): void;
|
||||
/** A frame arrived well after the cap allows: the device is not keeping up. */
|
||||
onSlow?(): void;
|
||||
/** Frames are about to start again after a pause (first start, tab visible, resize, thaw). */
|
||||
onResume?(): void;
|
||||
/** The WebGL context was lost. The loop has already stopped for good. */
|
||||
onContextLost?(): void;
|
||||
}
|
||||
|
||||
export interface FrameLoop {
|
||||
/** The user prefers reduced motion right now. */
|
||||
readonly frozen: boolean;
|
||||
readonly disposed: boolean;
|
||||
/** Start frames if none are scheduled. */
|
||||
wake(): void;
|
||||
/** A new bounded run under reduced motion, then wake. A still ignores it. */
|
||||
restart(): void;
|
||||
stop(): void;
|
||||
}
|
||||
|
||||
export function startFrameLoop(o: FrameLoopOptions): FrameLoop {
|
||||
const reducedMotion = window.matchMedia?.('(prefers-reduced-motion: reduce)');
|
||||
let raf = 0;
|
||||
let lastRendered = -Infinity;
|
||||
let framesLeft = o.still ? 1 : Infinity; // a still frame, or reduced motion, bounds this
|
||||
let disposed = false;
|
||||
|
||||
const tick = (now: number) => {
|
||||
raf = 0;
|
||||
if (disposed) return;
|
||||
const due = o.unthrottled || now - lastRendered >= FRAME_INTERVAL_MS - 2;
|
||||
if (due) {
|
||||
if (lastRendered > 0 && now - lastRendered > FRAME_INTERVAL_MS * 1.5) o.onSlow?.();
|
||||
lastRendered = now;
|
||||
o.render(now);
|
||||
framesLeft -= 1;
|
||||
}
|
||||
if (framesLeft > 0 && (o.unthrottled || document.visibilityState === 'visible'))
|
||||
raf = requestAnimationFrame(tick);
|
||||
};
|
||||
|
||||
const wake = () => {
|
||||
if (disposed || raf) return;
|
||||
if (reducedMotion?.matches && framesLeft <= 0) return;
|
||||
o.onResume?.();
|
||||
raf = requestAnimationFrame(tick);
|
||||
};
|
||||
const undo: Array<() => void> = [];
|
||||
const listen = (target: EventTarget, type: string, handler: (e: Event) => void) => {
|
||||
target.addEventListener(type, handler);
|
||||
undo.push(() => target.removeEventListener(type, handler));
|
||||
};
|
||||
const stop = () => {
|
||||
disposed = true;
|
||||
if (raf) cancelAnimationFrame(raf);
|
||||
undo.forEach((f) => f());
|
||||
};
|
||||
if (!o.unthrottled) {
|
||||
listen(document, 'visibilitychange', () => {
|
||||
if (document.visibilityState === 'visible') wake();
|
||||
});
|
||||
}
|
||||
listen(window, 'resize', () => wake());
|
||||
// A lost context takes every texture and program with it and nothing here rebuilds them, so the
|
||||
// scene ends and the host decides what to show. preventDefault: otherwise the browser never
|
||||
// restores the context, which is what we want, and a restore would find a dead scene.
|
||||
listen(o.canvas, 'webglcontextlost', (e) => {
|
||||
e.preventDefault();
|
||||
stop();
|
||||
o.onContextLost?.();
|
||||
});
|
||||
if (reducedMotion?.matches && !o.still) framesLeft = REDUCED_MOTION_FRAMES;
|
||||
// The preference can flip while the scene lives: freeze to a bounded run, or thaw to a free one.
|
||||
if (reducedMotion && !o.still) {
|
||||
listen(reducedMotion, 'change', () => {
|
||||
framesLeft = reducedMotion.matches ? REDUCED_MOTION_FRAMES : Infinity;
|
||||
wake();
|
||||
});
|
||||
}
|
||||
wake();
|
||||
|
||||
return {
|
||||
get frozen() {
|
||||
return reducedMotion?.matches ?? false;
|
||||
},
|
||||
get disposed() {
|
||||
return disposed;
|
||||
},
|
||||
wake,
|
||||
restart() {
|
||||
if (o.still || disposed) return;
|
||||
if (reducedMotion?.matches) framesLeft = REDUCED_MOTION_FRAMES;
|
||||
wake();
|
||||
},
|
||||
stop,
|
||||
};
|
||||
}
|
||||
Loading…
Reference in a new issue