Hard WaterAnimated
Raymarched ocean waves
A rolling sea seen from above the surface, with a sky-colored horizon.
About the Raymarched ocean waves shader
A rolling sea seen from above the surface, with a sky-colored horizon. 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 1100 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
- Height
uses h, sin- Camera
uses normalize- March
uses map, h- Normal
uses calcNormal- Color
colours float3(0.0, 0.25, 0.4), float3(0.2, 0.7, 0.8)- Sky
uses lerp · colours float3(0.8,0.9,1.0), float3(0.3,0.55,0.9)
Only the building blocks and the colours are listed. Work out how they fit together, then measure the numbers from the target.
How to make a raymarched ocean waves shader
- A heightfield is an SDF if you scale it down (the 0.6) so rays never overshoot.
- Water color: base = lerp(deep, light, n.y * n.y), plus a sun highlight: pow(max(dot(reflect(rd, n), L), 0.0), 40.0) with L = normalize(float3(0.4, 0.6, 0.7)).
- Fog towards the sky: lerp(col, sky, 1.0 - exp(-0.002 * t * t)).
Techniques used
- Ray marching with a signed distance field: each pixel walks a ray through a distance function until it hits a surface.
- Sine waves: smooth oscillation for waves, pulses and motion over time.
- Reflection: mirror a direction around a surface normal.
- Diffuse lighting: brightness from the angle between the surface and the light.
- Exponential falloff: glows, fog and light absorption that fade smoothly with distance.
- lerp (mix): blend between two colors or values.
Raymarched ocean waves 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: Helper: height()
float height(float x, float z)
{
return 0.18 * sin(x * 1.3 + time) + 0.12 * sin(z * 1.7 - time * 1.2) + 0.08 * sin((x + z) * 2.9 + time * 1.7);
} - A helper function the main shader calls. It is handed out in the challenge Toolbox, so you can paste it in with one click.
Part 2: Helper: map()
float map(float3 p)
{
return (p.y - height(p.x, p.z)) * 0.6;
} - The scene: a distance function that says how far a point is from the nearest surface. Ray marching uses it to take safe steps.
Part 3: Helper: calcNormal()
float3 calcNormal(float3 p) // needs map() declared above it
{
float2 e = float2(0.001, 0.0);
return normalize(float3(
map(p + e.xyy) - map(p - e.xyy),
map(p + e.yxy) - map(p - e.yxy),
map(p + e.yyx) - map(p - e.yyx)));
} - A helper function the main shader calls. It is handed out in the challenge Toolbox, so you can paste it in with one click.
Part 4: Set up the coordinates
Inside float4 frag(float2 uv)
float2 p = (uv - 0.5) * float2(aspect, 1.0) * 2.0;
float3 ro = float3(0.0, 1.0, -4.0);
float3 rd = normalize(float3(p.x, p.y - 0.25, 1.5)); - 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.
- normalize() scales a direction to length 1, so only its direction matters.
- float3(r, g, b) is a colour: each channel from 0 (none) to 1 (full).
Part 5: Build the pattern
Still inside frag()
float t = 0.0; Part 6: The main loop
Still inside frag()
for (int i = 0; i < 100; i++)
{
float d = map(ro + rd * t);
t += d;
if (d < 0.001 || t > 40.0) break;
} - The loop runs 100 times. Each pass is one step of the repeated work; the browser caps loops, so keep the count modest.
- The code inside the braces only runs when the condition is true.
Part 7: Choose the colours
Still inside frag()
float3 sky = lerp(float3(0.8, 0.9, 1.0), float3(0.3, 0.55, 0.9), uv.y);
float3 col = sky; - uv runs from 0 to 1 across the picture, so uv.x and uv.y say how far along the screen this pixel is.
- lerp(a, b, t) blends from a to b. With a mask as t it paints one colour over another.
- float3(r, g, b) is a colour: each channel from 0 (none) to 1 (full).
Part 8: Choose the colours (continued)
Still inside frag()
if (t < 40.0)
{
float3 pos = ro + rd * t;
float3 n = calcNormal(pos);
float3 L = normalize(float3(0.4, 0.6, 0.7));
float3 base = lerp(float3(0.0, 0.25, 0.4), float3(0.2, 0.7, 0.8), n.y * n.y);
col = base + pow(max(dot(reflect(rd, n), L), 0.0), 40.0);
col = lerp(col, sky, 1.0 - exp(-0.002 * t * t));
} - The code inside the braces only runs when the condition is true.
- lerp(a, b, t) blends from a to b. With a mask as t it paints one colour over another.
- reflect() mirrors a direction around a surface normal: where a ray goes after bouncing.
- pow(x, k) reshapes a value: a high power keeps only the brightest part, which makes small sharp highlights.
Part 9: Colour the pixel and return it
Still inside frag()
return float4(col, 1.0); - The shader returns the final colour: red, green, blue, and an alpha of 1 (fully opaque).
The full code
HLSL Unity, Unreal
float height(float x, float z)
{
return 0.18 * sin(x * 1.3 + time) + 0.12 * sin(z * 1.7 - time * 1.2) + 0.08 * sin((x + z) * 2.9 + time * 1.7);
}
float map(float3 p)
{
return (p.y - height(p.x, p.z)) * 0.6;
}
float3 calcNormal(float3 p) // needs map() declared above it
{
float2 e = float2(0.001, 0.0);
return normalize(float3(
map(p + e.xyy) - map(p - e.xyy),
map(p + e.yxy) - map(p - e.yxy),
map(p + e.yyx) - map(p - e.yyx)));
}
float4 frag(float2 uv)
{
float2 p = (uv - 0.5) * float2(aspect, 1.0) * 2.0;
float3 ro = float3(0.0, 1.0, -4.0);
float3 rd = normalize(float3(p.x, p.y - 0.25, 1.5));
float t = 0.0;
for (int i = 0; i < 100; i++)
{
float d = map(ro + rd * t);
t += d;
if (d < 0.001 || t > 40.0) break;
}
float3 sky = lerp(float3(0.8, 0.9, 1.0), float3(0.3, 0.55, 0.9), uv.y);
float3 col = sky;
if (t < 40.0)
{
float3 pos = ro + rd * t;
float3 n = calcNormal(pos);
float3 L = normalize(float3(0.4, 0.6, 0.7));
float3 base = lerp(float3(0.0, 0.25, 0.4), float3(0.2, 0.7, 0.8), n.y * n.y);
col = base + pow(max(dot(reflect(rd, n), L), 0.0), 40.0);
col = lerp(col, sky, 1.0 - exp(-0.002 * t * t));
}
return float4(col, 1.0);
} GLSL Shadertoy, three.js, OpenGL
float height(float x, float z)
{
return 0.18 * sin(x * 1.3 + time) + 0.12 * sin(z * 1.7 - time * 1.2) + 0.08 * sin((x + z) * 2.9 + time * 1.7);
}
float map(vec3 p)
{
return (p.y - height(p.x, p.z)) * 0.6;
}
vec3 calcNormal(vec3 p) // needs map() declared above it
{
vec2 e = vec2(0.001, 0.0);
return normalize(vec3(
map(p + e.xyy) - map(p - e.xyy),
map(p + e.yxy) - map(p - e.yxy),
map(p + e.yyx) - map(p - e.yyx)));
}
vec4 frag(vec2 uv)
{
vec2 p = (uv - 0.5) * vec2(aspect, 1.0) * 2.0;
vec3 ro = vec3(0.0, 1.0, -4.0);
vec3 rd = normalize(vec3(p.x, p.y - 0.25, 1.5));
float t = 0.0;
for (int i = 0; i < 100; i++)
{
float d = map(ro + rd * t);
t += d;
if (d < 0.001 || t > 40.0) break;
}
vec3 sky = mix(vec3(0.8, 0.9, 1.0), vec3(0.3, 0.55, 0.9), uv.y);
vec3 col = sky;
if (t < 40.0)
{
vec3 pos = ro + rd * t;
vec3 n = calcNormal(pos);
vec3 L = normalize(vec3(0.4, 0.6, 0.7));
vec3 base = mix(vec3(0.0, 0.25, 0.4), vec3(0.2, 0.7, 0.8), n.y * n.y);
col = base + pow(max(dot(reflect(rd, n), L), 0.0), 40.0);
col = mix(col, sky, 1.0 - exp(-0.002 * t * t));
}
return vec4(col, 1.0);
} WGSL WebGPU, Bevy
fn height(x: f32, z: f32) -> f32
{
return 0.18 * sin(x * 1.3 + time) + 0.12 * sin(z * 1.7 - time * 1.2) + 0.08 * sin((x + z) * 2.9 + time * 1.7);
}
fn map(p: vec3f) -> f32
{
return (p.y - height(p.x, p.z)) * 0.6;
}
fn calcNormal(p: vec3f) -> vec3f // needs map() declared above it
{
let e = vec2f(0.001, 0.0);
return normalize(vec3f(map(p + e.xyy) - map(p - e.xyy), map(p + e.yxy) - map(p - e.yxy), map(p + e.yyx) - map(p - e.yyx)));
}
fn frag(uv: vec2f) -> vec4f
{
let p = (uv - 0.5) * vec2f(aspect, 1.0) * 2.0;
let ro = vec3f(0.0, 1.0, -4.0);
let rd = normalize(vec3f(p.x, p.y - 0.25, 1.5));
var t = 0.0;
for (var i = 0; i < 100; i++)
{
let d = map(ro + rd * t);
t += d;
if (d < 0.001 || t > 40.0) { break; }
}
let sky = mix(vec3f(0.8, 0.9, 1.0), vec3f(0.3, 0.55, 0.9), uv.y);
var col = sky;
if (t < 40.0)
{
let pos = ro + rd * t;
let n = calcNormal(pos);
let L = normalize(vec3f(0.4, 0.6, 0.7));
let base = mix(vec3f(0.0, 0.25, 0.4), vec3f(0.2, 0.7, 0.8), n.y * n.y);
col = base + pow(max(dot(reflect(rd, n), L), 0.0), 40.0);
col = mix(col, sky, 1.0 - exp(-0.002 * t * t));
}
return vec4f(col, 1.0);
} Godot Godot 4 shading language
shader_type canvas_item;
float height(float x, float z)
{
return 0.18 * sin(x * 1.3 + TIME) + 0.12 * sin(z * 1.7 - TIME * 1.2) + 0.08 * sin((x + z) * 2.9 + TIME * 1.7);
}
float map(vec3 p)
{
return (p.y - height(p.x, p.z)) * 0.6;
}
vec3 calcNormal(vec3 p) // needs map() declared above it
{
vec2 e = vec2(0.001, 0.0);
return normalize(vec3(
map(p + e.xyy) - map(p - e.xyy),
map(p + e.yxy) - map(p - e.yxy),
map(p + e.yyx) - map(p - e.yyx)));
}
vec4 frag(vec2 uv)
{
vec2 p = (uv - 0.5) * vec2(aspect, 1.0) * 2.0;
vec3 ro = vec3(0.0, 1.0, -4.0);
vec3 rd = normalize(vec3(p.x, p.y - 0.25, 1.5));
float t = 0.0;
for (int i = 0; i < 100; i++)
{
float d = map(ro + rd * t);
t += d;
if (d < 0.001 || t > 40.0) break;
}
vec3 sky = mix(vec3(0.8, 0.9, 1.0), vec3(0.3, 0.55, 0.9), uv.y);
vec3 col = sky;
if (t < 40.0)
{
vec3 pos = ro + rd * t;
vec3 n = calcNormal(pos);
vec3 L = normalize(vec3(0.4, 0.6, 0.7));
vec3 base = mix(vec3(0.0, 0.25, 0.4), vec3(0.2, 0.7, 0.8), n.y * n.y);
col = base + pow(max(dot(reflect(rd, n), L), 0.0), 40.0);
col = mix(col, sky, 1.0 - exp(-0.002 * t * t));
}
return vec4(col, 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));
} Metal iOS, macOS
float height(float x, float z)
{
return 0.18 * sin(x * 1.3 + time) + 0.12 * sin(z * 1.7 - time * 1.2) + 0.08 * sin((x + z) * 2.9 + time * 1.7);
}
float map(float3 p)
{
return (p.y - height(p.x, p.z)) * 0.6;
}
float3 calcNormal(float3 p) // needs map() declared above it
{
float2 e = float2(0.001, 0.0);
return normalize(float3(
map(p + e.xyy) - map(p - e.xyy),
map(p + e.yxy) - map(p - e.yxy),
map(p + e.yyx) - map(p - e.yyx)));
}
float4 frag(float2 uv)
{
float2 p = (uv - 0.5) * float2(aspect, 1.0) * 2.0;
float3 ro = float3(0.0, 1.0, -4.0);
float3 rd = normalize(float3(p.x, p.y - 0.25, 1.5));
float t = 0.0;
for (int i = 0; i < 100; i++)
{
float d = map(ro + rd * t);
t += d;
if (d < 0.001 || t > 40.0) break;
}
float3 sky = mix(float3(0.8, 0.9, 1.0), float3(0.3, 0.55, 0.9), uv.y);
float3 col = sky;
if (t < 40.0)
{
float3 pos = ro + rd * t;
float3 n = calcNormal(pos);
float3 L = normalize(float3(0.4, 0.6, 0.7));
float3 base = mix(float3(0.0, 0.25, 0.4), float3(0.2, 0.7, 0.8), n.y * n.y);
col = base + pow(max(dot(reflect(rd, n), L), 0.0), 40.0);
col = mix(col, sky, 1.0 - exp(-0.002 * t * t));
}
return float4(col, 1.0);
} Helper functions provided
The toolbox has: calcNormal(). Add them above your frag function with one click.
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 raymarched ocean waves shader?
A rolling sea seen from above the surface, with a sky-colored horizon. A good first step: a heightfield is an SDF if you scale it down (the 0.6) so rays never overshoot. 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 raymarched ocean waves shader use?
It is built from ray marching with a signed distance field, sine waves, reflection, diffuse lighting, exponential falloff. Each one is a small idea you can reuse in other effects.
Does this raymarched ocean waves 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.