Light Transport

Blue glasswork showing dispersion
Shimmer render of frame675.pbrt. The effects of dispersion are visible in the top-right portion of the image. The intricate glasswork requires a high maximum depth for paths traced in the scene.

I think spectral rendering and Rust are cool and spent a year obsessively coding a rendering engine about it. I hope you also think it's cool! This page is meant to help interested readers build an intuition for ray tracing and spectral rendering.

Renders are from Shimmer, the aforementioned engine.

Physically Based Rendering

The goal of physically based rendering (PBR) is to transform scene descriptions into synthesized images that are perceptually and measurably equivalent to images captured by a physical camera. A key approach is to frame the problem as one of electromagnetic radiation. While light can be understood as a wave, at the scale of rendered scenes it is well described by geometric optics: energy travelling along rays.

This leads to the ray tracing algorithm. The algorithm follows the paths of sampled rays of light through a scene as they interact with surfaces and participating media. The energy arriving at a camera's film is estimated over many sampled rays, and that film is converted to an image in a similar manner to a real camera.

Radiometry

To model the transport of light energy, it is necessary to establish the radiometric quantities that describe it. A photon at wavelength carries energy , where is Planck's constant and is the speed of light.

Most rendering models assume that lighting is in equilibrium and does not change over time. Under this steady state assumption, we measure light at an instant. This is radiant flux, or power, in watts:

Irradiance is the area density of flux arriving at a surface (or, for flux leaving a surface, radiant exitance), in . Intensity is the angular density of flux about a solid angle, in :

Radiance additionally considers the directional distribution of light. It is the flux density per unit area, per unit solid angle, where is the projected area perpendicular to :

Radiance is the key quantity. Intensity, irradiance, flux and energy can all be found by integrating it, and it is constant along rays through empty space, which makes it well suited to ray tracing.

The Light Transport Equation

The light transport equation (LTE) describes the equilibrium distribution of radiance in a scene. It gives the exitant radiance at a point on a surface in terms of the surface's emission, its scattering properties, and the distribution of incident illumination. It was introduced independently in 1986 by Kajiya at Caltech and by Immel, Cohen and Greenberg at Cornell.

The basis for the LTE is conservation of energy. The difference between the flux leaving an object and the flux entering it equals the difference between the flux it emits and the flux it absorbs:

That is, light entering a surface is either scattered or absorbed, and light exiting a surface either originates from incoming illumination or from emission. This gives the LTE: exitant radiance is the sum of emitted radiance and scattered radiance.

Here is the bidirectional scattering distribution function (BSDF), which describes how light arriving from direction is scattered toward at . The material of the surface dictates the BSDF. The term accounts for the angle between and the surface normal.

In a scene with no participating media, the incident radiance at is the exitant radiance from the first surface hit along , where is the ray-casting function:

So the unknown radiance appears on both sides of the equation. The LTE only has an analytic solution in very limited scenarios, and a numerical solution is necessary in the vast majority of cases. To develop one, it is useful to express the LTE in terms of paths of vertices.

A floating copy of the Physically Based Rendering book
Shimmer render of book.pbrt. This scene includes image textures and a layered BSDF, CoatedDiffuse: a thin dielectric coating on a diffuse material.

Surface Form of the LTE

Foregoing a full derivation, the LTE can be rewritten as an integral over the surfaces of the scene rather than over directions. For points , and on surfaces in the scene, with for pointing from toward :

The parallels to , and in the first form are clear upon examination. The BSDF is rewritten in its three-point form, , and is a geometric coupling term:

where is 1 when the two points are mutually unoccluded and 0 otherwise.

Shimmer render of crown.pbrt. This scene shows off a variety of materials.

The Ray Tracing Algorithm: Integrating over Paths

Consider that the surface form of the LTE is recursive: the term can itself be defined by the surface form. We can define a path traced through a scene with vertices

Then to find the exitant radiance from to , we repeatedly substitute the right-hand side into itself:

More compactly,

where is the radiance scattered over all paths with vertices.

Fundamentally, the ray tracing algorithm is a Monte Carlo method for solving this integral. If is on the film plane, a path can be sampled in the scene to find the radiance that path contributes at . Across many such sampled paths for many points on the film, weighted by their sampling density, the film's response can be estimated and then transformed into an image.

Much work in ray tracing then stems from how to efficiently and accurately converge on that image. Paths may be sampled more intelligently, lights may be sampled directly (an infinitesimally small light would never be hit by a random walk), and so on. But this provides the basis for the ray tracing algorithm.

A sunlit living room with a lounge chair and shelves
Shimmer render of kroken/camera-1.pbrt. This is a large, complex scene with geometry and textures that combine to 2.93GB in size.

Spectral Rendering

Many renderers represent light strictly in terms of RGB values. A spectral renderer instead transports a set of sampled wavelengths along each ray, along with the spectral values at those wavelengths, converting from RGB for scene input and back to RGB for the final image. This allows modelling behaviors of light that depend on wavelength, such as dispersion, iridescence, diffraction, metamerism and fluorescence.

It also avoids a problematic property of RGB light transport: the multiplication of two RGB values depends on the choice of color space. Pure green in sRGB, , is in ACES2065-1. Squaring the sRGB value leaves it unchanged, but squaring the ACES value gives , a different color which lies outside the gamut of physically realizable colors entirely. The equivalent operations in the spectral domain are physical, and do not depend on a choice of basis colors or whitepoint.

CIE chromaticity diagram with the ACES2065-1 gamut and a point just outside the spectral locus near 540nm
The RGB color (0.14, 0.67, 0.01) in ACES2065-1, plotted as the blue point near the 540nm label. It lies outside the horseshoe of physically realizable colors.

Spectral rendering does not do away with RGB. Artists cannot feasibly define materials using spectral functions, and display technology is based on RGB. So scene data must be converted from RGB to spectra, and the spectral energy accumulated on the film must be converted back to RGB. Both directions rest on the tristimulus theory of color.

Tristimulus Theory of Color

The tristimulus theory of color states that, as far as human perception is concerned, a spectral distribution can be represented as three scalar values , corresponding to the three types of cone cells in the human eye. For a given spectral distribution , these values are found by integrating the product of the distribution with a spectral matching function :

The choice of matching functions defines the color space. CIE XYZ is a device-independent color space with matching functions , and , derived from color matching experiments with human observers. The coefficient corresponds to luminance.

The CIE 1931 x-bar, y-bar and z-bar color matching functions plotted over 360 to 780 nanometers
The CIE 1931 XYZ matching functions, plotted from the analytic fit of Wyman, Sloan and Shirley (2013).

Other color spaces, such as sRGB and ACES2065-1, could be defined by their own matching functions. In practice each RGB color space is defined by its three primaries and a whitepoint, and there is an invertible matrix that transforms XYZ coefficients into that space's RGB coefficients. So it suffices to compute XYZ, and convert between color spaces with matrix multiplication.

CIE chromaticity diagram showing the sRGB and ACES2065-1 gamuts as triangles
The gamuts of sRGB and ACES2065-1 on the CIE chromaticity diagram. The vertices of each triangle are the color space's primaries. The colored region is the range of colors perceivable by humans.

Integrating against three matching functions projects an infinite-dimensional spectrum down to three numbers, so information is lost. Distinct spectral distributions which produce the same tristimulus values are called metamers. They are perceived as the same color.

Spectra to RGB

Transporting full spectral distributions along rays would be inefficient. Instead, a small number of wavelengths (typically four) are sampled for each path from the visible range, and only the spectral values at those wavelengths are carried. Across many samples, the full distributions are effectively modelled through a Monte Carlo process.

Because the full distribution arriving at the film is never known, the XYZ coefficients are themselves estimated with Monte Carlo. For sampled wavelengths with probability densities :

and likewise for and . Normalizing by the integral of means a constant spectrum of value 1 has luminance . The XYZ coefficients are then transformed to the output color space.

RGB to Spectra

The conversion from RGB to spectral distributions is more ambiguous. Because of metamers, a single RGB value corresponds to infinitely many spectral distributions. A renderer must choose a reasonable one. The chosen spectra should be smooth, efficient to evaluate, conserve energy (a reflectance can never exceed 1), and give the same RGB coefficients when converted back to the same color space.

Shimmer follows the approach of Jakob and Hanika, which represents a spectrum as a quadratic polynomial in passed through a sigmoid:

The sigmoid maps the real line onto , so every such spectrum is a valid reflectance, and the quadratic keeps it smooth. Three coefficients are a natural fit for three color components. Suitable coefficients are found by optimization for every RGB value in , minimizing the round-trip error, and precomputed into a lookup table for each color space. Evaluating a spectrum at render time is then a table lookup and a few arithmetic operations.

Emission is not bounded by 1, so for light sources the RGB value is scaled into range, and the resulting spectrum is scaled back up and multiplied by the color space's standard illuminant (for example, D65 for sRGB).

A carved Ganesha statue
Shimmer render of ganesha.pbrt. This scene shows off image textures and coated diffuse materials, and contains over 4.3 million triangles.
Intricate blue glass machinery
Shimmer render of transparent_machine.pbrt.

References