A crystal is a stack of mirrors that only work at one wavelength
Take a lot of identical objects and arrange them so the pattern repeats at fixed intervals in three directions. That is a crystal. The repeating units can be atoms, and in table salt they are; they can also be plastic beads, protein molecules, or, in the case of opal, tiny balls of silica glass that settled out of groundwater over a few million years and packed together like oranges in a crate.
Once the arrangement repeats, you can slice through it with a family of parallel flat planes that all contain the same population of scatterers. There are many such families, running through the stack at different angles, and each family has its own fixed spacing between neighbouring planes. Call that spacing d.
Now shine light in. A little of it bounces off the first plane, a little off the second, a little off the third, and so on down. The wave that came back from the second plane travelled further than the wave from the first, and by simple geometry the extra distance is 2d·sinθ, where θ is measured from the plane rather than from its normal. If that extra distance happens to be a whole number of wavelengths, all the returning waves are in step and reinforce each other. If it is not, they arrive out of step and cancel. That is Bragg's law, written down in 1913 and worth a Nobel Prize the year after:
Note what it does. It takes something you cannot see, the internal spacing d, and converts it into something you can measure, the angle at which a particular wavelength comes back strongly. Run it backwards and a diffraction pattern becomes a map of where the atoms sit. That inversion is the whole of X-ray crystallography, and it is how we learned the structure of DNA, haemoglobin, and most of the drugs in your medicine cabinet.
Why X-rays for atoms and daylight for opal
Bragg's law contains a hard constraint. Since sinθ can never exceed 1, you need λ ≤ 2d or no order ever satisfies the equation. Atoms in a solid sit a few tenths of a nanometre apart, so seeing atomic planes requires radiation with a wavelength of that order, which means X-rays. Visible light, at four hundred to seven hundred nanometres, is thousands of times too coarse. It walks straight past the atomic lattice without noticing it is there.
Opal cheats the constraint by building a lattice out of much larger bricks. Its silica spheres run from about 150 to 400 nm across, roughly a thousand times an atomic diameter, which puts the plane spacing squarely inside the visible band. The stone is doing X-ray crystallography on itself, in your hand, with sunlight, and reporting the answer as colour. Physicists call this arrangement a photonic crystal: same mathematics, wavelengths shifted up by three orders of magnitude.
The tradeoff is that the effect is now visible to the naked eye and enormously sensitive to angle, which is the whole appeal of the stone and the reason a photograph of an opal never looks like the opal.
The vector form, and why the shader uses it
Bragg's law in the form above assumes the light comes in at θ and leaves at θ, mirror-fashion, off one chosen family of planes. That assumption is fine at a diffractometer where you control the geometry. It is useless here, because the eye and the lamp are wherever they happen to be, the stone is full of patches tilted every which way, and no single family is in the mirror position.
The general statement is due to Max von Laue and is a condition on vectors instead of angles. Write the direction light travels before scattering as ωin and after as ωout, both unit vectors. The wave gains momentum equal to the difference, scaled by 2πn/λ. Constructive interference happens when that gain lands exactly on one of a discrete set of vectors G determined by the lattice:
The set of allowed G is called the reciprocal lattice. It is a second lattice, living in a space of directions rather than positions, with one point for every family of planes in the real crystal. A family with wide spacing d gives a short vector, a family with tight spacing gives a long one, because |Ghkl| = 2π/dhkl. The three integers hkl are Miller indices, a label for the family, and {111} or {200} below are just names of particular sets of planes.
Split the condition into length and direction and it becomes something a shader can evaluate in a few lines. The length fixes the wavelength with no free parameter:
The alignment test is not enforced exactly, because a real diffracting patch is finite. A stack of N planes has an angular acceptance of roughly 1/N rather than zero width, so the renderer scores alignment with a Gaussian, w = exp(−(1 − |q̂·Ĝ|)/σ²). The coherence-width slider is σ. Narrow it and the patches get smaller, harder-edged and more saturated, which is what a well-ordered stone looks like; widen it and the stone goes milky as many wavelengths fire at once. Crystallographers meet the same effect as Scherrer broadening, where a diffraction peak's width tells you how big the crystallites were.
Set the eye and the lamp back into the mirror geometry and the vector form collapses to Bragg's law, as it must. In that case |ωout − ωin| = 2cos θi, with θi measured from the plane normal, and λ = 2 n d cos θi = 2 d sin θ.
Which planes exist in a heap of spheres
Silica spheres settling under gravity end up in the densest packing available, face-centred cubic, the same arrangement as stacked cannonballs. That fixes the geometry completely. For touching spheres of diameter D the cube edge is a = √2·D, and the two families that matter are:
There are four distinct ⟨111⟩ directions in a cube, along its body diagonals, and three ⟨100⟩ directions, along its edges. The shader tests all seven per sample. The {111} family is the strong one and does nearly all the work, which is why the {200} contribution is a checkbox rather than always on.
One more correction is needed before the numbers come out right. Light inside the stone does not travel at c. It sees a mixture of glass and water-filled gaps, and moves at c divided by an effective index. Averaging the dielectric constant over the packing fraction f = 0.74 gives
and folding Snell's law at the polished surface into Bragg's law produces the form used throughout the photonic-crystal literature, in which θ is measured outside the stone where you can actually measure it:
Two consequences you can watch on the sliders. Turning the stone away from the lamp raises θ, shrinks the square root and shifts the colour toward blue, which is the blue-shift every opal shows as you tilt it. And filling the voids with something of higher index raises neff and pushes the whole stone toward red, which is a real effect used in photonic vapour sensors: soak the crystal in solvent and the colour moves.
Not one crystal, a few thousand
A gem-quality opal is a mosaic. Ordered patches tens to hundreds of microns across, each with its own lattice orientation and its own mean sphere size, sit packed against each other and against regions where the spheres never sorted themselves at all. Gemmologists call the disordered material potch.
That mosaic structure is why play-of-colour arrives as discrete blades rather than a smooth wash. Each patch satisfies the Laue condition only for its own narrow window of geometry, so at any instant a scattered few of them are lit and the rest are dark, and rotating the stone by a degree swaps which ones. The same physics turns up in powder diffraction, where you deliberately grind a crystal into randomly oriented grains so that some fraction of them is always in the diffracting position, no matter how you point the beam.
The renderer builds the mosaic as a jittered three-dimensional Voronoi partition. Every cell draws a lattice orientation whose [111] axis is tipped off the growth direction by the orientation-spread control, a sphere diameter drawn around the global mean, and a coin flip against the ordered fraction deciding whether it diffracts or is inert. Set orientation spread to zero and you get a single crystal that sweeps cleanly from red to green as it turns. Push it to 90° and you get a powder.
From a wavelength to a pixel
Each accepted diffraction event yields one wavelength in nanometres, which then has to become a screen colour. The route runs through the CIE 1931 standard observer, three curves measured in the 1920s by asking people to match monochromatic light with a mix of three primaries. Integrating a spectrum against those curves gives the tristimulus values X, Y and Z, and a fixed 3×3 matrix takes XYZ to linear sRGB. The shader uses the piecewise-Gaussian fit of Wyman, Sloan and Shirley rather than a lookup table, because ten lines of arithmetic beats a texture fetch on a GPU.
Finite patch size broadens the spectral peak as well as the angular one, so before accumulation each tristimulus value is desaturated toward equal-energy white in proportion to σ. Beyond that the shading is ordinary computer graphics: exact unpolarised Fresnel at the surface, Beer–Lambert extinction along the internal path, single-scattering for the body, and one refraction out the back for the see-through.
Sampling is importance-weighted rather than uniform. At high turbidity almost all the returned light comes from the first fraction of a millimetre, so the sample positions are placed at equal intervals of transmittance instead of equal intervals of distance. Every sample then carries the same weight and none are wasted in the dark interior, which is most of why the patches read as shimmer rather than grain.
Where the model is wrong
The point of computing this instead of faking it is that the failures are legible, so here they are:
- Scattering is kinematic. Diffraction efficiency is a tunable gain rather than a structure factor computed from the sphere form factor, and there is no dynamical coupling. A real stop band has a width and a reflectance set by index contrast and layer count, and it depletes the transmitted beam. Here it does neither.
- The internal illumination direction is refracted once, at the surface normal where the camera ray entered, then reused along the whole path. Exact when lamp and eye look at the same patch of dome, and it drifts at grazing lamp angles.
- Single scattering only. No light diffracts in one domain and again in the next.
- No dispersion in the surface refraction. neff is fixed across the visible band, so there is no prismatic fire on the girdle. Fixing it means spectral path tracing at three or more marches per pixel.
- The Voronoi mosaic is isotropic. Real domains grow columnar along the sedimentation direction, which is where directional harlequin patterning comes from.
- Domains differ from their surroundings only in their diffraction, so nothing scatters at a domain wall.
References
- Chris Wyman, Peter-Pike Sloan, Peter Shirley. Simple Analytic Approximations to the CIE XYZ Color Matching Functions. Journal of Computer Graphics Techniques 2(2), 1–11, 2013. jcgt.org/published/0002/02/01
- Jean-Michel Lourtioz et al. Optics of an opal modeled with a stratified effective index and the effect of the interface. arXiv:1507.08548. arxiv.org/abs/1507.08548 — derivation of λ/D = 2(2/3)1/2(neff²−sin²θ)1/2.
- Kevin Sanderson et al. Asymmetrical Optical Response of Opal Photonic Crystals with Graded Thickness. ECS Advances, 2023. iopscience.iop.org/article/10.1149/2754-2734/ad0cd4 — d111 = 0.816 D, Bragg–Snell fits to angle-resolved transmission.
- Nadia Yaacoub et al. Structure and Optical Properties of Opal Films Made by an Out-of-Plane Electric Field-Assisted Capillary Deposition Method. ACS Omega, 2022. pmc.ncbi.nlm.nih.gov/articles/PMC8908520 — volume-averaged neff, f = 0.74.
- Ciara Egan et al. Dispersion and Thickness Control in Evaporation-induced Self-Assembly of Opal Photonic Crystals. arXiv:2306.16749. arxiv.org/abs/2306.16749 — how far the Bragg–Snell model holds against measured sphere diameters.
- John D. Joannopoulos, Steven G. Johnson, Joshua N. Winn, Robert D. Meade. Photonic Crystals: Molding the Flow of Light, 2nd ed. Princeton University Press, 2008. ab-initio.mit.edu/book — the dynamical treatment this renderer does not implement.