Medium Filters

Ordered dithering

Retro 1-bit look: black or white per pixel by comparing brightness to a Bayer matrix.

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

About the Ordered dithering shader

Retro 1-bit look: black or white per pixel by comparing brightness to a Bayer matrix. Post-processing filters: the effects applied to a finished picture, such as blur, edge detection, dithering and color grading. Each reads the picture and writes a changed one.

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 1204 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
work in 2 × 2 screen-pixel blocks: pixel = floor(uv * float2(?, ?))
Threshold
bayer4(pixel) from the Toolbox (0..1)
Output
step(threshold, luminance) (luminance = dot(scene(uv), float3(0.299, 0.587, 0.114)))

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 ordered dithering shader

  1. Brightness above the threshold = white, below = black. Because the threshold pattern repeats, gradients turn into dot patterns.
  2. Acerola-style dithering is exactly this, plus a color palette.

Techniques used

  • step: a hard 0-or-1 cutoff for crisp shapes and thresholds.

Ordered dithering 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: scene()

float3 scene(float2 uv)
{
    float3 col = lerp(float3(0.95, 0.75, 0.45), float3(0.25, 0.45, 0.85), uv.y);
    float2 p = (uv - 0.5) * float2(aspect, 1.0);
    col = lerp(col, float3(1.0, 0.95, 0.7), step(length(p - float2(0.3, 0.2)), 0.12));
    float hill = 0.3 + 0.08 * sin(uv.x * 9.0);
    col = lerp(col, float3(0.1, 0.35, 0.2), step(uv.y, hill));
    float2 q = abs(p - float2(-0.25, -0.05)) - float2(0.14, 0.12);
    col = lerp(col, float3(0.85, 0.15, 0.15), step(max(q.x, q.y), 0.0));
    col = lerp(col, float3(1.0, 1.0, 1.0), step(0.5, frac(uv.x * 14.0)) * step(abs(uv.y - 0.12), 0.04));
    return col;
}
  • 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 2: Helper: bayer4()

float bayer4(float2 p)
{
    float2 q = fmod(floor(p), 4.0);
    float x = q.x, y = q.y;
    float v = 0.0;
    // 4x4 Bayer matrix thresholds, 0..15
    if (y < 0.5) v = (x < 0.5) ? 0.0 : (x < 1.5) ? 8.0 : (x < 2.5) ? 2.0 : 10.0;
    else if (y < 1.5) v = (x < 0.5) ? 12.0 : (x < 1.5) ? 4.0 : (x < 2.5) ? 14.0 : 6.0;
    else if (y < 2.5) v = (x < 0.5) ? 3.0 : (x < 1.5) ? 11.0 : (x < 2.5) ? 1.0 : 9.0;
    else v = (x < 0.5) ? 15.0 : (x < 1.5) ? 7.0 : (x < 2.5) ? 13.0 : 5.0;
    return (v + 0.5) / 16.0;
}
  • 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 3: Set up the coordinates

Inside float4 frag(float2 uv)

float2 pix = floor(uv * float2(200.0, 150.0));
float l = dot(scene(uv), float3(0.299, 0.587, 0.114));
float v = step(bayer4(pix), l);
  • step(a, x) is a hard cutoff: 1 once x passes a, 0 before.
  • floor() picks the cell a point is in and frac() is the position inside that cell. Together they repeat a pattern.
  • float3(r, g, b) is a colour: each channel from 0 (none) to 1 (full).

Part 4: Return the result

Still inside frag()

return float4(v, v, v, 1.0);
  • The shader returns the final colour: red, green, blue, and an alpha of 1 (fully opaque).

The full code

HLSL Unity, Unreal
float3 scene(float2 uv)
{
    float3 col = lerp(float3(0.95, 0.75, 0.45), float3(0.25, 0.45, 0.85), uv.y);
    float2 p = (uv - 0.5) * float2(aspect, 1.0);
    col = lerp(col, float3(1.0, 0.95, 0.7), step(length(p - float2(0.3, 0.2)), 0.12));
    float hill = 0.3 + 0.08 * sin(uv.x * 9.0);
    col = lerp(col, float3(0.1, 0.35, 0.2), step(uv.y, hill));
    float2 q = abs(p - float2(-0.25, -0.05)) - float2(0.14, 0.12);
    col = lerp(col, float3(0.85, 0.15, 0.15), step(max(q.x, q.y), 0.0));
    col = lerp(col, float3(1.0, 1.0, 1.0), step(0.5, frac(uv.x * 14.0)) * step(abs(uv.y - 0.12), 0.04));
    return col;
}

float bayer4(float2 p)
{
    float2 q = fmod(floor(p), 4.0);
    float x = q.x, y = q.y;
    float v = 0.0;
    // 4x4 Bayer matrix thresholds, 0..15
    if (y < 0.5) v = (x < 0.5) ? 0.0 : (x < 1.5) ? 8.0 : (x < 2.5) ? 2.0 : 10.0;
    else if (y < 1.5) v = (x < 0.5) ? 12.0 : (x < 1.5) ? 4.0 : (x < 2.5) ? 14.0 : 6.0;
    else if (y < 2.5) v = (x < 0.5) ? 3.0 : (x < 1.5) ? 11.0 : (x < 2.5) ? 1.0 : 9.0;
    else v = (x < 0.5) ? 15.0 : (x < 1.5) ? 7.0 : (x < 2.5) ? 13.0 : 5.0;
    return (v + 0.5) / 16.0;
}

float4 frag(float2 uv)
{
    float2 pix = floor(uv * float2(200.0, 150.0));
    float l = dot(scene(uv), float3(0.299, 0.587, 0.114));
    float v = step(bayer4(pix), l);
    return float4(v, v, v, 1.0);
}
GLSL Shadertoy, three.js, OpenGL
vec3 scene(vec2 uv)
{
    vec3 col = mix(vec3(0.95, 0.75, 0.45), vec3(0.25, 0.45, 0.85), uv.y);
    vec2 p = (uv - 0.5) * vec2(aspect, 1.0);
    col = mix(col, vec3(1.0, 0.95, 0.7), step(length(p - vec2(0.3, 0.2)), 0.12));
    float hill = 0.3 + 0.08 * sin(uv.x * 9.0);
    col = mix(col, vec3(0.1, 0.35, 0.2), step(uv.y, hill));
    vec2 q = abs(p - vec2(-0.25, -0.05)) - vec2(0.14, 0.12);
    col = mix(col, vec3(0.85, 0.15, 0.15), step(max(q.x, q.y), 0.0));
    col = mix(col, vec3(1.0, 1.0, 1.0), step(0.5, fract(uv.x * 14.0)) * step(abs(uv.y - 0.12), 0.04));
    return col;
}

float bayer4(vec2 p)
{
    vec2 q = mod(floor(p), 4.0);
    float x = q.x, y = q.y;
    float v = 0.0;
    // 4x4 Bayer matrix thresholds, 0..15
    if (y < 0.5) v = (x < 0.5) ? 0.0 : (x < 1.5) ? 8.0 : (x < 2.5) ? 2.0 : 10.0;
    else if (y < 1.5) v = (x < 0.5) ? 12.0 : (x < 1.5) ? 4.0 : (x < 2.5) ? 14.0 : 6.0;
    else if (y < 2.5) v = (x < 0.5) ? 3.0 : (x < 1.5) ? 11.0 : (x < 2.5) ? 1.0 : 9.0;
    else v = (x < 0.5) ? 15.0 : (x < 1.5) ? 7.0 : (x < 2.5) ? 13.0 : 5.0;
    return (v + 0.5) / 16.0;
}

vec4 frag(vec2 uv)
{
    vec2 pix = floor(uv * vec2(200.0, 150.0));
    float l = dot(scene(uv), vec3(0.299, 0.587, 0.114));
    float v = step(bayer4(pix), l);
    return vec4(v, v, v, 1.0);
}
WGSL WebGPU, Bevy
fn scene(uv: vec2f) -> vec3f
{
    var col = mix(vec3f(0.95, 0.75, 0.45), vec3f(0.25, 0.45, 0.85), uv.y);
    let p = (uv - 0.5) * vec2f(aspect, 1.0);
    col = mix(col, vec3f(1.0, 0.95, 0.7), step(length(p - vec2f(0.3, 0.2)), 0.12));
    let hill = 0.3 + 0.08 * sin(uv.x * 9.0);
    col = mix(col, vec3f(0.1, 0.35, 0.2), step(uv.y, hill));
    let q = abs(p - vec2f(-0.25, -0.05)) - vec2f(0.14, 0.12);
    col = mix(col, vec3f(0.85, 0.15, 0.15), step(max(q.x, q.y), 0.0));
    col = mix(col, vec3f(1.0, 1.0, 1.0), step(0.5, fract(uv.x * 14.0)) * step(abs(uv.y - 0.12), 0.04));
    return col;
}

fn bayer4(p: vec2f) -> f32
{
    let q = (floor(p) % 4.0);
    let x = q.x; let y = q.y;
    var v = 0.0;
    // 4x4 Bayer matrix thresholds, 0..15
    if (y < 0.5) { v = select(select(select(10.0, 2.0, (x < 2.5)), 8.0, (x < 1.5)), 0.0, (x < 0.5)); }
    else if (y < 1.5) { v = select(select(select(6.0, 14.0, (x < 2.5)), 4.0, (x < 1.5)), 12.0, (x < 0.5)); }
    else if (y < 2.5) { v = select(select(select(9.0, 1.0, (x < 2.5)), 11.0, (x < 1.5)), 3.0, (x < 0.5)); }
    else { v = select(select(select(5.0, 13.0, (x < 2.5)), 7.0, (x < 1.5)), 15.0, (x < 0.5)); }
    return (v + 0.5) / 16.0;
}

fn frag(uv: vec2f) -> vec4f
{
    let pix = floor(uv * vec2f(200.0, 150.0));
    let l = dot(scene(uv), vec3f(0.299, 0.587, 0.114));
    let v = step(bayer4(pix), l);
    return vec4f(v, v, v, 1.0);
}
Godot Godot 4 shading language
shader_type canvas_item;

vec3 scene(vec2 uv)
{
    vec3 col = mix(vec3(0.95, 0.75, 0.45), vec3(0.25, 0.45, 0.85), uv.y);
    vec2 p = (uv - 0.5) * vec2(aspect, 1.0);
    col = mix(col, vec3(1.0, 0.95, 0.7), step(length(p - vec2(0.3, 0.2)), 0.12));
    float hill = 0.3 + 0.08 * sin(uv.x * 9.0);
    col = mix(col, vec3(0.1, 0.35, 0.2), step(uv.y, hill));
    vec2 q = abs(p - vec2(-0.25, -0.05)) - vec2(0.14, 0.12);
    col = mix(col, vec3(0.85, 0.15, 0.15), step(max(q.x, q.y), 0.0));
    col = mix(col, vec3(1.0, 1.0, 1.0), step(0.5, fract(uv.x * 14.0)) * step(abs(uv.y - 0.12), 0.04));
    return col;
}

float bayer4(vec2 p)
{
    vec2 q = mod(floor(p), 4.0);
    float x = q.x, y = q.y;
    float v = 0.0;
    // 4x4 Bayer matrix thresholds, 0..15
    if (y < 0.5) v = (x < 0.5) ? 0.0 : (x < 1.5) ? 8.0 : (x < 2.5) ? 2.0 : 10.0;
    else if (y < 1.5) v = (x < 0.5) ? 12.0 : (x < 1.5) ? 4.0 : (x < 2.5) ? 14.0 : 6.0;
    else if (y < 2.5) v = (x < 0.5) ? 3.0 : (x < 1.5) ? 11.0 : (x < 2.5) ? 1.0 : 9.0;
    else v = (x < 0.5) ? 15.0 : (x < 1.5) ? 7.0 : (x < 2.5) ? 13.0 : 5.0;
    return (v + 0.5) / 16.0;
}

vec4 frag(vec2 uv)
{
    vec2 pix = floor(uv * vec2(200.0, 150.0));
    float l = dot(scene(uv), vec3(0.299, 0.587, 0.114));
    float v = step(bayer4(pix), l);
    return vec4(v, v, v, 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
float3 scene(float2 uv)
{
    float3 col = mix(float3(0.95, 0.75, 0.45), float3(0.25, 0.45, 0.85), uv.y);
    float2 p = (uv - 0.5) * float2(aspect, 1.0);
    col = mix(col, float3(1.0, 0.95, 0.7), step(length(p - float2(0.3, 0.2)), 0.12));
    float hill = 0.3 + 0.08 * sin(uv.x * 9.0);
    col = mix(col, float3(0.1, 0.35, 0.2), step(uv.y, hill));
    float2 q = abs(p - float2(-0.25, -0.05)) - float2(0.14, 0.12);
    col = mix(col, float3(0.85, 0.15, 0.15), step(max(q.x, q.y), 0.0));
    col = mix(col, float3(1.0, 1.0, 1.0), step(0.5, fract(uv.x * 14.0)) * step(abs(uv.y - 0.12), 0.04));
    return col;
}

float bayer4(float2 p)
{
    float2 q = fmod(floor(p), 4.0);
    float x = q.x, y = q.y;
    float v = 0.0;
    // 4x4 Bayer matrix thresholds, 0..15
    if (y < 0.5) v = (x < 0.5) ? 0.0 : (x < 1.5) ? 8.0 : (x < 2.5) ? 2.0 : 10.0;
    else if (y < 1.5) v = (x < 0.5) ? 12.0 : (x < 1.5) ? 4.0 : (x < 2.5) ? 14.0 : 6.0;
    else if (y < 2.5) v = (x < 0.5) ? 3.0 : (x < 1.5) ? 11.0 : (x < 2.5) ? 1.0 : 9.0;
    else v = (x < 0.5) ? 15.0 : (x < 1.5) ? 7.0 : (x < 2.5) ? 13.0 : 5.0;
    return (v + 0.5) / 16.0;
}

float4 frag(float2 uv)
{
    float2 pix = floor(uv * float2(200.0, 150.0));
    float l = dot(scene(uv), float3(0.299, 0.587, 0.114));
    float v = step(bayer4(pix), l);
    return float4(v, v, v, 1.0);
}

Open it in the editor and change it

Helper functions provided

The toolbox has: scene(), bayer4(). 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 ordered dithering shader?

Retro 1-bit look: black or white per pixel by comparing brightness to a Bayer matrix. A good first step: brightness above the threshold = white, below = black. Because the threshold pattern repeats, gradients turn into dot patterns. 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 ordered dithering shader use?

It is built from step. Each one is a small idea you can reuse in other effects.

Does this ordered dithering 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.