Task 2 of 6
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.
smin(a, b, k) with gpu.addFunction()h = Math.max(k - Math.abs(a - b), 0) / kMath.min(a, b) - h * h * k * 0.25smin(d1, d2, k) in the kernel instead of Math.minFar 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.
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;
});This page is an interactive exercise — the editor, the GPU runner and your saved progress need JavaScript. The text above is the full brief.