Mandelbulb
glint · Examples · Tutorials · Gallery
The 3D Mandelbrot: a power-8 bulb raymarched with a distance estimator, orbit-trap colouring and a breathing power.
Audio-reactive: it moves with music from a microphone, line-in or a file.

An example in glint, a free live shader editor in the browser: the GLSL code floats over the running picture and recompiles as you type.
The code
One pass, in GLSL.
fragment shader
#version 300 es
precision highp float;
uniform float time;
uniform vec2 resolution;
uniform vec2 mouse;
uniform vec3 spectrum;
uniform vec3 spectrumIntegrated;
uniform float beat;
uniform float power;
uniform float breathe;
in vec2 v_texcoord;
out vec4 fragColor;
mat2 rot(float a) { float c = cos(a), s = sin(a); return mat2(c, -s, s, c); }
// Ray from an orbiting camera: yaw from mouse.x (plus slow drift), pitch from mouse.y.
void orbit(vec2 uv, float dist, float drift, out vec3 ro, out vec3 rd) {
float yaw = (mouse.x - 0.5) * 6.2831 + time * drift;
float pitch = clamp((mouse.y - 0.5) * 2.5, -1.4, 1.4);
ro = vec3(0.0, 0.0, dist);
ro.yz *= rot(pitch);
ro.xz *= rot(yaw);
vec3 fw = normalize(-ro), rt = normalize(cross(fw, vec3(0, 1, 0))), up = cross(rt, fw);
rd = normalize(uv.x * rt + uv.y * up + 1.8 * fw);
}
// Distance estimator for the Mandelbulb (the "dr" running derivative gives distance = 0.5*log(r)*r/dr).
vec4 bulb(vec3 p) {
float pw = power + breathe * sin(time * 0.3) + spectrum.x * 2.0;
vec3 z = p;
float dr = 1.0, r = 0.0;
vec3 trap = vec3(1e9);
for (int i = 0; i < 8; i++) {
r = length(z);
if (r > 2.0) break;
float theta = acos(clamp(z.z / r, -1.0, 1.0)) * pw;
float phi = atan(z.y, z.x) * pw;
dr = pow(r, pw - 1.0) * pw * dr + 1.0;
z = pow(r, pw) * vec3(sin(theta) * cos(phi), sin(phi) * sin(theta), cos(theta)) + p;
trap = min(trap, abs(z));
}
return vec4(0.5 * log(r) * r / dr, trap);
}
vec3 normalAt(vec3 p, float t) {
// Epsilon grows with distance: finer than a pixel is just noise.
vec2 e = vec2(0.0015 * t, 0.0);
return normalize(vec3(bulb(p + e.xyy).x - bulb(p - e.xyy).x, bulb(p + e.yxy).x - bulb(p - e.yxy).x, bulb(p + e.yyx).x - bulb(p - e.yyx).x));
}
void main() {
vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
vec3 ro, rd;
orbit(uv, 3.1, 0.1, ro, rd);
float t = 0.0;
vec4 h = vec4(0.0);
float glow = 0.0;
for (int i = 0; i < 160; i++) {
h = bulb(ro + rd * t);
glow += exp(-h.x * 40.0);
if (h.x < 0.0005 * t || t > 6.0) break;
t += h.x * 0.75;
}
vec3 col = vec3(0.01, 0.01, 0.03) + glow * 0.003 * vec3(0.4, 0.6, 1.0) * (1.0 + 3.0 * beat);
if (t < 6.0) {
vec3 p = ro + rd * t, n = normalAt(p, t);
vec3 l = normalize(vec3(-0.5, 0.8, 0.6));
// Smooth colour from how close the orbit came to the origin (log of the trap distance).
float trap = log(length(h.yzw) + 1e-3);
vec3 base = 0.55 + 0.45 * cos(6.2831 * (trap * 0.35 + vec3(0.05, 0.25, 0.5) + time * 0.02));
float dif = max(dot(n, l), 0.0);
float rim = pow(1.0 - max(dot(n, -rd), 0.0), 3.0);
col = base * (0.15 + 0.85 * dif) + rim * vec3(0.4, 0.5, 1.0) + pow(max(dot(reflect(rd, n), l), 0.0), 32.0) * 0.5;
}
fragColor = vec4(pow(col, vec3(0.4545)), 1.0);
}More Showpieces examples
Lava world · Deep glow · Prism · Menger sponge · Volumetric clouds · Ocean · KIFS tunnel · Truchet weave