Medium EffectsAnimated

Radar sweep

A green radar screen: rings, crosshair and a fading sweep arm.

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

About the Radar sweep shader

A green radar screen: rings, crosshair and a fading sweep arm. Visual effects built from small, reusable pieces. Every challenge lists the exact numbers, so the work is in how the code fits together.

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 482 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

Polar
r = length(p), a = atan2(p.y, p.x)
Sweep
pow(frac(-a / ? - time * ?), ?)
Rings
smoothstep(?, 0.5, abs(frac(r * ?) - 0.5))
Cross
1.0 - smoothstep(0.0, ?, min(abs(p.x), abs(p.y)))
Color
float3(0.02,0.08,0.04) + float3(0.1,0.9,0.4) * (sweep * ? + rings * ? + cross * ?)
Screen
multiply by 1.0 - smoothstep(?, ?, r)

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 radar sweep shader

  1. frac() of the angle (scaled to 0..1) plus time makes a repeating ramp that chases around the circle.
  2. Raise it to a power to make the tail fade quickly.

Techniques used

  • smoothstep: turns a number into a soft 0-to-1 transition, used for soft edges and masks.
  • Polar coordinates: angle and distance from a center, for spirals, rings and radial effects.

Radar sweep 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: Set up the coordinates

Inside float4 frag(float2 uv)

float2 p = (uv - 0.5) * float2(aspect, 1.0);
float r = length(p);
float a = atan2(p.y, p.x);
  • 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.
  • length() is the distance from the origin, the starting point of discs, rings and glows.
  • atan2 gives the angle around the centre, which is what spirals, spokes and radial patterns are built from.

Part 2: Build the pattern

Still inside frag()

float sweep = pow(frac(-a / 6.28318 - time * 0.3), 3.0);
float rings = smoothstep(0.47, 0.5, abs(frac(r * 10.0) - 0.5));
float cross = 1.0 - smoothstep(0.0, 0.004, min(abs(p.x), abs(p.y)));
  • smoothstep(a, b, x) turns x into a smooth 0-to-1 value between a and b. It is the soft edge or mask.
  • time is the number of seconds since the shader started, so anything that uses it moves.
  • floor() picks the cell a point is in and frac() is the position inside that cell. Together they repeat a pattern.
  • pow(x, k) reshapes a value: a high power keeps only the brightest part, which makes small sharp highlights.

Part 3: Colour the pixel and return it

Still inside frag()

float3 col = float3(0.02, 0.08, 0.04) + float3(0.1, 0.9, 0.4) * (sweep * 0.8 + rings * 0.25 + cross * 0.2);
col *= 1.0 - smoothstep(0.44, 0.45, r);
return float4(col, 1.0);
  • smoothstep(a, b, x) turns x into a smooth 0-to-1 value between a and b. It is the soft edge or mask.
  • 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
float4 frag(float2 uv)
{
    float2 p = (uv - 0.5) * float2(aspect, 1.0);
    float r = length(p);
    float a = atan2(p.y, p.x);
    float sweep = pow(frac(-a / 6.28318 - time * 0.3), 3.0);
    float rings = smoothstep(0.47, 0.5, abs(frac(r * 10.0) - 0.5));
    float cross = 1.0 - smoothstep(0.0, 0.004, min(abs(p.x), abs(p.y)));
    float3 col = float3(0.02, 0.08, 0.04) + float3(0.1, 0.9, 0.4) * (sweep * 0.8 + rings * 0.25 + cross * 0.2);
    col *= 1.0 - smoothstep(0.44, 0.45, r);
    return float4(col, 1.0);
}
GLSL Shadertoy, three.js, OpenGL
vec4 frag(vec2 uv)
{
    vec2 p = (uv - 0.5) * vec2(aspect, 1.0);
    float r = length(p);
    float a = atan(p.y, p.x);
    float sweep = pow(fract(-a / 6.28318 - time * 0.3), 3.0);
    float rings = smoothstep(0.47, 0.5, abs(fract(r * 10.0) - 0.5));
    float cross = 1.0 - smoothstep(0.0, 0.004, min(abs(p.x), abs(p.y)));
    vec3 col = vec3(0.02, 0.08, 0.04) + vec3(0.1, 0.9, 0.4) * (sweep * 0.8 + rings * 0.25 + cross * 0.2);
    col *= 1.0 - smoothstep(0.44, 0.45, r);
    return vec4(col, 1.0);
}
WGSL WebGPU, Bevy
fn frag(uv: vec2f) -> vec4f
{
    let p = (uv - 0.5) * vec2f(aspect, 1.0);
    let r = length(p);
    let a = atan2(p.y, p.x);
    let sweep = pow(fract(-a / 6.28318 - time * 0.3), 3.0);
    let rings = smoothstep(0.47, 0.5, abs(fract(r * 10.0) - 0.5));
    let cross = 1.0 - smoothstep(0.0, 0.004, min(abs(p.x), abs(p.y)));
    var col = vec3f(0.02, 0.08, 0.04) + vec3f(0.1, 0.9, 0.4) * (sweep * 0.8 + rings * 0.25 + cross * 0.2);
    col *= 1.0 - smoothstep(0.44, 0.45, r);
    return vec4f(col, 1.0);
}
Godot Godot 4 shading language
shader_type canvas_item;

vec4 frag(vec2 uv)
{
    vec2 p = (uv - 0.5) * vec2(aspect, 1.0);
    float r = length(p);
    float a = atan(p.y, p.x);
    float sweep = pow(fract(-a / 6.28318 - TIME * 0.3), 3.0);
    float rings = smoothstep(0.47, 0.5, abs(fract(r * 10.0) - 0.5));
    float cross = 1.0 - smoothstep(0.0, 0.004, min(abs(p.x), abs(p.y)));
    vec3 col = vec3(0.02, 0.08, 0.04) + vec3(0.1, 0.9, 0.4) * (sweep * 0.8 + rings * 0.25 + cross * 0.2);
    col *= 1.0 - smoothstep(0.44, 0.45, r);
    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
float4 frag(float2 uv)
{
    float2 p = (uv - 0.5) * float2(aspect, 1.0);
    float r = length(p);
    float a = atan2(p.y, p.x);
    float sweep = pow(fract(-a / 6.28318 - time * 0.3), 3.0);
    float rings = smoothstep(0.47, 0.5, abs(fract(r * 10.0) - 0.5));
    float cross = 1.0 - smoothstep(0.0, 0.004, min(abs(p.x), abs(p.y)));
    float3 col = float3(0.02, 0.08, 0.04) + float3(0.1, 0.9, 0.4) * (sweep * 0.8 + rings * 0.25 + cross * 0.2);
    col *= 1.0 - smoothstep(0.44, 0.45, r);
    return float4(col, 1.0);
}

Open it in the editor and change it

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 radar sweep shader?

A green radar screen: rings, crosshair and a fading sweep arm. A good first step: frac() of the angle (scaled to 0..1) plus time makes a repeating ramp that chases around the circle. 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 radar sweep shader use?

It is built from smoothstep, polar coordinates. Each one is a small idea you can reuse in other effects.

Does this radar sweep 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.