World-space scan
Light up or reveal surfaces by their distance from a point or a plane in the world, as a pulse that sweeps through the scene.
Cards
One live example to tweak, learn and copy.
How it works
A scanner pulse in a game, the sonar ring that shows hidden enemies, a hologram that builds itself up from the floor: each one marks surfaces by where they are in the world, not by where they sit on their own texture. A ring of light that expands from a point crosses a wall, a crate and a character in one continuous line, because all three are measured against the same point.
World position in the fragment shader
The fragment shader doesn’t know where its pixel is in the world unless someone tells it. The
vertex shaderThe GPU program that runs once for every vertex of a mesh. It places the vertex on screen by writing gl_Position, and passes values on to the fragment shader. Vertex shader The GPU program that runs once for every vertex of a mesh. It places the vertex on screen by writing gl_Position, and passes values on to the fragment shader. Varying A value the vertex shader writes for each vertex (an out) and the fragment shader reads (an in). The rasterizer blends it across the triangle, so each pixel gets its own mix.VERTEX in view space, and its inverse view matrix turns it back into world
space; in a node editor it is the “world position” input.
This is the coordinate spacesThe frame a position or direction is measured in, such as object, world, view, clip or screen space. Only values in the same space can be combined. Coordinate space The frame a position or direction is measured in, such as object, world, view, clip or screen space. Only values in the same space can be combined.
Distance as the shape
The shape is a distance. From a point it gives spheres that grow outward (a scanner, a shockwave, a sonar). From a
plane, a dot product with the plane’s normal, it gives a flat front that sweeps up or across (a build-up from the
floor, a security laser). A radius that grows with time and wraps with Smoothstep A soft threshold. It is 0 below the first edge, 1 above the second, and follows an S-shaped curve with flat ends in between.fract is a periodic
pulse, and smoothstepA soft threshold. It is 0 below the first edge, 1 above the second, and follows an S-shaped curve with flat ends in between.t = clamp((x − e0) / (e1 − e0), 0, 1); s = t² (3 − 2t)
Revealing, not only highlighting
Written as an emissive, additive layer, the scan lights up objects that are already there. Written as alpha clipDiscarding every fragment whose alpha is below a threshold and drawing the rest fully opaque. The surface stays in the opaque pass, writes depth and needs no sorting, but its edges are hard. Alpha clip Discarding every fragment whose alpha is below a threshold and drawing the rest fully opaque. The surface stays in the opaque pass, writes depth and needs no sorting, but its edges are hard.if (alpha < threshold) discard;, the
same shell becomes a reveal edge: everything inside the radius exists, everything outside is discarded, which is how
“materializing” effects build an object out of thin air. The distance function stays the same in both; only the
output changes.
Further reading
- sin (opens in a new tab)Reference
- Sine wave (opens in a new tab)Reference
- Shaping functions (opens in a new tab)Book
- Coordinate Systems (opens in a new tab)Article
- OpenGL Transformation (opens in a new tab)Article
- gl_FragCoord (opens in a new tab)Reference
- step (opens in a new tab)Reference
- smoothstep (opens in a new tab)Reference
- The OpenGL ES Shading Language, version 3.00 (opens in a new tab)Docs
- Spatial shaders (opens in a new tab)Docs
- GeometryInstance3D (opens in a new tab)Reference
- distance (opens in a new tab)Reference
- exp (opens in a new tab)Reference
- fract (opens in a new tab)Reference