Swimming Pool
Looking down into a tiled pool on a sunny day. The water's surface is a sum of six waves, and each view ray is bent where it enters by Snell's law, so the tiles wobble. Sunlight is bent by the same surface: where the waves act as lenses they squeeze a patch of light into a smaller patch of floor, brighter by just as much, and the ratio of the two areas is the Jacobian determinant of the map from where light enters to where it lands, worked out from three nearby rays for every pixel. That makes the net of bright lines. The water takes red away on the way down and back, the surface mirrors the sky more toward the far side, drops fall in and send out rings that bend the tiles and the light, and every motion comes round in a minute.
· Light & Materials · Fragment shader
Teaches Caustics: light bent by a moving surface, gathered on the floor
Builds on Flowing Color Field (hash21), Rippling Water (Fresnel mirror), Kiln Floor Tiles (wrap into a period), Ocean Waves, Plucked Strings (time in the loop), Underwater (the sun's direction, smooth sound noise, a bubble's note)
Watch
The idea
Sunlight crossing a pool's surface is bent by Snell's law, toward straight down. Where the surface is flat every ray is bent alike and the floor is lit evenly. Where a wave curves the surface, it is a weak lens: the light that came through a patch of surface lands on a smaller patch of floor, which is brighter by just that much, and the patch beside it lands on a larger one, which is dimmer. The bright lines on a pool floor, the caustics, are the places where the waves squeeze the light most.
That can be worked out exactly. Follow the sunlight that comes through a point of the surface down to the floor: it is a map from the surface to the floor. How much that map stretches or squeezes a small patch is its Jacobian determinant, det, the area a patch lands on over the area it came through, as on the foam of Ocean Waves. The floor's brightness there is 1 over that: 1 under still water, more where the waves gather the light. Underwater drew its net of lines as a wave pattern; this one works it out from the surface itself, so every ring a drop sends out bends the light on the floor too.
The view is bent by the same surface. Each ray from the eye is bent where it enters the water, so the grout lines between the tiles wave as the surface moves.
The eye is 2.2 m over the water, looking down 58°, and the floor is 2.2 m down at the default depth. All lengths are in meters. Every wave goes round a whole number of times a minute, so the picture and the sound loop at 60 seconds. It is built in five steps, each a shader you can run on its own, and then finished.
Stage 1 · The tiles
The pool, drained. Each pixel's place s runs from −1 to 1 up the picture (wider across), and the ray from the eye is normalize(fwd · FOCAL + s.x · right + s.y · up), with fwd tipped down by PITCH, 1.01 radians, 58°, and FOCAL 1.7, so the picture spans about 61° from top to bottom. With no water yet, each ray runs straight to the floor, (EYE.y + depth) / −rd.y away.
tileColor draws the floor. The floor point divided by tile (25 cm) gives a grid: floor numbers each tile, and hash21 from Flowing Color Field makes each one its own shade of pool blue, 88% to 112% of (0.2, 0.5, 0.75). edge is how far a point is from the nearest grout line, and the grout, a warm cream, covers the outer 5% of the tile on every side, a tenth of a tile between two tiles.
Far off, a pixel spans many tiles, and drawn sharp they would shimmer. foot is how wide a pixel is on the floor, more where the ray is long and slanting, and aa is that in tiles. The grout's edges are softened over aa, and as aa grows from 15% to half a tile the tile is blended toward its average: grout 19% of it, the share of a tile the grout covers (1 − 0.9²), and a middle shade of blue.
The sun comes from sunInAir, as in Underwater: sun degrees over the horizon, ahead and a little to the right. For now the floor is lit by SUNLIGHT · L.y, the sunlight falling on it at a slant, plus a little blue SKYLIGHT. Then the eye: 1 − exp(−light · 1.2) keeps bright light from clipping, a gamma of 2.2, and a speck of noise, half a level of 255 either way, so the blues don't band.
The sound for now is an empty pool: murmur noise, a random value at each of 300 steps a second joined smoothly, a different one in each ear, at a low level.
stage-1-the-tiles.metal
// Stage 1 · The tiles
// The pool, drained. The eye is 2.2 m up, looking down; each ray runs
// straight to the floor. Tiles are squares in cream grout, each its own
// shade of blue. Where a pixel spans more than a tile, it takes their
// average color, so the far floor doesn't shimmer.
// @param depth 2.2 0.3 4 How deep the water is over the tiles, in meters
// @param sun 65 30 90 The sun's height over the horizon, in degrees
// @param tile 0.25 0.1 0.5 How wide a tile is, in meters
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
constant float LOOP = 60.0;
// The eye is 2.2 m over the water, looking down 58 degrees.
constant float3 EYE = float3(0.0, 2.2, -0.4);
constant float PITCH = 1.01;
constant float FOCAL = 1.7;
// Sunlight and the sky's light as they reach the water.
constant float3 SUNLIGHT = float3(1.0, 0.92, 0.8);
constant float3 SKYLIGHT = float3(0.1, 0.16, 0.26);
// Grout, a warm cream, a tenth of each tile wide.
constant float3 GROUT = float3(0.86, 0.82, 0.74);
constant float GROUT_W = 0.05;
float hash21(float2 p) {
p = fract(p * float2(123.34, 456.21));
p += dot(p, p + 45.32);
return fract(p.x * p.y);
}
float loopTime(float t) {
return t - LOOP * floor(t / LOOP);
}
// Toward the sun in the sky, from its height in degrees.
float3 sunInAir(constant Params& p) {
float e = p.sun * TAU / 360.0;
return float3(0.35 * cos(e), sin(e), 0.94 * cos(e));
}
// The floor: pool-blue tiles, each a little different, in cream grout.
float3 tileColor(float2 x, float foot, constant Params& p) {
float2 g = x / p.tile;
float2 id = floor(g);
float2 f = abs(fract(g) - 0.5);
// How far this point is from the middle of the nearest grout line.
float edge = 0.5 - max(f.x, f.y);
float aa = foot / p.tile;
float grout = 1.0 - smoothstep(GROUT_W - aa, GROUT_W + aa, edge);
// Where a pixel spans a tile, its average color instead.
float far = smoothstep(0.15, 0.5, aa);
grout = mix(grout, 0.19, far);
float v = mix(hash21(id), 0.5, far);
float3 blue = float3(0.2, 0.5, 0.75) * (0.88 + 0.24 * v);
return mix(blue, GROUT, grout);
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float2 s = (uv - 0.5) * u.resolution / u.resolution.y * 2.0;
float3 fwd = float3(0.0, -sin(PITCH), cos(PITCH));
float3 up = float3(0.0, cos(PITCH), sin(PITCH));
float3 rd = normalize(fwd * FOCAL + float3(s.x, 0.0, 0.0) + up * s.y);
// No water yet: the ray runs straight down to the floor.
float dist = (EYE.y + p.depth) / -rd.y;
float2 x = EYE.xz + rd.xz * dist;
// How wide a pixel is on the floor, to soften what is finer.
float px = 2.0 / (u.resolution.y * FOCAL);
float foot = 1.5 * dist * px / max(-rd.y, 0.3);
float3 tile = tileColor(x, foot, p);
// Sunlight falls on the floor at a slant, and the sky fills in.
float3 L = sunInAir(p);
float3 light = tile * (SUNLIGHT * L.y + SKYLIGHT);
float3 col = 1.0 - exp(-light * 1.2);
col = pow(col, float3(1.0 / 2.2));
// A speck of noise keeps the blues from banding.
float2 at = floor(uv * u.resolution);
float jitter = fract(52.98 * fract(dot(at, float2(0.0671, 0.00584))));
col += (jitter - 0.5) / 255.0;
return float4(col, 1.0);
}
// ---- The sound ----
// Noise with nothing above `rate` Hz, as in Underwater.
float murmur(float t, float rate, float seed) {
float x = t * rate;
float i = floor(x), f = fract(x);
f = f * f * (3.0 - 2.0 * f);
float h0 = hash21(float2(i, seed)), h1 = hash21(float2(i + 1.0, seed));
return mix(h0, h1, f) * 2.0 - 1.0;
}
float2 sound(float t, constant Params& p) {
float tl = loopTime(t);
// An empty pool: only the hush of air, a little apart in each ear.
float2 v = 0.15 * float2(murmur(tl, 300.0, 1.0), murmur(tl, 300.0, 2.0));
// Silence where the minute comes round, so it joins.
float turn = min(tl, LOOP - tl);
float fade = 1.0 - exp(-turn * 30.0);
return tanh(v) * p.volume * fade;
}Stage 2 · Waves bend the view
Now water fills the pool. surfaceAt gives the surface's height at a point and its slope, the sum of six waves from poolWave: each has a heading, a wavelength from 1.9 m down to 30 cm, an amplitude from 2.4 cm up and down to 2.8 mm, and a number of turns a minute, 18 to 45. Each wave is A · sin(ph) with ph = k · dot(dir, q) − turns · a, plus a start of 1.7 · i for wave i, where k is 2π over the wavelength and a the minute as an angle, 2π · t / 60, so every wave comes back to where it started after a minute. The slope of a sine is a cosine, so the slope is A · k · cos(ph) · dir, and the surface's normal is normalize(−slope.x, 1, −slope.z). waves scales all six.
Each view ray meets the water where it crosses height 0, at q, and is bent there toward the normal by refract(rd, n, 1 / 1.333), Snell's law with water's index of refraction, then runs on depth / −down.y to the floor. Moving the normal moves where the ray lands, so the grout lines wave, more the deeper the water, since the bent ray has farther to go.
This is a simplification. The waves are small beside the 2.2 m to the eye, so the ray is taken to meet a flat plane at height 0, and only the surface's slope is used, not its height. Real waves this long would also move about three times as fast: these were chosen for how they look, each going round a whole number of times a minute so the picture loops.
The sound starts here: the water lapping at the walls, described below.
stage-2-waves-bend-the-view.metal41 new lines since stage 1
// Stage 2 · Waves bend the view
// Now water fills the pool. Its surface is a sum of six waves; the
// slope of each sine is a cosine, which gives the surface's normal.
// Snell's law bends each view ray toward the normal (n = 1.333), so
// the tiles wobble, more the deeper the water.
// @param depth 2.2 0.3 4 How deep the water is over the tiles, in meters
// @param waves 1 0 2 How tall the waves are: 0 is still water
// @param sun 65 30 90 The sun's height over the horizon, in degrees
// @param tile 0.25 0.1 0.5 How wide a tile is, in meters
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
constant float LOOP = 60.0;
// The eye is 2.2 m over the water, looking down 58 degrees.
constant float3 EYE = float3(0.0, 2.2, -0.4);
constant float PITCH = 1.01;
constant float FOCAL = 1.7;
// Light bends by this ratio going from air into water.
constant float WATER_N = 1.333;
// Sunlight and the sky's light as they reach the water.
constant float3 SUNLIGHT = float3(1.0, 0.92, 0.8);
constant float3 SKYLIGHT = float3(0.1, 0.16, 0.26);
// Grout, a warm cream, a tenth of each tile wide.
constant float3 GROUT = float3(0.86, 0.82, 0.74);
constant float GROUT_W = 0.05;
float hash21(float2 p) {
p = fract(p * float2(123.34, 456.21));
p += dot(p, p + 45.32);
return fract(p.x * p.y);
}
float loopTime(float t) {
return t - LOOP * floor(t / LOOP);
}
// Toward the sun in the sky, from its height in degrees.
float3 sunInAir(constant Params& p) {
float e = p.sun * TAU / 360.0;
return float3(0.35 * cos(e), sin(e), 0.94 * cos(e));
}
// The floor: pool-blue tiles, each a little different, in cream grout.
float3 tileColor(float2 x, float foot, constant Params& p) {
float2 g = x / p.tile;
float2 id = floor(g);
float2 f = abs(fract(g) - 0.5);
// How far this point is from the middle of the nearest grout line.
float edge = 0.5 - max(f.x, f.y);
float aa = foot / p.tile;
float grout = 1.0 - smoothstep(GROUT_W - aa, GROUT_W + aa, edge);
// Where a pixel spans a tile, its average color instead.
float far = smoothstep(0.15, 0.5, aa);
grout = mix(grout, 0.19, far);
float v = mix(hash21(id), 0.5, far);
float3 blue = float3(0.2, 0.5, 0.75) * (0.88 + 0.24 * v);
return mix(blue, GROUT, grout);
}
// Wave i: heading, wavelength and amplitude in m, turns a minute.
float4 poolWave(int i) {
if (i == 0) return float4(0.3, 1.9, 0.024, 18.0);
if (i == 1) return float4(2.0, 1.3, 0.016, 22.0);
if (i == 2) return float4(-1.1, 0.83, 0.01, 27.0);
if (i == 3) return float4(1.0, 0.57, 0.0064, 33.0);
if (i == 4) return float4(-2.6, 0.41, 0.0044, 39.0);
return float4(2.7, 0.3, 0.0028, 45.0);
}
// The water's height at q and its slope: a sum of six waves.
float3 surfaceAt(float2 q, float t, constant Params& p) {
float a = TAU * t / LOOP;
float3 sum = float3(0.0);
for (int i = 0; i < 6; i++) {
float4 w = poolWave(i);
float2 dir = float2(cos(w.x), sin(w.x));
float k = TAU / w.y;
float ph = k * dot(dir, q) - w.w * a + 1.7 * float(i);
// The slope of a sine is a cosine.
sum += w.z * float3(sin(ph), k * cos(ph) * dir);
}
return sum * p.waves;
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float2 s = (uv - 0.5) * u.resolution / u.resolution.y * 2.0;
float t = loopTime(u.time);
float3 fwd = float3(0.0, -sin(PITCH), cos(PITCH));
float3 up = float3(0.0, cos(PITCH), sin(PITCH));
float3 rd = normalize(fwd * FOCAL + float3(s.x, 0.0, 0.0) + up * s.y);
// The ray meets the water at height 0, where it is bent.
float air = EYE.y / -rd.y;
float2 q = EYE.xz + rd.xz * air;
float3 w = surfaceAt(q, t, p);
float3 n = normalize(float3(-w.y, 1.0, -w.z));
float3 down = refract(rd, n, 1.0 / WATER_N);
float swim = p.depth / -down.y;
float2 x = q + down.xz * swim;
// How wide a pixel is on the floor, to soften what is finer.
float px = 2.0 / (u.resolution.y * FOCAL);
float foot = 1.5 * (air + swim) * px / max(-down.y, 0.3);
float3 tile = tileColor(x, foot, p);
// Sunlight falls on the floor at a slant, and the sky fills in.
float3 L = sunInAir(p);
float3 light = tile * (SUNLIGHT * L.y + SKYLIGHT);
float3 col = 1.0 - exp(-light * 1.2);
col = pow(col, float3(1.0 / 2.2));
// A speck of noise keeps the blues from banding.
float2 at = floor(uv * u.resolution);
float jitter = fract(52.98 * fract(dot(at, float2(0.0671, 0.00584))));
col += (jitter - 0.5) / 255.0;
return float4(col, 1.0);
}
// ---- The sound ----
// Noise with nothing above `rate` Hz, as in Underwater.
float murmur(float t, float rate, float seed) {
float x = t * rate;
float i = floor(x), f = fract(x);
f = f * f * (3.0 - 2.0 * f);
float h0 = hash21(float2(i, seed)), h1 = hash21(float2(i + 1.0, seed));
return mix(h0, h1, f) * 2.0 - 1.0;
}
// Water slapping a wall at q: noise, loud as the water rises fast,
// over a low rumble that swells as it stands high.
float lapAt(float2 q, float t, float seed, constant Params& p) {
float h0 = surfaceAt(q, t, p).x;
float rise = (surfaceAt(q, t + 0.02, p).x - h0) / 0.02;
float slap = min(max(rise, 0.0) / 0.08, 1.5);
float splash = murmur(t, 1500.0, seed) - 0.6 * murmur(t, 400.0, seed);
float body = murmur(t, 140.0, seed + 5.0) * max(0.3 + h0 * 8.0, 0.0);
return splash * slap * slap + 0.5 * body * p.waves;
}
float2 sound(float t, constant Params& p) {
float tl = loopTime(t);
// Each ear hears the wall on its side.
float2 v = float2(lapAt(float2(-2.4, 1.6), tl, 1.0, p),
lapAt(float2(2.4, 1.6), tl, 2.0, p));
v *= 0.5;
// Silence where the minute comes round, so it joins.
float turn = min(tl, LOOP - tl);
float fade = 1.0 - exp(-turn * 30.0);
return tanh(v) * p.volume * fade;
}4 lines from stage 1 that are gone
float dist = (EYE.y + p.depth) / -rd.y;
float2 x = EYE.xz + rd.xz * dist;
⋯
float foot = 1.5 * dist * px / max(-rd.y, 0.3);
⋯
float2 v = 0.15 * float2(murmur(tl, 300.0, 1.0), murmur(tl, 300.0, 2.0));Stage 3 · Caustics
The same surface bends the sunlight. landAt follows the sunlight that comes through the surface at a point q: bent by the normal there, refract(−L, n, 1 / 1.333), and on down to the floor, depth / −ray.y along.
A pixel needs the opposite: which point of the surface sent light to its floor point x. causticAt starts where still water would have sent it, x moved back up along the sun's bent path, and corrects that once: wherever the light from there actually lands, it moves q by how far that missed x. Then it sends three rays 4 mm apart, from q and from 4 mm along each way, and sees where they land, fx and fz. The patch between them has grown by det = (fx.x · fz.y − fx.y · fz.x) / (e · e), the Jacobian determinant. Under still water it is 1. Where the waves focus the light it falls toward 0, and the light on the floor, 1 / det, would be endless there, so the shader takes 1.05 / (|det| + 0.05): 1 under still water, at most 21. Where the light has gone past its focus the patch is turned over and det is negative, and |det| counts it like the rest.
This is a simplification too. Each pixel follows a single ray, found by one correction step, so where the waves fold the light past its focus and several points of the surface send light to the same place, only one of them is counted, and the lines there are approximate. Far off, where a pixel covers more than the lines are wide, the brightness is blended toward 1 by exp(−foot · 20), so the far floor doesn't sparkle.
stage-3-caustics.metal19 new lines since stage 2
// Stage 3 · Caustics
// The same surface bends the sunlight. A small patch of light through
// the surface lands on a patch of floor; where the waves act as a lens
// it lands smaller, and is brighter by the ratio of the areas, which
// the Jacobian determinant measures from three nearby rays.
// @param depth 2.2 0.3 4 How deep the water is over the tiles, in meters
// @param waves 1 0 2 How tall the waves are: 0 is still water
// @param sun 65 30 90 The sun's height over the horizon, in degrees
// @param tile 0.25 0.1 0.5 How wide a tile is, in meters
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
constant float LOOP = 60.0;
// The eye is 2.2 m over the water, looking down 58 degrees.
constant float3 EYE = float3(0.0, 2.2, -0.4);
constant float PITCH = 1.01;
constant float FOCAL = 1.7;
// Light bends by this ratio going from air into water.
constant float WATER_N = 1.333;
// Sunlight and the sky's light as they reach the water.
constant float3 SUNLIGHT = float3(1.0, 0.92, 0.8);
constant float3 SKYLIGHT = float3(0.1, 0.16, 0.26);
// Grout, a warm cream, a tenth of each tile wide.
constant float3 GROUT = float3(0.86, 0.82, 0.74);
constant float GROUT_W = 0.05;
float hash21(float2 p) {
p = fract(p * float2(123.34, 456.21));
p += dot(p, p + 45.32);
return fract(p.x * p.y);
}
float loopTime(float t) {
return t - LOOP * floor(t / LOOP);
}
// Toward the sun in the sky, from its height in degrees.
float3 sunInAir(constant Params& p) {
float e = p.sun * TAU / 360.0;
return float3(0.35 * cos(e), sin(e), 0.94 * cos(e));
}
// The floor: pool-blue tiles, each a little different, in cream grout.
float3 tileColor(float2 x, float foot, constant Params& p) {
float2 g = x / p.tile;
float2 id = floor(g);
float2 f = abs(fract(g) - 0.5);
// How far this point is from the middle of the nearest grout line.
float edge = 0.5 - max(f.x, f.y);
float aa = foot / p.tile;
float grout = 1.0 - smoothstep(GROUT_W - aa, GROUT_W + aa, edge);
// Where a pixel spans a tile, its average color instead.
float far = smoothstep(0.15, 0.5, aa);
grout = mix(grout, 0.19, far);
float v = mix(hash21(id), 0.5, far);
float3 blue = float3(0.2, 0.5, 0.75) * (0.88 + 0.24 * v);
return mix(blue, GROUT, grout);
}
// Wave i: heading, wavelength and amplitude in m, turns a minute.
float4 poolWave(int i) {
if (i == 0) return float4(0.3, 1.9, 0.024, 18.0);
if (i == 1) return float4(2.0, 1.3, 0.016, 22.0);
if (i == 2) return float4(-1.1, 0.83, 0.01, 27.0);
if (i == 3) return float4(1.0, 0.57, 0.0064, 33.0);
if (i == 4) return float4(-2.6, 0.41, 0.0044, 39.0);
return float4(2.7, 0.3, 0.0028, 45.0);
}
// The water's height at q and its slope: a sum of six waves.
float3 surfaceAt(float2 q, float t, constant Params& p) {
float a = TAU * t / LOOP;
float3 sum = float3(0.0);
for (int i = 0; i < 6; i++) {
float4 w = poolWave(i);
float2 dir = float2(cos(w.x), sin(w.x));
float k = TAU / w.y;
float ph = k * dot(dir, q) - w.w * a + 1.7 * float(i);
// The slope of a sine is a cosine.
sum += w.z * float3(sin(ph), k * cos(ph) * dir);
}
return sum * p.waves;
}
// Where light through the surface at q lands.
float2 landAt(float2 q, float t, constant Params& p) {
float3 w = surfaceAt(q, t, p);
float3 n = normalize(float3(-w.y, 1.0, -w.z));
float3 ray = refract(-sunInAir(p), n, 1.0 / WATER_N);
return q + ray.xz * p.depth / -ray.y;
}
// How bright the sunlight is at floor point x.
float causticAt(float2 x, float t, constant Params& p) {
// Where x's light came in: a guess, corrected once.
float3 down = refract(-sunInAir(p), float3(0, 1, 0), 1.0 / WATER_N);
float2 q = x - down.xz * p.depth / -down.y;
q += x - landAt(q, t, p);
// Three rays 4 mm apart: how much their patch grew.
float e = 0.004;
float2 f0 = landAt(q, t, p);
float2 fx = landAt(q + float2(e, 0.0), t, p) - f0;
float2 fz = landAt(q + float2(0.0, e), t, p) - f0;
// The Jacobian determinant, softened near 0.
float det = (fx.x * fz.y - fx.y * fz.x) / (e * e);
return 1.05 / (abs(det) + 0.05);
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float2 s = (uv - 0.5) * u.resolution / u.resolution.y * 2.0;
float t = loopTime(u.time);
float3 fwd = float3(0.0, -sin(PITCH), cos(PITCH));
float3 up = float3(0.0, cos(PITCH), sin(PITCH));
float3 rd = normalize(fwd * FOCAL + float3(s.x, 0.0, 0.0) + up * s.y);
// The ray meets the water at height 0, where it is bent.
float air = EYE.y / -rd.y;
float2 q = EYE.xz + rd.xz * air;
float3 w = surfaceAt(q, t, p);
float3 n = normalize(float3(-w.y, 1.0, -w.z));
float3 down = refract(rd, n, 1.0 / WATER_N);
float swim = p.depth / -down.y;
float2 x = q + down.xz * swim;
// How wide a pixel is on the floor, to soften what is finer.
float px = 2.0 / (u.resolution.y * FOCAL);
float foot = 1.5 * (air + swim) * px / max(-down.y, 0.3);
float3 tile = tileColor(x, foot, p);
// Sunlight, gathered where the waves focus it.
float3 L = sunInAir(p);
float focus = mix(1.0, causticAt(x, t, p), exp(-foot * 20.0));
float3 light = tile * (SUNLIGHT * L.y * focus + SKYLIGHT);
float3 col = 1.0 - exp(-light * 1.2);
col = pow(col, float3(1.0 / 2.2));
// A speck of noise keeps the blues from banding.
float2 at = floor(uv * u.resolution);
float jitter = fract(52.98 * fract(dot(at, float2(0.0671, 0.00584))));
col += (jitter - 0.5) / 255.0;
return float4(col, 1.0);
}
// ---- The sound ----
// Noise with nothing above `rate` Hz, as in Underwater.
float murmur(float t, float rate, float seed) {
float x = t * rate;
float i = floor(x), f = fract(x);
f = f * f * (3.0 - 2.0 * f);
float h0 = hash21(float2(i, seed)), h1 = hash21(float2(i + 1.0, seed));
return mix(h0, h1, f) * 2.0 - 1.0;
}
// Water slapping a wall at q: noise, loud as the water rises fast,
// over a low rumble that swells as it stands high.
float lapAt(float2 q, float t, float seed, constant Params& p) {
float h0 = surfaceAt(q, t, p).x;
float rise = (surfaceAt(q, t + 0.02, p).x - h0) / 0.02;
float slap = min(max(rise, 0.0) / 0.08, 1.5);
float splash = murmur(t, 1500.0, seed) - 0.6 * murmur(t, 400.0, seed);
float body = murmur(t, 140.0, seed + 5.0) * max(0.3 + h0 * 8.0, 0.0);
return splash * slap * slap + 0.5 * body * p.waves;
}
float2 sound(float t, constant Params& p) {
float tl = loopTime(t);
// Each ear hears the wall on its side.
float2 v = float2(lapAt(float2(-2.4, 1.6), tl, 1.0, p),
lapAt(float2(2.4, 1.6), tl, 2.0, p));
v *= 0.5;
// Silence where the minute comes round, so it joins.
float turn = min(tl, LOOP - tl);
float fade = 1.0 - exp(-turn * 30.0);
return tanh(v) * p.volume * fade;
}1 line from stage 2 that are gone
float3 light = tile * (SUNLIGHT * L.y + SKYLIGHT);Stage 4 · Light lost in the water
Water absorbs, as in Underwater, with the same ABSORB: each meter keeps exp(−c) of each color, with c 0.4 for red, 0.06 for green and 0.012 for blue. Here there is no scattering, only the light that is taken.
Sunlight loses some on its slanted way down: bent to 18.5° from straight down at the default 65°, it crosses depth / −sunDown.y, 2.32 m of water, which keeps 40% of its red, 87% of its green and 97% of its blue. The skylight comes in from all over and is taken to cross 1.2 times the depth. Then the floor's light loses more on its way back up to the eye, through swim meters of water: in the middle of the picture the bent ray is 23.5° from straight down, 2.4 m. All told, under still water, sunlight that reaches the eye from the middle of the floor keeps 15% of its red, 75% of its green and 94% of its blue, so deeper water is bluer and darker.
stage-4-light-lost-in-the-water.metal6 new lines since stage 3
// Stage 4 · Light lost in the water
// Water absorbs: each meter keeps exp(-c) of a color, with c 0.4 for
// red, 0.06 for green and 0.012 for blue. Sunlight loses some on its
// slanted way down, and the floor's light more on its way back up, so
// deeper water is bluer and darker.
// @param depth 2.2 0.3 4 How deep the water is over the tiles, in meters
// @param waves 1 0 2 How tall the waves are: 0 is still water
// @param sun 65 30 90 The sun's height over the horizon, in degrees
// @param tile 0.25 0.1 0.5 How wide a tile is, in meters
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
constant float LOOP = 60.0;
// The eye is 2.2 m over the water, looking down 58 degrees.
constant float3 EYE = float3(0.0, 2.2, -0.4);
constant float PITCH = 1.01;
constant float FOCAL = 1.7;
// Light bends by this ratio going from air into water.
constant float WATER_N = 1.333;
// What a meter of clear water absorbs of red, green, blue.
constant float3 ABSORB = float3(0.4, 0.06, 0.012);
// Sunlight and the sky's light as they reach the water.
constant float3 SUNLIGHT = float3(1.0, 0.92, 0.8);
constant float3 SKYLIGHT = float3(0.1, 0.16, 0.26);
// Grout, a warm cream, a tenth of each tile wide.
constant float3 GROUT = float3(0.86, 0.82, 0.74);
constant float GROUT_W = 0.05;
float hash21(float2 p) {
p = fract(p * float2(123.34, 456.21));
p += dot(p, p + 45.32);
return fract(p.x * p.y);
}
float loopTime(float t) {
return t - LOOP * floor(t / LOOP);
}
// Toward the sun in the sky, from its height in degrees.
float3 sunInAir(constant Params& p) {
float e = p.sun * TAU / 360.0;
return float3(0.35 * cos(e), sin(e), 0.94 * cos(e));
}
// The floor: pool-blue tiles, each a little different, in cream grout.
float3 tileColor(float2 x, float foot, constant Params& p) {
float2 g = x / p.tile;
float2 id = floor(g);
float2 f = abs(fract(g) - 0.5);
// How far this point is from the middle of the nearest grout line.
float edge = 0.5 - max(f.x, f.y);
float aa = foot / p.tile;
float grout = 1.0 - smoothstep(GROUT_W - aa, GROUT_W + aa, edge);
// Where a pixel spans a tile, its average color instead.
float far = smoothstep(0.15, 0.5, aa);
grout = mix(grout, 0.19, far);
float v = mix(hash21(id), 0.5, far);
float3 blue = float3(0.2, 0.5, 0.75) * (0.88 + 0.24 * v);
return mix(blue, GROUT, grout);
}
// Wave i: heading, wavelength and amplitude in m, turns a minute.
float4 poolWave(int i) {
if (i == 0) return float4(0.3, 1.9, 0.024, 18.0);
if (i == 1) return float4(2.0, 1.3, 0.016, 22.0);
if (i == 2) return float4(-1.1, 0.83, 0.01, 27.0);
if (i == 3) return float4(1.0, 0.57, 0.0064, 33.0);
if (i == 4) return float4(-2.6, 0.41, 0.0044, 39.0);
return float4(2.7, 0.3, 0.0028, 45.0);
}
// The water's height at q and its slope: a sum of six waves.
float3 surfaceAt(float2 q, float t, constant Params& p) {
float a = TAU * t / LOOP;
float3 sum = float3(0.0);
for (int i = 0; i < 6; i++) {
float4 w = poolWave(i);
float2 dir = float2(cos(w.x), sin(w.x));
float k = TAU / w.y;
float ph = k * dot(dir, q) - w.w * a + 1.7 * float(i);
// The slope of a sine is a cosine.
sum += w.z * float3(sin(ph), k * cos(ph) * dir);
}
return sum * p.waves;
}
// Where light through the surface at q lands.
float2 landAt(float2 q, float t, constant Params& p) {
float3 w = surfaceAt(q, t, p);
float3 n = normalize(float3(-w.y, 1.0, -w.z));
float3 ray = refract(-sunInAir(p), n, 1.0 / WATER_N);
return q + ray.xz * p.depth / -ray.y;
}
// How bright the sunlight is at floor point x.
float causticAt(float2 x, float t, constant Params& p) {
// Where x's light came in: a guess, corrected once.
float3 down = refract(-sunInAir(p), float3(0, 1, 0), 1.0 / WATER_N);
float2 q = x - down.xz * p.depth / -down.y;
q += x - landAt(q, t, p);
// Three rays 4 mm apart: how much their patch grew.
float e = 0.004;
float2 f0 = landAt(q, t, p);
float2 fx = landAt(q + float2(e, 0.0), t, p) - f0;
float2 fz = landAt(q + float2(0.0, e), t, p) - f0;
// The Jacobian determinant, softened near 0.
float det = (fx.x * fz.y - fx.y * fz.x) / (e * e);
return 1.05 / (abs(det) + 0.05);
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float2 s = (uv - 0.5) * u.resolution / u.resolution.y * 2.0;
float t = loopTime(u.time);
float3 fwd = float3(0.0, -sin(PITCH), cos(PITCH));
float3 up = float3(0.0, cos(PITCH), sin(PITCH));
float3 rd = normalize(fwd * FOCAL + float3(s.x, 0.0, 0.0) + up * s.y);
// The ray meets the water at height 0, where it is bent.
float air = EYE.y / -rd.y;
float2 q = EYE.xz + rd.xz * air;
float3 w = surfaceAt(q, t, p);
float3 n = normalize(float3(-w.y, 1.0, -w.z));
float3 down = refract(rd, n, 1.0 / WATER_N);
float swim = p.depth / -down.y;
float2 x = q + down.xz * swim;
// How wide a pixel is on the floor, to soften what is finer.
float px = 2.0 / (u.resolution.y * FOCAL);
float foot = 1.5 * (air + swim) * px / max(-down.y, 0.3);
float3 tile = tileColor(x, foot, p);
// Sunlight on the floor: what got in, gathered, after its path.
float3 L = sunInAir(p);
float3 sunDown = refract(-L, float3(0, 1, 0), 1.0 / WATER_N);
float focus = mix(1.0, causticAt(x, t, p), exp(-foot * 20.0));
float3 sun = SUNLIGHT * L.y * focus;
sun *= exp(-ABSORB * p.depth / -sunDown.y);
float3 sky = SKYLIGHT * exp(-ABSORB * p.depth * 1.2);
// Back up to the eye, through swim meters of water.
float3 light = tile * (sun + sky) * exp(-ABSORB * swim);
float3 col = 1.0 - exp(-light * 1.2);
col = pow(col, float3(1.0 / 2.2));
// A speck of noise keeps the blues from banding.
float2 at = floor(uv * u.resolution);
float jitter = fract(52.98 * fract(dot(at, float2(0.0671, 0.00584))));
col += (jitter - 0.5) / 255.0;
return float4(col, 1.0);
}
// ---- The sound ----
// Noise with nothing above `rate` Hz, as in Underwater.
float murmur(float t, float rate, float seed) {
float x = t * rate;
float i = floor(x), f = fract(x);
f = f * f * (3.0 - 2.0 * f);
float h0 = hash21(float2(i, seed)), h1 = hash21(float2(i + 1.0, seed));
return mix(h0, h1, f) * 2.0 - 1.0;
}
// Water slapping a wall at q: noise, loud as the water rises fast,
// over a low rumble that swells as it stands high.
float lapAt(float2 q, float t, float seed, constant Params& p) {
float h0 = surfaceAt(q, t, p).x;
float rise = (surfaceAt(q, t + 0.02, p).x - h0) / 0.02;
float slap = min(max(rise, 0.0) / 0.08, 1.5);
float splash = murmur(t, 1500.0, seed) - 0.6 * murmur(t, 400.0, seed);
float body = murmur(t, 140.0, seed + 5.0) * max(0.3 + h0 * 8.0, 0.0);
return splash * slap * slap + 0.5 * body * p.waves;
}
float2 sound(float t, constant Params& p) {
float tl = loopTime(t);
// Each ear hears the wall on its side.
float2 v = float2(lapAt(float2(-2.4, 1.6), tl, 1.0, p),
lapAt(float2(2.4, 1.6), tl, 2.0, p));
v *= 0.5;
// Silence where the minute comes round, so it joins.
float turn = min(tl, LOOP - tl);
float fade = 1.0 - exp(-turn * 30.0);
return tanh(v) * p.volume * fade;
}1 line from stage 3 that are gone
float3 light = tile * (SUNLIGHT * L.y * focus + SKYLIGHT);Stage 5 · The surface mirrors the sky
Water mirrors a share of the light that meets it, and the share grows with the angle. mirror is Schlick's Fresnel, as on Rippling Water: 0.02 + 0.98 · (1 − facing)⁵, where facing is how squarely the eye looks at the surface, dot(−rd, n). Seen straight down that is 2%; in the middle of the picture, 32° from straight down, still about 2%; at the top of the picture about 6.5%, and toward the edge-on it climbs to all of it. So the far water shows more sky.
The ray's mirror image, reflect(rd, n), looks up into poolSky: pale near the horizon, bluer overhead, and the sun in it, a sharp warm-white disc, kiln(0.95), and a softer halo, kiln(0.8), both from the kiln palette. Where a wave tilts the surface just so that the sun's mirror image reaches the eye, it glints. The sunlight the water mirrors back up never gets in, so the floor's sunlight is scaled by 1 − mirror(L.y), 98% with the sun high.
stage-5-the-surface-mirrors-the-sky.metal27 new lines since stage 4
// Stage 5 · The surface mirrors the sky
// Water mirrors 2% of the light seen straight down, rising toward all
// of it edge-on (Schlick's Fresnel), so the far water shows more sky.
// Where a wave turns the sun to the eye it glints warm white, and the
// sunlight it mirrors never reaches the floor.
// @param depth 2.2 0.3 4 How deep the water is over the tiles, in meters
// @param waves 1 0 2 How tall the waves are: 0 is still water
// @param sun 65 30 90 The sun's height over the horizon, in degrees
// @param tile 0.25 0.1 0.5 How wide a tile is, in meters
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
constant float LOOP = 60.0;
// The eye is 2.2 m over the water, looking down 58 degrees.
constant float3 EYE = float3(0.0, 2.2, -0.4);
constant float PITCH = 1.01;
constant float FOCAL = 1.7;
// Light bends by this ratio going from air into water.
constant float WATER_N = 1.333;
// What a meter of clear water absorbs of red, green, blue.
constant float3 ABSORB = float3(0.4, 0.06, 0.012);
// Sunlight and the sky's light as they reach the water.
constant float3 SUNLIGHT = float3(1.0, 0.92, 0.8);
constant float3 SKYLIGHT = float3(0.1, 0.16, 0.26);
// Grout, a warm cream, a tenth of each tile wide.
constant float3 GROUT = float3(0.86, 0.82, 0.74);
constant float GROUT_W = 0.05;
float hash21(float2 p) {
p = fract(p * float2(123.34, 456.21));
p += dot(p, p + 45.32);
return fract(p.x * p.y);
}
// The kiln palette from Flowing Color Field.
float3 kiln(float t) {
float3 c0 = float3(0.020, 0.012, 0.020);
float3 c1 = float3(0.280, 0.040, 0.030);
float3 c2 = float3(0.880, 0.260, 0.050);
float3 c3 = float3(1.000, 0.680, 0.200);
float3 c4 = float3(1.000, 0.970, 0.840);
t = clamp(t, 0.0, 1.0);
float3 c = mix(c0, c1, smoothstep(0.00, 0.22, t));
c = mix(c, c2, smoothstep(0.22, 0.50, t));
c = mix(c, c3, smoothstep(0.50, 0.76, t));
return mix(c, c4, smoothstep(0.76, 1.00, t));
}
float loopTime(float t) {
return t - LOOP * floor(t / LOOP);
}
// Toward the sun in the sky, from its height in degrees.
float3 sunInAir(constant Params& p) {
float e = p.sun * TAU / 360.0;
return float3(0.35 * cos(e), sin(e), 0.94 * cos(e));
}
// The floor: pool-blue tiles, each a little different, in cream grout.
float3 tileColor(float2 x, float foot, constant Params& p) {
float2 g = x / p.tile;
float2 id = floor(g);
float2 f = abs(fract(g) - 0.5);
// How far this point is from the middle of the nearest grout line.
float edge = 0.5 - max(f.x, f.y);
float aa = foot / p.tile;
float grout = 1.0 - smoothstep(GROUT_W - aa, GROUT_W + aa, edge);
// Where a pixel spans a tile, its average color instead.
float far = smoothstep(0.15, 0.5, aa);
grout = mix(grout, 0.19, far);
float v = mix(hash21(id), 0.5, far);
float3 blue = float3(0.2, 0.5, 0.75) * (0.88 + 0.24 * v);
return mix(blue, GROUT, grout);
}
// Wave i: heading, wavelength and amplitude in m, turns a minute.
float4 poolWave(int i) {
if (i == 0) return float4(0.3, 1.9, 0.024, 18.0);
if (i == 1) return float4(2.0, 1.3, 0.016, 22.0);
if (i == 2) return float4(-1.1, 0.83, 0.01, 27.0);
if (i == 3) return float4(1.0, 0.57, 0.0064, 33.0);
if (i == 4) return float4(-2.6, 0.41, 0.0044, 39.0);
return float4(2.7, 0.3, 0.0028, 45.0);
}
// The water's height at q and its slope: a sum of six waves.
float3 surfaceAt(float2 q, float t, constant Params& p) {
float a = TAU * t / LOOP;
float3 sum = float3(0.0);
for (int i = 0; i < 6; i++) {
float4 w = poolWave(i);
float2 dir = float2(cos(w.x), sin(w.x));
float k = TAU / w.y;
float ph = k * dot(dir, q) - w.w * a + 1.7 * float(i);
// The slope of a sine is a cosine.
sum += w.z * float3(sin(ph), k * cos(ph) * dir);
}
return sum * p.waves;
}
// Where light through the surface at q lands.
float2 landAt(float2 q, float t, constant Params& p) {
float3 w = surfaceAt(q, t, p);
float3 n = normalize(float3(-w.y, 1.0, -w.z));
float3 ray = refract(-sunInAir(p), n, 1.0 / WATER_N);
return q + ray.xz * p.depth / -ray.y;
}
// How bright the sunlight is at floor point x.
float causticAt(float2 x, float t, constant Params& p) {
// Where x's light came in: a guess, corrected once.
float3 down = refract(-sunInAir(p), float3(0, 1, 0), 1.0 / WATER_N);
float2 q = x - down.xz * p.depth / -down.y;
q += x - landAt(q, t, p);
// Three rays 4 mm apart: how much their patch grew.
float e = 0.004;
float2 f0 = landAt(q, t, p);
float2 fx = landAt(q + float2(e, 0.0), t, p) - f0;
float2 fz = landAt(q + float2(0.0, e), t, p) - f0;
// The Jacobian determinant, softened near 0.
float det = (fx.x * fz.y - fx.y * fz.x) / (e * e);
return 1.05 / (abs(det) + 0.05);
}
// How much water mirrors (Schlick's Fresnel): little seen from above,
// nearly all edge-on.
float mirror(float facing) {
return 0.02 + 0.98 * pow(1.0 - clamp(facing, 0.0, 1.0), 5.0);
}
// The sky the water mirrors, and the sun in it.
float3 poolSky(float3 r, constant Params& p) {
float up = sqrt(max(r.y, 0.0));
float3 col = mix(float3(0.9, 0.95, 1.0), float3(0.25, 0.45, 0.9), up);
float sun = dot(r, sunInAir(p));
col += kiln(0.95) * 8.0 * pow(max(sun, 0.0), 1500.0);
col += kiln(0.8) * 0.6 * pow(max(sun, 0.0), 200.0);
return col;
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float2 s = (uv - 0.5) * u.resolution / u.resolution.y * 2.0;
float t = loopTime(u.time);
float3 fwd = float3(0.0, -sin(PITCH), cos(PITCH));
float3 up = float3(0.0, cos(PITCH), sin(PITCH));
float3 rd = normalize(fwd * FOCAL + float3(s.x, 0.0, 0.0) + up * s.y);
// The ray meets the water at height 0, where it is bent.
float air = EYE.y / -rd.y;
float2 q = EYE.xz + rd.xz * air;
float3 w = surfaceAt(q, t, p);
float3 n = normalize(float3(-w.y, 1.0, -w.z));
float3 down = refract(rd, n, 1.0 / WATER_N);
float swim = p.depth / -down.y;
float2 x = q + down.xz * swim;
// How wide a pixel is on the floor, to soften what is finer.
float px = 2.0 / (u.resolution.y * FOCAL);
float foot = 1.5 * (air + swim) * px / max(-down.y, 0.3);
float3 tile = tileColor(x, foot, p);
// Sunlight on the floor: what got in, gathered, after its path.
float3 L = sunInAir(p);
float3 sunDown = refract(-L, float3(0, 1, 0), 1.0 / WATER_N);
float focus = mix(1.0, causticAt(x, t, p), exp(-foot * 20.0));
float3 sun = SUNLIGHT * L.y * (1.0 - mirror(L.y)) * focus;
sun *= exp(-ABSORB * p.depth / -sunDown.y);
float3 sky = SKYLIGHT * exp(-ABSORB * p.depth * 1.2);
// Back up to the eye, through swim meters of water.
float3 below = tile * (sun + sky) * exp(-ABSORB * swim);
// The surface mirrors the sky, more toward the far side.
float3 r = reflect(rd, n);
float3 light = mix(below, poolSky(r, p), mirror(dot(-rd, n)));
float3 col = 1.0 - exp(-light * 1.2);
col = pow(col, float3(1.0 / 2.2));
// A speck of noise keeps the blues from banding.
float2 at = floor(uv * u.resolution);
float jitter = fract(52.98 * fract(dot(at, float2(0.0671, 0.00584))));
col += (jitter - 0.5) / 255.0;
return float4(col, 1.0);
}
// ---- The sound ----
// Noise with nothing above `rate` Hz, as in Underwater.
float murmur(float t, float rate, float seed) {
float x = t * rate;
float i = floor(x), f = fract(x);
f = f * f * (3.0 - 2.0 * f);
float h0 = hash21(float2(i, seed)), h1 = hash21(float2(i + 1.0, seed));
return mix(h0, h1, f) * 2.0 - 1.0;
}
// Water slapping a wall at q: noise, loud as the water rises fast,
// over a low rumble that swells as it stands high.
float lapAt(float2 q, float t, float seed, constant Params& p) {
float h0 = surfaceAt(q, t, p).x;
float rise = (surfaceAt(q, t + 0.02, p).x - h0) / 0.02;
float slap = min(max(rise, 0.0) / 0.08, 1.5);
float splash = murmur(t, 1500.0, seed) - 0.6 * murmur(t, 400.0, seed);
float body = murmur(t, 140.0, seed + 5.0) * max(0.3 + h0 * 8.0, 0.0);
return splash * slap * slap + 0.5 * body * p.waves;
}
float2 sound(float t, constant Params& p) {
float tl = loopTime(t);
// Each ear hears the wall on its side.
float2 v = float2(lapAt(float2(-2.4, 1.6), tl, 1.0, p),
lapAt(float2(2.4, 1.6), tl, 2.0, p));
v *= 0.5;
// Silence where the minute comes round, so it joins.
float turn = min(tl, LOOP - tl);
float fade = 1.0 - exp(-turn * 30.0);
return tanh(v) * p.volume * fade;
}2 lines from stage 4 that are gone
float3 sun = SUNLIGHT * L.y * focus;
⋯
float3 light = tile * (sun + sky) * exp(-ABSORB * swim);Stage 6 · Swimming Pool
The finished shader adds a lane line and drops.
- The lane line. One column of dark navy tiles, the third tile column right of the eye's line, from the same grid as the rest, softened with the same
aa, running away from the eye up the picture. Bent by the same surface, it shows the waves and rings plainly. - Drops.
dropsdrops a minute fall in (rounded; 4 at the default).dropOf(k, n)says where dropklands, across a patch 2.6 m wide and 1 m to 2.8 m ahead, and when, up to 40% of the way into its share of the minute. Its randomness is hashed fromkwrapped to the count (wrap, from Kiln Floor Tiles), so the same drops land in the same places each minute. - Rings.
ringAtadds the rings the last two drops sent out: a sine 12 cm from crest to crest, spreading 30 cm a second, inside a bump that reaches about 10 cm to either side of the ring and runs out with it. A ring starts 4 mm high and dies away with a time constant of 1.5 s, and lower as it spreads, oversqrt(1 + 3r). Its slope is worked out exactly, the derivative of all of that, and added to the waves', sosurfaceAtgives the rings to everything that reads the surface: the view bends round them and the light on the floor under them gathers into rings of its own.wavesscales only the six waves, not the rings.
the kiln palette and shade(), from the finished shader
// The kiln palette from Flowing Color Field.
float3 kiln(float t) {
float3 c0 = float3(0.020, 0.012, 0.020);
float3 c1 = float3(0.280, 0.040, 0.030);
float3 c2 = float3(0.880, 0.260, 0.050);
float3 c3 = float3(1.000, 0.680, 0.200);
float3 c4 = float3(1.000, 0.970, 0.840);
t = clamp(t, 0.0, 1.0);
float3 c = mix(c0, c1, smoothstep(0.00, 0.22, t));
c = mix(c, c2, smoothstep(0.22, 0.50, t));
c = mix(c, c3, smoothstep(0.50, 0.76, t));
return mix(c, c4, smoothstep(0.76, 1.00, t));
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float2 s = (uv - 0.5) * u.resolution / u.resolution.y * 2.0;
float t = loopTime(u.time);
float3 fwd = float3(0.0, -sin(PITCH), cos(PITCH));
float3 up = float3(0.0, cos(PITCH), sin(PITCH));
float3 rd = normalize(fwd * FOCAL + float3(s.x, 0.0, 0.0) + up * s.y);
// The ray meets the water at height 0, where it is bent.
float air = EYE.y / -rd.y;
float2 q = EYE.xz + rd.xz * air;
float3 w = surfaceAt(q, t, p);
float3 n = normalize(float3(-w.y, 1.0, -w.z));
float3 down = refract(rd, n, 1.0 / WATER_N);
float swim = p.depth / -down.y;
float2 x = q + down.xz * swim;
// How wide a pixel is on the floor, to soften what is finer.
float px = 2.0 / (u.resolution.y * FOCAL);
float foot = 1.5 * (air + swim) * px / max(-down.y, 0.3);
float3 tile = tileColor(x, foot, p);
// Sunlight on the floor: what got in, gathered, after its path.
float3 L = sunInAir(p);
float3 sunDown = refract(-L, float3(0, 1, 0), 1.0 / WATER_N);
float focus = mix(1.0, causticAt(x, t, p), exp(-foot * 20.0));
float3 sun = SUNLIGHT * L.y * (1.0 - mirror(L.y)) * focus;
sun *= exp(-ABSORB * p.depth / -sunDown.y);
float3 sky = SKYLIGHT * exp(-ABSORB * p.depth * 1.2);
// Back up to the eye, through swim meters of water.
float3 below = tile * (sun + sky) * exp(-ABSORB * swim);
// The surface mirrors the sky, more toward the far side.
float3 r = reflect(rd, n);
float3 light = mix(below, poolSky(r, p), mirror(dot(-rd, n)));
float3 col = 1.0 - exp(-light * 1.2);
col = pow(col, float3(1.0 / 2.2));
// A speck of noise keeps the blues from banding.
float2 at = floor(uv * u.resolution);
float jitter = fract(52.98 * fract(dot(at, float2(0.0671, 0.00584))));
col += (jitter - 0.5) / 255.0;
return float4(col, 1.0);
}swimming-pool.metal, the whole file51 new lines since stage 5
// Ray Kiln · Swimming Pool
//
// @episode Swimming Pool
// @length 60
// @variation 30
// @variations 1,2,3,4,5,6,7,10
// @sweeps depth, waves, sun
// @still 22
// @teaches Caustics: light bent by a moving surface, gathered on the floor
// @category Fragment
// @tags swimming pool, caustics, refraction, Snell's law, heightfield, normals, jacobian, absorption, fresnel, ripples, procedural sound
// @final Swimming Pool | Drops ring the water and bend the tiles and the light under them, a lane line runs along the floor, and each drop plinks as it lands.
//
// Looking down into a tiled pool on a sunny day. The water's surface is a
// sum of six waves. Each view ray is bent where it enters the water, by
// Snell's law, so the tiles wobble. Sunlight is bent by the same surface:
// where the waves act as lenses they squeeze a patch of light into a
// smaller patch of floor, which is brighter by just that much. The ratio
// of the two areas is the Jacobian determinant of the map from a point on
// the surface to where its light lands, and it makes the bright lines.
// Every wave goes round a whole number of times a minute, so it loops.
// Each @param line below becomes a slider in the host app and a row in the page's parameters table:
// // @param name default min max description
// @param depth 2.2 0.3 4 How deep the water is over the tiles, in meters
// @param waves 1 0 2 How tall the waves are: 0 is still water
// @param sun 65 30 90 The sun's height over the horizon, in degrees
// @param tile 0.25 0.1 0.5 How wide a tile is, in meters
// @param drops 4 0 12 How many drops a minute fall in and ring the water
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
constant float LOOP = 60.0;
// The eye is 2.2 m over the water, looking down 58 degrees.
constant float3 EYE = float3(0.0, 2.2, -0.4);
constant float PITCH = 1.01;
constant float FOCAL = 1.7;
// Light bends by this ratio going from air into water.
constant float WATER_N = 1.333;
// What a meter of clear water absorbs of red, green, blue.
constant float3 ABSORB = float3(0.4, 0.06, 0.012);
// Sunlight and the sky's light as they reach the water.
constant float3 SUNLIGHT = float3(1.0, 0.92, 0.8);
constant float3 SKYLIGHT = float3(0.1, 0.16, 0.26);
// Grout, a warm cream, a tenth of each tile wide.
constant float3 GROUT = float3(0.86, 0.82, 0.74);
constant float GROUT_W = 0.05;
float hash21(float2 p) {
p = fract(p * float2(123.34, 456.21));
p += dot(p, p + 45.32);
return fract(p.x * p.y);
}
// The kiln palette from Flowing Color Field.
float3 kiln(float t) {
float3 c0 = float3(0.020, 0.012, 0.020);
float3 c1 = float3(0.280, 0.040, 0.030);
float3 c2 = float3(0.880, 0.260, 0.050);
float3 c3 = float3(1.000, 0.680, 0.200);
float3 c4 = float3(1.000, 0.970, 0.840);
t = clamp(t, 0.0, 1.0);
float3 c = mix(c0, c1, smoothstep(0.00, 0.22, t));
c = mix(c, c2, smoothstep(0.22, 0.50, t));
c = mix(c, c3, smoothstep(0.50, 0.76, t));
return mix(c, c4, smoothstep(0.76, 1.00, t));
}
float loopTime(float t) {
return t - LOOP * floor(t / LOOP);
}
// Wraps x into 0 to m, so a count repeats each loop.
float wrap(float x, float m) {
return x - m * floor(x / m);
}
// Toward the sun in the sky, from its height in degrees.
float3 sunInAir(constant Params& p) {
float e = p.sun * TAU / 360.0;
return float3(0.35 * cos(e), sin(e), 0.94 * cos(e));
}
// The floor: pool-blue tiles, each a little different, in cream grout.
float3 tileColor(float2 x, float foot, constant Params& p) {
float2 g = x / p.tile;
float2 id = floor(g);
float2 f = abs(fract(g) - 0.5);
// How far this point is from the middle of the nearest grout line.
float edge = 0.5 - max(f.x, f.y);
float aa = foot / p.tile;
float grout = 1.0 - smoothstep(GROUT_W - aa, GROUT_W + aa, edge);
// Where a pixel spans a tile, its average color instead.
float far = smoothstep(0.15, 0.5, aa);
grout = mix(grout, 0.19, far);
float v = mix(hash21(id), 0.5, far);
float3 blue = float3(0.2, 0.5, 0.75) * (0.88 + 0.24 * v);
// The lane line: one column of dark tiles.
float lane = 1.0 - smoothstep(0.5 - aa, 0.5 + aa, abs(g.x - 2.5));
blue = mix(blue, float3(0.04, 0.07, 0.2), lane);
return mix(blue, GROUT, grout);
}
// Wave i: heading, wavelength and amplitude in m, turns a minute.
float4 poolWave(int i) {
if (i == 0) return float4(0.3, 1.9, 0.024, 18.0);
if (i == 1) return float4(2.0, 1.3, 0.016, 22.0);
if (i == 2) return float4(-1.1, 0.83, 0.01, 27.0);
if (i == 3) return float4(1.0, 0.57, 0.0064, 33.0);
if (i == 4) return float4(-2.6, 0.41, 0.0044, 39.0);
return float4(2.7, 0.3, 0.0028, 45.0);
}
// Drop k of n a minute: where it lands, and when.
float3 dropOf(float k, float n) {
float kk = wrap(k, n);
float2 at = float2(-1.3 + 2.6 * hash21(float2(kk, 1.3)),
1.0 + 1.8 * hash21(float2(kk, 7.9)));
float when = (k + 0.4 * hash21(float2(kk, 4.1))) * LOOP / n;
return float3(at, when);
}
// The rings the last two drops sent out: height and slope.
float3 ringAt(float2 q, float t, constant Params& p) {
float3 sum = float3(0.0);
float n = floor(p.drops + 0.5);
if (n < 1.0) return sum;
float k0 = floor(t * n / LOOP);
for (int j = 0; j < 2; j++) {
float3 d = dropOf(k0 - float(j), n);
float age = wrap(t - d.z, LOOP);
float2 v = q - d.xy;
float r = max(length(v), 0.0001);
// A ring 12 cm from crest to crest, spreading 30 cm a second.
float x = r - 0.3 * age;
float k = TAU / 0.12;
float env = 0.004 * exp(-x * x / 0.01 - age / 1.5);
env /= sqrt(1.0 + 3.0 * r);
float h = env * sin(k * x);
float dh = env * k * cos(k * x) - h * 2.0 * x / 0.01;
dh -= h * 1.5 / (1.0 + 3.0 * r);
sum += float3(h, dh * v / r);
}
return sum;
}
// The water's height at q and its slope: a sum of six waves,
// and the rings from the drops.
float3 surfaceAt(float2 q, float t, constant Params& p) {
float a = TAU * t / LOOP;
float3 sum = float3(0.0);
for (int i = 0; i < 6; i++) {
float4 w = poolWave(i);
float2 dir = float2(cos(w.x), sin(w.x));
float k = TAU / w.y;
float ph = k * dot(dir, q) - w.w * a + 1.7 * float(i);
// The slope of a sine is a cosine.
sum += w.z * float3(sin(ph), k * cos(ph) * dir);
}
sum *= p.waves;
return sum + ringAt(q, t, p);
}
// Where light through the surface at q lands.
float2 landAt(float2 q, float t, constant Params& p) {
float3 w = surfaceAt(q, t, p);
float3 n = normalize(float3(-w.y, 1.0, -w.z));
float3 ray = refract(-sunInAir(p), n, 1.0 / WATER_N);
return q + ray.xz * p.depth / -ray.y;
}
// How bright the sunlight is at floor point x.
float causticAt(float2 x, float t, constant Params& p) {
// Where x's light came in: a guess, corrected once.
float3 down = refract(-sunInAir(p), float3(0, 1, 0), 1.0 / WATER_N);
float2 q = x - down.xz * p.depth / -down.y;
q += x - landAt(q, t, p);
// Three rays 4 mm apart: how much their patch grew.
float e = 0.004;
float2 f0 = landAt(q, t, p);
float2 fx = landAt(q + float2(e, 0.0), t, p) - f0;
float2 fz = landAt(q + float2(0.0, e), t, p) - f0;
// The Jacobian determinant, softened near 0.
float det = (fx.x * fz.y - fx.y * fz.x) / (e * e);
return 1.05 / (abs(det) + 0.05);
}
// How much water mirrors (Schlick's Fresnel): little seen from above,
// nearly all edge-on.
float mirror(float facing) {
return 0.02 + 0.98 * pow(1.0 - clamp(facing, 0.0, 1.0), 5.0);
}
// The sky the water mirrors, and the sun in it.
float3 poolSky(float3 r, constant Params& p) {
float up = sqrt(max(r.y, 0.0));
float3 col = mix(float3(0.9, 0.95, 1.0), float3(0.25, 0.45, 0.9), up);
float sun = dot(r, sunInAir(p));
col += kiln(0.95) * 8.0 * pow(max(sun, 0.0), 1500.0);
col += kiln(0.8) * 0.6 * pow(max(sun, 0.0), 200.0);
return col;
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float2 s = (uv - 0.5) * u.resolution / u.resolution.y * 2.0;
float t = loopTime(u.time);
float3 fwd = float3(0.0, -sin(PITCH), cos(PITCH));
float3 up = float3(0.0, cos(PITCH), sin(PITCH));
float3 rd = normalize(fwd * FOCAL + float3(s.x, 0.0, 0.0) + up * s.y);
// The ray meets the water at height 0, where it is bent.
float air = EYE.y / -rd.y;
float2 q = EYE.xz + rd.xz * air;
float3 w = surfaceAt(q, t, p);
float3 n = normalize(float3(-w.y, 1.0, -w.z));
float3 down = refract(rd, n, 1.0 / WATER_N);
float swim = p.depth / -down.y;
float2 x = q + down.xz * swim;
// How wide a pixel is on the floor, to soften what is finer.
float px = 2.0 / (u.resolution.y * FOCAL);
float foot = 1.5 * (air + swim) * px / max(-down.y, 0.3);
float3 tile = tileColor(x, foot, p);
// Sunlight on the floor: what got in, gathered, after its path.
float3 L = sunInAir(p);
float3 sunDown = refract(-L, float3(0, 1, 0), 1.0 / WATER_N);
float focus = mix(1.0, causticAt(x, t, p), exp(-foot * 20.0));
float3 sun = SUNLIGHT * L.y * (1.0 - mirror(L.y)) * focus;
sun *= exp(-ABSORB * p.depth / -sunDown.y);
float3 sky = SKYLIGHT * exp(-ABSORB * p.depth * 1.2);
// Back up to the eye, through swim meters of water.
float3 below = tile * (sun + sky) * exp(-ABSORB * swim);
// The surface mirrors the sky, more toward the far side.
float3 r = reflect(rd, n);
float3 light = mix(below, poolSky(r, p), mirror(dot(-rd, n)));
float3 col = 1.0 - exp(-light * 1.2);
col = pow(col, float3(1.0 / 2.2));
// A speck of noise keeps the blues from banding.
float2 at = floor(uv * u.resolution);
float jitter = fract(52.98 * fract(dot(at, float2(0.0671, 0.00584))));
col += (jitter - 0.5) / 255.0;
return float4(col, 1.0);
}
// ---- The sound ----
// Noise with nothing above `rate` Hz, as in Underwater.
float murmur(float t, float rate, float seed) {
float x = t * rate;
float i = floor(x), f = fract(x);
f = f * f * (3.0 - 2.0 * f);
float h0 = hash21(float2(i, seed)), h1 = hash21(float2(i + 1.0, seed));
return mix(h0, h1, f) * 2.0 - 1.0;
}
// A bubble's note: Minnaert's 3.26 / radius Hz, sliding up.
float blip(float since, float radius) {
if (since < 0.0 || since > 0.3) return 0.0;
float f = 3.26 / radius;
float env = smoothstep(0.0, 0.004, since) * exp(-since / 0.035);
return env * sin(TAU * f * (since + 10.0 * since * since));
}
// Water slapping a wall at q: noise, loud as the water rises fast,
// over a low rumble that swells as it stands high.
float lapAt(float2 q, float t, float seed, constant Params& p) {
float h0 = surfaceAt(q, t, p).x;
float rise = (surfaceAt(q, t + 0.02, p).x - h0) / 0.02;
float slap = min(max(rise, 0.0) / 0.08, 1.5);
float splash = murmur(t, 1500.0, seed) - 0.6 * murmur(t, 400.0, seed);
float body = murmur(t, 140.0, seed + 5.0) * max(0.3 + h0 * 8.0, 0.0);
return splash * slap * slap + 0.5 * body * p.waves;
}
float2 sound(float t, constant Params& p) {
float tl = loopTime(t);
// Each ear hears the wall on its side.
float2 v = float2(lapAt(float2(-2.4, 1.6), tl, 1.0, p),
lapAt(float2(2.4, 1.6), tl, 2.0, p));
v *= 0.5;
// A plink for each drop as the ring starts.
float n = floor(p.drops + 0.5);
if (n >= 1.0) {
float k0 = floor(tl * n / LOOP);
for (int j = 0; j < 2; j++) {
float3 d = dropOf(k0 - float(j), n);
float b = blip(wrap(tl - d.z, LOOP), 0.0025);
float pan = clamp(0.5 + 0.3 * d.x, 0.0, 1.0);
v += 0.4 * b * float2(1.0 - pan, pan);
}
}
// Silence where the minute comes round, so it joins.
float turn = min(tl, LOOP - tl);
float fade = 1.0 - exp(-turn * 30.0);
return tanh(v) * p.volume * fade;
}1 line from stage 5 that are gone
return sum * p.waves;The sound
The sound is the water at the pool's walls. Each ear hears the wave sum at a wall on its side, 2.4 m to the left and to the right, 1.6 m ahead: lapAt reads surfaceAt there and 20 ms later. When the water rises there, a slap of band noise plays, loud as it rises fast (its speed of rising over 8 cm a second, held to at most 1.5, then squared): murmur, Underwater's noise, at 1,500 steps a second, so with almost nothing above 1.5 kHz, minus 0.6 of a slower one at 400 steps a second. Under it a low body, murmur at 140 steps a second, follows the height, louder as the water stands high, times waves.
In the finished shader a plink sounds as each drop lands, at the moment its ring starts: the sound calls the same dropOf as the picture. blip, Underwater's bubble note, rings at Minnaert's frequency, 3.26 / radius Hz, about 1.3 kHz for a 2.5 mm bubble, starts within 4 ms, dies away with a time constant of 35 ms, and slides up as it rings. Each plink is panned by where its drop lands, from left to right across the pool.
Everything goes through tanh before volume, and fades to silence over a few hundredths of a second at each end of the minute, so the sound loops at 60 seconds with the picture. With waves 0 and drops 0 the water is flat and still, and the pool is silent. Over the first 20 seconds at the defaults the peak is −3.5 dBFS and the RMS −20.1 dBFS; over the whole minute, −3.3 dBFS and −21.3 dBFS.
New in Ray Kiln
The motion view is new in the playground: press Motion under the picture, or V, and instead of the picture you see how much each pixel changed since the frame before, as brightness. What moves lights up and what stands still goes dark. The change is the largest of red, green and blue's, over the time on the shader's clock between the two frames, so it reads the same on a 60 Hz and a 120 Hz screen, in slow motion and played backwards. It is drawn in gray on a log scale, black for no change and white for a full flip every 60th of a second, and the scale is under the picture. Paused it goes black; a step or a drag of the clock then shows that move's change. It is on screen only: exports, stills, the loupe and Pin keep the shader's own picture.
This pool shows what it is for. With the sliders as they come, nearly the whole picture is gray, since the waves bend every view ray: the caustic lines are bright threads, most of them a pair of edges with a dark seam between where the line's middle stays bright, the grout wobbles as a second net, and the darkest places are the flat blue inside the tiles. Set waves to 0 and drops to 0 and the view is black, as the water is still. Then raise drops to 12: all that lights is what the drops do: a smooth ring of light on the floor, and offset from it a ring-shaped band where only the grout lines show, the tiles seen through the ripple.
Try this
- In The tiles, set
depthto 4: the floor is farther away, and the tiles look smaller and more crowded. At 0.3 they are large. - In Waves bend the view, set
wavesto 0: the grid is straight and still. Setdepthto 0.3: the tiles barely wobble, since a bent ray has little water to cross. - In Caustics, set
wavesto 0: the floor is evenly lit, with no bright lines. Setdepthto 0.3: the lines fade to soft blotches, since the light has too little way to gather. At 4 they crowd into a sharp, bright net. - In Light lost in the water, set
depthto 0.3: the tiles are pale, nearly white-blue. At 4 they are a deep, saturated, darker blue. - In The surface mirrors the sky, set
sunto 30: a large glare of glints shows on the far water at the top right, where the waves mirror the low sun. - In the finished shader, set
dropsto 0: the rings stop. Setwavesto 0 as well, and the pool is silent.
Parameters
Pick a parameter and drag the slider. Each frame is the same moment of the shader, with only that one setting changed.
| Parameter | Default | Range | What it does |
|---|---|---|---|
depth | 2.2 | 0.3 to 4 | How deep the water is over the tiles, in meters |
waves | 1 | 0 to 2 | How tall the waves are: 0 is still water |
sun | 65 | 30 to 90 | The sun's height over the horizon, in degrees |
tile | 0.25 | 0.1 to 0.5 | How wide a tile is, in meters |
drops | 4 | 0 to 12 | How many drops a minute fall in and ring the water |
volume | 0.8 | 0 to 1 | Loudness of the sound |
More from this shader
The same shader 8 more ways: each from a different moment, with different settings, one after another. Each chapter below says which settings moved most, then lists them all.
Wallpapers
A still from the shader, rendered at full size for a desktop or a phone screen. Free to use as your wallpaper.
Clips to post
Fifteen seconds of the shader with its sound, sized for posting: square for feeds, vertical for stories and short videos. Free to post, ideally with a link to this page.
Made from this episode
The full source
This is the whole episode: the picture, the parameters and the sound.
swimming-pool.metal
// Ray Kiln · Swimming Pool
//
// @episode Swimming Pool
// @length 60
// @variation 30
// @variations 1,2,3,4,5,6,7,10
// @sweeps depth, waves, sun
// @still 22
// @teaches Caustics: light bent by a moving surface, gathered on the floor
// @category Fragment
// @tags swimming pool, caustics, refraction, Snell's law, heightfield, normals, jacobian, absorption, fresnel, ripples, procedural sound
// @final Swimming Pool | Drops ring the water and bend the tiles and the light under them, a lane line runs along the floor, and each drop plinks as it lands.
//
// Looking down into a tiled pool on a sunny day. The water's surface is a
// sum of six waves. Each view ray is bent where it enters the water, by
// Snell's law, so the tiles wobble. Sunlight is bent by the same surface:
// where the waves act as lenses they squeeze a patch of light into a
// smaller patch of floor, which is brighter by just that much. The ratio
// of the two areas is the Jacobian determinant of the map from a point on
// the surface to where its light lands, and it makes the bright lines.
// Every wave goes round a whole number of times a minute, so it loops.
// Each @param line below becomes a slider in the host app and a row in the page's parameters table:
// // @param name default min max description
// @param depth 2.2 0.3 4 How deep the water is over the tiles, in meters
// @param waves 1 0 2 How tall the waves are: 0 is still water
// @param sun 65 30 90 The sun's height over the horizon, in degrees
// @param tile 0.25 0.1 0.5 How wide a tile is, in meters
// @param drops 4 0 12 How many drops a minute fall in and ring the water
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
constant float LOOP = 60.0;
// The eye is 2.2 m over the water, looking down 58 degrees.
constant float3 EYE = float3(0.0, 2.2, -0.4);
constant float PITCH = 1.01;
constant float FOCAL = 1.7;
// Light bends by this ratio going from air into water.
constant float WATER_N = 1.333;
// What a meter of clear water absorbs of red, green, blue.
constant float3 ABSORB = float3(0.4, 0.06, 0.012);
// Sunlight and the sky's light as they reach the water.
constant float3 SUNLIGHT = float3(1.0, 0.92, 0.8);
constant float3 SKYLIGHT = float3(0.1, 0.16, 0.26);
// Grout, a warm cream, a tenth of each tile wide.
constant float3 GROUT = float3(0.86, 0.82, 0.74);
constant float GROUT_W = 0.05;
float hash21(float2 p) {
p = fract(p * float2(123.34, 456.21));
p += dot(p, p + 45.32);
return fract(p.x * p.y);
}
// The kiln palette from Flowing Color Field.
float3 kiln(float t) {
float3 c0 = float3(0.020, 0.012, 0.020);
float3 c1 = float3(0.280, 0.040, 0.030);
float3 c2 = float3(0.880, 0.260, 0.050);
float3 c3 = float3(1.000, 0.680, 0.200);
float3 c4 = float3(1.000, 0.970, 0.840);
t = clamp(t, 0.0, 1.0);
float3 c = mix(c0, c1, smoothstep(0.00, 0.22, t));
c = mix(c, c2, smoothstep(0.22, 0.50, t));
c = mix(c, c3, smoothstep(0.50, 0.76, t));
return mix(c, c4, smoothstep(0.76, 1.00, t));
}
float loopTime(float t) {
return t - LOOP * floor(t / LOOP);
}
// Wraps x into 0 to m, so a count repeats each loop.
float wrap(float x, float m) {
return x - m * floor(x / m);
}
// Toward the sun in the sky, from its height in degrees.
float3 sunInAir(constant Params& p) {
float e = p.sun * TAU / 360.0;
return float3(0.35 * cos(e), sin(e), 0.94 * cos(e));
}
// The floor: pool-blue tiles, each a little different, in cream grout.
float3 tileColor(float2 x, float foot, constant Params& p) {
float2 g = x / p.tile;
float2 id = floor(g);
float2 f = abs(fract(g) - 0.5);
// How far this point is from the middle of the nearest grout line.
float edge = 0.5 - max(f.x, f.y);
float aa = foot / p.tile;
float grout = 1.0 - smoothstep(GROUT_W - aa, GROUT_W + aa, edge);
// Where a pixel spans a tile, its average color instead.
float far = smoothstep(0.15, 0.5, aa);
grout = mix(grout, 0.19, far);
float v = mix(hash21(id), 0.5, far);
float3 blue = float3(0.2, 0.5, 0.75) * (0.88 + 0.24 * v);
// The lane line: one column of dark tiles.
float lane = 1.0 - smoothstep(0.5 - aa, 0.5 + aa, abs(g.x - 2.5));
blue = mix(blue, float3(0.04, 0.07, 0.2), lane);
return mix(blue, GROUT, grout);
}
// Wave i: heading, wavelength and amplitude in m, turns a minute.
float4 poolWave(int i) {
if (i == 0) return float4(0.3, 1.9, 0.024, 18.0);
if (i == 1) return float4(2.0, 1.3, 0.016, 22.0);
if (i == 2) return float4(-1.1, 0.83, 0.01, 27.0);
if (i == 3) return float4(1.0, 0.57, 0.0064, 33.0);
if (i == 4) return float4(-2.6, 0.41, 0.0044, 39.0);
return float4(2.7, 0.3, 0.0028, 45.0);
}
// Drop k of n a minute: where it lands, and when.
float3 dropOf(float k, float n) {
float kk = wrap(k, n);
float2 at = float2(-1.3 + 2.6 * hash21(float2(kk, 1.3)),
1.0 + 1.8 * hash21(float2(kk, 7.9)));
float when = (k + 0.4 * hash21(float2(kk, 4.1))) * LOOP / n;
return float3(at, when);
}
// The rings the last two drops sent out: height and slope.
float3 ringAt(float2 q, float t, constant Params& p) {
float3 sum = float3(0.0);
float n = floor(p.drops + 0.5);
if (n < 1.0) return sum;
float k0 = floor(t * n / LOOP);
for (int j = 0; j < 2; j++) {
float3 d = dropOf(k0 - float(j), n);
float age = wrap(t - d.z, LOOP);
float2 v = q - d.xy;
float r = max(length(v), 0.0001);
// A ring 12 cm from crest to crest, spreading 30 cm a second.
float x = r - 0.3 * age;
float k = TAU / 0.12;
float env = 0.004 * exp(-x * x / 0.01 - age / 1.5);
env /= sqrt(1.0 + 3.0 * r);
float h = env * sin(k * x);
float dh = env * k * cos(k * x) - h * 2.0 * x / 0.01;
dh -= h * 1.5 / (1.0 + 3.0 * r);
sum += float3(h, dh * v / r);
}
return sum;
}
// The water's height at q and its slope: a sum of six waves,
// and the rings from the drops.
float3 surfaceAt(float2 q, float t, constant Params& p) {
float a = TAU * t / LOOP;
float3 sum = float3(0.0);
for (int i = 0; i < 6; i++) {
float4 w = poolWave(i);
float2 dir = float2(cos(w.x), sin(w.x));
float k = TAU / w.y;
float ph = k * dot(dir, q) - w.w * a + 1.7 * float(i);
// The slope of a sine is a cosine.
sum += w.z * float3(sin(ph), k * cos(ph) * dir);
}
sum *= p.waves;
return sum + ringAt(q, t, p);
}
// Where light through the surface at q lands.
float2 landAt(float2 q, float t, constant Params& p) {
float3 w = surfaceAt(q, t, p);
float3 n = normalize(float3(-w.y, 1.0, -w.z));
float3 ray = refract(-sunInAir(p), n, 1.0 / WATER_N);
return q + ray.xz * p.depth / -ray.y;
}
// How bright the sunlight is at floor point x.
float causticAt(float2 x, float t, constant Params& p) {
// Where x's light came in: a guess, corrected once.
float3 down = refract(-sunInAir(p), float3(0, 1, 0), 1.0 / WATER_N);
float2 q = x - down.xz * p.depth / -down.y;
q += x - landAt(q, t, p);
// Three rays 4 mm apart: how much their patch grew.
float e = 0.004;
float2 f0 = landAt(q, t, p);
float2 fx = landAt(q + float2(e, 0.0), t, p) - f0;
float2 fz = landAt(q + float2(0.0, e), t, p) - f0;
// The Jacobian determinant, softened near 0.
float det = (fx.x * fz.y - fx.y * fz.x) / (e * e);
return 1.05 / (abs(det) + 0.05);
}
// How much water mirrors (Schlick's Fresnel): little seen from above,
// nearly all edge-on.
float mirror(float facing) {
return 0.02 + 0.98 * pow(1.0 - clamp(facing, 0.0, 1.0), 5.0);
}
// The sky the water mirrors, and the sun in it.
float3 poolSky(float3 r, constant Params& p) {
float up = sqrt(max(r.y, 0.0));
float3 col = mix(float3(0.9, 0.95, 1.0), float3(0.25, 0.45, 0.9), up);
float sun = dot(r, sunInAir(p));
col += kiln(0.95) * 8.0 * pow(max(sun, 0.0), 1500.0);
col += kiln(0.8) * 0.6 * pow(max(sun, 0.0), 200.0);
return col;
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float2 s = (uv - 0.5) * u.resolution / u.resolution.y * 2.0;
float t = loopTime(u.time);
float3 fwd = float3(0.0, -sin(PITCH), cos(PITCH));
float3 up = float3(0.0, cos(PITCH), sin(PITCH));
float3 rd = normalize(fwd * FOCAL + float3(s.x, 0.0, 0.0) + up * s.y);
// The ray meets the water at height 0, where it is bent.
float air = EYE.y / -rd.y;
float2 q = EYE.xz + rd.xz * air;
float3 w = surfaceAt(q, t, p);
float3 n = normalize(float3(-w.y, 1.0, -w.z));
float3 down = refract(rd, n, 1.0 / WATER_N);
float swim = p.depth / -down.y;
float2 x = q + down.xz * swim;
// How wide a pixel is on the floor, to soften what is finer.
float px = 2.0 / (u.resolution.y * FOCAL);
float foot = 1.5 * (air + swim) * px / max(-down.y, 0.3);
float3 tile = tileColor(x, foot, p);
// Sunlight on the floor: what got in, gathered, after its path.
float3 L = sunInAir(p);
float3 sunDown = refract(-L, float3(0, 1, 0), 1.0 / WATER_N);
float focus = mix(1.0, causticAt(x, t, p), exp(-foot * 20.0));
float3 sun = SUNLIGHT * L.y * (1.0 - mirror(L.y)) * focus;
sun *= exp(-ABSORB * p.depth / -sunDown.y);
float3 sky = SKYLIGHT * exp(-ABSORB * p.depth * 1.2);
// Back up to the eye, through swim meters of water.
float3 below = tile * (sun + sky) * exp(-ABSORB * swim);
// The surface mirrors the sky, more toward the far side.
float3 r = reflect(rd, n);
float3 light = mix(below, poolSky(r, p), mirror(dot(-rd, n)));
float3 col = 1.0 - exp(-light * 1.2);
col = pow(col, float3(1.0 / 2.2));
// A speck of noise keeps the blues from banding.
float2 at = floor(uv * u.resolution);
float jitter = fract(52.98 * fract(dot(at, float2(0.0671, 0.00584))));
col += (jitter - 0.5) / 255.0;
return float4(col, 1.0);
}
// ---- The sound ----
// Noise with nothing above `rate` Hz, as in Underwater.
float murmur(float t, float rate, float seed) {
float x = t * rate;
float i = floor(x), f = fract(x);
f = f * f * (3.0 - 2.0 * f);
float h0 = hash21(float2(i, seed)), h1 = hash21(float2(i + 1.0, seed));
return mix(h0, h1, f) * 2.0 - 1.0;
}
// A bubble's note: Minnaert's 3.26 / radius Hz, sliding up.
float blip(float since, float radius) {
if (since < 0.0 || since > 0.3) return 0.0;
float f = 3.26 / radius;
float env = smoothstep(0.0, 0.004, since) * exp(-since / 0.035);
return env * sin(TAU * f * (since + 10.0 * since * since));
}
// Water slapping a wall at q: noise, loud as the water rises fast,
// over a low rumble that swells as it stands high.
float lapAt(float2 q, float t, float seed, constant Params& p) {
float h0 = surfaceAt(q, t, p).x;
float rise = (surfaceAt(q, t + 0.02, p).x - h0) / 0.02;
float slap = min(max(rise, 0.0) / 0.08, 1.5);
float splash = murmur(t, 1500.0, seed) - 0.6 * murmur(t, 400.0, seed);
float body = murmur(t, 140.0, seed + 5.0) * max(0.3 + h0 * 8.0, 0.0);
return splash * slap * slap + 0.5 * body * p.waves;
}
float2 sound(float t, constant Params& p) {
float tl = loopTime(t);
// Each ear hears the wall on its side.
float2 v = float2(lapAt(float2(-2.4, 1.6), tl, 1.0, p),
lapAt(float2(2.4, 1.6), tl, 2.0, p));
v *= 0.5;
// A plink for each drop as the ring starts.
float n = floor(p.drops + 0.5);
if (n >= 1.0) {
float k0 = floor(tl * n / LOOP);
for (int j = 0; j < 2; j++) {
float3 d = dropOf(k0 - float(j), n);
float b = blip(wrap(tl - d.z, LOOP), 0.0025);
float pan = clamp(0.5 + 0.3 * d.x, 0.0, 1.0);
v += 0.4 * b * float2(1.0 - pan, pan);
}
}
// Silence where the minute comes round, so it joins.
float turn = min(tl, LOOP - tl);
float fade = 1.0 - exp(-turn * 30.0);
return tanh(v) * p.volume * fade;
}Running it in your own project
Every Ray Kiln shader is the same shape. The standalone download above is the shader with the small wrapper around it that the Ray Kiln host adds, so it compiles with the ordinary Metal compiler and runs in your own app. It defines three entry points: rk_vertex (a full-screen triangle), rk_fragment (calls the shader's shade) and, for shaders with sound, the compute kernel rk_sound.
To draw it, pass the uniforms at fragment buffer 0 and the parameter values at buffer 1, in the order they are declared:
Drawing it, in Swift
struct Uniforms {
var resolution: SIMD2<Float>
var time: Float
var timeDelta: Float
var mouse: SIMD2<Float>
var frame: UInt32
var pad: UInt32 = 0
}
let library = try device.makeLibrary(source: standaloneSource, options: nil)
let descriptor = MTLRenderPipelineDescriptor()
descriptor.vertexFunction = library.makeFunction(name: "rk_vertex")
descriptor.fragmentFunction = library.makeFunction(name: "rk_fragment")
descriptor.colorAttachments[0].pixelFormat = .bgra8Unorm
let pipeline = try device.makeRenderPipelineState(descriptor: descriptor)
// Each frame, inside a render pass:
var uniforms = Uniforms(resolution: size, time: time, timeDelta: dt, mouse: mouse, frame: frame)
var params: [Float] = [2.2, 1, 65, 0.25, 4, 0.8] // the @param defaults, in the order they are declared
encoder.setRenderPipelineState(pipeline)
encoder.setFragmentBytes(&uniforms, length: MemoryLayout<Uniforms>.stride, index: 0)
encoder.setFragmentBytes(¶ms, length: params.count * MemoryLayout<Float>.stride, index: 1)
encoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3)Time is in seconds, mouse is in pixels from the bottom left, and uv in the shader runs 0 to 1 with (0, 0) at the bottom left. Write the output as sRGB: the shader's numbers go to the screen as they are.
More in Light & Materials
Press ? for keyboard shortcuts.


