Medium 2DAnimated

Analog clock

A round clock face with 12 tick marks and three hands (sped up).

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

About the Analog clock shader

A round clock face with 12 tick marks and three hands (sped up). Two-dimensional shapes and compositions: circles, boxes, lines and how to combine them.

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

Hand angles
second: time * 0.5, minute: time * ?, hour: time * ?
Hand tips
length ? / ? / ? along float2(sin(angle), cos(angle))
Hand look
1.0 - smoothstep(w, w + ?, distance to segment), w = ? second (red float3(0.9,0.2,0.2)), ? minute, ? hour (both float3(0.1,0.1,0.12))
Face
radius ? (edge ? → ?), float3(0.93,0.92,0.88) on float3(0.08,0.09,0.13)
Ticks
step(?, cos(atan2(p.x, p.y) * ?)) * step(?, r) * step(r, ?), dark float3(0.1,0.1,0.12)

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 analog clock shader

  1. The toolbox sdSeg() gives the distance from a pixel to a line segment: perfect for clock hands.
  2. Tick marks come from angle: cos(a * 12) peaks 12 times around the circle.

Techniques used

  • smoothstep: turns a number into a soft 0-to-1 transition, used for soft edges and masks.
  • Polar coordinates: angle and distance from a center, for spirals, rings and radial effects.
  • Rotation: rotate coordinates with sine and cosine.
  • 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.

Analog clock 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: sdSeg()

float sdSeg(float2 p, float2 a, float2 b)
{
    float2 pa = p - a, ba = b - a;
    float h = saturate(dot(pa, ba) / dot(ba, ba));
    return length(pa - ba * h);
}
  • 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: Set up the coordinates

Inside float4 frag(float2 uv)

float2 p = (uv - 0.5) * float2(aspect, 1.0);
float r = length(p);
float3 col = float3(0.08, 0.09, 0.13);
  • 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.
  • length() is the distance from the origin, the starting point of discs, rings and glows.
  • float3(r, g, b) is a colour: each channel from 0 (none) to 1 (full).

Part 3: Choose the colours

Still inside frag()

col = lerp(col, float3(0.93, 0.92, 0.88), 1.0 - smoothstep(0.4, 0.41, r));
float a = atan2(p.x, p.y);
float tick = step(0.96, cos(a * 12.0)) * step(0.33, r) * step(r, 0.38);
  • atan2 gives the angle around the centre, which is what spirals, spokes and radial patterns are built from.
  • 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 4: Choose the colours (continued)

Still inside frag()

col = lerp(col, float3(0.1, 0.1, 0.12), tick);
float ah = time * 0.005, am = time * 0.05, as = time * 0.5;
float dh = sdSeg(p, float2(0.0, 0.0), 0.2 * float2(sin(ah), cos(ah)));
  • lerp(a, b, t) blends from a to b. With a mask as t it paints one colour over another.
  • time is the number of seconds since the shader started, so anything that uses it moves.
  • float3(r, g, b) is a colour: each channel from 0 (none) to 1 (full).

Part 5: Choose the colours (continued)

Still inside frag()

float dm = sdSeg(p, float2(0.0, 0.0), 0.3 * float2(sin(am), cos(am)));
float ds = sdSeg(p, float2(0.0, 0.0), 0.34 * float2(sin(as), cos(as)));
col = lerp(col, float3(0.1, 0.1, 0.12), 1.0 - smoothstep(0.012, 0.018, dh));
  • smoothstep(a, b, x) turns x into a smooth 0-to-1 value between a and b. It is the soft edge or mask.
  • 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 6: Colour the pixel and return it

Still inside frag()

col = lerp(col, float3(0.1, 0.1, 0.12), 1.0 - smoothstep(0.01, 0.016, dm));
col = lerp(col, float3(0.9, 0.2, 0.2), 1.0 - smoothstep(0.004, 0.01, ds));
return float4(col, 1.0);
  • smoothstep(a, b, x) turns x into a smooth 0-to-1 value between a and b. It is the soft edge or mask.
  • 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).
  • The shader returns the final colour: red, green, blue, and an alpha of 1 (fully opaque).

The full code

HLSL Unity, Unreal
float sdSeg(float2 p, float2 a, float2 b)
{
    float2 pa = p - a, ba = b - a;
    float h = saturate(dot(pa, ba) / dot(ba, ba));
    return length(pa - ba * h);
}

float4 frag(float2 uv)
{
    float2 p = (uv - 0.5) * float2(aspect, 1.0);
    float r = length(p);
    float3 col = float3(0.08, 0.09, 0.13);
    col = lerp(col, float3(0.93, 0.92, 0.88), 1.0 - smoothstep(0.4, 0.41, r));
    float a = atan2(p.x, p.y);
    float tick = step(0.96, cos(a * 12.0)) * step(0.33, r) * step(r, 0.38);
    col = lerp(col, float3(0.1, 0.1, 0.12), tick);
    float ah = time * 0.005, am = time * 0.05, as = time * 0.5;
    float dh = sdSeg(p, float2(0.0, 0.0), 0.2 * float2(sin(ah), cos(ah)));
    float dm = sdSeg(p, float2(0.0, 0.0), 0.3 * float2(sin(am), cos(am)));
    float ds = sdSeg(p, float2(0.0, 0.0), 0.34 * float2(sin(as), cos(as)));
    col = lerp(col, float3(0.1, 0.1, 0.12), 1.0 - smoothstep(0.012, 0.018, dh));
    col = lerp(col, float3(0.1, 0.1, 0.12), 1.0 - smoothstep(0.01, 0.016, dm));
    col = lerp(col, float3(0.9, 0.2, 0.2), 1.0 - smoothstep(0.004, 0.01, ds));
    return float4(col, 1.0);
}
GLSL Shadertoy, three.js, OpenGL
float sdSeg(vec2 p, vec2 a, vec2 b)
{
    vec2 pa = p - a, ba = b - a;
    float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
    return length(pa - ba * h);
}

vec4 frag(vec2 uv)
{
    vec2 p = (uv - 0.5) * vec2(aspect, 1.0);
    float r = length(p);
    vec3 col = vec3(0.08, 0.09, 0.13);
    col = mix(col, vec3(0.93, 0.92, 0.88), 1.0 - smoothstep(0.4, 0.41, r));
    float a = atan(p.x, p.y);
    float tick = step(0.96, cos(a * 12.0)) * step(0.33, r) * step(r, 0.38);
    col = mix(col, vec3(0.1, 0.1, 0.12), tick);
    float ah = time * 0.005, am = time * 0.05, as = time * 0.5;
    float dh = sdSeg(p, vec2(0.0, 0.0), 0.2 * vec2(sin(ah), cos(ah)));
    float dm = sdSeg(p, vec2(0.0, 0.0), 0.3 * vec2(sin(am), cos(am)));
    float ds = sdSeg(p, vec2(0.0, 0.0), 0.34 * vec2(sin(as), cos(as)));
    col = mix(col, vec3(0.1, 0.1, 0.12), 1.0 - smoothstep(0.012, 0.018, dh));
    col = mix(col, vec3(0.1, 0.1, 0.12), 1.0 - smoothstep(0.01, 0.016, dm));
    col = mix(col, vec3(0.9, 0.2, 0.2), 1.0 - smoothstep(0.004, 0.01, ds));
    return vec4(col, 1.0);
}
WGSL WebGPU, Bevy
fn sdSeg(p: vec2f, a: vec2f, b: vec2f) -> f32
{
    let pa = p - a; let ba = b - a;
    let h = saturate(dot(pa, ba) / dot(ba, ba));
    return length(pa - ba * h);
}

fn frag(uv: vec2f) -> vec4f
{
    let p = (uv - 0.5) * vec2f(aspect, 1.0);
    let r = length(p);
    var col = vec3f(0.08, 0.09, 0.13);
    col = mix(col, vec3f(0.93, 0.92, 0.88), 1.0 - smoothstep(0.4, 0.41, r));
    let a = atan2(p.x, p.y);
    let tick = step(0.96, cos(a * 12.0)) * step(0.33, r) * step(r, 0.38);
    col = mix(col, vec3f(0.1, 0.1, 0.12), tick);
    let ah = time * 0.005; let am = time * 0.05; let as_ = time * 0.5;
    let dh = sdSeg(p, vec2f(0.0, 0.0), 0.2 * vec2f(sin(ah), cos(ah)));
    let dm = sdSeg(p, vec2f(0.0, 0.0), 0.3 * vec2f(sin(am), cos(am)));
    let ds = sdSeg(p, vec2f(0.0, 0.0), 0.34 * vec2f(sin(as_), cos(as_)));
    col = mix(col, vec3f(0.1, 0.1, 0.12), 1.0 - smoothstep(0.012, 0.018, dh));
    col = mix(col, vec3f(0.1, 0.1, 0.12), 1.0 - smoothstep(0.01, 0.016, dm));
    col = mix(col, vec3f(0.9, 0.2, 0.2), 1.0 - smoothstep(0.004, 0.01, ds));
    return vec4f(col, 1.0);
}
Godot Godot 4 shading language
shader_type canvas_item;

float sdSeg(vec2 p, vec2 a, vec2 b)
{
    vec2 pa = p - a, ba = b - a;
    float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
    return length(pa - ba * h);
}

vec4 frag(vec2 uv)
{
    vec2 p = (uv - 0.5) * vec2(aspect, 1.0);
    float r = length(p);
    vec3 col = vec3(0.08, 0.09, 0.13);
    col = mix(col, vec3(0.93, 0.92, 0.88), 1.0 - smoothstep(0.4, 0.41, r));
    float a = atan(p.x, p.y);
    float tick = step(0.96, cos(a * 12.0)) * step(0.33, r) * step(r, 0.38);
    col = mix(col, vec3(0.1, 0.1, 0.12), tick);
    float ah = TIME * 0.005, am = TIME * 0.05, as = TIME * 0.5;
    float dh = sdSeg(p, vec2(0.0, 0.0), 0.2 * vec2(sin(ah), cos(ah)));
    float dm = sdSeg(p, vec2(0.0, 0.0), 0.3 * vec2(sin(am), cos(am)));
    float ds = sdSeg(p, vec2(0.0, 0.0), 0.34 * vec2(sin(as), cos(as)));
    col = mix(col, vec3(0.1, 0.1, 0.12), 1.0 - smoothstep(0.012, 0.018, dh));
    col = mix(col, vec3(0.1, 0.1, 0.12), 1.0 - smoothstep(0.01, 0.016, dm));
    col = mix(col, vec3(0.9, 0.2, 0.2), 1.0 - smoothstep(0.004, 0.01, ds));
    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 sdSeg(float2 p, float2 a, float2 b)
{
    float2 pa = p - a, ba = b - a;
    float h = saturate(dot(pa, ba) / dot(ba, ba));
    return length(pa - ba * h);
}

float4 frag(float2 uv)
{
    float2 p = (uv - 0.5) * float2(aspect, 1.0);
    float r = length(p);
    float3 col = float3(0.08, 0.09, 0.13);
    col = mix(col, float3(0.93, 0.92, 0.88), 1.0 - smoothstep(0.4, 0.41, r));
    float a = atan2(p.x, p.y);
    float tick = step(0.96, cos(a * 12.0)) * step(0.33, r) * step(r, 0.38);
    col = mix(col, float3(0.1, 0.1, 0.12), tick);
    float ah = time * 0.005, am = time * 0.05, as = time * 0.5;
    float dh = sdSeg(p, float2(0.0, 0.0), 0.2 * float2(sin(ah), cos(ah)));
    float dm = sdSeg(p, float2(0.0, 0.0), 0.3 * float2(sin(am), cos(am)));
    float ds = sdSeg(p, float2(0.0, 0.0), 0.34 * float2(sin(as), cos(as)));
    col = mix(col, float3(0.1, 0.1, 0.12), 1.0 - smoothstep(0.012, 0.018, dh));
    col = mix(col, float3(0.1, 0.1, 0.12), 1.0 - smoothstep(0.01, 0.016, dm));
    col = mix(col, float3(0.9, 0.2, 0.2), 1.0 - smoothstep(0.004, 0.01, ds));
    return float4(col, 1.0);
}

Open it in the editor and change it

Helper functions provided

The toolbox has: sdSeg(). 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 coding

Questions about this shader

How do I make an analog clock shader?

A round clock face with 12 tick marks and three hands (sped up). A good first step: the toolbox sdSeg() gives the distance from a pixel to a line segment: perfect for clock hands. 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 analog clock shader use?

It is built from smoothstep, polar coordinates, rotation, sine waves, step. Each one is a small idea you can reuse in other effects.

Does this analog clock 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.