Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 39 ยท Shaders and Post-processing

Render Targets, Minimaps and GPU Computation

Render to textures with WebGLRenderTarget in Three.js: TV screens, minimaps with scissor viewports, read pixels back, and ping-pong GPU simulations like the Game of Life.

So far every render went to the canvas. But the GPU can just as easily draw into a texture instead, and that texture can then be used like any other: on a TV screen inside the scene, as the input of a post-processing pass, as a mirror, a minimap, a shadow map, or even as a storage for numbers that a shader computes. This is the idea behind post-processing (you used EffectComposer already) and behind GPU simulations, and it is surprisingly simple in Three.js thanks to WebGLRenderTarget.

In this lesson you will render a scene to a texture and show it on a screen, draw a minimap with a second viewport, read pixels back to the CPU, and build a "ping-pong" simulation: Conway's Game of Life running entirely in a shader.

WebGLRenderTarget in three steps

JavaScript
const target = new THREE.WebGLRenderTarget(512, 512, {
  minFilter: THREE.LinearFilter,
  magFilter: THREE.LinearFilter,
  samples: 4,                 // optional MSAA
  depthBuffer: true
});

renderer.setRenderTarget(target);
renderer.render(sceneA, cameraA);
renderer.setRenderTarget(null);        // back to the canvas

material.map = target.texture;         // use the result as a normal texture

Remember two things. First, always reset the target with setRenderTarget(null), or all following renders go off-screen. Second, a render target's texture should not be read while it is also being written: you cannot sample a target you are currently drawing into, which is why ping-pong simulations use two targets.

Targets hold color in the format you choose: UnsignedByteType (default, 8-bit), HalfFloatType or FloatType (needed for data such as positions or HDR, subject to device support). Set target.texture.colorSpace to SRGBColorSpace only for targets that hold final colors.

Where render targets are used

TV screens A second scene on a texture
Mirrors, portals A second camera's view
Post-processing Scene to image to filters
GPGPU Textures as data arrays

Same API for all four: setRenderTarget, render, use target.texture.

1 / 2

A TV inside the scene

Here a tiny "world" of spinning shapes is drawn into a render target and then used as the map of a screen mesh in the main scene. Both scenes keep animating, which is the same technique used for security-camera monitors, in-game phones and portals.

Scene on a TV screen

The screen's aspect ratio (3 by 2.2) differs from the target's (1:1), so the knot is slightly squeezed. Match the target size to the screen ratio when it matters.

Several views at once: viewports and scissor

You do not need render targets for a minimap or a split screen. renderer.setViewport restricts drawing to a rectangle of the canvas and setScissorTest clips clearing to it, so you can render the same scene twice with two cameras.

JavaScript
renderer.setScissorTest(true);

// main view: the whole canvas
renderer.setViewport(0, 0, W, H);
renderer.setScissor(0, 0, W, H);
renderer.render(scene, camera);

// minimap: a 25% square in the top-right corner (y counts from the bottom!)
const s = Math.floor(H * 0.3);
renderer.setViewport(W - s - 10, H - s - 10, s, s);
renderer.setScissor(W - s - 10, H - s - 10, s, s);
renderer.render(scene, topCamera);

Viewport coordinates are in CSS pixels and start at the bottom-left. When you do this, remember that autoClear clears only the scissor area, so each view clears its own rectangle.

Minimap with a second camera

The orthographic top camera has no perspective, which is what you want from a map. A flat colored disc under the player or an extra icon only visible on a specific layer would complete it (see layers in the raycasting lesson).

Reading pixels back

Sometimes the CPU needs the result: a screenshot, GPU picking, or checking a simulation. For a render target use renderer.readRenderTargetPixels(target, x, y, w, h, buffer); for async reading without stalling the pipeline, readRenderTargetPixelsAsync. Reading is slow because it synchronizes the GPU with the CPU, so do it rarely. To save a screenshot of the canvas call renderer.domElement.toBlob(...) right after a render (or create the renderer with preserveDrawingBuffer: true).

Ping-pong: computing on the GPU

A render target is also an array of numbers. If a shader reads the current state from one texture and writes the next state into another, repeatedly swapping them ("ping-pong"), you have a GPU simulation: fluids, cloth, flocking, particle physics with a million particles. This general-purpose use of the GPU is called GPGPU.

The classic first example is Conway's Game of Life: each cell is alive or dead, and in each step a cell is alive next time if it has exactly three living neighbours, or if it is alive now and has two. The shader counts the eight neighbours by sampling the texture around the pixel.

Game of Life computed in a shader

All 2,304 cells update in parallel in a single draw call; nothing about the simulation exists in JavaScript arrays after the first frame. Scale the same pattern to a 1024 by 1024 grid and it still runs at full speed, which would crush a CPU loop in JavaScript.

For simulations that need decimals (positions, velocities) create the targets with type: THREE.FloatType or HalfFloatType. Three.js includes a helper, GPUComputationRenderer in three/addons/misc/, that wraps the ping-pong bookkeeping for several variables. In recent versions the new WebGPU renderer offers real compute shaders, a cleaner alternative (see WebGPU and TSL).

Pitfalls

  • Forgetting setRenderTarget(null): the canvas goes blank because everything draws off-screen.
  • Sampling and writing the same target: undefined behavior; ping-pong two targets.
  • Target resolution: a 4096 by 4096 target costs 64 MB or more. Use only the size you need.
  • Disposing: call target.dispose() when you are done with a target, as for any GPU resource.
  • Color space: color targets used as textures need the right colorSpace; data targets keep NoColorSpace.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What must you call after rendering to a WebGLRenderTarget so later renders go to the screen again?

    setRenderTarget(null) restores the default framebuffer (the canvas).
  2. 2. Where does the viewport origin sit when you use renderer.setViewport(x, y, w, h)?

    WebGL viewport coordinates start at the bottom-left of the canvas, so a top-right minimap uses H - size - padding for y.
  3. 3. Why does a GPU simulation use two render targets and swap them?

    Each step reads the previous state from one target and writes the new state to the other; then they swap roles.
  4. 4. Which data type do you pick for a target that stores positions needing decimals?

    8-bit targets can only store 256 levels per channel; float formats keep real numbers.

Summary

  • A WebGLRenderTarget is an off-screen image: setRenderTarget, render, setRenderTarget(null), then use target.texture anywhere a texture fits.
  • That enables screens inside the scene, mirrors and portals, post-processing and shadow maps.
  • Viewports and scissor let you draw several camera views (minimaps, split screens) in one canvas without targets.
  • readRenderTargetPixels brings data back to the CPU; use it rarely.
  • Two targets in a ping-pong arrangement turn the GPU into a general computer (GPGPU), as the Game of Life shows.

Next: Performance Fundamentals