Skip to main content

Cone

Code Example

float coneSDF(vec3 p, float r, float h) {
vec2 q = vec2(length(p.xz), p.y + h * 0.5);
vec2 tip = q - vec2(0, h);
vec2 dir = normalize(vec2(h, r));
float d = max(dot(tip, dir), -q.y);
float e = dot(tip, vec2(dir.y, -dir.x));
if ((q.y > h) && (e < 0.)) d = max(d, length(tip));
if ((q.x > r) && (e > length(vec2(h, r))))
d = max(d, length(q - vec2(r, 0)));
return d;
}