Medium Coding adventuresAnimated
Field of view with a wall
A stealth-game vision cone: the guard sees in a cone that sweeps left and right, only up to a distance, and cannot see behind the grey pillar.
About the Field of view with a wall shader
A stealth-game vision cone: the guard sees in a cone that sweeps left and right, only up to a distance, and cannot see behind the grey pillar. 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 953 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
- Space
p = (uv - 0.5) * float2(aspect, 1.0)- Guard
g = float2(-0.5, -?), facing angle ? * sin(time * ?) from the x axis: dir = float2(cos(a), sin(a))- Cone
v = p - g; inCone = smoothstep(?, ?, dot(normalize(v), dir)) (cos of the half-angle is about ?); range = 1.0 - smoothstep(?, ?, length(v))- Pillar
center float2(?, 0.0), radius 0.12. A pixel is hidden if the line from the guard to the pixel passes closer than ? to the center: hidden = step(segDist(g, p, c), ?) (segDist is in the Toolbox)- Floor
? + ? * checker, checker = step(0.5, frac((floor(p.x * ?) + floor(p.y * ?)) * 0.5)); float3 floor = float3(floor, floor, floor * ?)- Light
floor + float3(1.0, 0.95, 0.6) * ? * inCone * range * (1.0 - hidden)- Shapes
pillar disc float3(0.45, 0.45, 0.5), guard dot radius ? float3(1.0, 1.0, 1.0)
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 field of view with a wall shader
- The cone is a comparison of angles: the dot product of the direction to the pixel and the facing direction is the cosine of the angle between them.
- A pillar hides everything behind it as seen from the guard: test the segment from the guard to the pixel against the pillar circle.
- Draw the pillar and guard last so they sit on top of the light.
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.
Field of view with a 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: segDist()
float segDist(float2 a, float2 b, float2 c)
{
float2 ab = b - a;
float t = saturate(dot(c - a, ab) / dot(ab, ab));
return length(a + ab * t - c);
} - 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);
float2 g = float2(-0.5, -0.1);
float a = 0.35 * sin(time * 0.7); - 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.
- Putting time inside sin or cos makes the value swing back and forth, which is what animates the effect.
- time is the number of seconds since the shader started, so anything that uses it moves.
Part 3: Build the pattern
Still inside frag()
float2 dir = float2(cos(a), sin(a));
float2 v = p - g;
float inCone = smoothstep(0.87, 0.89, dot(normalize(v), dir)); - smoothstep(a, b, x) turns x into a smooth 0-to-1 value between a and b. It is the soft edge or mask.
- normalize() scales a direction to length 1, so only its direction matters.
Part 4: Build the pattern (continued)
Still inside frag()
float range = 1.0 - smoothstep(0.55, 0.95, length(v));
float2 pc = float2(0.1, 0.0);
float hidden = step(segDist(g, p, pc), 0.12); - length() is the distance from the origin, the starting point of discs, rings and glows.
- smoothstep(a, b, x) turns x into a smooth 0-to-1 value between a and b. It is the soft edge or mask.
- step(a, x) is a hard cutoff: 1 once x passes a, 0 before.
- segDist is the distance from a point to a line segment, here used to see whether something blocks the line of sight.
Part 5: Choose the colours
Still inside frag()
float checker = step(0.5, frac((floor(p.x * 8.0) + floor(p.y * 8.0)) * 0.5));
float f = 0.1 + 0.04 * checker;
float3 col = float3(f, f, f * 1.2); - 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.
Part 6: Choose the colours (continued)
Still inside frag()
col += float3(1.0, 0.95, 0.6) * 0.35 * inCone * range * (1.0 - hidden);
col = lerp(col, float3(0.45, 0.45, 0.5), 1.0 - smoothstep(0.118, 0.122, length(p - pc)));
col = lerp(col, float3(1.0, 1.0, 1.0), 1.0 - smoothstep(0.023, 0.027, length(p - g))); - 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 7: Colour the pixel and return it
Still inside frag()
return float4(col, 1.0); - The shader returns the final colour: red, green, blue, and an alpha of 1 (fully opaque).
The full code
HLSL Unity, Unreal
float segDist(float2 a, float2 b, float2 c)
{
float2 ab = b - a;
float t = saturate(dot(c - a, ab) / dot(ab, ab));
return length(a + ab * t - c);
}
float4 frag(float2 uv)
{
float2 p = (uv - 0.5) * float2(aspect, 1.0);
float2 g = float2(-0.5, -0.1);
float a = 0.35 * sin(time * 0.7);
float2 dir = float2(cos(a), sin(a));
float2 v = p - g;
float inCone = smoothstep(0.87, 0.89, dot(normalize(v), dir));
float range = 1.0 - smoothstep(0.55, 0.95, length(v));
float2 pc = float2(0.1, 0.0);
float hidden = step(segDist(g, p, pc), 0.12);
float checker = step(0.5, frac((floor(p.x * 8.0) + floor(p.y * 8.0)) * 0.5));
float f = 0.1 + 0.04 * checker;
float3 col = float3(f, f, f * 1.2);
col += float3(1.0, 0.95, 0.6) * 0.35 * inCone * range * (1.0 - hidden);
col = lerp(col, float3(0.45, 0.45, 0.5), 1.0 - smoothstep(0.118, 0.122, length(p - pc)));
col = lerp(col, float3(1.0, 1.0, 1.0), 1.0 - smoothstep(0.023, 0.027, length(p - g)));
return float4(col, 1.0);
} GLSL Shadertoy, three.js, OpenGL
float segDist(vec2 a, vec2 b, vec2 c)
{
vec2 ab = b - a;
float t = clamp(dot(c - a, ab) / dot(ab, ab), 0.0, 1.0);
return length(a + ab * t - c);
}
vec4 frag(vec2 uv)
{
vec2 p = (uv - 0.5) * vec2(aspect, 1.0);
vec2 g = vec2(-0.5, -0.1);
float a = 0.35 * sin(time * 0.7);
vec2 dir = vec2(cos(a), sin(a));
vec2 v = p - g;
float inCone = smoothstep(0.87, 0.89, dot(normalize(v), dir));
float range = 1.0 - smoothstep(0.55, 0.95, length(v));
vec2 pc = vec2(0.1, 0.0);
float hidden = step(segDist(g, p, pc), 0.12);
float checker = step(0.5, fract((floor(p.x * 8.0) + floor(p.y * 8.0)) * 0.5));
float f = 0.1 + 0.04 * checker;
vec3 col = vec3(f, f, f * 1.2);
col += vec3(1.0, 0.95, 0.6) * 0.35 * inCone * range * (1.0 - hidden);
col = mix(col, vec3(0.45, 0.45, 0.5), 1.0 - smoothstep(0.118, 0.122, length(p - pc)));
col = mix(col, vec3(1.0, 1.0, 1.0), 1.0 - smoothstep(0.023, 0.027, length(p - g)));
return vec4(col, 1.0);
} WGSL WebGPU, Bevy
fn segDist(a: vec2f, b: vec2f, c: vec2f) -> f32
{
let ab = b - a;
let t = saturate(dot(c - a, ab) / dot(ab, ab));
return length(a + ab * t - c);
}
fn frag(uv: vec2f) -> vec4f
{
let p = (uv - 0.5) * vec2f(aspect, 1.0);
let g = vec2f(-0.5, -0.1);
let a = 0.35 * sin(time * 0.7);
let dir = vec2f(cos(a), sin(a));
let v = p - g;
let inCone = smoothstep(0.87, 0.89, dot(normalize(v), dir));
let range = 1.0 - smoothstep(0.55, 0.95, length(v));
let pc = vec2f(0.1, 0.0);
let hidden = step(segDist(g, p, pc), 0.12);
let checker = step(0.5, fract((floor(p.x * 8.0) + floor(p.y * 8.0)) * 0.5));
let f = 0.1 + 0.04 * checker;
var col = vec3f(f, f, f * 1.2);
col += vec3f(1.0, 0.95, 0.6) * 0.35 * inCone * range * (1.0 - hidden);
col = mix(col, vec3f(0.45, 0.45, 0.5), 1.0 - smoothstep(0.118, 0.122, length(p - pc)));
col = mix(col, vec3f(1.0, 1.0, 1.0), 1.0 - smoothstep(0.023, 0.027, length(p - g)));
return vec4f(col, 1.0);
} Godot Godot 4 shading language
shader_type canvas_item;
float segDist(vec2 a, vec2 b, vec2 c)
{
vec2 ab = b - a;
float t = clamp(dot(c - a, ab) / dot(ab, ab), 0.0, 1.0);
return length(a + ab * t - c);
}
vec4 frag(vec2 uv)
{
vec2 p = (uv - 0.5) * vec2(aspect, 1.0);
vec2 g = vec2(-0.5, -0.1);
float a = 0.35 * sin(TIME * 0.7);
vec2 dir = vec2(cos(a), sin(a));
vec2 v = p - g;
float inCone = smoothstep(0.87, 0.89, dot(normalize(v), dir));
float range = 1.0 - smoothstep(0.55, 0.95, length(v));
vec2 pc = vec2(0.1, 0.0);
float hidden = step(segDist(g, p, pc), 0.12);
float checker = step(0.5, fract((floor(p.x * 8.0) + floor(p.y * 8.0)) * 0.5));
float f = 0.1 + 0.04 * checker;
vec3 col = vec3(f, f, f * 1.2);
col += vec3(1.0, 0.95, 0.6) * 0.35 * inCone * range * (1.0 - hidden);
col = mix(col, vec3(0.45, 0.45, 0.5), 1.0 - smoothstep(0.118, 0.122, length(p - pc)));
col = mix(col, vec3(1.0, 1.0, 1.0), 1.0 - smoothstep(0.023, 0.027, length(p - g)));
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 segDist(float2 a, float2 b, float2 c)
{
float2 ab = b - a;
float t = saturate(dot(c - a, ab) / dot(ab, ab));
return length(a + ab * t - c);
}
float4 frag(float2 uv)
{
float2 p = (uv - 0.5) * float2(aspect, 1.0);
float2 g = float2(-0.5, -0.1);
float a = 0.35 * sin(time * 0.7);
float2 dir = float2(cos(a), sin(a));
float2 v = p - g;
float inCone = smoothstep(0.87, 0.89, dot(normalize(v), dir));
float range = 1.0 - smoothstep(0.55, 0.95, length(v));
float2 pc = float2(0.1, 0.0);
float hidden = step(segDist(g, p, pc), 0.12);
float checker = step(0.5, fract((floor(p.x * 8.0) + floor(p.y * 8.0)) * 0.5));
float f = 0.1 + 0.04 * checker;
float3 col = float3(f, f, f * 1.2);
col += float3(1.0, 0.95, 0.6) * 0.35 * inCone * range * (1.0 - hidden);
col = mix(col, float3(0.45, 0.45, 0.5), 1.0 - smoothstep(0.118, 0.122, length(p - pc)));
col = mix(col, float3(1.0, 1.0, 1.0), 1.0 - smoothstep(0.023, 0.027, length(p - g)));
return float4(col, 1.0);
} Helper functions provided
The toolbox has: segDist(). 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 field of view with a wall shader?
A stealth-game vision cone: the guard sees in a cone that sweeps left and right, only up to a distance, and cannot see behind the grey pillar. A good first step: the cone is a comparison of angles: the dot product of the direction to the pixel and the facing direction is the cosine of the angle between them. 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 field of view with a wall 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 field of view with a 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.