Color handling in OSL shaders

Octane is a spectral renderer, so it handles the color variable type in a non-standard way.

Spectrum and RGB

Depending on which function returned a given color value, a color variable may be represented internally as RGB or as a spectral color. When expressions use both RGB colors and spectral colors, the RGB colors will be converted to spectral colors.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// a will be an RGB color
color a = 1;
// b will be an RGB color
color b = {1, 0.5, 0};
// c will be a spectral color
color c = _gaussian(1, 0.1);

// adding two RGB colors results in another RGB color
color d = a + b;
// adding a spectral color to another color always results in
// a spectral color
color e = a + c;

In practice, most colors will end up being represented as RGB colors. The main exceptions are blackbody and Gaussian spectra.

For input variables the representation is determined by the node connected to the corresponding input pin.

RGB colors support element access (using []) and casting to point-like types. For spectral colors this can be done only approximately, and the result will have poor color fidelity. The compiler will emit a warning if this happens.

1
2
3
4
// f1 will contain 0.5 as expected
float f1 = b[1];
// the value of v1 will depend on the wavelength sampling.
vector v1 = (vector) c;

Last update: November 15, 2021