# Metaballs: Sticky, Gooey Blobs

## Introduction

Metaballs, or "blobs", are a class of surfaces popular for the way in which their component parts can be made to blend into each other. Their surfaces "stick" to each other like molten lava or of honey. Metaballs in their original form are based on the formula used to calculate the strength of electric fields. They begin very strong near the center of their components, and drop off to zero as the distance increases. The transition is smooth and the falloff rate decreases exponentially, leaving strong "blobs" in the middle and a fuzzy halo that fades to black around the edges. This formula is computationally expensive, however, and is difficult to handle due to its boundless and exponential expansion. As a result, alternate equations have been developed that do not suffer from the same limitations. Since we're also dealing with blobs whose components are circular, you'll see the equation for the circle pop up in one form or another: `f(x,y,x) = (x-xA)^2 + (y-yA)^2 + (z-zA)^2 - Radius^2` In the examples below, the surfaces have been reduced from three to one dimension in order to simplify their inner workings. The blue circles represent the components, and the solid yellow curves represent the strength functions. You can move things around and change the parameters by dragging the solid points. You can also trace the strength curve by moving the orange point, labeled P. Below you'll also find a gallery of images showing what the curves and surfaces look like in two and three dimensions.

## Method 1 Description

```f(x,y,z) = distanceAB / sqrt((x-xA)^2 + (y-yA)^2 + (z-zA)^2) g(x,y,z) = distanceCD / sqrt((x-xC)^2 + (y-yC)^2 + (z-zC)^2) h(x,y,z) = f(x,y,z) + g(x,y,z) - Threshold1 i(x,y,z) = min(Threshold2, max(0, h(x,y,z)))``` This method is based on the formula for the strength of an electric field (as well as a rough approximation of weak gravitational fields). Notice how the strength of the surface increases toward infinity as one approaches the components, and decreases toward - but never quite reaches - the threshold as you go farther and farther from the components. This lack of boundaries makes the surface somewhat difficult to deal with unless one enforces arbitrary limits. This formula is computationally expensive due to the division and root.

## Method 2 Description

```f(x,y,z) = (x-xA)^2 + (y-yA)^2 + (z-zA)^2 - distanceAB^2 g(x,y,z) = (x-xC)^2 + (y-yC)^2 + (z-zC)^2 - distanceCD^2 h(x,y,z) = Strength^f(x,y,z) + Strength^g(x,y,z) - 1 - Strength - Threshold i(x,y,z) = max(0, h(x,y,z))``` This formula has the nice feature of ranging from infinitely strong and "tight" to perfectly flat, while never exceeding the boundary of its components. In fact, if the threshold is zero, the effect is the same as if no mixing were occurring at all. If the threshold is equal to one, the surface disappears entirely. One disadvantage is that the radii of the components have a strong effect on the surface's strength, so it can be hard to gauge beforehand the influence they will have on the final shape. The equation features no divisions or roots, so it is computationally "cheaper" than the first formula.

## Method 3 Description

```f(x,y,z) = min(sqrt((x-xA)^2 + (y-yA)^2 + (z-zA)^2), distanceAB) g(x,y,z) = min(sqrt((x-xC)^2 + (y-yC)^2 + (z-zC)^2), distanceCD) h(x,y,z) = Strength * (1 - (f(x,y,z) / distanceAB)^2)^2 i(x,y,z) = Strength * (1 - (g(x,y,z) / distanceCD)^2)^2 j(x,y,z) = h(x,y,z) + i(x,y,z) - Threshold``` This formula is the easiest to manage, as several important facts about the surface are known beforehand: the strength of each component, the maximum strength of the surface, and the volume the surface encompasses. The strength of each component is always equal to the Strength parameter, and the maximum possible strength is simply the sum of the individual components minus the threshold. The surface also never extends beyond the lateral perimeter formed by its components. The curvature of the surface is not as smooth as some of the others, however, so the visual appeal slightly suffers. Also, the formula contains several divisions and roots, making it computationally expensive.

## Method 4 Description

```f(x,y,z) = distanceAB / ((x-xA)^2 + (y-yA)^2 + (z-zA)^2) g(x,y,z) = distanceCD / ((x-xC)^2 + (y-yC)^2 + (z-zC)^2) h(x,y,z) = f(x,y,z) + g(x,y,z) - Threshold1 i(x,y,z) = min(Threshold2, max(0, h(x,y,z)))``` This method is identical to the first, except the square root has been eliminated. As a result, the tightness of the strength is much greater, and the surface looks noticibly less organic, but the computational load is diminished.

## Conclusion

It is also possible to use other shapes in place of circles as the basis of the blobs. For instance, in the first formula, in order to create a diamond-shaped pattern, one could change the first line to this: `f(x,y,z) = distanceAB / (abs(x-xA) + abs(y-yA) + abs(z-zA))` And, in the second formula one could change the first line to this: `f(x,y,z) = abs(x-xA) + abs(y-yA) + abs(z-zA) - distanceAB`

## References

1. GameDev.net - Method 1
2. POV-Ray isosurface tutorial - Method 2
3. POV-Ray blob tutorial - Method 3
4. Wikipedia - Method 4