Easy 2D
Traffic light
A dark box with three round lights: red, yellow and green.
About the Traffic light shader
A dark box with three round lights: red, yellow and green. Two-dimensional shapes and compositions: circles, boxes, lines and how to combine them.
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 528 characters in Unity (Built-in)) and GPU speed. Work in HLSL, GLSL, WGSL, Metal or Godot and export the result when you are done.
The numbers you need
- Background
float3(0.12, 0.12, 0.15)- Box
half-size (0.14, 0.42), color float3(0.2, 0.2, 0.22)- Lights
radius 0.1 (edge from 0.09), at y = +0.28, 0.0, -0.28- Colors
red (0.95,0.2,0.15), yellow (0.98,0.8,0.1), green (0.2,0.85,0.35)
How to make a traffic light shader
- A small disc() helper makes this short: 1 - smoothstep(r - 0.01, r, length(p - center)).
- Layer each shape over the previous with lerp(col, color, mask).
Techniques used
- smoothstep: turns a number into a soft 0-to-1 transition, used for soft edges and masks.
- step: a hard 0-or-1 cutoff for crisp shapes and thresholds.
- lerp (mix): blend between two colors or values.
Traffic light 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: disc()
float disc(float2 p, float r)
{
return 1.0 - smoothstep(r - 0.01, r, length(p));
} - 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 p = (uv - 0.5) * float2(aspect, 1.0);
float3 col = float3(0.12, 0.12, 0.15);
float box = step(abs(p.x), 0.14) * step(abs(p.y), 0.42); - Centre the coordinates on the middle of the picture and multiply x by the aspect ratio, so a circle stays round on a wide picture.
- Subtracting 0.5 moves the origin from the bottom-left corner to the middle of the picture.
- step(a, x) is a hard cutoff: 1 once x passes a, 0 before.
- abs() mirrors a value around zero, which gives symmetric shapes, outlines and sharp ridges.
Part 3: Choose the colours
Still inside frag()
col = lerp(col, float3(0.2, 0.2, 0.22), box);
col = lerp(col, float3(0.95, 0.2, 0.15), disc(p - float2(0.0, 0.28), 0.1));
col = lerp(col, float3(0.98, 0.8, 0.1), disc(p, 0.1)); - 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()
col = lerp(col, float3(0.2, 0.85, 0.35), disc(p + float2(0.0, 0.28), 0.1));
return float4(col, 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 disc(float2 p, float r)
{
return 1.0 - smoothstep(r - 0.01, r, length(p));
}
float4 frag(float2 uv)
{
float2 p = (uv - 0.5) * float2(aspect, 1.0);
float3 col = float3(0.12, 0.12, 0.15);
float box = step(abs(p.x), 0.14) * step(abs(p.y), 0.42);
col = lerp(col, float3(0.2, 0.2, 0.22), box);
col = lerp(col, float3(0.95, 0.2, 0.15), disc(p - float2(0.0, 0.28), 0.1));
col = lerp(col, float3(0.98, 0.8, 0.1), disc(p, 0.1));
col = lerp(col, float3(0.2, 0.85, 0.35), disc(p + float2(0.0, 0.28), 0.1));
return float4(col, 1.0);
} GLSL Shadertoy, three.js, OpenGL
float disc(vec2 p, float r)
{
return 1.0 - smoothstep(r - 0.01, r, length(p));
}
vec4 frag(vec2 uv)
{
vec2 p = (uv - 0.5) * vec2(aspect, 1.0);
vec3 col = vec3(0.12, 0.12, 0.15);
float box = step(abs(p.x), 0.14) * step(abs(p.y), 0.42);
col = mix(col, vec3(0.2, 0.2, 0.22), box);
col = mix(col, vec3(0.95, 0.2, 0.15), disc(p - vec2(0.0, 0.28), 0.1));
col = mix(col, vec3(0.98, 0.8, 0.1), disc(p, 0.1));
col = mix(col, vec3(0.2, 0.85, 0.35), disc(p + vec2(0.0, 0.28), 0.1));
return vec4(col, 1.0);
} WGSL WebGPU, Bevy
fn disc(p: vec2f, r: f32) -> f32
{
return 1.0 - smoothstep(r - 0.01, r, length(p));
}
fn frag(uv: vec2f) -> vec4f
{
let p = (uv - 0.5) * vec2f(aspect, 1.0);
var col = vec3f(0.12, 0.12, 0.15);
let box = step(abs(p.x), 0.14) * step(abs(p.y), 0.42);
col = mix(col, vec3f(0.2, 0.2, 0.22), box);
col = mix(col, vec3f(0.95, 0.2, 0.15), disc(p - vec2f(0.0, 0.28), 0.1));
col = mix(col, vec3f(0.98, 0.8, 0.1), disc(p, 0.1));
col = mix(col, vec3f(0.2, 0.85, 0.35), disc(p + vec2f(0.0, 0.28), 0.1));
return vec4f(col, 1.0);
} Godot Godot 4 shading language
shader_type canvas_item;
float disc(vec2 p, float r)
{
return 1.0 - smoothstep(r - 0.01, r, length(p));
}
vec4 frag(vec2 uv)
{
vec2 p = (uv - 0.5) * vec2(aspect, 1.0);
vec3 col = vec3(0.12, 0.12, 0.15);
float box = step(abs(p.x), 0.14) * step(abs(p.y), 0.42);
col = mix(col, vec3(0.2, 0.2, 0.22), box);
col = mix(col, vec3(0.95, 0.2, 0.15), disc(p - vec2(0.0, 0.28), 0.1));
col = mix(col, vec3(0.98, 0.8, 0.1), disc(p, 0.1));
col = mix(col, vec3(0.2, 0.85, 0.35), disc(p + vec2(0.0, 0.28), 0.1));
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 disc(float2 p, float r)
{
return 1.0 - smoothstep(r - 0.01, r, length(p));
}
float4 frag(float2 uv)
{
float2 p = (uv - 0.5) * float2(aspect, 1.0);
float3 col = float3(0.12, 0.12, 0.15);
float box = step(abs(p.x), 0.14) * step(abs(p.y), 0.42);
col = mix(col, float3(0.2, 0.2, 0.22), box);
col = mix(col, float3(0.95, 0.2, 0.15), disc(p - float2(0.0, 0.28), 0.1));
col = mix(col, float3(0.98, 0.8, 0.1), disc(p, 0.1));
col = mix(col, float3(0.2, 0.85, 0.35), disc(p + float2(0.0, 0.28), 0.1));
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 traffic light shader?
A dark box with three round lights: red, yellow and green. A good first step: a small disc() helper makes this short: 1 - smoothstep(r - 0.01, r, length(p - center)). 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 traffic light shader use?
It is built from smoothstep, step, lerp (mix). Each one is a small idea you can reuse in other effects.
Does this traffic light 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.