GLSL shader example

Capsule SDF Between Two Points

Clamp the segment projection to its endpoints, measure distance to the closest point, then subtract the radius.

Live example

Edit the code and the preview updates as you type.

What to observe

Capsule SDF Between Two Points.

Mechanism

Clamp the segment projection to its endpoints, measure distance to the closest point, then subtract the radius.

The controlling code

vec2 b=vec2(.15+.6*u_parameter,.3*sin(u_time*.4));float d=sdCapsule(p,vec2(-.45,-.2),b,.12);c=mix(c,vec3(.22,.9,.7),1.-smoothstep(-aa,aa,d));

Experiment

Change the last sdCapsule argument from .12 to .2. The segment grows thicker with round caps, while the fixed endpoint vec2(-.45,-.2) and moving endpoint b stay in place.

Debugging and cost

The projection clamp in sdCapsule selects the nearest point on the finite segment, including its endpoints. Replace b with vec2(.45,-.2) to stop the endpoint motion and inspect a horizontal capsule without changing the distance calculation.

Built-in functions used

Learn it step by step