I'm trying to understand all primitives In this article.After hard work I just learn Sphere and Cube , I wrote description here.
I could draw 2D shapes but I have trouble understanding 3D below codes:
Torus - signed - exact
float sdTorus( vec3 p, vec2 t )
{
vec2 q = vec2(length(p.xz)-t.x,p.y);
return length(q)-t.y;
}
I can't imagine how this shape drew and I don't know why two length used here.
Cone - signed - exact
float sdCone( vec3 p, vec2 c )
{
// c must be normalized
float q = length(p.xy);
return dot(c,vec2(q,p.z));
}
I don't know why dot product used here?
Hexagonal Prism - signed - exact
float sdHexPrism( vec3 p, vec2 h )
{
vec3 q = abs(p);
return max(q.z-h.y,max((q.x*0.866025+q.y*0.5),q.y)-h.x);
}
How the calculations are done? why abs and maximum used ?
Triangular Prism - signed - exact
float sdTriPrism( vec3 p, vec2 h )
{
vec3 q = abs(p);
return max(q.z-h.y,max(q.x*0.866025+p.y*0.5,-p.y)-h.x*0.5);
}
How the calculations are done? why abs and maximum used ?
Capsule / Line - signed - exact
float sdCapsule( vec3 p, vec3 a, vec3 b, float r )
{
vec3 pa = p - a, ba = b - a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
return length( pa - ba*h ) - r;
}
How the calculations are done? why clamp used here? and why clamp and dot product used here?
I'm confused please help me to understand this shapes.I hope someone explain each shape.