Here's a clear step-by-step walkthrough of this signed distance function (SDF) for a hexagonal prism:
Overview:
The function calculates the shortest distance from a given pointp(3D vector) to the surface of a hexagonal prism. The prism is defined by the radius (h.x) of the hexagon and the half-height (h.y) of the prism.
Step-by-step breakdown:
Step 1: Define constants
────────────────────────────────────────────────────────────────────
glslCopy codeconst vec3 k = vec3, 0.5, 0.57735);
k.xy((-√3/2, 1/2)) forms a vector pointing perpendicular to one of the hexagon edges, specifically chosen to simplify calculations (represents a symmetry axis).
k.z = tan(π/6) = 1/√3 ≈ 0.57735, a factor useful for scaling along the hexagon's radius.
k.xy((-√3/2, 1/2)) forms a vector pointing perpendicular to one of the hexagon edges, specifically chosen to simplify calculations (represents a symmetry axis).
k.z = tan(π/6) = 1/√3 ≈ 0.57735, a factor useful for scaling along the hexagon's radius.
Step 2: Symmetry (your comment refers to this line)
────────────────────────────────────────────────────────────────────
glslCopy codep = );
The hexagonal prism is symmetrical about all coordinate planes.
Takingabs(p)effectively "folds" space into the positive quadrant/octant. This symmetry allows simplified distance calculation because the hexagonal prism is invariant under reflection about its symmetry planes.
The hexagonal prism is symmetrical about all coordinate planes.
Takingabs(p)effectively "folds" space into the positive quadrant/octant. This symmetry allows simplified distance calculation because the hexagonal prism is invariant under reflection about its symmetry planes.
This matches your intuition: due to symmetry, distances calculated in negative quadrants are equivalent to their positive counterparts.
Step 3: Hexagonal Reflection
────────────────────────────────────────────────────────────────────
glslCopy codep.xy -= 2.0 * min(dot(k.xy, p.xy), 0.0) * k.xy;
Reflects the pointp.xyacross the boundary line formed by vectork.xyif it's outside the fundamental region (a 60° sector).
dot(k.xy, p.xy)measures how farp.xyextends along the normal to the chosen boundary line.
The reflection (2.0 * min(..., 0.0)*k.xy) moves points outside the fundamental triangle sector back into it.
After this step, we only need to calculate distance in one-sixth of the hexagon, further simplifying the calculation.
Reflects the pointp.xyacross the boundary line formed by vectork.xyif it's outside the fundamental region (a 60° sector).
dot(k.xy, p.xy)measures how farp.xyextends along the normal to the chosen boundary line.
The reflection (2.0 * min(..., 0.0)*k.xy) moves points outside the fundamental triangle sector back into it.
After this step, we only need to calculate distance in one-sixth of the hexagon, further simplifying the calculation.
Step 4: Compute Distance Components
────────────────────────────────────────────────────────────────────
glslCopy codevec2 d = vec2(
length(p.xy - vec2(clamp(p.x, -k.zh.x, k.zh.x), h.x)) * sign(p.y - h.x),
p.z - h.y);
Horizontal distance (hexagonal cross-section):clamp(p.x, -k.zh.x, k.zh.x)restrictsp.xto the hexagonal side boundaries.vec2(clamp(...), h.x)projects the point onto the nearest horizontal hexagon edge.length(...)measures the horizontal distance to the nearest edge/corner.sign(p.y - h.x)ensures points inside yield negative distances, outside yield positive distances. It distinguishes inside vs. outside.
clamp(p.x, -k.zh.x, k.zh.x)restrictsp.xto the hexagonal side boundaries.
vec2(clamp(...), h.x)projects the point onto the nearest horizontal hexagon edge.
length(...)measures the horizontal distance to the nearest edge/corner.
sign(p.y - h.x)ensures points inside yield negative distances, outside yield positive distances. It distinguishes inside vs. outside.
Vertical distance (prism height):p.z - h.yis simply vertical distance from the prism's top/bottom plane. Negative if within vertical bounds.
p.z - h.yis simply vertical distance from the prism's top/bottom plane. Negative if within vertical bounds.
Horizontal distance (hexagonal cross-section):
clamp(p.x, -k.zh.x, k.zh.x)restrictsp.xto the hexagonal side boundaries.
vec2(clamp(...), h.x)projects the point onto the nearest horizontal hexagon edge.
length(...)measures the horizontal distance to the nearest edge/corner.
sign(p.y - h.x)ensures points inside yield negative distances, outside yield positive distances. It distinguishes inside vs. outside.
clamp(p.x, -k.zh.x, k.zh.x)restrictsp.xto the hexagonal side boundaries.
vec2(clamp(...), h.x)projects the point onto the nearest horizontal hexagon edge.
length(...)measures the horizontal distance to the nearest edge/corner.
sign(p.y - h.x)ensures points inside yield negative distances, outside yield positive distances. It distinguishes inside vs. outside.
Vertical distance (prism height):
p.z - h.yis simply vertical distance from the prism's top/bottom plane. Negative if within vertical bounds.
p.z - h.yis simply vertical distance from the prism's top/bottom plane. Negative if within vertical bounds.
Step 5: Combine Horizontal & Vertical distances
────────────────────────────────────────────────────────────────────
glslCopy codereturn min(max(d.x,d.y), 0.0) + length(max(d, 0.0));
The final distance combines vertical and horizontal components:max(d.x, d.y)picks the largest positive component: if both negative, the point is inside the prism (the largest negative value indicates proximity to a wall from the inside).min(..., 0.0)ensures that internal points (negative distances) remain negative (correct inside-sign handling).length(max(d, 0.0))handles external distances: if outside the prism, this computes the length from the point to the nearest edge or face.
max(d.x, d.y)picks the largest positive component: if both negative, the point is inside the prism (the largest negative value indicates proximity to a wall from the inside).
min(..., 0.0)ensures that internal points (negative distances) remain negative (correct inside-sign handling).
length(max(d, 0.0))handles external distances: if outside the prism, this computes the length from the point to the nearest edge or face.
The final distance combines vertical and horizontal components:
max(d.x, d.y)picks the largest positive component: if both negative, the point is inside the prism (the largest negative value indicates proximity to a wall from the inside).
min(..., 0.0)ensures that internal points (negative distances) remain negative (correct inside-sign handling).
length(max(d, 0.0))handles external distances: if outside the prism, this computes the length from the point to the nearest edge or face.
max(d.x, d.y)picks the largest positive component: if both negative, the point is inside the prism (the largest negative value indicates proximity to a wall from the inside).
min(..., 0.0)ensures that internal points (negative distances) remain negative (correct inside-sign handling).
length(max(d, 0.0))handles external distances: if outside the prism, this computes the length from the point to the nearest edge or face.
Summary (Intuition):
abs(p)exploits symmetry to reduce computation.
Reflection ensures we only need to handle one small sector (1/6th of hexagon).
Then, distance to hexagon edge and prism vertical faces are computed separately.
Finally, we combine vertical and horizontal components, correctly handling interior/exterior sign.
abs(p)exploits symmetry to reduce computation.
Reflection ensures we only need to handle one small sector (1/6th of hexagon).
Then, distance to hexagon edge and prism vertical faces are computed separately.
Finally, we combine vertical and horizontal components, correctly handling interior/exterior sign.
This results in an efficient and compact hexagonal prism SDF.