Three.js Liquid Metal Shader (Ray-Traced Metaballs)

T-1000-style liquid metal in Three.js, with smooth-union metaballs merging into a molten pool, ray-traced with Fresnel-Schlick reflections, two bounces and a studio environment, in mercury, gold, copper or chrome.

Key topics: three.js liquid metal, metaballs, ray marching, reflections, fresnel schlick, mercury shader

What this code does

- Metaballs: several moving sphere SDFs and a flattened pool are blended with smooth-min, so blobs merge and split.
- Reflections: hits on metal reflect the ray and continue marching, giving self-reflection across blobs.
- Fresnel-Schlick: reflectance F0 per metal rises to white at grazing angles.
- Studio Lighting: the environment is a dark gradient with soft rectangular light boxes, which reads well on chrome.
- Ripples: a tiny animated displacement on the SDF makes the surface shimmer.
- Controls: blob count, merge radius, speed, surface ripples and metal type.

Continue Learning

JavaScript Implementation

ES6+
// Metaballs via smooth-min of sphere SDFs, shaded as a perfect reflector with Fresnel-Schlick
const metalGLSL = `
  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); }
  float map (vec3 p) {
    float d = 1e9;
    for (int i = 0; i < 6; i++) {
      vec3 c = vec3(sin(uTime + float(i) * 1.7), 0.9 + sin(uTime * 0.7 + float(i)) * 0.7, cos(uTime * 0.4 + float(i) * 2.3));
      d = smin(d, length(p - c) - 0.45, 0.55);
    }
    return d;
  }
  // after hitting: n = gradient of map; F = F0 + (1 - F0) * pow(1 - dot(n, -rd), 5); color = F * env(reflect(rd, n))`