Files
vibemake/js/render-budget.js
T
Jerry 1c837766cd feat: integrate robot assembly and simulation with resilient loading
Add backend-driven assembly and simulation experiences, MAKE branding, mobile rendering budgets and desktop-only CAD/PCB routes. Improve logo loading progress, document image recovery and URDF request handling; include dev packaging and regression coverage.
2026-10-30 18:38:39 +08:00

120 lines
4.5 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,
};
}
// Simulation also needs a CPU/worker budget for control ticks. Keep the
// material/light setup; spend fewer pixels on high-DPI screens instead.
export function simulationProfile(options) {
const profile = deviceProfile(options);
const compact = options?.compact ?? globalThis.matchMedia?.('(max-width: 640px)').matches ?? false;
const mobile = compact || profile.fps <= 30;
return { ...profile, fps: mobile ? 30 : 60, antialias: !mobile && profile.antialias, maxDpr: Math.min(profile.maxDpr, mobile ? 1 : 1.25),
maxPixels: Math.min(profile.maxPixels, mobile ? 550_000 : 1_100_000) };
}
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 (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); }
}