Medium Patterns
Brick wall
A running-bond brick wall: every other row is shifted by half a brick, each brick a slightly different red.
About the Brick wall shader
A running-bond brick wall: every other row is shifted by half a brick, each brick a slightly different red. 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 462 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
? bricks across, ? rows: g = uv * float2(?, ?)- Offset
g.x += 0.5 * fmod(row, 2.0), row = floor(g.y)- Mortar
saturate(step(f.x, ?) + step(f.y, ?))- Brick color
lerp(float3(0.65,0.25,0.18), float3(0.8,0.38,0.25), hash(float2(floor(g.x), row)))- Mortar color
float3(0.8, 0.78, 0.72)
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 brick wall shader
- Shift x by half a brick on odd rows BEFORE you take floor/frac.
- Use the per-brick id (floor(g.x), row) as the hash input for the color.
Techniques used
- A hash function: a cheap way to get a repeatable random number from a position.
- Tiling with floor and frac: repeat a pattern by splitting space into cells.
- step: a hard 0-or-1 cutoff for crisp shapes and thresholds.
- lerp (mix): blend between two colors or values.
Brick wall 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(6.0, 10.0);
float row = floor(g.y);
g.x += 0.5 * fmod(row, 2.0); - 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 f = frac(g);
float mortar = saturate(step(f.x, 0.06) + step(f.y, 0.12));
float h = hash(float2(floor(g.x), row)); - A hash turns a position into a repeatable pseudo-random number, so every cell can get its own random value.
- 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.
- saturate() clamps a value into the 0 to 1 range.
Part 4: Colour the pixel and return it
Still inside frag()
float3 brick = lerp(float3(0.65, 0.25, 0.18), float3(0.8, 0.38, 0.25), h);
return float4(lerp(brick, float3(0.8, 0.78, 0.72), mortar), 1.0); - 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(6.0, 10.0);
float row = floor(g.y);
g.x += 0.5 * fmod(row, 2.0);
float2 f = frac(g);
float mortar = saturate(step(f.x, 0.06) + step(f.y, 0.12));
float h = hash(float2(floor(g.x), row));
float3 brick = lerp(float3(0.65, 0.25, 0.18), float3(0.8, 0.38, 0.25), h);
return float4(lerp(brick, float3(0.8, 0.78, 0.72), mortar), 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(6.0, 10.0);
float row = floor(g.y);
g.x += 0.5 * mod(row, 2.0);
vec2 f = fract(g);
float mortar = clamp(step(f.x, 0.06) + step(f.y, 0.12), 0.0, 1.0);
float h = hash(vec2(floor(g.x), row));
vec3 brick = mix(vec3(0.65, 0.25, 0.18), vec3(0.8, 0.38, 0.25), h);
return vec4(mix(brick, vec3(0.8, 0.78, 0.72), mortar), 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
{
var g = uv * vec2f(6.0, 10.0);
let row = floor(g.y);
g.x += 0.5 * (row % 2.0);
let f = fract(g);
let mortar = saturate(step(f.x, 0.06) + step(f.y, 0.12));
let h = hash(vec2f(floor(g.x), row));
let brick = mix(vec3f(0.65, 0.25, 0.18), vec3f(0.8, 0.38, 0.25), h);
return vec4f(mix(brick, vec3f(0.8, 0.78, 0.72), mortar), 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(6.0, 10.0);
float row = floor(g.y);
g.x += 0.5 * mod(row, 2.0);
vec2 f = fract(g);
float mortar = clamp(step(f.x, 0.06) + step(f.y, 0.12), 0.0, 1.0);
float h = hash(vec2(floor(g.x), row));
vec3 brick = mix(vec3(0.65, 0.25, 0.18), vec3(0.8, 0.38, 0.25), h);
return vec4(mix(brick, vec3(0.8, 0.78, 0.72), mortar), 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(6.0, 10.0);
float row = floor(g.y);
g.x += 0.5 * fmod(row, 2.0);
float2 f = fract(g);
float mortar = saturate(step(f.x, 0.06) + step(f.y, 0.12));
float h = hash(float2(floor(g.x), row));
float3 brick = mix(float3(0.65, 0.25, 0.18), float3(0.8, 0.38, 0.25), h);
return float4(mix(brick, float3(0.8, 0.78, 0.72), mortar), 1.0);
} 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 codingQuestions about this shader
How do I make a brick wall shader?
A running-bond brick wall: every other row is shifted by half a brick, each brick a slightly different red. A good first step: shift x by half a brick on odd rows BEFORE you take floor/frac. 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 brick wall shader use?
It is built from hash function, tiling with floor and frac, step, lerp (mix). Each one is a small idea you can reuse in other effects.
Does this brick wall 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.