GLSL shader example

SDF Subtraction: A Circular Cutout

Subtraction uses max(a,-b): stay inside the box while excluding the circle.

Live example

Edit the code and the preview updates as you type.

What to observe

SDF Subtraction: A Circular Cutout.

Mechanism

Subtraction uses max(a,-b): stay inside the box while excluding the circle.

The controlling code

float a=box(p,vec2(.55)),b=length(p-vec2(sin(u_time*.6)*.4,0))-(.15+.25*u_parameter);float d=max(a,-b);c=mix(c,vec3(.7,.4,1.),1.-smoothstep(-aa,aa,d));

Experiment

Set the hole radius .15+.25*u_parameter to .4. The moving circular cutout grows, while the purple box still uses vec2(.55) as its half-size.

Debugging and cost

The combined field max(a,-b) keeps the box interior only outside the circle. Replace it with max(a,b) to switch from subtraction to intersection, showing only the portion inside both shapes with the same two distance evaluations.

Built-in functions used

Learn it step by step