I E

Sunstone dev log 29: physical units audit

My main shader calculates radiance (Wm−2sr−1Wm^{-2}sr^{-1}) at each pixel of the framebuffer. Currently there are two kinds of light sources: directional and ambient.

Directional light

A directional light has an illuminance (lm⋅m−2lm\cdot{m}^{-2}), colour, and direction. Its radiance equation is

L=fr⋅E⋅cos⁡θ L = f_r \cdot E \cdot \cos\theta

where frf_r is the surface’s BRDF (parameters omitted), EE is the light’s irradiance, and cos⁡θ\cos\theta is the angle between the light’s direction and the surface’s normal.

The first problem: I’ve given an illuminance for the directional light, so how do I get the irradiance EE?

I’m working in the sRGB colour space, which has a linear map to luminance via CIE XYZ:

Y(r,g,b)=0.2126r+0.7152g+0.0722b Y(r, g, b) = 0.2126r + 0.7152g + 0.0722b

Since we can compute luminance from an sRGB triple, we can treat the triple as a spectral radiance distribution. My colour components are in the range [0,1][0, 1], so the maximum YY for a colour is 1. Scaling a triple by 1Y\frac{1}{Y} gives a triple with with Y=1Y = 1. In other words, a spectral radiance distribution of luminance 1.

Y(1Y(r,g,b)(r,g,b))=1Y(r,g,b)Y(r,g,b)=1 \begin{aligned} & \, Y(\frac{1}{Y(r, g, b)} (r, g, b)) \\ = & \, \frac{1}{Y(r, g, b)} Y(r, g, b) \\ = & \, 1 \end{aligned}

So scaling an sRGB triple colour\text{colour} by Lv1Y(colour)L_v \frac{1}{Y(\text{colour})} (where LvL_v is luminance) gives a spectral radiance distribution with luminance LvL_v.

L=Lv1Y(colour)colour L = L_v \frac{1}{Y(\text{colour})} \text{colour}

This also relates irradiance and illuminance (E=Ev1Y(colour)colourE = E_v \frac{1}{Y(\text{colour})} \text{colour}), which are the actual quantities used for the directional light.

The second problem: if EE is irradiance, how does L=fr⋅E⋅cos⁡θL = f_r \cdot E \cdot \cos\theta become radiance? The BRDF simply has units sr−1sr^{-1}.

Ambient light

Ambient light has a luminance (lm⋅m−2⋅sr−1lm\cdot{m}^{-2}\cdot{sr}^{-1}) and a colour. The spectral radiance distribution is Lin=Lv1Y(colour)colourL_{in} = L_v \frac{1}{Y(\text{colour})} \text{colour}, giving the radiance equation

L=∫Ωfr⋅Lin⋅cos⁡θωdω‾ L = \int_{\Omega} f_r \cdot L_{in} \cdot \cos\theta_{\omega} \, d\bar{\omega}

I assume that ambient radiance is constant and direction-independence, so LinL_{in} factors out of the integral:

L=Lin∫Ωfr⋅cos⁡θωdω‾ L = L_{in} \int_{\Omega} f_r \cdot \cos\theta_{\omega} \, d\bar{\omega}

The remaining integral is approximated as described here. The BRDF has units sr−1sr^{-1}, and the integral essentially multiplies by srsr, giving a unitless quantity, when is then used to scale radiance.

Camera

The post-processing shader uses the radiance values to produce the final image. The camera equation computes radiant exposure (Jm−2Jm^{-2}), which is written to the framebuffer (see my argument for this here). This is the step where tone mapping can be applied.

Next steps

← LLM demos Newest