diff --git a/builder/web/src/lib/trailview/engine.ts b/builder/web/src/lib/trailview/engine.ts index dc2b00a237..1d73600d3d 100644 --- a/builder/web/src/lib/trailview/engine.ts +++ b/builder/web/src/lib/trailview/engine.ts @@ -154,7 +154,7 @@ export class TrailView { private feet?: Feet; private ready = false; /** No frame draws before `start` lets it: a frame asks for every tile, and the tiles under the eye come first. */ - private held = true; + private starting = true; /** The attach of the other passes, which starts once a frame draws the view about the eye (`eyeDrawn`); it rejects when one fails. */ private rest: Promise = Promise.resolve(); private eyeDrawn?: () => void; @@ -251,7 +251,7 @@ export class TrailView { throw error; }); await Promise.race([tiles.arrived, new Promise(resolve => setTimeout(resolve, EYE_WAIT_MS))]); - engine.held = false; + engine.starting = false; engine.request(); return engine; } @@ -448,8 +448,13 @@ export class TrailView { } private idle(): boolean { - return this.complete && this.terrain.idle && this.groundLook.idle && this.houses.idle && this.trees.idle && this.forest.idle && this.plants.idle && this.instances.idle - && this.sky.idle && !this.shadow.pending && this.settled() && !this.converging; + return !this.loading && !this.shadow.pending && this.settled() && !this.converging; + } + + /** Whether the late passes still attach, or a tile, a model or a page is on its way. */ + private get loading(): boolean { + return !this.complete || !this.terrain.idle || !this.groundLook.idle || !this.houses.idle || !this.trees.idle || !this.forest.idle || !this.plants.idle || !this.instances.idle + || !this.sky.idle; } /** @@ -664,7 +669,7 @@ export class TrailView { if (!this.destroyed) this.ask(); return; } - if (this.held) return; + if (this.starting) return; if (!this.pacer.due(now)) return this.request(); const start = performance.now(); const steer = this.controls?.steer(), dt = this.last ? Math.min(0.1, (now - this.last) / 1000) : 0; @@ -679,7 +684,8 @@ export class TrailView { this.last = steer || !settled ? now : 0; const eye = this.eye(), place = this.place(); const { gpu, uniform, times, targets } = this.gpuState; - if (this.fit()) { + const resized = this.fit(); + if (resized) { this.temporal.reset(); this.lastFrame = undefined; } @@ -709,22 +715,32 @@ export class TrailView { this.grab.reject(new Error(eye ? 'The still draws nothing: the ground layers are not ready.' : 'The still draws nothing: no terrain is known under the eye.')); this.grab = undefined; } - let step: Step | undefined, offset: [number, number] = [0, 0], ndc: [number, number] = [0, 0], moved = true, changed = false, cascade = -1; + let step: Step | undefined, offset: [number, number] = [0, 0], ndc: [number, number] = [0, 0], moved = true, changed = false, cascade = -1, hold = false; let lights: Lights | undefined, grabbed: Grabbed | undefined; if (eye && group) { const camera = [...viewProj, eye.east, eye.north, eye.z, eye.above, scale], extras = JSON.stringify([this.extras, this.skyMagnitude]); moved = camera.some((value, i) => value !== this.camera[i]); - changed = this.completed || this.terrain.newContent || this.groundLook.newContent || this.houses.newContent || this.trees.newContent || this.forest.newContent || this.plants.newContent || this.instances.newContent || this.sky.newContent || this.lightPools.newContent || this.route.newContent || extras !== this.drawnExtras; + const newExtras = extras !== this.drawnExtras; + changed = this.completed || this.terrain.newContent || this.groundLook.newContent || this.houses.newContent || this.trees.newContent || this.forest.newContent || this.plants.newContent || this.instances.newContent || this.sky.newContent || this.lightPools.newContent || this.route.newContent || newExtras; + // While a view at rest loads, its frames run the updates and the compute but draw LOAD_RATE frames a second. Input and a resize draw + // at once; a frame that would draw nothing does not hold, so it asks for no more frames. + hold = !this.output && !this.grab && (changed || this.temporal.converging) && this.pacer.holds(now, !moved && !newExtras && !resized, this.loading || this.terrain.meshesLoading); + // The frame that draws after held ones takes in their change: the restart, and the instances placed twice. + if (!hold) changed ||= this.temporal.held; this.camera = camera; if (moved) this.sight = { viewProj, eye: [eye.east, eye.north, eye.z] }; this.drawnExtras = extras; this.completed = false; lights = this.lights(); const light = lights.key, casters = this.houses.newContent || this.trees.newContent || this.forest.newContent || this.plants.newContent; - if (this.complete) cascade = this.shadow.due([eye.east, eye.north, eye.z - eye.above], eye.above, light, { casters, reshaped: this.terrain.reshaped }, moved); + const casting = { casters, reshaped: this.terrain.reshaped }; + // A held frame keeps its changes for the next frame that draws: `due()` marks the map that it returns as drawn. + if (this.complete && hold) this.shadow.hold(casting); + else if (this.complete) cascade = this.shadow.due([eye.east, eye.north, eye.z - eye.above], eye.above, light, casting, moved); this.shadow.write(gpu, [eye.east, eye.north, eye.z], light); // A new map changes the frame as new content does. - step = this.temporal.next(moved, changed || cascade >= 0, debug ? 1 : STILL_FRAMES); + if (hold) this.temporal.hold(changed); + else step = this.temporal.next(moved, changed || cascade >= 0, debug ? 1 : STILL_FRAMES); if (step && !debug) offset = jitter(step.sample); // The jitter in clip space: render pixels are y down. ndc = [2 * offset[0] / width, -2 * offset[1] / height]; @@ -805,11 +821,12 @@ export class TrailView { // The tree models that the workers built go to the device cache once the view is idle, so a load never waits for the writes. if (this.trees.unsaved && this.idle()) this.trees.save(); // Frames go on while the eye moves or settles on the ground, while the terrain or the ground has - // work that no load waits for, and until a still view has all its jittered frames. + // work that no load waits for, and until a still view has all its jittered frames, which a held frame's change restarts. if (steer || !settled || this.terrain.working || this.groundLook.working || this.houses.working || this.shadow.pending || this.converging) this.request(); if (steer || this.moved) this.onView?.(this.view); this.moved = false; - for (const resolve of this.rendered.splice(0)) resolve(); + // The waits are for a frame that draws: a held frame shows none of what it took in. + if (!hold) for (const resolve of this.rendered.splice(0)) resolve(); } /** diff --git a/builder/web/src/lib/trailview/light/shadow.test.ts b/builder/web/src/lib/trailview/light/shadow.test.ts index 1f0075482f..b66e9c89dc 100644 --- a/builder/web/src/lib/trailview/light/shadow.test.ts +++ b/builder/web/src/lib/trailview/light/shadow.test.ts @@ -72,4 +72,15 @@ describe('the object shadow maps', () => { expect(shadow.pending).toBe(false); expect(shadow.due([60, 0, 20], 1.6, sunAt(4), NOTHING, false)).toBe(0); }); + + it('draws for the casters that frames which drew nothing saw', () => { + const shadow = new Shadow(); + expect(shadow.due([0, 0, 0], 1.6, sun, NOTHING, false)).toBe(0); + expect(shadow.due([0, 0, 0], 1.6, sun, NOTHING, false)).toBe(1); + shadow.hold({ casters: true, reshaped: Infinity }); + shadow.hold(NOTHING); + expect(shadow.due([0, 0, 0], 1.6, sun, NOTHING, false)).toBe(0); + expect(shadow.due([0, 0, 0], 1.6, sun, NOTHING, false)).toBe(1); + expect(shadow.due([0, 0, 0], 1.6, sun, NOTHING, false)).toBe(-1); + }); }); diff --git a/builder/web/src/lib/trailview/light/shadow.ts b/builder/web/src/lib/trailview/light/shadow.ts index ed90744e5d..2545500867 100644 --- a/builder/web/src/lib/trailview/light/shadow.ts +++ b/builder/web/src/lib/trailview/light/shadow.ts @@ -75,6 +75,8 @@ interface Maps { export class Shadow { private resources?: Maps; private readonly states: State[] = CASCADES.map(() => ({ waiting: false, frames: 0, active: false })); + /** What changed for the casters on frames that drew nothing. */ + private held: Casting = { casters: false, reshaped: Infinity }; /** Per cascade the share that the receivers take from it by the eye's height, 0 without a map. */ private readonly weights = CASCADES.map(() => 0); @@ -137,9 +139,11 @@ export class Shadow { * The cascade whose map this frame draws, or -1: for the ground under the eye (east, north, height), * the eye's height above it, the direction to the sun, what changed and whether the eye moved. Of * the cascades that are due, the one that waited longest draws. A cascade that the eye's height fades - * out draws none, nor any under a sun that casts no object shadows. + * out draws none, nor any under a sun that casts no object shadows. It takes in what `hold()` kept. */ due(ground: number[], above: number, sun: number[], casting: Casting, moving: boolean): number { + casting = { casters: casting.casters || this.held.casters, reshaped: Math.min(casting.reshaped, this.held.reshaped) }; + this.held = { casters: false, reshaped: Infinity }; let best = -1; CASCADES.forEach((c, k) => { const state = this.states[k], fade = 1 - smoothstep(c.above / 2, c.above, above); @@ -165,6 +169,11 @@ export class Shadow { return best; } + /** Keeps what changed for the casters on a frame that draws nothing, for the next `due()`. */ + hold({ casters, reshaped }: Casting): void { + this.held = { casters: this.held.casters || casters, reshaped: Math.min(this.held.reshaped, reshaped) }; + } + /** * A cascade's projection from render space about the eye (east, north, height in the frame), columns * first: x along the sun and y across it, from -1 to 1 over the map, each snapped to whole texels of diff --git a/builder/web/src/lib/trailview/pacer.test.ts b/builder/web/src/lib/trailview/pacer.test.ts index 8df4f83dd7..b498a35852 100644 --- a/builder/web/src/lib/trailview/pacer.test.ts +++ b/builder/web/src/lib/trailview/pacer.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import { Pacer } from './pacer'; +import { LOAD_RATE, Pacer } from './pacer'; /** The display frames of one second that draw, at `hz` with a timestamp noise of ±`noise` ms. */ function drawn(hz: number, rate = 60, noise = 0): number[] { @@ -32,6 +32,16 @@ describe('Pacer', () => { expect(steps(drawn(165, 30, 0.5)).size).toBe(1); }); + it('draws LOAD_RATE frames a second of a view at rest that loads, and at once for input or the end of the load', () => { + const pacer = new Pacer(60), at = (i: number) => 1000 + (i * 1000) / 60 + 0.5 * Math.sin(i * 7.1); + const shown = [...Array(60).keys()].filter(i => pacer.due(at(i)) && !pacer.holds(at(i), true, true)); + expect(shown).toHaveLength(LOAD_RATE); + expect(steps(shown)).toEqual(new Set([60 / LOAD_RATE])); + expect(pacer.holds(at(59), true, true)).toBe(true); + expect(pacer.holds(at(59), false, true)).toBe(false); + expect(pacer.holds(at(60), true, false)).toBe(false); + }); + it('draws a lone frame at once', () => { const pacer = new Pacer(60); expect(pacer.due(0)).toBe(true); diff --git a/builder/web/src/lib/trailview/pacer.ts b/builder/web/src/lib/trailview/pacer.ts index 47b47b24ba..ca992360b4 100644 --- a/builder/web/src/lib/trailview/pacer.ts +++ b/builder/web/src/lib/trailview/pacer.ts @@ -4,6 +4,8 @@ const PERIODS = 8; const IDLE_MS = 100; /** How far past a half the display frames per drawn frame must move before the step changes: a ratio such as 1.5 does not flip. */ const HYSTERESIS = 0.15; +/** The frames per second that a view at rest draws while it loads: each would be replaced before anyone looks at it. */ +export const LOAD_RATE = 10; /** * Picks the display frames that draw, about `rate` (> 0) per second: every `step`th display frame, the @@ -14,6 +16,8 @@ const HYSTERESIS = 0.15; export class Pacer { private step = 1; private drawn = -Infinity; + private shown = -Infinity; + private period = 0; private previous?: number; private readonly periods: number[] = []; @@ -27,11 +31,21 @@ export class Pacer { this.periods.push(gap); if (this.periods.length > PERIODS) this.periods.shift(); } - const period = [...this.periods].sort((a, b) => a - b)[this.periods.length >> 1] ?? 0; + const period = this.period = [...this.periods].sort((a, b) => a - b)[this.periods.length >> 1] ?? 0; const ratio = period && 1000 / this.rate / period; if (Math.abs(ratio - this.step) > 0.5 + HYSTERESIS) this.step = Math.max(1, Math.round(ratio)); if (now - this.drawn < (this.step - 0.5) * period) return false; this.drawn = now; return true; } + + /** + * Whether a frame at `now` that runs draws nothing: the view is at `rest` and `loading`, and the last frame + * that drew is less than a LOAD_RATE period ago, half a display period early at the latest, as in `due()`. + */ + holds(now: number, rest: boolean, loading: boolean): boolean { + if (rest && loading && now - this.shown < 1000 / LOAD_RATE - this.period / 2) return true; + this.shown = now; + return false; + } } diff --git a/builder/web/src/lib/trailview/resolve/temporal.test.ts b/builder/web/src/lib/trailview/resolve/temporal.test.ts index f9e1980aa8..4a0368c4a1 100644 --- a/builder/web/src/lib/trailview/resolve/temporal.test.ts +++ b/builder/web/src/lib/trailview/resolve/temporal.test.ts @@ -46,6 +46,15 @@ describe('the accumulation', () => { expect(temporal.next(true, false)?.weight).toBe(MOVING_WEIGHT); }); + it('starts again on the next frame for a change that frames which drew nothing saw', () => { + const temporal = new Temporal(); + for (let k = 0; k < STILL_FRAMES; k++) still(temporal); + temporal.hold(true); + temporal.hold(false); + expect(temporal.converging).toBe(true); + expect(still(temporal)).toEqual({ weight: 1, sample: 1, clip: false }); + }); + it('takes one frame in a view without jitter', () => { const temporal = new Temporal(); expect(temporal.next(false, false, 1)?.weight).toBe(1); diff --git a/builder/web/src/lib/trailview/resolve/temporal.ts b/builder/web/src/lib/trailview/resolve/temporal.ts index 250c69d683..45d27ac8ce 100644 --- a/builder/web/src/lib/trailview/resolve/temporal.ts +++ b/builder/web/src/lib/trailview/resolve/temporal.ts @@ -35,21 +35,30 @@ export class Temporal { private moving = 0; private valid = false; private frames = STILL_FRAMES; + /** A change that a frame which drew nothing saw: the next `next()` takes it in. */ + held = false; /** Whether a still view needs more frames. */ get converging(): boolean { - return this.k < this.frames; + return this.held || this.k < this.frames; } - /** The history is lost: the next frame starts it again. */ + /** The history is lost: the next frame starts it again. A held change stays: the instances still need it. */ reset(): void { this.valid = false; this.k = 0; } + /** A frame draws nothing: the next frame that draws takes in its change. */ + hold(changed: boolean): void { + this.held ||= changed; + } + /** The step of the next frame, or undefined when the still view has all its frames. `frames` is the number that a still view takes. */ next(moved: boolean, changed: boolean, frames = STILL_FRAMES): Step | undefined { this.frames = frames; + changed ||= this.held; + this.held = false; if (moved) { this.k = 0; this.moving = this.moving % STILL_FRAMES + 1;