Initializing 3D Canvas...

Implicit Modeling

2 min read1 page

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. Imagine a tiny, invisible star floating in space. 2. That star emits an energy field. The energy is very strong at the center, and smoothly fades away to zero as you move further out. 3. We write a mathematical function that can take any XYZ coordinate in space, and return a single number: the exact strength of the energy at that point. 4. This creates a continuous, infinite "Scalar Field" extending in all directions.
python
1# Point Mass Potential
2def calculate_influence(query_point, center, strength, radius):
3 """How strong is the gravity/heat/magnetism at this exact spot?"""
4
5 # 1. How far away is the query point?
6 dist = distance(query_point, center)
7
8 # 2. If it's too far away, there is 0 influence.
9 if dist > radius:
10 return 0
11
12 # 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.
15
16 # This specific formula creates a beautiful, smooth bell curve:
17 influence = strength * (1 - (dist / radius)**2)**3
18
19 return influence
Influence Radius
3.00
2 min read1 page

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:

1. Instead of tracking "Energy", an SDF tracks the exact physical distance to the nearest surface. 2. If the function returns 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.
python
1# Constructive Solid Geometry (CSG) Math
2def distance_to_shapes(query_point, box_size, sphere_radius):
3 """Signed Distance Fields (SDFs)."""
4
5 # 1. Distance to a Sphere is easy:
6 # (Distance from center) - (Radius)
7 dist_sphere = length(query_point) - sphere_radius
8
9 # 2. Distance to a Box is harder:
10 q = abs(query_point) - box_size
11 dist_box = length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0)
12
13 # 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
SDF Shape (Sphere / Box)
0.00
2 min read1 page

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:

1. Union (Merge): To merge two shapes, just use the 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!
python
1# Combining Multiple Fields
2def combined_sdf(query_point, sphere_A, sphere_B):
3 """The magic of Boolean Operations on SDFs."""
4
5 # 1. Calculate distance to Sphere A
6 dist_A = distance_to_sphere(query_point, sphere_A.center, sphere_A.radius)
7
8 # 2. Calculate distance to Sphere B
9 dist_B = distance_to_sphere(query_point, sphere_B.center, sphere_B.radius)
10
11 # 3. Union (Merge them together):
12 # Simply take the MINIMUM distance!
13 dist_union = min(dist_A, dist_B)
14
15 # 4. Smooth Union (Metaball blending):
16 # Use an exponential math trick to melt them together like wax
17 dist_smooth = smooth_min(dist_A, dist_B, blend_factor=0.5)
18
19 return dist_smooth
Distance Between Spheres
0.00
3 min read1 page

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:

1. A distance of 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!
python
1# Expanding and Shrinking (Offsetting)
2def apply_iso_value(sdf_distance, iso_value):
3 """How to instantly inflate or deflate a shape."""
4
5 # 1. The SDF formula returns the exact distance to the surface.
6 # A value of 0.0 is the exact boundary of the object.
7
8 # 2. If we subtract an Iso-Value from that distance...
9 # We shift the boundary!
10
11 offset_distance = sdf_distance - iso_value
12
13 # 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!
18
19 return offset_distance
Iso-Value Offset
0.00
2 min read1 page

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. We create a 3D grid (Bounding Box) that covers the area we want to look at. 2. We loop through every single corner of that grid. 3. We ask our SDF formula: "What is the distance at this exact XYZ corner?" 4. We save that number into the grid corner. 5. Suddenly, we have exactly what Marching Cubes needs to run!
python
1# Sampling the SDF into a Grid
2def evaluate_sdf_to_grid(sdf_formula, grid_size, resolution):
3 """Bridge between Math and Voxelization."""
4
5 # 1. Create a 3D grid of empty boxes
6 grid = create_spatial_grid(grid_size, resolution)
7
8 # 2. Loop through every corner of every box
9 for corner in grid.corners:
10
11 # 3. Plug the corner's exact XYZ coordinate into the SDF Math Formula!
12 distance_value = sdf_formula(corner.x, corner.y, corner.z)
13
14 # 4. Save that number into the Grid
15 corner.value = distance_value
16
17 return grid
Sampling Resolution
0.00
2 min read1 page

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. We take a 2D slice right through the middle of our 3D grid. 2. If a grid corner has a Negative distance (inside the object), we color it Red. The deeper inside, the darker the red. 3. If a grid corner has a Positive distance (outside the object), we color it Blue. The further away, the darker the blue. 4. Where the red and blue meet (Distance = 0), we draw a white line. This white line is the exact surface that Marching Cubes will extract!
python
1# Mapping Distances to Colors
2def generate_sdf_heatmap(grid):
3 """Visualizing the invisible math fields."""
4
5 for corner in grid.corners:
6
7 # 1. Get the distance value
8 dist = corner.value
9
10 # 2. Inside the object (Negative distances)
11 if dist < 0:
12 # Map distance -5 to 0 into Dark Red to Light Red
13 corner.color = Color.Lerp(DarkRed, LightRed, abs(dist))
14
15 # 3. Outside the object (Positive distances)
16 else:
17 # Map distance 0 to 5 into Light Blue to Dark Blue
18 corner.color = Color.Lerp(LightBlue, DarkBlue, dist)
19
20 # 4. Exactly on the boundary
21 if abs(dist) < 0.1:
22 corner.color = White
Z-Axis Slice
0.00
2 min read1 page

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:

1. SDF Formulation: We define 3 mathematical spheres moving in space. 2. Smooth Union: We merge their distance fields using the 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.
python
1# Interactive Metaballs
2def render_metaballs(spheres, time_t):
3 """The magic of 90s demoscene and modern lava lamps."""
4
5 # 1. Update the positions of all spheres based on time
6 animate_spheres(spheres, time_t)
7
8 # 2. Create the grid
9 grid = create_grid(resolution=0.5)
10
11 # 3. For every corner, calculate the combined SDF
12 for corner in grid.corners:
13
14 total_distance = 999999
15
16 # 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)
20
21 corner.value = total_distance
22
23 # 4. Run Marching Cubes
24 mesh = extract_isosurface(grid, threshold=0.0)
25
26 return mesh
Animation Time
50.00