Overview
Multiple render targets (MRT) let a single draw call write several output images at once. It is the core of deferred rendering, where a "G-buffer" stores surface information such as normals and depth for later passes. This WebGL2 demo renders a scene once into two attachments, normals and depth, and shows them in a four-panel layout alongside the beauty pass and a relit preview built only from the normals.
How it works
The render target is created with new WebGLRenderTarget(width, height, { count: 2 }), which gives it two colour textures. An override material written in GLSL 3 declares two outputs:
layout(location = 0) out vec4receives the surface normal, encoded from the −1…1 range into 0…1 so it can be stored as colour.layout(location = 1) out vec4receives the depth, scaled for visibility.
When the scene is drawn with this material, each fragment writes both values in one pass. The four panels then show:
- the normal beauty render,
- the normal attachment, where colours indicate surface direction,
- the depth attachment, where brightness indicates distance,
- a relit view that computes simple lighting purely from the stored normals, demonstrating that a later pass can shade the scene without touching the original geometry.
If the browser lacks WebGL2, the demo detects it and falls back to the beauty pass with a status message.
Exploring the demo
Orbit and compare the normal and depth panels with the beauty render. Adjust the depth scale to spread the depth values across the visible range, and change the relight exposure to see the reconstructed lighting brighten or dim.
Practical advice
Every attachment costs memory and bandwidth for every pixel, so production G-buffers pack data tightly, for example normals in two channels and roughness and metalness together. Use half-float formats when precision allows. See a full deferred renderer in deferred vs forward rendering, GPU picking with an ID attachment in multiple render targets, and screen-space effects built on these buffers in SSAO and SSGI.