Files
vibemake/js/render-budget.js
T

133 lines
4.8 KiB
JavaScript

// Shared by the detail viewer and the sandbox bundle. No OS sniffing: start
// conservatively on touch/limited devices, then respond to sustained frame cost.
export function deviceProfile({
cores = globalThis.navigator?.hardwareConcurrency || 0,
memory = globalThis.navigator?.deviceMemory || 0,
coarse = globalThis.matchMedia?.('(pointer: coarse)').matches || false,
saveData = globalThis.navigator?.connection?.saveData || false,
} = {}) {
const limited = saveData || (cores > 0 && cores <= 4) || (memory > 0 && memory <= 4);
return {
fps: coarse || limited ? 30 : 60,
maxDpr: limited ? 1 : coarse ? 1.25 : 1.5,
maxPixels: limited ? 600_000 : coarse ? 900_000 : 1_600_000,
antialias: !coarse && !limited,
shadows: !coarse && !limited,
};
}
// Model viewers render the uploaded assets at the screen's native resolution.
// Do not silently trade image quality for performance; heavy assets must be
// optimized at their source. Frame limiting and on-demand rendering still apply.
export function modelViewerProfile(options) {
const profile = deviceProfile(options);
return {
...profile,
antialias: true,
shadows: true,
maxDpr: Infinity,
maxPixels: Infinity,
adaptiveQuality: false,
};
}
// Simulation uses the same full-quality rendering on desktop and mobile.
// The control clock remains independent; never hide heavy assets by blurring
// the canvas, disabling antialiasing or adapting resolution after slow frames.
export function simulationProfile(options) {
return { ...modelViewerProfile(options), fps: 60 };
}
export class RenderBudget {
constructor(profile = deviceProfile()) {
this.base = { ...profile };
Object.assign(this, profile);
this.level = 0;
this.resetTiming();
}
pixelRatio(width, height, dpr = globalThis.devicePixelRatio || 1) {
return Math.min(dpr, this.maxDpr, Math.sqrt(this.maxPixels / Math.max(1, width * height)));
}
resetTiming() {
this.last = null;
this.resetSamples();
}
resetSamples() { this.sampleMs = 0; this.costMs = 0; this.frames = 0; this.slowWindows = 0; }
next(now) {
const interval = 1000 / this.fps;
if (this.last === null) { this.last = now; this.due = now + interval; return interval; }
const elapsed = now - this.last;
if (now < this.due - 1) return 0;
// Keep the refresh-rate remainder, but never catch up missed frames.
this.last = now;
this.due += (Math.floor(Math.max(0, now - this.due) / interval) + 1) * interval;
return elapsed;
}
observe(elapsed, cost) {
if (this.adaptiveQuality === false) return false;
if (elapsed > 250 || elapsed <= 0 || this.level === 3) return false;
this.sampleMs += elapsed;
this.costMs += cost;
this.frames++;
if (this.sampleMs < 1500 || this.frames < 8) return false;
const interval = 1000 / this.fps;
const slow = this.sampleMs / this.frames > interval * 1.35 || this.costMs / this.frames > interval * 0.7;
this.slowWindows = slow ? this.slowWindows + 1 : 0;
this.sampleMs = this.costMs = this.frames = 0;
if (this.slowWindows < 2) return false;
this.level++;
this.maxDpr = this.base.maxDpr * 0.8 ** this.level;
this.maxPixels = this.base.maxPixels * 0.65 ** this.level;
this.fps = this.level === 3 ? 24 : 30;
this.shadows = false;
this.resetTiming();
return true;
}
}
// render(dt) returns true only while animation is needed. Static models stop
// requesting frames altogether; invalidate() resumes for an interaction.
export class RenderLoop {
constructor({ render, budget = new RenderBudget(), onQuality = () => {},
request = (fn) => requestAnimationFrame(fn), cancel = (id) => cancelAnimationFrame(id),
now = () => performance.now() }) {
Object.assign(this, { render, budget, onQuality, request, cancel, now });
this.active = true;
this.pending = null;
this.tick = (time) => {
this.pending = null;
if (!this.active) return;
const elapsed = budget.next(time);
if (!elapsed) { this.schedule(); return; }
this.dirty = false;
this.rendering = true;
const start = this.now();
const again = render(Math.min(elapsed / 1000, 0.05));
this.rendering = false;
if (budget.observe(elapsed, this.now() - start)) this.onQuality();
if (again || this.dirty) this.schedule();
else budget.resetTiming();
};
}
schedule() {
if (this.active && this.pending === null) this.pending = this.request(this.tick);
}
invalidate() {
this.dirty = true;
if (!this.rendering) this.schedule();
}
setActive(active) {
this.active = active && !this.disposed;
if (this.pending !== null) this.cancel(this.pending);
this.pending = null;
this.budget.resetTiming();
if (this.active) this.invalidate();
}
dispose() { this.disposed = true; this.setActive(false); }
}