Polynomial Smooth Minimum for SDF Blending in GLSL

The polynomial smooth minimum function replaces the sharp, non-differentiable union operator of signed distance fields (SDFs) with a continuous, organic transition. In GLSL raymarching shaders, standard Boolean unions use min(d1, d2), which creates a visible, hard seam where two shapes intersect. By applying a localized polynomial curve across a user-defined threshold, the smooth minimum mathematically bridges the gap between distances, producing a seamless fillet or "metaball" blending effect while preserving stable surface normals for lighting.

The Problem with Standard Minimums

In signed distance field rendering, the union of two geometric primitives with distance functions \(d_1\) and \(d_2\) is represented as:

float d = min(d1, d2);

While computationally inexpensive, the derivative of \(\min(d_1, d_2)\) is discontinuous at points where \(d_1 = d_2\). In 3D raymarching, surface normals are derived from the spatial gradient of the SDF using central differences:

\[\mathbf{n} = \text{normalize}(\nabla d)\]

A discontinuous derivative at the intersection seam yields sudden jumps in the normal vectors. This results in hard edges and prevents organic fusion between colliding geometries.

The Quadratic Polynomial Formulation

The most common polynomial smooth minimum in computer graphics, formulated by Inigo Quilez, introduces a smoothing radius \(k\). When the distance between the two surfaces is greater than \(k\), the function evaluates to the standard minimum. When the surfaces are closer than \(k\), a quadratic polynomial interpolates between them and subtracts a corrective offset.

The formulation in GLSL is written as:

float smin(float a, float b, float k) {
    float h = clamp(0.5 + 0.5 * (b - a) / k, 0.0, 1.0);
    return mix(b, a, h) - k * h * (1.0 - h);
}

How the Math Works

  1. Normalized Interpolation Factor (\(h\)): The term \((b - a) / k\) measures the relative difference between the two distances normalized by the smoothing factor \(k\). Adding \(0.5\) shifts the domain so that when \(a = b\), \(h = 0.5\). The clamp operation restricts \(h\) to the range \([0.0, 1.0]\), ensuring that the smoothing effect only occurs inside the interval \([-k, k]\).

  2. Linear Interpolation (mix): The expression mix(b, a, h) provides a standard linear interpolation between the two distance values. If \(h = 0.0\), the result is \(b\); if \(h = 1.0\), the result is \(a\).

  3. Subtractive Fillet (\(k \cdot h \cdot (1.0 - h)\)): Linear interpolation alone does not produce a rounded union; it would flatten the intersection into an average. The quadratic term \(h(1.0 - h)\) forms a parabolic arc that peaks at \(h = 0.5\) (where it equals \(0.25\)) and falls to zero at \(h = 0\) and \(h = 1\). Multiplying this arc by \(k\) and subtracting it from the interpolated value pulls the resulting distance outward from the primitives, effectively creating the smooth rounded fillet between the two surfaces.

Cubic Smooth Minimum Variant

For applications requiring higher-order continuity (such as specular highlights that remain uninterrupted across the blending boundary), a cubic variant can be used:

float sminCubic(float a, float b, float k) {
    float h = max(k - abs(a - b), 0.0) / k;
    return min(a, b) - h * h * h * k * (1.0 / 6.0);
}

The cubic formulation ensures \(C^2\) continuity (continuous second derivatives), eliminating subtle visual creasing that can appear in specular highlights when using quadratic smoothing.

Metric Degradation Considerations

A true signed distance field satisfies the Eikonal equation:

\[|\nabla d| = 1\]

Subtracting the polynomial term \(k \cdot h \cdot (1.0 - h)\) causes the resulting field to under-estimate the true Euclidean distance inside the blend zone. While safe from ray-overshooting (because the distance decreases rather than increases), this metric degradation distorts the gradient magnitude.

To prevent artifacts such as ambient occlusion banding or sphere-tracing slowdowns when using large values of \(k\):

  • Keep \(k\) as small as visually acceptable.
  • Step conservatively in raymarching loops (e.g., multiply the ray step size by a relaxation factor like \(0.8\) or \(0.9\)).
  • Re-normalize normal vectors calculated from gradient differences.