Medium NatureAnimated

Ocean horizon

Sky above, deep water below, and a wavy foam line between them.

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

About the Ocean horizon shader

Sky above, deep water below, and a wavy foam line between them. Natural phenomena such as fire, clouds, lightning and snow. Most are noise functions shaped by a few well-chosen curves.

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

Surface
surf = 0.5 + ? * sin(uv.x * ? + time * 2.0) + ? * sin(uv.x * ? - time * ?)
Sky
lerp(float3(0.95,0.85,0.7), float3(0.4,0.65,0.95), (uv.y - 0.5) * 2.0)
Sea
lerp(float3(0.02,0.1,0.25), float3(0.1,0.45,0.6), uv.y * 2.0)
Foam
1.0 - smoothstep(0.0, ?, abs(uv.y - surf)), white

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

  1. Pick sky or sea with step(surf, uv.y), then add the foam on top.

Techniques used

  • smoothstep: turns a number into a soft 0-to-1 transition, used for soft edges and masks.
  • Sine waves: smooth oscillation for waves, pulses and motion over time.
  • step: a hard 0-or-1 cutoff for crisp shapes and thresholds.
  • lerp (mix): blend between two colors or values.

Ocean horizon 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)

float surf = 0.5 + 0.03 * sin(uv.x * 12.0 + time * 2.0) + 0.02 * sin(uv.x * 25.0 - time * 3.0);
float3 sky = lerp(float3(0.95, 0.85, 0.7), float3(0.4, 0.65, 0.95), (uv.y - 0.5) * 2.0);
float3 sea = lerp(float3(0.02, 0.1, 0.25), float3(0.1, 0.45, 0.6), uv.y * 2.0);
  • 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.
  • 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.

Part 2: Choose the colours

Still inside frag()

float foam = 1.0 - smoothstep(0.0, 0.012, abs(uv.y - surf));
float3 col = lerp(sea, sky, step(surf, uv.y));
col = lerp(col, float3(1.0, 1.0, 1.0), foam);
  • uv runs from 0 to 1 across the picture, so uv.x and uv.y say how far along the screen this pixel is.
  • smoothstep(a, b, x) turns x into a smooth 0-to-1 value between a and b. It is the soft edge or mask.
  • step(a, x) is a hard cutoff: 1 once x passes a, 0 before.
  • lerp(a, b, t) blends from a to b. With a mask as t it paints one colour over another.

Part 3: 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
float4 frag(float2 uv)
{
    float surf = 0.5 + 0.03 * sin(uv.x * 12.0 + time * 2.0) + 0.02 * sin(uv.x * 25.0 - time * 3.0);
    float3 sky = lerp(float3(0.95, 0.85, 0.7), float3(0.4, 0.65, 0.95), (uv.y - 0.5) * 2.0);
    float3 sea = lerp(float3(0.02, 0.1, 0.25), float3(0.1, 0.45, 0.6), uv.y * 2.0);
    float foam = 1.0 - smoothstep(0.0, 0.012, abs(uv.y - surf));
    float3 col = lerp(sea, sky, step(surf, uv.y));
    col = lerp(col, float3(1.0, 1.0, 1.0), foam);
    return float4(col, 1.0);
}
GLSL Shadertoy, three.js, OpenGL
vec4 frag(vec2 uv)
{
    float surf = 0.5 + 0.03 * sin(uv.x * 12.0 + time * 2.0) + 0.02 * sin(uv.x * 25.0 - time * 3.0);
    vec3 sky = mix(vec3(0.95, 0.85, 0.7), vec3(0.4, 0.65, 0.95), (uv.y - 0.5) * 2.0);
    vec3 sea = mix(vec3(0.02, 0.1, 0.25), vec3(0.1, 0.45, 0.6), uv.y * 2.0);
    float foam = 1.0 - smoothstep(0.0, 0.012, abs(uv.y - surf));
    vec3 col = mix(sea, sky, step(surf, uv.y));
    col = mix(col, vec3(1.0, 1.0, 1.0), foam);
    return vec4(col, 1.0);
}
WGSL WebGPU, Bevy
fn frag(uv: vec2f) -> vec4f
{
    let surf = 0.5 + 0.03 * sin(uv.x * 12.0 + time * 2.0) + 0.02 * sin(uv.x * 25.0 - time * 3.0);
    let sky = mix(vec3f(0.95, 0.85, 0.7), vec3f(0.4, 0.65, 0.95), (uv.y - 0.5) * 2.0);
    let sea = mix(vec3f(0.02, 0.1, 0.25), vec3f(0.1, 0.45, 0.6), uv.y * 2.0);
    let foam = 1.0 - smoothstep(0.0, 0.012, abs(uv.y - surf));
    var col = mix(sea, sky, step(surf, uv.y));
    col = mix(col, vec3f(1.0, 1.0, 1.0), foam);
    return vec4f(col, 1.0);
}
Godot Godot 4 shading language
shader_type canvas_item;

vec4 frag(vec2 uv)
{
    float surf = 0.5 + 0.03 * sin(uv.x * 12.0 + TIME * 2.0) + 0.02 * sin(uv.x * 25.0 - TIME * 3.0);
    vec3 sky = mix(vec3(0.95, 0.85, 0.7), vec3(0.4, 0.65, 0.95), (uv.y - 0.5) * 2.0);
    vec3 sea = mix(vec3(0.02, 0.1, 0.25), vec3(0.1, 0.45, 0.6), uv.y * 2.0);
    float foam = 1.0 - smoothstep(0.0, 0.012, abs(uv.y - surf));
    vec3 col = mix(sea, sky, step(surf, uv.y));
    col = mix(col, vec3(1.0, 1.0, 1.0), foam);
    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
float4 frag(float2 uv)
{
    float surf = 0.5 + 0.03 * sin(uv.x * 12.0 + time * 2.0) + 0.02 * sin(uv.x * 25.0 - time * 3.0);
    float3 sky = mix(float3(0.95, 0.85, 0.7), float3(0.4, 0.65, 0.95), (uv.y - 0.5) * 2.0);
    float3 sea = mix(float3(0.02, 0.1, 0.25), float3(0.1, 0.45, 0.6), uv.y * 2.0);
    float foam = 1.0 - smoothstep(0.0, 0.012, abs(uv.y - surf));
    float3 col = mix(sea, sky, step(surf, uv.y));
    col = mix(col, float3(1.0, 1.0, 1.0), foam);
    return float4(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 horizon shader?

Sky above, deep water below, and a wavy foam line between them. A good first step: pick sky or sea with step(surf, uv.y), then add the foam on top. 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 horizon shader use?

It is built from smoothstep, sine waves, step, lerp (mix). Each one is a small idea you can reuse in other effects.

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