Below is a step‐by‐step breakdown of how this SDF computes the distance to a regular hexagonal prism. The key idea is that a regular hex cross‐section is highly symmetric, so the code “folds” the plane into a small fundamental region, computes distance, and unfolds it back implicitly:
────────────────────────────────────────────────────────────────────
glslCopy codefloat sdHexPrism(vec3 p, vec2 h)
{
// k.x = -sqrt(3)/2, k.y = 0.5, sqrt(3)/3
// These constants relate to the geometry of the hex cross‐section.
const vec3 k = vec3, 0.5, 0.57735);
// 1) Reflect p into the first octant:
// Because the hexagon is symmetric about x=0, y=0, and z=0,
// taking absolute values lets us handle only the "positive" sector
// and then reuse that distance for all other symmetric sectors.
p = abs(p);
// 2) Reflect across the line defined by dot(k.xy, p.xy) = 0 if needed:
// dot(k.xy, p.xy) will be negative if the point lies "behind" that boundary.
// If so, the code reflects it across that line (via the subtraction).
// This effectively folds the plane further so that the shape of the hex
// is turned into a simpler piece (like a rectangle or trapezoid).
p.xy -= 2.0 * min(dot(k.xy, p.xy), 0.0) * k.xy;
// 3) Compute distances in two dimensions:
//
// The cross-section in the XY plane:
// d.x = distance to the top edge or corner of the “folded” hex.
// The clamp() ensures x doesn’t exceed the hex’s diagonal boundary.
// The sign(...) picks +/- depending on whether we’re above y=h.x or not.
// d.y = distance in the Z direction from the top face of the prism
// (the prism extends from z=-h.y to +h.y, so after reflection we measure from .
//
// So d.x < 0 or d.y < 0 means we’re inside along that dimension; positive means outside.
vec2 d = vec2(
length( p.xy - vec2(clamp(p.x, -k.zh.x, k.zh.x), h.x) )
* sign(p.y - h.x),
);
// 4) Combine the two distances into a final SDF:
// - If both d.x and d.y are <= 0, we’re inside both the cross‐section and the prism height,
// so the distance is max(d.x, d.y). (Inside a shape in SDF is negative or zero.)
// - If one or both are > 0, we’re outside in that dimension and compute the distance
// to the boundary: length( max(d,0) ).
return min( max(d.x, d.y), 0.0 ) + length( max(d, 0.0) );
}
Key Points
p = abs(p);Because a regular hexagon is symmetric about both axes (and the prism is also symmetric aboutz=0z=0z=0), taking absolute values reduces the problem to one sector. The final distance is the same as if we considered all octants.
Folding/reflection in the XY planeThe linep.xy -= 2.0 * min(dot(k.xy, p.xy), 0.0)*k.xy;checks ifdot(k.xy,p.xy)\text{dot}(k.xy, p.xy)dot(k.xy, negative—meaningpppis on the “other side” of that diagonal—and if so reflects it. This folds the shape into a sector where the hex can be handled more simply.
Distance vectordAfter the folds, we compute two distances:d.xhandles the 2D hex boundary in the XY plane. Thesign(p.y - h.x)picks whether we measure vertical distance above that top edge or treat it as inside.d.yis just thezzzdistance from the top facez=h.yz = h.yz=h.y. Because we usedp.z = abs(p.z), the prism extends from−h.y-h.y−h.yto+h.y+h.y+h.y, so in the reflected space we measure from +h.y.
d.xhandles the 2D hex boundary in the XY plane. Thesign(p.y - h.x)picks whether we measure vertical distance above that top edge or treat it as inside.
d.yis just thezzzdistance from the top facez=h.yz = h.yz=h.y. Because we usedp.z = abs(p.z), the prism extends from−h.y-h.y−h.yto+h.y+h.y+h.y, so in the reflected space we measure from +h.y.
ResultThe return expressionmin(max(d.x,d.y), 0.0) + length(max(d, 0.0))is a common “box or polygon + extrusion” SDF pattern:Inside the prism (both components≤0\leq 0≤0), the signed distance is simply the largest negative value (the shape’s interior).Outside in any dimension (>0>0>0), the distance to the boundary islength(max(d,0)).
Inside the prism (both components≤0\leq 0≤0), the signed distance is simply the largest negative value (the shape’s interior).
Outside in any dimension (>0>0>0), the distance to the boundary islength(max(d,0)).
p = abs(p);Because a regular hexagon is symmetric about both axes (and the prism is also symmetric aboutz=0z=0z=0), taking absolute values reduces the problem to one sector. The final distance is the same as if we considered all octants.
Folding/reflection in the XY planeThe linep.xy -= 2.0 * min(dot(k.xy, p.xy), 0.0)*k.xy;checks ifdot(k.xy,p.xy)\text{dot}(k.xy, p.xy)dot(k.xy, negative—meaningpppis on the “other side” of that diagonal—and if so reflects it. This folds the shape into a sector where the hex can be handled more simply.
Distance vectordAfter the folds, we compute two distances:
d.xhandles the 2D hex boundary in the XY plane. Thesign(p.y - h.x)picks whether we measure vertical distance above that top edge or treat it as inside.
d.yis just thezzzdistance from the top facez=h.yz = h.yz=h.y. Because we usedp.z = abs(p.z), the prism extends from−h.y-h.y−h.yto+h.y+h.y+h.y, so in the reflected space we measure from +h.y.
d.xhandles the 2D hex boundary in the XY plane. Thesign(p.y - h.x)picks whether we measure vertical distance above that top edge or treat it as inside.
d.yis just thezzzdistance from the top facez=h.yz = h.yz=h.y. Because we usedp.z = abs(p.z), the prism extends from−h.y-h.y−h.yto+h.y+h.y+h.y, so in the reflected space we measure from +h.y.
ResultThe return expressionmin(max(d.x,d.y), 0.0) + length(max(d, 0.0))is a common “box or polygon + extrusion” SDF pattern:
Inside the prism (both components≤0\leq 0≤0), the signed distance is simply the largest negative value (the shape’s interior).
Outside in any dimension (>0>0>0), the distance to the boundary islength(max(d,0)).
Inside the prism (both components≤0\leq 0≤0), the signed distance is simply the largest negative value (the shape’s interior).
Outside in any dimension (>0>0>0), the distance to the boundary islength(max(d,0)).
The net effect is a clean, compact SDF for a hexagonal prism by exploiting symmetry to reduce the math to a simpler folded‐sector distance calculation.