Renderer Configuration and Performance Basics
🎯 Introduction
By the end of this article, you will be able to configure WebGLRenderer, handle high-density displays and color spaces, understand how shadows, pixel ratio, and geometry counts affect performance, and troubleshoot stuttering systematically.
🧱 Practical renderer settings
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true;
antialiassmooths edges but adds initialization and rendering cost.alphaallows a transparent canvas background that blends into the page.setPixelRatioimproves sharpness on Retina displays, but high ratios significantly increase pixel counts.outputColorSpaceconverts the results of GPU shader calculations into a color output suitable for displays.- Enable
shadowMap.enabledonly when shadows are needed.
CSS usually determines the canvas layout size, while JavaScript determines the renderer's drawing size. setSize(width, height, false) changes the drawing size without overriding the CSS layout.
This demo combines pixel ratio, sRGB output, and shadow settings in a minimal scene so you can see the rendered sphere, ground, and shadow:
📊 Common performance factors
Three kinds of counts commonly affect 3D performance:
- Draw calls: many separate meshes require the CPU to submit drawing commands frequently.
- Vertices and textures: highly segmented geometry and large textures increase GPU workload.
- Screen pixels: larger canvases and higher pixel ratios require more pixels to be processed each frame.
Start with simple checks: reduce unnecessary segments and texture dimensions, merge static objects, reuse geometries and materials, enable shadows only for key objects, and set objects that do not need to appear to visible = false.
Avoid continually adding lights, shadow maps, and post-processing effects just for realism. Validate performance on target devices with actual scenes.
🪤 Resource cleanup and troubleshooting order
At a minimum, clean up the following when the component leaves the page:
cancelAnimationFrame(frameId);
controls?.dispose();
scene.traverse((object) => {
if (!object.isMesh) return;
object.geometry.dispose();
const materials = Array.isArray(object.material)
? object.material
: [object.material];
materials.forEach((material) => material.dispose());
});
renderer.dispose();
For textures referenced by materials, also call texture.dispose() once you know they will not be reused. Do not release geometries, materials, or textures still shared by other scenes.
Troubleshoot rendering in this order: confirm that the renderer and canvas exist, confirm that the scene contains objects, check camera position and clipping planes, then check materials, lights, textures, and color spaces.
🧾 Summary
- Renderer drawing size and canvas CSS size are separate concepts.
- Balance display sharpness and rendering cost through pixel ratio.
- Configure color output and color maps with compatible color spaces.
- Shadows, lights, vertex counts, texture dimensions, and draw calls all affect performance.
- On component destruction, stop the loop, remove control listeners, and release GPU resources.
❓ Questions and answers
Q1: Should I always use renderer.setPixelRatio(window.devicePixelRatio)?
It improves sharpness on high-density displays, but device pixel ratios can be high. Capping with Math.min(..., 2) is a common starting compromise. Still validate it on your target devices.
Q2: Why does enlarging the canvas with CSS make it blurry?
Its drawing size has not increased accordingly. Read the container dimensions, call renderer.setSize, and update the camera aspect ratio.
Q3: Why do other objects turn black after I dispose of a material?
Multiple meshes share the material. Manage resources according to their sharing relationships and dispose of them only when no other users remain.
🧪 Exercise
Implement a resize function that updates the container, camera, and renderer together. Then make all small objects in the scene share one geometry and material.
// Write your code here
🎉 You can now render scenes that stay sharp, stable, and maintainable in real pages.
