Caustics
glint · Examples · Tutorials · Gallery
Sunlight through a rippling water surface onto a pool floor: an iterative caustic pattern, refracted tiles, chromatic spread and god rays.

An example in glint, a free live shader editor in the browser: the GLSL code floats over the running picture and recompiles as you type.
The code
One pass, in GLSL.
fragment shader
#version 300 es
precision highp float;
uniform float time;
uniform vec2 resolution;
uniform vec2 mouse;
uniform vec3 spectrum;
uniform vec3 spectrumIntegrated;
uniform float beat;
uniform float depth;
in vec2 v_texcoord;
out vec4 fragColor;
// The classic iterative water pattern (after Dave Hoskins / "TDM"): repeatedly offset a point by sin/cos of itself.
float caustic(vec2 p, float t) {
// The pattern is tuned for coordinates wrapped to 0..2pi and shifted far from the origin.
p = mod(p * 6.28318, 6.28318) - 250.0;
vec2 i = p;
float c = 1.0, inten = 0.005;
for (int n = 0; n < 5; n++) {
float tt = t * (1.0 - 3.5 / float(n + 1));
i = p + vec2(cos(tt - i.x) + sin(tt + i.y), sin(tt - i.y) + cos(tt + i.x));
c += 1.0 / length(vec2(p.x / (sin(i.x + tt) / inten), p.y / (cos(i.y + tt) / inten)));
}
c /= 5.0;
c = 1.17 - pow(c, 1.4);
return clamp(pow(abs(c), 8.0), 0.0, 4.0);
}
void main() {
vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
float t = time * 0.5 + 23.0;
// Surface normal from a few moving waves; it bends the view of the floor.
vec2 wave = 0.02 * vec2(sin(uv.y * 12.0 + time * 1.3) + sin(uv.x * 7.0 - time), cos(uv.x * 10.0 + time * 1.1) + cos(uv.y * 8.0 - time * 0.7));
vec2 floorUv = (uv + wave * depth) * 2.0;
// Tiles with grout.
vec2 g = abs(fract(floorUv * 3.0) - 0.5);
float grout = smoothstep(0.47, 0.5, max(g.x, g.y));
vec3 tile = mix(vec3(0.55, 0.8, 0.85), vec3(0.3, 0.5, 0.6), grout);
// Caustics, one sample per colour channel for a little dispersion.
vec2 cp = floorUv * 3.0;
vec3 caus = vec3(caustic(cp + 0.02, t), caustic(cp, t), caustic(cp - 0.02, t));
vec3 col = tile * (0.35 + caus * 1.4);
// Depth tint and light shafts from the surface.
col = mix(col, vec3(0.0, 0.25, 0.35), 0.35 * depth);
float shafts = pow(caustic(vec2(uv.x * 2.0 + uv.y * 0.6, t * 0.2) * 3.0, t * 0.5), 0.5);
col += vec3(0.4, 0.8, 0.9) * shafts * 0.15 * (0.5 + uv.y);
col *= 1.0 - 0.3 * dot(uv, uv);
fragColor = vec4(col, 1.0);
}More Showpieces examples
SmoothLife · Glitch · Quaternion Julia · Aurora · Glass orbs · Four to the floor