Implicit Modeling
Until now, we have built 3D shapes by manually drawing lines and triangles. But what if we could sculpt 3D geometry using gravity, magnetism, or heat? This is called Implicit Modeling (or Metaballs).
The Energy Field:
1# Point Mass Potential2def calculate_influence(query_point, center, strength, radius):3 """How strong is the gravity/heat/magnetism at this exact spot?"""45 # 1. How far away is the query point?6 dist = distance(query_point, center)78 # 2. If it's too far away, there is 0 influence.9 if dist > radius:10 return 01112 # 3. Otherwise, calculate the strength.13 # It is very strong at the center (dist=0)14 # and smoothly fades to 0 as it reaches the radius.1516 # This specific formula creates a beautiful, smooth bell curve:17 influence = strength * (1 - (dist / radius)**2)**31819 return influence
Metaball "Energy" is one way to define a field. A much more common and powerful way in modern graphics is to use Signed Distance Fields (SDFs).
Mathematical Perfect Shapes:
5.0, you are exactly 5 meters away from the surface. 3. If it returns -2.0, you are exactly 2 meters inside the solid object! (Hence the "Signed" part). 4. People have discovered mathematical formulas for almost every shape imaginable: Spheres, Boxes, Cylinders, Tori, and more. All without a single polygon.1# Constructive Solid Geometry (CSG) Math2def distance_to_shapes(query_point, box_size, sphere_radius):3 """Signed Distance Fields (SDFs)."""45 # 1. Distance to a Sphere is easy:6 # (Distance from center) - (Radius)7 dist_sphere = length(query_point) - sphere_radius89 # 2. Distance to a Box is harder:10 q = abs(query_point) - box_size11 dist_box = length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0)1213 # 3. We can return either one!14 # A negative number means the point is INSIDE the shape.15 # A positive number means it is OUTSIDE.16 return dist_sphere, dist_box
A single mathematical sphere is boring. The true power of Implicit Modeling and SDFs comes when you combine dozens or hundreds of them together. Because they are just numbers, we can combine shapes using basic arithmetic!
Min, Max, and Smooth:
min() function. The distance to the merged shape is simply the distance to whichever shape is closer. 2. Intersection: To find the overlapping area, use the max() function. 3. Subtraction: To carve a hole, use max(A, -B). (The negative sign flips the inside/outside of B!). 4. Smooth Blending: By using a special math function called smooth_min(), two spheres will physically melt into each other like water droplets or wax. This is exactly how 2D Metaballs work!1# Combining Multiple Fields2def combined_sdf(query_point, sphere_A, sphere_B):3 """The magic of Boolean Operations on SDFs."""45 # 1. Calculate distance to Sphere A6 dist_A = distance_to_sphere(query_point, sphere_A.center, sphere_A.radius)78 # 2. Calculate distance to Sphere B9 dist_B = distance_to_sphere(query_point, sphere_B.center, sphere_B.radius)1011 # 3. Union (Merge them together):12 # Simply take the MINIMUM distance!13 dist_union = min(dist_A, dist_B)1415 # 4. Smooth Union (Metaball blending):16 # Use an exponential math trick to melt them together like wax17 dist_smooth = smooth_min(dist_A, dist_B, blend_factor=0.5)1819 return dist_smooth
In standard 3D modeling, making an object thicker or thinner (Offsetting) is a nightmare. Polygons intersect, normals flip, and the mesh explodes. In Implicit Modeling, offsetting is literally subtraction.
Inflation and Deflation:
0.0 is exactly the surface. 2. If we add a positive Iso-Value to our formula, the surface magically expands outward perfectly in all directions like a balloon. 3. If we use a negative Iso-Value, the surface shrinks inward perfectly. 4. The topology handles itself. If you expand two distant spheres enough, they will eventually touch and merge into a pill shape automatically!1# Expanding and Shrinking (Offsetting)2def apply_iso_value(sdf_distance, iso_value):3 """How to instantly inflate or deflate a shape."""45 # 1. The SDF formula returns the exact distance to the surface.6 # A value of 0.0 is the exact boundary of the object.78 # 2. If we subtract an Iso-Value from that distance...9 # We shift the boundary!1011 offset_distance = sdf_distance - iso_value1213 # Example:14 # If a point was previously exactly on the surface (distance = 0.0)15 # And our iso_value is 2.0...16 # The new distance is -2.0 (it is now deep INSIDE the object)17 # The whole object just inflated by 2 meters!1819 return offset_distance
We have these amazing, infinite math formulas. But computers cannot render "infinite math" on a screen directly. We need to convert the Math Formula back into polygon Triangles. How? By bringing back the Spatial Grid!
Sampling the Volume:
1# Sampling the SDF into a Grid2def evaluate_sdf_to_grid(sdf_formula, grid_size, resolution):3 """Bridge between Math and Voxelization."""45 # 1. Create a 3D grid of empty boxes6 grid = create_spatial_grid(grid_size, resolution)78 # 2. Loop through every corner of every box9 for corner in grid.corners:1011 # 3. Plug the corner's exact XYZ coordinate into the SDF Math Formula!12 distance_value = sdf_formula(corner.x, corner.y, corner.z)1314 # 4. Save that number into the Grid15 corner.value = distance_value1617 return grid
To truly understand an SDF, we can visualize the math directly by turning the distances into colors. This creates a Heatmap of the 3D space.
Visualizing the Math:
1# Mapping Distances to Colors2def generate_sdf_heatmap(grid):3 """Visualizing the invisible math fields."""45 for corner in grid.corners:67 # 1. Get the distance value8 dist = corner.value910 # 2. Inside the object (Negative distances)11 if dist < 0:12 # Map distance -5 to 0 into Dark Red to Light Red13 corner.color = Color.Lerp(DarkRed, LightRed, abs(dist))1415 # 3. Outside the object (Positive distances)16 else:17 # Map distance 0 to 5 into Light Blue to Dark Blue18 corner.color = Color.Lerp(LightBlue, DarkBlue, dist)1920 # 4. Exactly on the boundary21 if abs(dist) < 0.1:22 corner.color = White
When you combine mathematical SDFs with Marching Cubes, you get the legendary Metaball effect. This was heavily used in 1990s computer graphics, and is still used today for simulating liquid, slime, and tearing metal.
The Full Pipeline:
smooth_min() math function. 3. Grid Evaluation: We sample this complex, merged math formula into a discrete 3D Voxel grid. 4. Marching Cubes: We run the Marching Cubes algorithm on the grid to extract the 0.0 Isosurface. 5. Result: Smooth, gooey geometry that perfectly merges and splits in real-time.1# Interactive Metaballs2def render_metaballs(spheres, time_t):3 """The magic of 90s demoscene and modern lava lamps."""45 # 1. Update the positions of all spheres based on time6 animate_spheres(spheres, time_t)78 # 2. Create the grid9 grid = create_grid(resolution=0.5)1011 # 3. For every corner, calculate the combined SDF12 for corner in grid.corners:1314 total_distance = 9999991516 # Smoothly merge all spheres together!17 for s in spheres:18 dist = distance_to_sphere(corner, s.center, s.radius)19 total_distance = smooth_min(total_distance, dist, blend=0.8)2021 corner.value = total_distance2223 # 4. Run Marching Cubes24 mesh = extract_isosurface(grid, threshold=0.0)2526 return mesh