GLSL Learning

GLSL shader example

Circle

Draw a crisp, anti-aliased circle with a signed distance function, length() and smoothstep().

Live example

Edit the code and the preview updates as you type.

How it works

Every pixel asks one question: how far am I from the center? length(p) gives that distance. Subtracting the radius turns it into a signed distance: negative inside the circle, zero exactly on the edge, positive outside.

smoothstep(-px, px, d) turns that distance into a 0..1 mask. px is the size of one pixel, so the edge fades over about two pixels. That is what removes the jagged staircase you get with a hard step().

The first line maps pixel coordinates to a range where the center is (0, 0) and the short side of the canvas goes from -1 to 1. Without dividing by the short side, the circle would stretch into an ellipse on a wide canvas.

Try changing it

ChangeResult
0.6 to 0.3A smaller circle (radius is 30% of the half-height)
px to 0.2A soft, blurry glow instead of a crisp edge
length(p) to length(p * vec2(1.0, 2.0))An ellipse squashed vertically
1.0 - smoothstep(...) to smoothstep(...)The colors swap: a hole in a filled background

Built-in functions used

Learn it step by step