Three.js Water Caustics Shader (Refraction Jacobian)

Physically motivated pool caustics in Three.js, where sunlight refracted through an animated water surface is focused into bright filaments on the floor, walls and objects, with optional chromatic dispersion.

Key topics: three.js caustics, water caustics, refraction, pool shader, light focusing, glsl

What this code does

- Refraction Map: each floor point follows the sun ray bent by the wave normal (`refract`) down to the pool floor.
- Light Concentration: brightness is the area ratio of a tiny patch before and after refraction, the inverse Jacobian determinant.
- Focal Depth: caustics sharpen when the floor sits near the surface's focal distance and blur or fold beyond it.
- Dispersion: tracing red, green and blue with slightly different indices of refraction adds rainbow fringes.
- Water Surface: the same wave function displaces the transparent surface with Fresnel reflection and sun glints.
- Controls: wave amplitude, scale and speed, focus depth, intensity, chromatic dispersion and sun angle.

Continue Learning

JavaScript Implementation

ES6+
// Caustic brightness = how much a small patch of refracted rays shrinks on the floor
const causticGLSL = `
  vec2 landing (vec2 surfacePoint) {
    vec3 n = waveNormal(surfacePoint);                  // normal of the animated water surface
    vec3 r = refract(-sunDir, n, 1.0 / 1.333);          // ray bent by the water
    return surfacePoint + r.xz / -r.y * poolDepth;       // where it hits the floor
  }
  float caustic (vec2 q) {
    float e = 0.04;
    vec2 a = landing(q), b = landing(q + vec2(e, 0.0)), c = landing(q + vec2(0.0, e));
    float area = abs((b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x));
    return e * e / max(area, 1e-5);                      // > 1 where rays converge
  }`