Hard 3DAnimated

Metaballs (smooth union)

Three blobs that merge like liquid. Raymarch them with a smooth minimum.

The target picture for the shader challenge "Metaballs (smooth union)"
Recreate this picture. You pass at a 99% match, then try to write it in fewer characters than the par.

About the Metaballs (smooth union) shader

Three blobs that merge like liquid. Raymarch them with a smooth minimum. Three-dimensional scenes drawn entirely by a fragment shader: lighting, shadows and shapes from signed distance functions.

How this challenge works

You write a fragment shader that draws the picture above, live in your browser. A match bar shows how close you are, and the score combines accuracy, code size (the par is 1185 characters in Unity (Built-in)) and GPU speed. Work in HLSL, GLSL, WGSL, Metal or Godot and export the result when you are done.

What it is built from

smin
uses max, abs, min
k
a plain value or condition
Ball 1
uses sin, cos
Ball 2
uses sin
Ball 3
a plain value or condition
Color
colours float3(0.35,0.85,0.65)
Rim light
uses pow, max, dot
Camera / light
a plain value or condition

Only the building blocks and the colours are listed. Work out how they fit together, then measure the numbers from the target.

How to make a metaballs (smooth union) shader

  1. Start from the Raymarching lesson. Only map() changes (plus color).
  2. smin(smin(d1, d2, 0.8), d3, 0.8) merges all three.
  3. The rim term brightens surfaces seen at a grazing angle; dot(n, -rd) is 1 facing the camera.

Techniques used

  • Ray marching with a signed distance field: each pixel walks a ray through a distance function until it hits a surface.
  • Sine waves: smooth oscillation for waves, pulses and motion over time.
  • Diffuse lighting: brightness from the angle between the surface and the light.

Metaballs (smooth union) shader code, explained step by step

Try to build it yourself first, because that is how it sticks. When you want to check your work or you are stuck, this is a complete working solution cut into parts, with the idea behind each part. The parts are in HLSL (Unity and Unreal); the full code below comes in every language.

Part 1: Helper: smin()

float smin(float a, float b, float k)
{
    float h = max(k - abs(a - b), 0.0) / k;
    return min(a, b) - h * h * k * 0.25;
}
  • A helper function the main shader calls. It is handed out in the challenge Toolbox, so you can paste it in with one click.

Part 2: Helper: map()

float map(float3 p)
{
    float3 c1 = float3(sin(time) * 0.9, cos(time * 1.3) * 0.4, 0.0);
    float3 c2 = float3(-sin(time * 0.8) * 0.9, sin(time * 1.1) * 0.5, 0.0);
    float d1 = length(p - c1) - 0.55;
    float d2 = length(p - c2) - 0.65;
    float d3 = length(p) - 0.45;
    return smin(smin(d1, d2, 0.8), d3, 0.8);
}
  • The scene: a distance function that says how far a point is from the nearest surface. Ray marching uses it to take safe steps.

Part 3: Helper: calcNormal()

float3 calcNormal(float3 p)
{
    float2 e = float2(0.001, 0.0);
    return normalize(float3(
        map(p + e.xyy) - map(p - e.xyy),
        map(p + e.yxy) - map(p - e.yxy),
        map(p + e.yyx) - map(p - e.yyx)));
}
  • A helper function the main shader calls. It is handed out in the challenge Toolbox, so you can paste it in with one click.

Part 4: Set up the coordinates

Inside float4 frag(float2 uv)

float2 p = (uv - 0.5) * float2(aspect, 1.0) * 2.0;
float3 ro = float3(0.0, 0.0, -3.0);
float3 rd = normalize(float3(p, 1.5));
  • Centre the coordinates on the middle of the picture and multiply x by the aspect ratio, so a circle stays round on a wide picture.
  • Subtracting 0.5 moves the origin from the bottom-left corner to the middle of the picture.
  • normalize() scales a direction to length 1, so only its direction matters.
  • float3(r, g, b) is a colour: each channel from 0 (none) to 1 (full).

Part 5: Build the pattern

Still inside frag()

float t = 0.0;

Part 6: The main loop

Still inside frag()

for (int i = 0; i < 64; i++)
{
    float d = map(ro + rd * t);
    t += d;
    if (d < 0.001 || t > 20.0) break;
}
  • The loop runs 64 times. Each pass is one step of the repeated work; the browser caps loops, so keep the count modest.
  • The code inside the braces only runs when the condition is true.

Part 7: Choose the colours

Still inside frag()

float3 col = float3(0.05, 0.06, 0.1);
  • float3(r, g, b) is a colour: each channel from 0 (none) to 1 (full).

Part 8: Choose the colours (continued)

Still inside frag()

if (t < 20.0)
{
    float3 pos = ro + rd * t;
    float3 n = calcNormal(pos);
    float diff = max(dot(n, normalize(float3(0.6, 0.8, -0.5))), 0.0);
    col = float3(0.35, 0.85, 0.65) * (0.15 + 0.85 * diff);
    col += pow(1.0 - max(dot(n, -rd), 0.0), 3.0) * 0.5;
}
  • The code inside the braces only runs when the condition is true.
  • The dot product of the surface normal and the light direction is the diffuse light: bright where the surface faces the light, dark where it faces away.
  • pow(x, k) reshapes a value: a high power keeps only the brightest part, which makes small sharp highlights.
  • normalize() scales a direction to length 1, so only its direction matters.

Part 9: Colour the pixel and return it

Still inside frag()

return float4(col, 1.0);
  • The shader returns the final colour: red, green, blue, and an alpha of 1 (fully opaque).

The full code

HLSL Unity, Unreal
float smin(float a, float b, float k)
{
    float h = max(k - abs(a - b), 0.0) / k;
    return min(a, b) - h * h * k * 0.25;
}

float map(float3 p)
{
    float3 c1 = float3(sin(time) * 0.9, cos(time * 1.3) * 0.4, 0.0);
    float3 c2 = float3(-sin(time * 0.8) * 0.9, sin(time * 1.1) * 0.5, 0.0);
    float d1 = length(p - c1) - 0.55;
    float d2 = length(p - c2) - 0.65;
    float d3 = length(p) - 0.45;
    return smin(smin(d1, d2, 0.8), d3, 0.8);
}

float3 calcNormal(float3 p)
{
    float2 e = float2(0.001, 0.0);
    return normalize(float3(
        map(p + e.xyy) - map(p - e.xyy),
        map(p + e.yxy) - map(p - e.yxy),
        map(p + e.yyx) - map(p - e.yyx)));
}

float4 frag(float2 uv)
{
    float2 p = (uv - 0.5) * float2(aspect, 1.0) * 2.0;
    float3 ro = float3(0.0, 0.0, -3.0);
    float3 rd = normalize(float3(p, 1.5));
    float t = 0.0;
    for (int i = 0; i < 64; i++)
    {
        float d = map(ro + rd * t);
        t += d;
        if (d < 0.001 || t > 20.0) break;
    }
    float3 col = float3(0.05, 0.06, 0.1);
    if (t < 20.0)
    {
        float3 pos = ro + rd * t;
        float3 n = calcNormal(pos);
        float diff = max(dot(n, normalize(float3(0.6, 0.8, -0.5))), 0.0);
        col = float3(0.35, 0.85, 0.65) * (0.15 + 0.85 * diff);
        col += pow(1.0 - max(dot(n, -rd), 0.0), 3.0) * 0.5;
    }
    return float4(col, 1.0);
}
GLSL Shadertoy, three.js, OpenGL
float smin(float a, float b, float k)
{
    float h = max(k - abs(a - b), 0.0) / k;
    return min(a, b) - h * h * k * 0.25;
}

float map(vec3 p)
{
    vec3 c1 = vec3(sin(time) * 0.9, cos(time * 1.3) * 0.4, 0.0);
    vec3 c2 = vec3(-sin(time * 0.8) * 0.9, sin(time * 1.1) * 0.5, 0.0);
    float d1 = length(p - c1) - 0.55;
    float d2 = length(p - c2) - 0.65;
    float d3 = length(p) - 0.45;
    return smin(smin(d1, d2, 0.8), d3, 0.8);
}

vec3 calcNormal(vec3 p)
{
    vec2 e = vec2(0.001, 0.0);
    return normalize(vec3(
        map(p + e.xyy) - map(p - e.xyy),
        map(p + e.yxy) - map(p - e.yxy),
        map(p + e.yyx) - map(p - e.yyx)));
}

vec4 frag(vec2 uv)
{
    vec2 p = (uv - 0.5) * vec2(aspect, 1.0) * 2.0;
    vec3 ro = vec3(0.0, 0.0, -3.0);
    vec3 rd = normalize(vec3(p, 1.5));
    float t = 0.0;
    for (int i = 0; i < 64; i++)
    {
        float d = map(ro + rd * t);
        t += d;
        if (d < 0.001 || t > 20.0) break;
    }
    vec3 col = vec3(0.05, 0.06, 0.1);
    if (t < 20.0)
    {
        vec3 pos = ro + rd * t;
        vec3 n = calcNormal(pos);
        float diff = max(dot(n, normalize(vec3(0.6, 0.8, -0.5))), 0.0);
        col = vec3(0.35, 0.85, 0.65) * (0.15 + 0.85 * diff);
        col += pow(1.0 - max(dot(n, -rd), 0.0), 3.0) * 0.5;
    }
    return vec4(col, 1.0);
}
WGSL WebGPU, Bevy
fn smin(a: f32, b: f32, k: f32) -> f32
{
    let h = max(k - abs(a - b), 0.0) / k;
    return min(a, b) - h * h * k * 0.25;
}

fn map(p: vec3f) -> f32
{
    let c1 = vec3f(sin(time) * 0.9, cos(time * 1.3) * 0.4, 0.0);
    let c2 = vec3f(-sin(time * 0.8) * 0.9, sin(time * 1.1) * 0.5, 0.0);
    let d1 = length(p - c1) - 0.55;
    let d2 = length(p - c2) - 0.65;
    let d3 = length(p) - 0.45;
    return smin(smin(d1, d2, 0.8), d3, 0.8);
}

fn calcNormal(p: vec3f) -> vec3f
{
    let e = vec2f(0.001, 0.0);
    return normalize(vec3f(map(p + e.xyy) - map(p - e.xyy), map(p + e.yxy) - map(p - e.yxy), map(p + e.yyx) - map(p - e.yyx)));
}

fn frag(uv: vec2f) -> vec4f
{
    let p = (uv - 0.5) * vec2f(aspect, 1.0) * 2.0;
    let ro = vec3f(0.0, 0.0, -3.0);
    let rd = normalize(vec3f(p, 1.5));
    var t = 0.0;
    for (var i = 0; i < 64; i++)
    {
        let d = map(ro + rd * t);
        t += d;
        if (d < 0.001 || t > 20.0) { break; }
    }
    var col = vec3f(0.05, 0.06, 0.1);
    if (t < 20.0)
    {
        let pos = ro + rd * t;
        let n = calcNormal(pos);
        let diff = max(dot(n, normalize(vec3f(0.6, 0.8, -0.5))), 0.0);
        col = vec3f(0.35, 0.85, 0.65) * (0.15 + 0.85 * diff);
        col += pow(1.0 - max(dot(n, -rd), 0.0), 3.0) * 0.5;
    }
    return vec4f(col, 1.0);
}
Godot Godot 4 shading language
shader_type canvas_item;

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

float map(vec3 p)
{
    vec3 c1 = vec3(sin(TIME) * 0.9, cos(TIME * 1.3) * 0.4, 0.0);
    vec3 c2 = vec3(-sin(TIME * 0.8) * 0.9, sin(TIME * 1.1) * 0.5, 0.0);
    float d1 = length(p - c1) - 0.55;
    float d2 = length(p - c2) - 0.65;
    float d3 = length(p) - 0.45;
    return smin(smin(d1, d2, 0.8), d3, 0.8);
}

vec3 calcNormal(vec3 p)
{
    vec2 e = vec2(0.001, 0.0);
    return normalize(vec3(
        map(p + e.xyy) - map(p - e.xyy),
        map(p + e.yxy) - map(p - e.yxy),
        map(p + e.yyx) - map(p - e.yyx)));
}

vec4 frag(vec2 uv)
{
    vec2 p = (uv - 0.5) * vec2(aspect, 1.0) * 2.0;
    vec3 ro = vec3(0.0, 0.0, -3.0);
    vec3 rd = normalize(vec3(p, 1.5));
    float t = 0.0;
    for (int i = 0; i < 64; i++)
    {
        float d = map(ro + rd * t);
        t += d;
        if (d < 0.001 || t > 20.0) break;
    }
    vec3 col = vec3(0.05, 0.06, 0.1);
    if (t < 20.0)
    {
        vec3 pos = ro + rd * t;
        vec3 n = calcNormal(pos);
        float diff = max(dot(n, normalize(vec3(0.6, 0.8, -0.5))), 0.0);
        col = vec3(0.35, 0.85, 0.65) * (0.15 + 0.85 * diff);
        col += pow(1.0 - max(dot(n, -rd), 0.0), 3.0) * 0.5;
    }
    return vec4(col, 1.0);
}

void fragment()
{
    // Godot's UV starts at the top-left, the lessons use bottom-left
    COLOR = frag(vec2(UV.x, 1.0 - UV.y));
}

Open it in the editor and change it

Helper functions provided

The toolbox has: calcNormal(), smin(). Add them above your frag function with one click.

Solve it in your language

Unity (Built-in), Unity (URP), Unreal, Shadertoy / WebGL, OpenGL (LWJGL, raylib), three.js, GameMaker, LÖVE (Lua), Godot, Apple (iOS, macOS), WebGPU / Bevy.

Start coding

Questions about this shader

How do I make a metaballs (smooth union) shader?

Three blobs that merge like liquid. A good first step: start from the Raymarching lesson. Only map() changes (plus color). In ShaderBattle you build it in the browser with a live match bar, so you see how close you are after every change.

What techniques does the metaballs (smooth union) shader use?

It is built from ray marching with a signed distance field, sine waves, diffuse lighting. Each one is a small idea you can reuse in other effects.

Does this metaballs (smooth union) shader work in Unity, Unreal, Godot and Shadertoy?

Yes. The idea is the same in every shader language. ShaderBattle shows the code in HLSL (Unity, Unreal), GLSL (Shadertoy, three.js, OpenGL), WGSL (WebGPU), Metal and Godot, and exports a ready-to-paste version.