Task 2 of 6

Two Spheres Melt Into One

Combining two SDFs is just Math.min — the nearest surface wins. But min leaves a hard crease where the shapes meet. Swap it for a smooth minimum and the fields blend: wherever the two distances are within k of each other, the result dips below both, bulging the surfaces toward each other. That bulge is a metaball.

Since every task from here on needs this helper, register it once with gpu.addFunction() — gpu.js transpiles it alongside the kernel, and any kernel on that GPU instance can call it by name.

Goal: implement the polynomial smooth minimum and use it to blend two sphere fields into one metaball field.

Requirements

Hint 1 — what should change?

Far from the seam, smin equals plain min. Exactly between the spheres the two distances are equal, so h = 1 and the field dips by k / 4. With the starter's arguments the midpoint should read 0.2 − 0.1 = 0.1.

Hint 2 — the function, verbatim
gpu.addFunction(function smin(a, b, k) {
  const h = Math.max(k - Math.abs(a - b), 0.0) / k;
  return Math.min(a, b) - h * h * k * 0.25;
});

Same idea elsewhere

Smooth blends of implicit surfaces are how molecular-surface renderers in CUDA draw proteins and how Metal-based sculpting apps merge clay-like blobs — the union operator is soft everywhere, and the GPU evaluates it millions of times per frame without blinking.

All tasks in Ray-Marched Metaballs

  1. The Sphere as a Number
  2. Two Spheres Melt Into One
  3. March Until You Hit Something
  4. Normals Without Geometry
  5. Turn On the Light
  6. Soft Shadows, Full Scene

This page is an interactive exercise — the editor, the GPU runner and your saved progress need JavaScript. The text above is the full brief.