spectralSum

Light seen across the whole visible spectrum, added up slice by slice into one rgb colour.

spectralSum(
  opts: {samples: number; jitter: Expr; deps?: Expr[]; hint?: string},
  body: (band: SpectralBand) =,
> Expr): Expr

Import materials from shaders/std and use materials.spectralSum.

The word behind dispersion: anything that bends each colour by a different amount (glass, water, a diamond) traces each slice separately and lets this word mix them back into colour. body(band) returns the rgb light seen at that slice; give it a slice-dependent index of refraction and white edges fan into spectra. samples is how many slices (8 is smooth, 4 is cheap, 2 the fewest that reach every colour; fixed when the shader compiles). jitter is a 0–1 per-pixel offset (interleavedNoise) that turns banding between slices into fine grain; pass a constant 0.5 where neighbours must agree. Anything the body reads from outside goes in deps. One real GPU loop.

Parameters

NameTypeNotes
opts{samples: number; jitter: Expr; deps?: Expr[]; hint?: string}
body(band: SpectralBand) => Expr

Example

const rgb = spectralSum({samples: 8, jitter, deps: [n]}, (band) => {
  const ior = add(u.refraction, mul(sub(band.t, 0.5), spread))
  return backdropThrough(ior)
})

Tip. The cost is samples times the body. Keep texture taps in the body to one or two.

See also

  • spectralWeight — The colour one slice of the visible spectrum adds to white light, as linear rgb weights.
  • throughSlab — Follow one ray through a transparent body: in through the front face, out through the back.
  • sampleSum

Used by

PrismaticGlass