Sdf Blending
Blending combines multiple SDF primitives into organic shapes. Unlike hard boolean operations, blending smoothly interpolates between distance fields using weight functions.
SDF Blending:
1# SDF Blending concept2# Unlike boolean operations (hard min/max), blending3# creates smooth organic transitions between shapes.45def blend_linear(d1, d2, t):6 """Linear interpolation: simplest blend."""7 return d1 * (1.0 - t) + d2 * t89# Smooth blending is essential for organic modeling,10# metaballs, character modeling, and procedural shapes.
The exponential smooth min uses log-sum-exp to create smooth transitions. Parameter k controls the sharpness of the blend.
Exponential Blend:
1# Exponential smooth minimum2import math34def exp_smooth_min(d1, d2, k=32):5 """Exponential smooth min blending."""6 e1 = math.exp(-k * d1)7 e2 = math.exp(-k * d2)8 return -math.log(e1 + e2) / k910# Higher k values produce sharper transitions.11# Lower k values produce wider, softer blends.
Polynomial smooth min uses cubic interpolation for smoother C1 continuous transitions. The blend radius k controls the width.
Polynomial Blend:
1# Polynomial smooth minimum (cubic)2def poly_smooth_min(d1, d2, k):3 """Polynomial smooth min with blend radius k."""4 h = max(k - abs(d1 - d2), 0.0) / k5 return min(d1, d2) - h * h * h * k * (1.0 / 6.0)67# Cubic version of smooth min. The h^3 term8# produces a smoother C1 continuous transition9# compared to the quadratic h^2 variant.
Weighted blending assigns per-primitive influence weights. Each SDF contribution is scaled by its normalized weight.
Weight Blend:
1# Weighted blend of multiple SDFs2def blend_weighted(sdfs, weights):3 """Blend N distance fields with per-field weights."""4 total_w = sum(weights)5 result = 0.06 for sdf_val, w in zip(sdfs, weights):7 result += sdf_val * (w / total_w)8 return result910# Weights control each primitive's contribution.11# Higher weight makes that primitive dominate.
When blending SDFs, material properties like color can be interpolated using the same blend factor, creating smooth color gradients at the junction.
Color Blend:
1# Color interpolation alongside SDF blending2def blend_with_color(sdf_a, sdf_b, color_a, color_b, k):3 """Blend SDFs and interpolate colors in blend region."""4 h = max(k - abs(sdf_a - sdf_b), 0.0) / k5 blended_dist = min(sdf_a, sdf_b) - h*h*k*0.256 # Interpolate color using same blend factor7 t = 0.5 + 0.5 * (sdf_b - sdf_a) / (k + 1e-6)8 t = max(0, min(1, t))9 color = lerp(color_a, color_b, t)10 return blended_dist, color
Linear interpolation between two SDFs creates a smooth morph. At t=0 the shape is entirely A, at t=1 entirely B, and in between a smooth transition occurs.
SDF Morph:
1# Shape morphing via linear SDF interpolation2def sdf_morph(sdf_a, sdf_b, point, t):3 """Morph between two shapes by interpolating SDFs."""4 da = sdf_a(point)5 db = sdf_b(point)6 return da * (1.0 - t) + db * t78# t=0 gives shape A, t=1 gives shape B,9# intermediate values create smooth transitions.
Applying smooth blending to the Stanford Bunny point cloud with a sphere primitive demonstrates organic shape fusion in practice.
Bunny Blend:
1# Organic blending applied to Stanford Bunny2# Blend bunny point cloud with a sphere SDF3def blend_bunny_sphere(bunny_sdf, sphere_center, sphere_r, k):4 """Smooth union of bunny SDF with a sphere."""5 for each point P in space:6 d_bunny = bunny_sdf(P)7 d_sphere = length(P - sphere_center) - sphere_r8 d = smooth_min(d_bunny, d_sphere, k)9 # Extract zero isosurface of blended field