Medium Coding adventures

Scattered dots (jittered grid)

Random-looking dots that never overlap: split the picture into cells and put one dot with a random size and color somewhere inside every cell. This is the cheap cousin of Poisson-disc sampling.

The target picture for the shader challenge "Scattered dots (jittered grid)"
Recreate this picture. You pass at a 99% match, then try to write it in fewer characters than the par.

About the Scattered dots (jittered grid) shader

Random-looking dots that never overlap: split the picture into cells and put one dot with a random size and color somewhere inside every cell. This is the cheap cousin of Poisson-disc sampling. Experiments in the spirit of well-known open-source graphics projects: a software rasterizer, a ray tracer, procedural planets, fluid particles and more, rebuilt as pixel shaders.

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 540 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
g = uv * float2(?, ?); cell = floor(g); f = frac(g)
Position
center = ? + ? * float2(hash(cell + 1.0), hash(cell + ?)): kept ? away from the cell border so neighbouring dots cannot touch
Size
radius = ? + ? * hash(cell + ?)
Dot
1.0 - smoothstep(radius - ?, radius, length(f - center))
Colors
background float3(0.96, 0.93, 0.86); the dot lerps from float3(0.9, 0.35, 0.3) to float3(0.2, 0.45, 0.75) by hash(cell + ?)

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 scattered dots (jittered grid) shader

  1. hash(cell + n) gives a different random number for each n, so add different offsets for position, size and color.
  2. Because the center stays inside the cell and the radius is smaller than the distance to the border, dots never overlap: no neighbour search needed.
  3. Real Poisson-disc sampling allows any position and checks the distance to other dots, which is slower but looks more natural.

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.
  • lerp (mix): blend between two colors or values.

Scattered dots (jittered grid) 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: hash()

float hash(float2 p)
{
    return frac(sin(dot(p, float2(127.1, 311.7))) * 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(10.0, 7.5);
float2 cell = 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()

float2 center = 0.3 + 0.4 * float2(hash(cell + 1.0), hash(cell + 7.0));
float radius = 0.08 + 0.12 * hash(cell + 3.0);
float dotm = 1.0 - smoothstep(radius - 0.02, radius, length(f - center));
  • A hash turns a position into a repeatable pseudo-random number, so every cell can get its own random value.
  • smoothstep(a, b, x) turns x into a smooth 0-to-1 value between a and b. It is the soft edge or mask.

Part 4: Colour the pixel and return it

Still inside frag()

float3 dotcol = lerp(float3(0.9, 0.35, 0.3), float3(0.2, 0.45, 0.75), hash(cell + 11.0));
return float4(lerp(float3(0.96, 0.93, 0.86), dotcol, dotm), 1.0);
  • A hash turns a position into a repeatable pseudo-random number, so every cell can get its own random value.
  • 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 hash(float2 p)
{
    return frac(sin(dot(p, float2(127.1, 311.7))) * 43758.5453);
}

float4 frag(float2 uv)
{
    float2 g = uv * float2(10.0, 7.5);
    float2 cell = floor(g);
    float2 f = frac(g);
    float2 center = 0.3 + 0.4 * float2(hash(cell + 1.0), hash(cell + 7.0));
    float radius = 0.08 + 0.12 * hash(cell + 3.0);
    float dotm = 1.0 - smoothstep(radius - 0.02, radius, length(f - center));
    float3 dotcol = lerp(float3(0.9, 0.35, 0.3), float3(0.2, 0.45, 0.75), hash(cell + 11.0));
    return float4(lerp(float3(0.96, 0.93, 0.86), dotcol, dotm), 1.0);
}
GLSL Shadertoy, three.js, OpenGL
float hash(vec2 p)
{
    return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453);
}

vec4 frag(vec2 uv)
{
    vec2 g = uv * vec2(10.0, 7.5);
    vec2 cell = floor(g);
    vec2 f = fract(g);
    vec2 center = 0.3 + 0.4 * vec2(hash(cell + 1.0), hash(cell + 7.0));
    float radius = 0.08 + 0.12 * hash(cell + 3.0);
    float dotm = 1.0 - smoothstep(radius - 0.02, radius, length(f - center));
    vec3 dotcol = mix(vec3(0.9, 0.35, 0.3), vec3(0.2, 0.45, 0.75), hash(cell + 11.0));
    return vec4(mix(vec3(0.96, 0.93, 0.86), dotcol, dotm), 1.0);
}
WGSL WebGPU, Bevy
fn hash(p: vec2f) -> f32
{
    return fract(sin(dot(p, vec2f(127.1, 311.7))) * 43758.5453);
}

fn frag(uv: vec2f) -> vec4f
{
    let g = uv * vec2f(10.0, 7.5);
    let cell = floor(g);
    let f = fract(g);
    let center = 0.3 + 0.4 * vec2f(hash(cell + 1.0), hash(cell + 7.0));
    let radius = 0.08 + 0.12 * hash(cell + 3.0);
    let dotm = 1.0 - smoothstep(radius - 0.02, radius, length(f - center));
    let dotcol = mix(vec3f(0.9, 0.35, 0.3), vec3f(0.2, 0.45, 0.75), hash(cell + 11.0));
    return vec4f(mix(vec3f(0.96, 0.93, 0.86), dotcol, dotm), 1.0);
}
Godot Godot 4 shading language
shader_type canvas_item;

float hash(vec2 p)
{
    return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453);
}

vec4 frag(vec2 uv)
{
    vec2 g = uv * vec2(10.0, 7.5);
    vec2 cell = floor(g);
    vec2 f = fract(g);
    vec2 center = 0.3 + 0.4 * vec2(hash(cell + 1.0), hash(cell + 7.0));
    float radius = 0.08 + 0.12 * hash(cell + 3.0);
    float dotm = 1.0 - smoothstep(radius - 0.02, radius, length(f - center));
    vec3 dotcol = mix(vec3(0.9, 0.35, 0.3), vec3(0.2, 0.45, 0.75), hash(cell + 11.0));
    return vec4(mix(vec3(0.96, 0.93, 0.86), dotcol, dotm), 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 hash(float2 p)
{
    return fract(sin(dot(p, float2(127.1, 311.7))) * 43758.5453);
}

float4 frag(float2 uv)
{
    float2 g = uv * float2(10.0, 7.5);
    float2 cell = floor(g);
    float2 f = fract(g);
    float2 center = 0.3 + 0.4 * float2(hash(cell + 1.0), hash(cell + 7.0));
    float radius = 0.08 + 0.12 * hash(cell + 3.0);
    float dotm = 1.0 - smoothstep(radius - 0.02, radius, length(f - center));
    float3 dotcol = mix(float3(0.9, 0.35, 0.3), float3(0.2, 0.45, 0.75), hash(cell + 11.0));
    return float4(mix(float3(0.96, 0.93, 0.86), dotcol, dotm), 1.0);
}

Open it in the editor and change it

Helper functions provided

The toolbox has: hash(). 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 scattered dots (jittered grid) shader?

Random-looking dots that never overlap: split the picture into cells and put one dot with a random size and color somewhere inside every cell. A good first step: hash(cell + n) gives a different random number for each n, so add different offsets for position, size and color. 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 scattered dots (jittered grid) shader use?

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

Does this scattered dots (jittered grid) 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.