Medium WaterAnimated
Pool caustics
The shimmering light-net you see on the floor of a swimming pool.
About the Pool caustics shader
The shimmering light-net you see on the floor of a swimming pool. Water is a classic shader topic: waves are a sum of sine waves, the surface reflects the sky, and foam and caustics finish the look.
How this challenge works
You write a fragment shader that draws the picture above, live in your browser. A match bar shows how close you are, and the score combines accuracy, code size (the par is 539 characters in Unity (Built-in)) and GPU speed. Work in HLSL, GLSL, WGSL, Metal or Godot and export the result when you are done.
What it is built from
- p
(uv - 0.5) * float2(aspect, 1.0) * ?- Loop
? iterations of: t = time * 0.5 + 1.0 (outside loop); inside: float2 i = p; c = 1.0; inten = ?; for n in 0..3: t = ... (see hints)- Result
a pool-blue color: ? + ? * pow(c, ?) tinted float3(0.1, 0.5, 0.9), mixed with float3(0.05,0.25,0.5)
The exact numbers are left out (shown as ?), but every colour is given. Measure the rest with the pixel readout on both pictures and Diff.
How to make a pool caustics shader
- This is the classic "tileable water caustic" loop popularised on Shadertoy: warp the point with sin/cos of itself several times and accumulate 1/length.
- Per iteration n: t = time * 0.5 * (1.0 - 3.5 / (float(n) + 1.0)); i = p + float2(cos(t - i.x) + sin(t + i.y), sin(t - i.y) + cos(t + i.x)); c += 1.0 / length(float2(p.x / (sin(i.x + t) / inten), p.y / (cos(i.y + t) / inten))). Then c /= 4; c = 1.17 - pow(c, 1.4).
- Finish with pow(abs(c), 8.0).
Techniques used
- Sine waves: smooth oscillation for waves, pulses and motion over time.
Pool caustics shader code, explained step by step
Try to build it yourself first, because that is how it sticks. When you want to check your work or you are stuck, this is a complete working solution cut into parts, with the idea behind each part. The parts are in HLSL (Unity and Unreal); the full code below comes in every language.
Part 1: Set up the coordinates
Inside float4 frag(float2 uv)
float2 p = (uv - 0.5) * float2(aspect, 1.0) * 6.0;
float2 i = p;
float c = 1.0; - Centre the coordinates on the middle of the picture and multiply x by the aspect ratio, so a circle stays round on a wide picture.
- Subtracting 0.5 moves the origin from the bottom-left corner to the middle of the picture.
Part 2: Build the pattern
Still inside frag()
float inten = 0.005; Part 3: The main loop
Still inside frag()
for (int n = 0; n < 4; n++)
{
float fn = float(n) + 1.0;
float t = time * 0.5 * (1.0 - 3.5 / fn);
i = p + float2(cos(t - i.x) + sin(t + i.y), sin(t - i.y) + cos(t + i.x));
c += 1.0 / length(float2(p.x / (sin(i.x + t) / inten), p.y / (cos(i.y + t) / inten)));
} - The loop runs 4 times. Each pass is one step of the repeated work; the browser caps loops, so keep the count modest.
- time is the number of seconds since the shader started, so anything that uses it moves.
Part 4: Build the pattern (continued)
Still inside frag()
c /= 4.0;
c = 1.17 - pow(c, 1.4);
float v = pow(abs(c), 8.0); - pow(x, k) reshapes a value: a high power keeps only the brightest part, which makes small sharp highlights.
- abs() mirrors a value around zero, which gives symmetric shapes, outlines and sharp ridges.
Part 5: Colour the pixel and return it
Still inside frag()
float3 col = float3(v, v, v) + float3(0.0, 0.3, 0.5);
return float4(saturate(col), 1.0); - saturate() clamps a value into the 0 to 1 range.
- float3(r, g, b) is a colour: each channel from 0 (none) to 1 (full).
- The shader returns the final colour: red, green, blue, and an alpha of 1 (fully opaque).
The full code
HLSL Unity, Unreal
float4 frag(float2 uv)
{
float2 p = (uv - 0.5) * float2(aspect, 1.0) * 6.0;
float2 i = p;
float c = 1.0;
float inten = 0.005;
for (int n = 0; n < 4; n++)
{
float fn = float(n) + 1.0;
float t = time * 0.5 * (1.0 - 3.5 / fn);
i = p + float2(cos(t - i.x) + sin(t + i.y), sin(t - i.y) + cos(t + i.x));
c += 1.0 / length(float2(p.x / (sin(i.x + t) / inten), p.y / (cos(i.y + t) / inten)));
}
c /= 4.0;
c = 1.17 - pow(c, 1.4);
float v = pow(abs(c), 8.0);
float3 col = float3(v, v, v) + float3(0.0, 0.3, 0.5);
return float4(saturate(col), 1.0);
} GLSL Shadertoy, three.js, OpenGL
vec4 frag(vec2 uv)
{
vec2 p = (uv - 0.5) * vec2(aspect, 1.0) * 6.0;
vec2 i = p;
float c = 1.0;
float inten = 0.005;
for (int n = 0; n < 4; n++)
{
float fn = float(n) + 1.0;
float t = time * 0.5 * (1.0 - 3.5 / fn);
i = p + vec2(cos(t - i.x) + sin(t + i.y), sin(t - i.y) + cos(t + i.x));
c += 1.0 / length(vec2(p.x / (sin(i.x + t) / inten), p.y / (cos(i.y + t) / inten)));
}
c /= 4.0;
c = 1.17 - pow(c, 1.4);
float v = pow(abs(c), 8.0);
vec3 col = vec3(v, v, v) + vec3(0.0, 0.3, 0.5);
return vec4(clamp(col, 0.0, 1.0), 1.0);
} WGSL WebGPU, Bevy
fn frag(uv: vec2f) -> vec4f
{
let p = (uv - 0.5) * vec2f(aspect, 1.0) * 6.0;
var i = p;
var c = 1.0;
let inten = 0.005;
for (var n = 0; n < 4; n++)
{
let fn_ = f32(n) + 1.0;
let t = time * 0.5 * (1.0 - 3.5 / fn_);
i = p + vec2f(cos(t - i.x) + sin(t + i.y), sin(t - i.y) + cos(t + i.x));
c += 1.0 / length(vec2f(p.x / (sin(i.x + t) / inten), p.y / (cos(i.y + t) / inten)));
}
c /= 4.0;
c = 1.17 - pow(c, 1.4);
let v = pow(abs(c), 8.0);
let col = vec3f(v, v, v) + vec3f(0.0, 0.3, 0.5);
return vec4f(saturate(col), 1.0);
} Godot Godot 4 shading language
shader_type canvas_item;
vec4 frag(vec2 uv)
{
vec2 p = (uv - 0.5) * vec2(aspect, 1.0) * 6.0;
vec2 i = p;
float c = 1.0;
float inten = 0.005;
for (int n = 0; n < 4; n++)
{
float fn = float(n) + 1.0;
float t = TIME * 0.5 * (1.0 - 3.5 / fn);
i = p + vec2(cos(t - i.x) + sin(t + i.y), sin(t - i.y) + cos(t + i.x));
c += 1.0 / length(vec2(p.x / (sin(i.x + t) / inten), p.y / (cos(i.y + t) / inten)));
}
c /= 4.0;
c = 1.17 - pow(c, 1.4);
float v = pow(abs(c), 8.0);
vec3 col = vec3(v, v, v) + vec3(0.0, 0.3, 0.5);
return vec4(clamp(col, 0.0, 1.0), 1.0);
}
void fragment()
{
// Godot's UV starts at the top-left, the lessons use bottom-left
COLOR = frag(vec2(UV.x, 1.0 - UV.y));
} Solve it in your language
Unity (Built-in), Unity (URP), Unreal, Shadertoy / WebGL, OpenGL (LWJGL, raylib), three.js, GameMaker, LÖVE (Lua), Godot, Apple (iOS, macOS), WebGPU / Bevy.
Start codingQuestions about this shader
How do I make a pool caustics shader?
The shimmering light-net you see on the floor of a swimming pool. A good first step: this is the classic "tileable water caustic" loop popularised on Shadertoy: warp the point with sin/cos of itself several times and accumulate 1/length. In ShaderBattle you build it in the browser with a live match bar, so you see how close you are after every change.
What techniques does the pool caustics shader use?
It is built from sine waves. Each one is a small idea you can reuse in other effects.
Does this pool caustics shader work in Unity, Unreal, Godot and Shadertoy?
Yes. The idea is the same in every shader language. ShaderBattle shows the code in HLSL (Unity, Unreal), GLSL (Shadertoy, three.js, OpenGL), WGSL (WebGPU), Metal and Godot, and exports a ready-to-paste version.