Search

Search papers and lessons

Volume Rendering

Level 3 · 12 min read

Volume rendering colours a pixel by following a ray through a semi-transparent medium and adding up each point's light, weighted by how much of it survives the points in front.

On this page

  • Colour = topic mix
  • Discrete math
  • Calculus
  • Computer vision
  • Linear algebra
  • ReadReady to readNot yet

TL;DR

Volume rendering draws things you can partly see through, such as fog, smoke, clouds and gas or tissue. For each pixel it follows one ray from the camera into the scene. It adds up the light that each point on the ray gives off, and it weights each point by how much of that light survives the points in front of it. The same rule, applied to a stack of layers, is the familiar Alpha Compositing of semi-transparent pictures.

Builds on: Definite Integral, Exponential Function, Vector · Leads to: NeRF: Representing Scenes as Neural Radiance Fields for View Synthesis

Where it's used

Clouds, smoke and fire in films and games. A cloud in an animated film is not a surface. Light enters it, is absorbed as it passes through, and fades the deeper it goes. A renderer computes the colour of each pixel that sees the cloud by following the camera ray through the cloud and combining the samples along it.

3D views of medical scans. A CT or MRI scan gives a grid of values, one per small cube of tissue, which is a Voxel Grid. Viewers turn that grid into a 3D picture by treating each cube as a semi-transparent sample and running the same ray sum.

Neural radiance fields (2020). Mildenhall and colleagues (NeRF: Representing Scenes as Neural Radiance Fields for View Synthesis) trained a Feedforward Neural Network that returns a density and a colour at any 3D point. To draw a new view of the scene, they ran volume rendering along each camera ray and compared the result with real photographs. Because every step is differentiable, the network could be trained by Backpropagation from the photographs alone.

How it works

Take one pixel. In a Pinhole Camera Model its camera ray is the straight line from the eye through that pixel, and it passes through a small misty region, and we cut the ray into two pieces of length 1. Piece 1 is nearer the camera and has density σ=1\sigma = 1 and colour c=0.5c = 0.5. Piece 2 is behind it, with density σ=2\sigma = 2 and colour c=0.25c = 0.25. Behind both pieces is black background with value 00.

  1. Shoot the ray. The camera sees the light that travels back along the ray. That light can come from any point on the ray, and light from the far piece has to pass through the near piece to reach the camera.
  2. Read the density and colour in each piece. Piece 1: σ=1\sigma = 1, c=0.5c = 0.5. Piece 2: σ=2\sigma = 2, c=0.25c = 0.25. Density says how strongly the piece blocks light per unit length, and colour says what light it gives off. Check: both numbers come from the scene, nothing is computed yet.
  3. Turn density into opacity. A piece of density σ\sigma and length δ\delta blocks the fraction α=1−e−σδ\alpha = 1 - e^{-\sigma\delta} of the light that passes through it. Piece 1: α1=1−e−1=0.6321\alpha_1 = 1 - e^{-1} = 0.6321. Piece 2: α2=1−e−2=0.8647\alpha_2 = 1 - e^{-2} = 0.8647. Check: both lie between 00 and 11, and the denser piece blocks more.
  4. Work out transmittance. Transmittance TT is the share of light that reaches a piece from behind, after the pieces in front have taken their share. Light from piece 2 has to pass piece 1, so it keeps 1−α1=0.36791 - \alpha_1 = 0.3679 of its brightness. Light from piece 1 keeps everything, so T1=1T_1 = 1. Check: 0.36790.3679 equals e−1e^{-1}, which is e−σ1δe^{-\sigma_1 \delta}.
00.511.522.533.5400.20.40.60.81distance along the raytransmittance
  • density 0.5
  • density 1
Transmittance falls exponentially with distance through the medium. The point marks distance 2 at density 0.5, where 0.3679 of the light survives.
  1. Weight each piece. The weight is w=Tαw = T\alpha, the share of the pixel that comes from that piece. w1=1×0.6321=0.6321w_1 = 1 \times 0.6321 = 0.6321. w2=0.3679×0.8647=0.3181w_2 = 0.3679 \times 0.8647 = 0.3181. Check: the two weights add to 0.95020.9502, which equals 1−e−31 - e^{-3}, the opacity of the whole ray (total density times length is 33).
  2. Add up the colours. The pixel is 0.6321×0.5+0.3181×0.25+0.0498×0=0.3161+0.0795=0.39560.6321 \times 0.5 + 0.3181 \times 0.25 + 0.0498 \times 0 = 0.3161 + 0.0795 = 0.3956. The 0.04980.0498 is the light that gets through both pieces and reaches the black background. Check: 0.39560.3956 lies between the two piece colours 0.250.25 and 0.50.5, as it must.
flowchart LR
  A[Camera ray for one pixel] --> B[Sample points along the ray]
  B --> C[Density and colour at each sample]
  C --> D[Opacity from density and step length]
  D --> E[Transmittance: light left after the samples in front]
  E --> F[Weight = transmittance times opacity]
  F --> G[Sum weighted colours plus background]

The math

Definition

Definition: Volume rendering integral

Along a ray r(t)=o+t d\mathbf{r}(t) = \mathbf{o} + t\,\mathbf{d} for tt between tnt_n and tft_f, let σ(t)≥0\sigma(t) \ge 0 be the density and c(t)c(t) the colour at the point r(t)\mathbf{r}(t). The rendered colour is

C=∫tntfT(t) σ(t) c(t) dt,T(t)=exp⁡ ⁣(−∫tntσ(s) ds).C = \int_{t_n}^{t_f} T(t)\,\sigma(t)\,c(t)\,dt, \qquad T(t) = \exp\!\left(-\int_{t_n}^{t} \sigma(s)\,ds\right).

Here o\mathbf{o} is the camera centre, d\mathbf{d} is the direction through the pixel, and tt measures distance along the ray. The function T(t)T(t) is the transmittance: the fraction of light from the point at distance tt that survives the medium between it and the camera.

Step 1 in symbols: the ray

A point on the ray is o+t d\mathbf{o} + t\,\mathbf{d}, a Vector sum. Smaller tt means nearer the camera, so the integral runs from the near end tnt_n to the far end tft_f.

Step 3 in symbols: opacity over one piece

If σ\sigma is constant on a piece of length δ\delta, then ∫σ ds=σδ\int \sigma\,ds = \sigma\delta over that piece. The light surviving the piece is e−σδe^{-\sigma\delta}, a use of the Exponential Function with a negative exponent, so the opacity is

α=1−e−σδ.\alpha = 1 - e^{-\sigma\delta}.

Step 4 in symbols: transmittance multiplies

Splitting the integral at a point shows that transmittance is multiplicative. Light surviving pieces 11 and 22 together is the product of the two survival fractions:

Ti+1=Ti (1−αi),T1=1.T_{i+1} = T_i\,(1 - \alpha_i), \qquad T_1 = 1.

For piece 2 in the walkthrough, T2=1×(1−0.6321)=0.3679T_2 = 1 \times (1 - 0.6321) = 0.3679.

Step 5 in symbols: the discrete sum

Split [tn,tf][t_n, t_f] into NN pieces with edges t1<⋯<tN+1t_1 < \dots < t_{N+1}, lengths δi=ti+1−ti\delta_i = t_{i+1} - t_i, and constant density σi\sigma_i and colour cic_i on each piece. On piece ii, the integral is exactly Ti (1−e−σiδi) ci=Ti αi ciT_i\,(1 - e^{-\sigma_i\delta_i})\,c_i = T_i\,\alpha_i\,c_i, so

C=∑i=1NTi αi ci+TN+1 cbg,Ti=∏j=1i−1(1−αj).C = \sum_{i=1}^{N} T_i\,\alpha_i\,c_i + T_{N+1}\,c_{\text{bg}}, \qquad T_i = \prod_{j=1}^{i-1}(1 - \alpha_j).

Check: because Ti−Ti+1=TiαiT_i - T_{i+1} = T_i\alpha_i, the weights telescope. They add to 1−TN+11 - T_{N+1}, so the leftover TN+1T_{N+1} is exactly the share that goes to the background cbgc_{\text{bg}}.

Worked examples

Example
Example 1: a uniform medium (tiny)

A ray of length 2 passes through a uniform medium with σ=0.5\sigma = 0.5 everywhere. The colour is white, c=1c = 1, and the background is black.

  1. Total density times length: ∫020.5 ds=1\int_0^2 0.5\,ds = 1.
  2. Transmittance at the far end: T(2)=e−1=0.3679T(2) = e^{-1} = 0.3679.
  3. Opacity of the ray: 1−T(2)=0.63211 - T(2) = 0.6321.
  4. Check by integrating directly: C=∫02e−0.5t 0.5 dt=[−e−0.5t]02=1−e−1=0.6321C = \int_0^2 e^{-0.5t}\,0.5\,dt = \left[-e^{-0.5t}\right]_0^2 = 1 - e^{-1} = 0.6321.
  5. Interpretation: white light through a medium that blocks 63 percent of it gives a grey pixel with value 0.6321. Step 3 and step 4 agree because the colour is 1 everywhere.
Example
Example 2: three samples (standard)

Three samples, each of length δ=1\delta = 1, with densities σ=(0,2,1)\sigma = (0, 2, 1) and colours c=(0.2,0.8,0.5)c = (0.2, 0.8, 0.5). The background is black.

  1. Opacities: α1=1−e0=0\alpha_1 = 1 - e^{0} = 0, α2=1−e−2=0.8647\alpha_2 = 1 - e^{-2} = 0.8647, α3=1−e−1=0.6321\alpha_3 = 1 - e^{-1} = 0.6321.
  2. Transmittances: T1=1T_1 = 1, T2=1×(1−0)=1T_2 = 1 \times (1 - 0) = 1, T3=1×(1−0.8647)=0.1353T_3 = 1 \times (1 - 0.8647) = 0.1353.
  3. Weights w=Tαw = T\alpha: w1=0w_1 = 0, w2=0.8647w_2 = 0.8647, w3=0.1353×0.6321=0.0855w_3 = 0.1353 \times 0.6321 = 0.0855.
  4. Total weight: 0.95020.9502, which equals 1−e−31 - e^{-3}. Check: the leftover 0.04980.0498 is T4T_4.
  5. Pixel: 0.8647×0.8+0.0855×0.5+0.0498×0=0.6917+0.0428=0.73450.8647 \times 0.8 + 0.0855 \times 0.5 + 0.0498 \times 0 = 0.6917 + 0.0428 = 0.7345.

Interpretation: the empty first sample contributes nothing. The dense second sample dominates because nothing in front of it blocks it. The third sample sits behind it, so its colour counts for much less.

Example
Example 3: fog in front of a red wall (realistic)

A camera looks through fog toward a red wall. Samples are taken every 2 metres at t=1,3,5,7,9t = 1, 3, 5, 7, 9. The density is 00 at samples 1, 2 and 5, and 0.50.5 per metre at samples 3 and 4. The red channel of the fog colour is 0.90.9 at sample 3 and 0.60.6 at sample 4. The red value of the wall behind is 0.10.1.

  1. Opacities with δ=2\delta = 2: α3=α4=1−e−1=0.6321\alpha_3 = \alpha_4 = 1 - e^{-1} = 0.6321, and α1=α2=α5=0\alpha_1 = \alpha_2 = \alpha_5 = 0.
  2. Transmittances: T3=1T_3 = 1, T4=1−0.6321=0.3679T_4 = 1 - 0.6321 = 0.3679, and T5=T6=0.3679×(1−0.6321)=0.1353T_5 = T_6 = 0.3679 \times (1 - 0.6321) = 0.1353, where T6T_6 is the light reaching the wall.
  3. Weights: w3=1×0.6321=0.6321w_3 = 1 \times 0.6321 = 0.6321 and w4=0.3679×0.6321=0.2325w_4 = 0.3679 \times 0.6321 = 0.2325.
  4. Total weight from the fog: 0.86470.8647, which is 1−e−21 - e^{-2}. The wall gets the leftover 0.13530.1353.
  5. Red value: 0.6321×0.9+0.2325×0.6+0.1353×0.1=0.5689+0.1395+0.0135=0.72200.6321 \times 0.9 + 0.2325 \times 0.6 + 0.1353 \times 0.1 = 0.5689 + 0.1395 + 0.0135 = 0.7220.

Try it: suppose the fog thins out at sample 4, so its density is 00 there and everything else is unchanged. Work out the red value the same way, noting which weight becomes 00 and what reaches the wall. Answer: 0.60570.6057. The contribution from sample 3 is still 0.56890.5689. After sample 3 only 0.36790.3679 of the light survives, so the wall contributes 0.3679×0.1=0.03680.3679 \times 0.1 = 0.0368. Then 0.5689+0.0368=0.60570.5689 + 0.0368 = 0.6057.

Common mistakes and tradeoffs

It is tempting to think that density is the same as opacity. In fact opacity is 1−e−σδ1 - e^{-\sigma\delta}, which depends on the step length δ\delta as well. The same density over a shorter step blocks less light, so a renderer must keep δ\delta in the formula.

It is tempting to average the sample colours. In fact each colour is weighted by the light that reaches the camera through it, because the samples in front block the ones behind. A bright sample hidden behind a dense one should count for little, and averaging would give it the same say as a visible one.

It is tempting to use α\alpha directly as the weight. In fact the weight is TαT\alpha. In Example 2, using α3=0.6321\alpha_3 = 0.6321 as its weight would give the hidden third sample far too much influence. Forgetting TT is the most common bug.

It is tempting to expect the weights to add to 11. In fact they add to the opacity of the ray. A fully opaque ray has weights that sum to 11, and a clear ray leaves everything to the background.

Use volume rendering for things that are genuinely translucent, such as fog, smoke, cloud and learned radiance fields. Its formula is smooth in the density and colour, so you can fit it to photographs with gradients. The cost is many samples per ray, and the answer depends on how finely you sample. For solid surfaces, drawing triangles with a rasteriser is much cheaper and sharper. Gaussian Splatting avoids sampling each ray by projecting soft blobs onto the image and blending them in depth order, which is faster to draw but gives up some of the physical form.

History

In the 18th century, Pierre Bouguer and Johann Heinrich Lambert described how light weakens exponentially as it passes through an absorbing material. In 1852 August Beer added how the weakening depends on concentration, and the result is now called the Beer-Lambert law. In 1984 James Kajiya and Brian Von Herzen published a method for ray tracing volume densities in computer graphics. Nelson Max's 1995 survey of optical models for direct volume rendering set out forms close to those in use today. The method spread through scientific visualisation and visual effects. In 2020 Mildenhall and colleagues used the same integral with a neural network to render new views of real scenes (NeRF: Representing Scenes as Neural Radiance Fields for View Synthesis).

Recap

  • A pixel corresponds to one ray, and its colour is an integral along that ray.
  • Density gives the opacity of each small piece of ray: α=1−e−σδ\alpha = 1 - e^{-\sigma\delta}.
  • Transmittance is the light that survives the pieces in front, Ti+1=Ti(1−αi)T_{i+1} = T_i(1 - \alpha_i).
  • Each colour is weighted by TαT\alpha, the weights add to the opacity, and the background receives the rest.
  • Read next: Alpha Compositing for the same blending rule in two dimensions, Gaussian Splatting for a faster alternative, and NeRF: Representing Scenes as Neural Radiance Fields for View Synthesis, which trains a network to supply the densities and colours.

Check yourself

QuestionRecall: what fraction of light survives a piece of length δ\delta with constant density σ\sigma?Show answer

It is e−σδe^{-\sigma\delta}. The opacity of that piece is one minus this, 1−e−σδ1 - e^{-\sigma\delta}. Surviving and blocked light always add to one.

QuestionComputation: a single sample has density 11 and length 33. What is its opacity?Show answer

The opacity is α=1−e−1×3=1−e−3≈1−0.0498=0.9502\alpha = 1 - e^{-1 \times 3} = 1 - e^{-3} \approx 1 - 0.0498 = 0.9502. Almost all the light is blocked, because the product of density and length is large.

QuestionWhy: why must the weight of each sample include the transmittance TT, not just its opacity α\alpha?Show answer

A sample's colour reaches the camera only if the light it gives off survives the samples in front of it. The opacity alone says how much this sample blocks, but a sample hidden behind a dense one should contribute little. Multiplying by TT accounts for that.

QuestionApplied: a renderer doubles the number of samples along a fixed ray, keeping the density and colour functions the same. Does the total opacity of the ray change?Show answer

No. The total opacity is 1−e−∫σ ds1 - e^{-\int \sigma\,ds}, which depends only on the integral of density along the ray. Cutting each piece in two gives σiδi\sigma_i\delta_i values that add up to the same integral, and the product of the (1−αi)(1 - \alpha_i) factors is unchanged. Each individual αi\alpha_i changes, but the total does not. What the extra samples do change is how accurately the colour sum approximates the integral.

Sources