home/ atoms/ sdf-segment-primitive

The SDF of a line segment is the distance from a point to the nearest point on the clamped segment

A line segment from A to B has a well-known analytic SDF. Project the query point P onto the line AB using the parameter h = clamp(dot(P-A, B-A) / dot(B-A, B-A), 0, 1). The nearest point on the segment is A + h*(B-A). The SDF is length(P - nearestPoint). An extended capsule is formed by subtracting a radius r from this distance. IQ uses this for ears, arms, and legs — stick primitives with a varying radius. The h parameter also serves as a color/material coordinate (tip vs base).

Examples

float sdCapsule(vec3 p, vec3 a, vec3 b, float r){ vec3 ab=b-a,pa=p-a; float h=clamp(dot(pa,ab)/dot(ab,ab),0.,1.); return length(pa-ab*h)-r; }

Assessment

Describe how h (the segment projection parameter) can be used to vary the radius along a leg SDF to create a tapered limb thicker at the hip and thinner at the ankle.

“distance to a stick, which is a line segment in a space. I know these things because, I know it by, well, there's a bit of thinking”
corpus · inigo-quilez-live-coding-happy-jumping-video · chunk 11