Hard MeshAnimated

Ocean surface

A rolling water surface that reflects the sky at grazing angles.

The target picture for the shader challenge "Ocean surface"
Recreate this picture. You pass at a 99% match, then try to write it in fewer characters than the par.

About the Ocean surface shader

A rolling water surface that reflects the sky at grazing angles. Vertex and fragment shaders on real 3D meshes: moving vertices, lighting, toon shading, holograms and dissolves, as used in Unity, Unreal and Godot.

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 491 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

Waves (sum of 3 sines)
uses sin
Normal
a plain value or condition
Fresnel
uses pow, saturate, dot
Colors
uses lerp · colours float3(0.0, 0.2, 0.35), float3(0.6, 0.8, 1.0)
Highlight
uses pow, max, dot, reflect, normalize

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 ocean surface shader

  1. The mirror-reflection of the view ray off the surface is reflect(-i_viewDir, n).
  2. A sharp power of that dot product against the sun direction makes the glint.

Techniques used

  • Fresnel: surfaces are more reflective at grazing angles, which makes rims glow.
  • 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.
  • lerp (mix): blend between two colors or values.

Ocean surface 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: Vertex shader: Return the result

Inside float3 vert(float3 pos, float3 normal, float2 uv)

pos.y = 0.10 * sin(pos.x * 2.0 + time * 1.5) + 0.07 * sin(pos.z * 3.0 - time * 1.2) + 0.04 * sin((pos.x + pos.z) * 6.0 + time * 2.5);
return pos;
  • Putting time inside sin or cos makes the value swing back and forth, which is what animates the effect.
  • time is the number of seconds since the shader started, so anything that uses it moves.
  • The vertex shader moves each vertex of the mesh before anything is drawn. Move the vertices and the whole surface changes shape.

Part 2: Choose the colours

Inside float4 frag(float2 uv)

float3 n = i_faceNormal;
float f = pow(1.0 - saturate(dot(n, i_viewDir)), 3.0);
float3 col = lerp(float3(0.0, 0.2, 0.35), float3(0.6, 0.8, 1.0), f);
  • lerp(a, b, t) blends from a to b. With a mask as t it paints one colour over another.
  • The dot product of the surface normal and the light direction is the diffuse light: bright where the surface faces the light, dark where it faces away.
  • pow(x, k) reshapes a value: a high power keeps only the brightest part, which makes small sharp highlights.
  • saturate() clamps a value into the 0 to 1 range.

Part 3: Colour the pixel and return it

Still inside frag()

col += pow(max(dot(reflect(-i_viewDir, n), normalize(float3(0.4, 0.6, 0.7))), 0.0), 40.0);
return float4(col, 1.0);
  • 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.
  • normalize() scales a direction to length 1, so only its direction matters.
  • A mesh shader receives the position, surface direction and view direction of each pixel (i_worldPos, i_normal, i_viewDir).
  • The shader returns the final colour: red, green, blue, and an alpha of 1 (fully opaque).

The full code

HLSL Unity, Unreal
float3 vert(float3 pos, float3 normal, float2 uv)
{
    pos.y = 0.10 * sin(pos.x * 2.0 + time * 1.5) + 0.07 * sin(pos.z * 3.0 - time * 1.2) + 0.04 * sin((pos.x + pos.z) * 6.0 + time * 2.5);
    return pos;
}

float4 frag(float2 uv)
{
    float3 n = i_faceNormal;
    float f = pow(1.0 - saturate(dot(n, i_viewDir)), 3.0);
    float3 col = lerp(float3(0.0, 0.2, 0.35), float3(0.6, 0.8, 1.0), f);
    col += pow(max(dot(reflect(-i_viewDir, n), normalize(float3(0.4, 0.6, 0.7))), 0.0), 40.0);
    return float4(col, 1.0);
}
GLSL three.js, Shadertoy, OpenGL
vec3 vert(vec3 pos, vec3 normal, vec2 uv)
{
    pos.y = 0.10 * sin(pos.x * 2.0 + time * 1.5) + 0.07 * sin(pos.z * 3.0 - time * 1.2) + 0.04 * sin((pos.x + pos.z) * 6.0 + time * 2.5);
    return pos;
}

vec4 frag(vec2 uv)
{
    vec3 n = i_faceNormal;
    float f = pow(1.0 - clamp(dot(n, i_viewDir), 0.0, 1.0), 3.0);
    vec3 col = mix(vec3(0.0, 0.2, 0.35), vec3(0.6, 0.8, 1.0), f);
    col += pow(max(dot(reflect(-i_viewDir, n), normalize(vec3(0.4, 0.6, 0.7))), 0.0), 40.0);
    return vec4(col, 1.0);
}

Open it in the editor and change it

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 coding

Questions about this shader

How do I make an ocean surface shader?

A rolling water surface that reflects the sky at grazing angles. A good first step: the mirror-reflection of the view ray off the surface is reflect(-i_viewDir, n). 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 ocean surface shader use?

It is built from fresnel, sine waves, reflection, diffuse lighting, lerp (mix). Each one is a small idea you can reuse in other effects.

Does this ocean surface 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.