Hard PatternsAnimated

Cellular Voronoi

Moving cell centers; each pixel shows its distance to the nearest one.

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

About the Cellular Voronoi shader

Moving cell centers; each pixel shows its distance to the nearest one. Procedural patterns: tiles, stripes, rings, grids and waves made from math instead of images. They scale to any resolution and can animate for free.

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

Grid
uses floor, frac
Search
uses cells
Point in a cell
uses hash2, sin
Distance
uses min, length
Color
uses lerp, smoothstep · colours float3(0.9,0.6,0.2), float3(0.1,0.15,0.35)

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 cellular voronoi shader

  1. A point can be nearer from a neighbouring cell, so check all 9 cells around you.
  2. Two nested for-loops with int counters; convert with float(i).

Techniques used

  • A hash function: a cheap way to get a repeatable random number from a position.
  • smoothstep: turns a number into a soft 0-to-1 transition, used for soft edges and masks.
  • Tiling with floor and frac: repeat a pattern by splitting space into cells.
  • Sine waves: smooth oscillation for waves, pulses and motion over time.
  • lerp (mix): blend between two colors or values.

Cellular Voronoi 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: hash2()

float2 hash2(float2 p)
{
    return frac(sin(float2(dot(p, float2(127.1, 311.7)), dot(p, float2(269.5, 183.3)))) * 43758.5453);
}
  • A hash function. Shaders have no random(), so this turns a position into a pseudo-random number between 0 and 1. The same input always gives the same output, which is what makes noise repeatable.

Part 2: Set up the coordinates

Inside float4 frag(float2 uv)

float2 g = uv * float2(6.0, 4.5);
float2 id = floor(g);
float2 f = frac(g);
  • floor() picks the cell a point is in and frac() is the position inside that cell. Together they repeat a pattern.

Part 3: Build the pattern

Still inside frag()

float d = 8.0;

Part 4: The main loop

Still inside frag()

for (int j = -1; j <= 1; j++)
{
    for (int i = -1; i <= 1; i++)
    {
        float2 b = float2(float(i), float(j));
        float2 o = hash2(id + b);
        o = 0.5 + 0.5 * sin(time + 6.28318 * o);
        d = min(d, length(b + o - f));
    }
}
  • The loop runs 3 times. Each pass is one step of the repeated work; the browser caps loops, so keep the count modest.
  • A hash turns a position into a repeatable pseudo-random number, so every cell can get its own random value.
  • 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 5: Colour the pixel and return it

Still inside frag()

float3 col = lerp(float3(0.9, 0.6, 0.2), float3(0.1, 0.15, 0.35), smoothstep(0.0, 0.8, d));
col += 1.0 - smoothstep(0.0, 0.06, d);
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
float2 hash2(float2 p)
{
    return frac(sin(float2(dot(p, float2(127.1, 311.7)), dot(p, float2(269.5, 183.3)))) * 43758.5453);
}

float4 frag(float2 uv)
{
    float2 g = uv * float2(6.0, 4.5);
    float2 id = floor(g);
    float2 f = frac(g);
    float d = 8.0;
    for (int j = -1; j <= 1; j++)
    {
        for (int i = -1; i <= 1; i++)
        {
            float2 b = float2(float(i), float(j));
            float2 o = hash2(id + b);
            o = 0.5 + 0.5 * sin(time + 6.28318 * o);
            d = min(d, length(b + o - f));
        }
    }
    float3 col = lerp(float3(0.9, 0.6, 0.2), float3(0.1, 0.15, 0.35), smoothstep(0.0, 0.8, d));
    col += 1.0 - smoothstep(0.0, 0.06, d);
    return float4(col, 1.0);
}
GLSL Shadertoy, three.js, OpenGL
vec2 hash2(vec2 p)
{
    return fract(sin(vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3)))) * 43758.5453);
}

vec4 frag(vec2 uv)
{
    vec2 g = uv * vec2(6.0, 4.5);
    vec2 id = floor(g);
    vec2 f = fract(g);
    float d = 8.0;
    for (int j = -1; j <= 1; j++)
    {
        for (int i = -1; i <= 1; i++)
        {
            vec2 b = vec2(float(i), float(j));
            vec2 o = hash2(id + b);
            o = 0.5 + 0.5 * sin(time + 6.28318 * o);
            d = min(d, length(b + o - f));
        }
    }
    vec3 col = mix(vec3(0.9, 0.6, 0.2), vec3(0.1, 0.15, 0.35), smoothstep(0.0, 0.8, d));
    col += 1.0 - smoothstep(0.0, 0.06, d);
    return vec4(col, 1.0);
}
WGSL WebGPU, Bevy
fn hash2(p: vec2f) -> vec2f
{
    return fract(sin(vec2f(dot(p, vec2f(127.1, 311.7)), dot(p, vec2f(269.5, 183.3)))) * 43758.5453);
}

fn frag(uv: vec2f) -> vec4f
{
    let g = uv * vec2f(6.0, 4.5);
    let id = floor(g);
    let f = fract(g);
    var d = 8.0;
    for (var j = -1; j <= 1; j++)
    {
        for (var i = -1; i <= 1; i++)
        {
            let b = vec2f(f32(i), f32(j));
            var o = hash2(id + b);
            o = 0.5 + 0.5 * sin(time + 6.28318 * o);
            d = min(d, length(b + o - f));
        }
    }
    var col = mix(vec3f(0.9, 0.6, 0.2), vec3f(0.1, 0.15, 0.35), smoothstep(0.0, 0.8, d));
    col += 1.0 - smoothstep(0.0, 0.06, d);
    return vec4f(col, 1.0);
}
Godot Godot 4 shading language
shader_type canvas_item;

vec2 hash2(vec2 p)
{
    return fract(sin(vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3)))) * 43758.5453);
}

vec4 frag(vec2 uv)
{
    vec2 g = uv * vec2(6.0, 4.5);
    vec2 id = floor(g);
    vec2 f = fract(g);
    float d = 8.0;
    for (int j = -1; j <= 1; j++)
    {
        for (int i = -1; i <= 1; i++)
        {
            vec2 b = vec2(float(i), float(j));
            vec2 o = hash2(id + b);
            o = 0.5 + 0.5 * sin(TIME + 6.28318 * o);
            d = min(d, length(b + o - f));
        }
    }
    vec3 col = mix(vec3(0.9, 0.6, 0.2), vec3(0.1, 0.15, 0.35), smoothstep(0.0, 0.8, d));
    col += 1.0 - smoothstep(0.0, 0.06, d);
    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
float2 hash2(float2 p)
{
    return fract(sin(float2(dot(p, float2(127.1, 311.7)), dot(p, float2(269.5, 183.3)))) * 43758.5453);
}

float4 frag(float2 uv)
{
    float2 g = uv * float2(6.0, 4.5);
    float2 id = floor(g);
    float2 f = fract(g);
    float d = 8.0;
    for (int j = -1; j <= 1; j++)
    {
        for (int i = -1; i <= 1; i++)
        {
            float2 b = float2(float(i), float(j));
            float2 o = hash2(id + b);
            o = 0.5 + 0.5 * sin(time + 6.28318 * o);
            d = min(d, length(b + o - f));
        }
    }
    float3 col = mix(float3(0.9, 0.6, 0.2), float3(0.1, 0.15, 0.35), smoothstep(0.0, 0.8, d));
    col += 1.0 - smoothstep(0.0, 0.06, d);
    return float4(col, 1.0);
}

Open it in the editor and change it

Helper functions provided

The toolbox has: hash2(). 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 a cellular Voronoi shader?

Moving cell centers; each pixel shows its distance to the nearest one. A good first step: a point can be nearer from a neighbouring cell, so check all 9 cells around you. 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 cellular Voronoi shader use?

It is built from hash function, smoothstep, tiling with floor and frac, sine waves, lerp (mix). Each one is a small idea you can reuse in other effects.

Does this cellular Voronoi 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.