Feature Library

Thin films and gratings

4.40 GPU · 0.11 CPU ms
A still of the Thin films and gratings demo
Still
The live demo starts when it’s in view.Move the pointer to move the light (or drag, or the arrow keys) · pick a surface

The rainbow sheen of a soap bubble, oil on water, a silicon wafer, an optical disc and a beetle’s shell, worked out across the spectrum.

What it costs a frame

4.40 ms
0.11 ms
GPU
4.40 ms
CPU, median
0.11 ms
CPU, 95th pct
0.30 ms
Draw calls
0
Weight
Heavy

Measured 11 November 2026 on 13th Gen Intel(R) Core(TM) i9-13900H, Chrome 157, 1920×1080 at 1×. 600 frames × 3 rounds, median kept. What ran: All four surfaces side by side with the films flowing and the light swinging: one full-screen pass, each film summed over 12 wavelengths, timed with its own timestamp queries. Phones: tiers are being measured next.

On every machine it was measured on
MachineGPU msCPU msCPU p95
unknown GPU · 13th Gen Intel(R) Core(TM) i9-13900Hreference · measuring tool4.400.110.30
apple · Apple A19 Proin a browser tab · 16 November 20266.930.140.84
Intel Arc 140V (Xe2-LPG) · Intel Core Ultra 7 258Vin a browser tab · 15 November 20261.110.190.42

Bars against one 16.7 ms frame. A browser-tab run is timed in a visible tab without the tool’s scripted input, so read it as close, not exact.

What it needs

Renders with
WebGPU or WebGLWebGPU where there is one, WebGL2 where there is not.
Your browser
Runs hereNo WebGPU needed
Depends on
NothingNo other packages

What it’s for

  1. Soap bubbles and foam
  2. Oil, petrol and puddles
  3. Coated lenses, glass and metals
  4. Chips, wafers and discs
  5. Beetles, feathers and pearl
  6. Holographic foil and stickers

Pitfalls, in brief

  1. thinFilm returns a tint, not a colour: multiply what the surface reflects (the environment, a light) by it.
  2. Thickness is in nanometres and the wavelengths are too.
  3. cosTheta is between the normal and the eye, in the outer medium (air).
  4. The film’s base matters as much as the film: soap on air turns black as it thins to nothing, oil on water fades to the water’s own reflection, oxide on silicon to silicon’s grey mirror.
  5. A grating needs a small, bright light: it maps each direction to one wavelength, so a broad soft light averages to white.
4 more in brief
  1. The CIE fit gives some negative sRGB at the spectrum’s pure colours: they are clipped at 0, so spectral colours are a touch duller than the real ones.
  2. The renderer draws at the canvas’s own width and height: size the canvas (CSS size × devicePixelRatio) before drawing.
  3. Never more than one draw on the GPU at a time: a draw asked for meanwhile waits and only the latest is drawn.
  4. Without WebGPU the renderer draws with WebGL2 from thin-film-glsl.ts, a line-for-line twin of the WGSL: change one and change the other, or the two paths drift apart.
9 pitfalls in full, the hook-up guide, the module source and its tests come with the part.

Where it comes from

From SpaciousPro. Fully custom code.

Chris Wyman, Peter-Pike Sloan & Peter Shirley, Simple Analytic Approximations to the CIE XYZ Color Matching Functions (2013): cieRgb is their multi-lobe fit of the 1931 observer, turned to linear sRGB with the standard matrix.

Airy’s formula for a thin film, with Fresnel’s coefficients: filmReflectance is the textbook single-layer sum (as in Born & Wolf, Principles of Optics), for s-polarised light.

The grating equation: grating solves sin θi + sin θo = mλ/d for each of the first three orders.

Mark Jarzynski & Marc Olano, Hash Functions for GPU Rendering (2020): Their pcg3d hashes the demo scene’s value noise, in place of the original’s sin hash.

thin filmiridescencediffraction gratingspectralciewgslglslraw webgpuwebgl2 fallbackray tracing

Near it in the frame