Sdf Booleans
Constructive Solid Geometry on SDFs reduces to simple scalar operations. Union uses min, intersection uses max, and subtraction combines max with negation.
SDF CSG:
min(d1, d2) — closest surface wins. 2. Intersection: max(d1, d2) — only overlap region. 3. Difference: max(d1, -d2) — carve d2 from d1.1# SDF Boolean operations combine two distance fields2# using simple scalar operations: min, max, negation.34def sdf_union(d1, d2):5 return min(d1, d2) # Closest surface wins67def sdf_intersection(d1, d2):8 return max(d1, d2) # Farthest surface wins910def sdf_difference(d1, d2):11 return max(d1, -d2) # Keep d1, carve out d2
Union produces the combined volume of both shapes. At each point, the minimum SDF value determines which surface is visible.
Union:
1# SDF Union: min of two distance fields2def sdf_union(d1, d2):3 """Keep the surface closest to the query point."""4 return min(d1, d2)56# Union merges two shapes by selecting the minimum7# signed distance at every spatial sample point.
Intersection retains only the overlapping volume. The maximum SDF value ensures both surfaces constrain the result.
Intersection:
1# SDF Intersection: max of two distance fields2def sdf_intersection(d1, d2):3 """Keep only the overlapping region."""4 return max(d1, d2)56# The result is the region where both shapes exist7# simultaneously (both d1 and d2 are negative).
Difference carves shape B out of shape A. Negating d2 flips its interior, then max selects only the region of A outside B.
Difference:
1# SDF Difference (Subtraction): max(d1, -d2)2def sdf_difference(d1, d2):3 """Remove shape B from shape A."""4 return max(d1, -d2)56# Negating d2 flips its inside/outside, then taking7# the max keeps only the part of A that is outside B.
Smooth union blends two SDFs using polynomial interpolation near the intersection boundary, creating organic fillet-like transitions.
Smooth Union:
1# Smooth union using polynomial blending2def sdf_smooth_union(d1, d2, k):3 """Smooth min blending two SDFs with radius k."""4 h = max(k - abs(d1 - d2), 0.0) / k5 return min(d1, d2) - h * h * k * 0.2567# k controls the blending radius.8# k=0 reduces to hard min (standard union).
Smooth subtraction applies the same polynomial blending to the difference operation, creating rounded cavities instead of sharp cuts.
Smooth Subtraction:
1# Smooth subtraction using polynomial blending2def sdf_smooth_sub(d1, d2, k):3 """Smooth difference: carve d2 from d1 with blend radius k."""4 h = max(k - abs(-d2 - d1), 0.0) / k5 return max(-d2, d1) + h * h * k * 0.2567# Same polynomial blending as smooth union,8# but applied to the difference operation.
Chamfer blending creates a flat 45-degree bevel at the intersection edge by adding a diagonal distance term to the max operation.
Chamfer:
1# Chamfer intersection: creates a 45-degree bevel2def sdf_chamfer_intersection(d1, d2, r):3 """Chamfer blend at intersection edges."""4 import math5 return max(max(d1, d2),6 (d1 + d2) * math.sqrt(0.5) - r)78# The chamfer term adds a diagonal cut where9# the two surfaces meet, creating a flat bevel.
Fillet blending rounds the intersection edge using a circular arc profile by combining the positive parts of both distance fields.
Fillet:
1# Fillet intersection: creates a rounded edge2def sdf_fillet_intersection(d1, d2, r):3 """Round fillet blend at intersection edges."""4 import math5 return max(max(d1, d2),6 math.sqrt(max(d1, 0)**2 + max(d2, 0)**2) - r)78# The fillet term uses the Euclidean combination of9# the positive parts of d1 and d2, producing a circular10# arc at the intersection edge.