Full devicePixelRatio vs capped 1× for mobile three.js scenes: fill-rate vs sharpness trade-off
0 reputation · 11 Jan 2020, 09:24 UTC
0 reputation · 11 Jan 2020, 09:24 UTC
A three.js scene targets low-end mobile GPUs where thermal throttling and fill-rate limits are the primary performance constraints. The same build must also render acceptably on high-DPI displays (devicePixelRatio 2–3) without dedicated quality settings per device class.
renderer.setPixelRatio(window.devicePixelRatio) multiplies drawing-buffer dimensions by the display scale, increasing fragment work roughly with the square of the ratio (≈9× at DPR 3). renderer.setPixelRatio(1) caps the buffer to CSS pixels, preserving frame budget but producing visibly softer edges on Retina-class screens. setSize(width, height, true) keeps CSS and buffer sizes in sync; passing false lets them diverge, which then requires manual pointer normalization for Raycaster.
The team needs a single pixel-ratio policy that satisfies the mobile fill-rate ceiling while keeping visual degradation on high-DPI displays within a defined threshold.
37275 reputation · 11 Jan 2020, 15:07 UTC
Do not lock every device to setPixelRatio(1). Use a single capped policy—renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))—as the default, and only step down to 1× when measured frame time exceeds your budget on the weakest target phone. A hard 1× lock saves the most fragment budget but visibly softens Retina-class edges; full device pixel ratio can multiply fragment work by roughly dpr² (about 9× at DPR 3), which is what saturates low-end mobile GPUs and triggers thermal throttling.
setPixelRatio(r) makes the drawing buffer CSS width × r by CSS height × r; fragment shading cost scales with buffer area, so the cost is quadratic in r.setSize(width, height, true) writes both the buffer size and the canvas CSS style to width and height; passing false leaves the style untouched.Raycaster.setFromCamera normalizes pointer coordinates against canvas.getBoundingClientRect(), i.e. the CSS box, not the drawing buffer. A buffer/CSS size difference caused by pixel ratio alone does not shift raycast results.const dpr = Math.min(window.devicePixelRatio, 2);
renderer.setPixelRatio(dpr);
renderer.setSize(canvas.clientWidth, canvas.clientHeight, true);
Re-read devicePixelRatio on resize or visualviewport change; it can change at runtime. Add hysteresis and a cooldown if you later make the cap adaptive, so the ratio does not oscillate during thermal throttling.
With updateStyle=true, the canvas CSS box stays in sync with the layout size, so Raycaster pointer math remains correct even though the buffer is larger. Measurable pointer-offset bugs appear only when the CSS box diverges from what the page layout or pointer events expect—typically after setSize(w, h, false) or manual style overrides. Verify canvas.clientWidth versus canvas.width after every resize.
canvas.width, canvas.height, canvas.clientWidth, and canvas.clientHeight after setPixelRatio and setSize on a DPR-2/DPR-3 device or DevTools emulation.dpr².What is your per-frame GPU time budget in milliseconds on the lowest-end target device? If even 2× exceeds that budget, the recommendation changes to a universal 1× cap plus non-pixel-ratio quality levers such as antialias: false, smaller shadow maps, and reduced overdraw.
This assumes modern three.js (pin your version; output/color-space defaults changed around r152+) and a standard WebGL mobile context. The exact DPR cap (2 vs 1.5) and the frame-time threshold are scene-dependent and must be measured on real hardware; they are not assumed here.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.