Skip to content

Commit 1d9cbae

Browse files
authored
Manual: add a chapter about limiting internal resolution to avoid performance issues (#31942)
1 parent 4df2228 commit 1d9cbae

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

manual/en/responsive.html

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,39 @@ <h2 id="handling-hd-dpi-displays">Handling HD-DPI displays</h2>
257257
<p>It might be hard to see the difference but if you have an HD-DPI
258258
display and you compare this sample to those above you should
259259
notice the edges are more crisp.</p>
260+
261+
<h2 id="hd-dpi-limiting-maximum-drawing-buffer-size">
262+
HD-DPI: Limiting maximum drawing buffer size
263+
</h2>
264+
265+
<p>When using a fractional UI scaling factor on some operating system
266+
(eg: 150% on OSX or Linux) the assumption that the real physical
267+
resolution equals <code>width * window.devicePixelRatio</code> and
268+
<code>height * window.devicePixelRatio</code> no longer holds.
269+
This may lead to excessive GPU load, lower frame rates and high power
270+
consumption.</p>
271+
<p>A possible mitigation is to cap the maximum internal resolution (e.g. limit
272+
width × height) so the buffer remains within safe bounds.</p>
273+
274+
<pre class="prettyprint showlinemods notranslate lang-js" translate="no"> function resizeRendererToDisplaySize(renderer, maxPixelCount=3840*2160) {
275+
const canvas = renderer.domElement;
276+
const pixelRatio = window.devicePixelRatio;
277+
let width = Math.floor( canvas.clientWidth * pixelRatio );
278+
let height = Math.floor( canvas.clientHeight * pixelRatio );
279+
const pixelCount = width * height;
280+
const renderScale = pixelCount > maxPixelCount ? Math.sqrt(maxPixelCount / pixelCount) : 1;
281+
width = Math.floor(width * renderScale);
282+
height = Math.floor(height * renderScale);
283+
284+
const needResize = canvas.width !== width || canvas.height !== height;
285+
if (needResize) {
286+
renderer.setSize(width, height, false);
287+
}
288+
return needResize;
289+
}
290+
</pre>
260291
<p>This article covered a very basic but fundamental topic. Next up lets quickly
261292
<a href="primitives.html">go over the basic primitives that three.js provides</a>.</p>
262-
263293
</div>
264294
</div>
265295
</div>

0 commit comments

Comments
 (0)