Hard RaymarchAnimated
Marching squares
Draw the contour of a moving scalar field with the marching-squares idea: for each cell, look at the 4 corners and draw a segment across it.
About the Marching squares shader
Draw the contour of a moving scalar field with the marching-squares idea: for each cell, look at the 4 corners and draw a segment across it. Ray marching draws 3D scenes inside a pixel shader by walking a ray through a distance function. No triangles needed.
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 643 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
- Field
uses field, sin- Grid
uses floor, frac- Corners
uses field- Fill
a plain value or condition- Colors
uses step · colours float3(0.2, 0.7, 0.9), float3(0.07, 0.08, 0.14)- Mix
uses lerp, smoothstep
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 marching squares shader
- Sebastian Lague's marching cubes video explains the 3D version; here you do the 2D picture: sample a field at grid corners, interpolate inside each cell.
- bilinear: lerp(lerp(a, b, f.x), lerp(c, d, f.x), f.y) with a,b,c,d the four corner field values. (Same as value noise!)
Techniques used
- 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.
- step: a hard 0-or-1 cutoff for crisp shapes and thresholds.
- lerp (mix): blend between two colors or values.
Marching squares 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: field()
float field(float2 q)
{
return sin(q.x * 0.9 + time) + sin(q.y * 1.1 - time * 0.8) + sin((q.x + q.y) * 0.6 + time * 0.5);
} - 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 g = uv * float2(24.0, 18.0);
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: Choose the colours
Still inside frag()
float a = field(id), b = field(id + float2(1.0, 0.0)), c = field(id + float2(0.0, 1.0)), d = field(id + float2(1.0, 1.0));
float v = lerp(lerp(a, b, f.x), lerp(c, d, f.x), f.y);
float3 col = lerp(float3(0.07, 0.08, 0.14), float3(0.2, 0.7, 0.9), 1.0 - smoothstep(-0.05, 0.05, v)); - 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 4: Colour the pixel and return it
Still inside frag()
float gridLine = saturate(step(f.x, 0.03) + step(f.y, 0.03));
col += float3(0.2, 0.2, 0.2) * gridLine * 0.15;
return float4(col, 1.0); - step(a, x) is a hard cutoff: 1 once x passes a, 0 before.
- saturate() clamps a value into the 0 to 1 range.
- 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 field(float2 q)
{
return sin(q.x * 0.9 + time) + sin(q.y * 1.1 - time * 0.8) + sin((q.x + q.y) * 0.6 + time * 0.5);
}
float4 frag(float2 uv)
{
float2 g = uv * float2(24.0, 18.0);
float2 id = floor(g);
float2 f = frac(g);
float a = field(id), b = field(id + float2(1.0, 0.0)), c = field(id + float2(0.0, 1.0)), d = field(id + float2(1.0, 1.0));
float v = lerp(lerp(a, b, f.x), lerp(c, d, f.x), f.y);
float3 col = lerp(float3(0.07, 0.08, 0.14), float3(0.2, 0.7, 0.9), 1.0 - smoothstep(-0.05, 0.05, v));
float gridLine = saturate(step(f.x, 0.03) + step(f.y, 0.03));
col += float3(0.2, 0.2, 0.2) * gridLine * 0.15;
return float4(col, 1.0);
} GLSL Shadertoy, three.js, OpenGL
float field(vec2 q)
{
return sin(q.x * 0.9 + time) + sin(q.y * 1.1 - time * 0.8) + sin((q.x + q.y) * 0.6 + time * 0.5);
}
vec4 frag(vec2 uv)
{
vec2 g = uv * vec2(24.0, 18.0);
vec2 id = floor(g);
vec2 f = fract(g);
float a = field(id), b = field(id + vec2(1.0, 0.0)), c = field(id + vec2(0.0, 1.0)), d = field(id + vec2(1.0, 1.0));
float v = mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
vec3 col = mix(vec3(0.07, 0.08, 0.14), vec3(0.2, 0.7, 0.9), 1.0 - smoothstep(-0.05, 0.05, v));
float gridLine = clamp(step(f.x, 0.03) + step(f.y, 0.03), 0.0, 1.0);
col += vec3(0.2, 0.2, 0.2) * gridLine * 0.15;
return vec4(col, 1.0);
} WGSL WebGPU, Bevy
fn field(q: vec2f) -> f32
{
return sin(q.x * 0.9 + time) + sin(q.y * 1.1 - time * 0.8) + sin((q.x + q.y) * 0.6 + time * 0.5);
}
fn frag(uv: vec2f) -> vec4f
{
let g = uv * vec2f(24.0, 18.0);
let id = floor(g);
let f = fract(g);
let a = field(id); let b = field(id + vec2f(1.0, 0.0)); let c = field(id + vec2f(0.0, 1.0)); let d = field(id + vec2f(1.0, 1.0));
let v = mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
var col = mix(vec3f(0.07, 0.08, 0.14), vec3f(0.2, 0.7, 0.9), 1.0 - smoothstep(-0.05, 0.05, v));
let gridLine = saturate(step(f.x, 0.03) + step(f.y, 0.03));
col += vec3f(0.2, 0.2, 0.2) * gridLine * 0.15;
return vec4f(col, 1.0);
} Godot Godot 4 shading language
shader_type canvas_item;
float field(vec2 q)
{
return sin(q.x * 0.9 + TIME) + sin(q.y * 1.1 - TIME * 0.8) + sin((q.x + q.y) * 0.6 + TIME * 0.5);
}
vec4 frag(vec2 uv)
{
vec2 g = uv * vec2(24.0, 18.0);
vec2 id = floor(g);
vec2 f = fract(g);
float a = field(id), b = field(id + vec2(1.0, 0.0)), c = field(id + vec2(0.0, 1.0)), d = field(id + vec2(1.0, 1.0));
float v = mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
vec3 col = mix(vec3(0.07, 0.08, 0.14), vec3(0.2, 0.7, 0.9), 1.0 - smoothstep(-0.05, 0.05, v));
float gridLine = clamp(step(f.x, 0.03) + step(f.y, 0.03), 0.0, 1.0);
col += vec3(0.2, 0.2, 0.2) * gridLine * 0.15;
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 field(float2 q)
{
return sin(q.x * 0.9 + time) + sin(q.y * 1.1 - time * 0.8) + sin((q.x + q.y) * 0.6 + time * 0.5);
}
float4 frag(float2 uv)
{
float2 g = uv * float2(24.0, 18.0);
float2 id = floor(g);
float2 f = fract(g);
float a = field(id), b = field(id + float2(1.0, 0.0)), c = field(id + float2(0.0, 1.0)), d = field(id + float2(1.0, 1.0));
float v = mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
float3 col = mix(float3(0.07, 0.08, 0.14), float3(0.2, 0.7, 0.9), 1.0 - smoothstep(-0.05, 0.05, v));
float gridLine = saturate(step(f.x, 0.03) + step(f.y, 0.03));
col += float3(0.2, 0.2, 0.2) * gridLine * 0.15;
return float4(col, 1.0);
} 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 marching squares shader?
Draw the contour of a moving scalar field with the marching-squares idea: for each cell, look at the 4 corners and draw a segment across it. A good first step: sebastian Lague's marching cubes video explains the 3D version; here you do the 2D picture: sample a field at grid corners, interpolate inside each cell. 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 marching squares shader use?
It is built from smoothstep, tiling with floor and frac, sine waves, step, lerp (mix). Each one is a small idea you can reuse in other effects.
Does this marching squares 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.