Here’s a fully‐ray-marched Mandelbulb fractal that orbits, pulses and glows in 3D. It uses DE (distance‐estimator) ray‐marching, soft shadows, ambient occlusion and basic specular lighting. Camera orbits the fractal and you get a surreal, evolving sculpture. Drop it into Shadertoy, hit “Play,” and tweak to your heart’s content!
//------------------------------------------------------------------------------
// "Mandelbulb Dream" by ChatGPT
// A ray-marched 3D Mandelbulb with soft shadows, AO & specular glow.
//------------------------------------------------------------------------------
// Shadertoy uniforms:
// iResolution (vec3), iTime (float), iMouse (vec4)
//------------------------------------------------------------------------------
#define MAX_STEPS 120
#define MAX_DIST 100.
#define SURF_DIST 0.001
// -- Distance Estimator for Mandelbulb ------------------------
float mandelbulbDE(vec3 p){
vec3 w = p;
float dr = 1.0;
float power = 8.0 + 2.0*sin(iTime*0.2); // animate power
float r = 0.0;
for(int i=0; i<12; i++){
r = length(w);
if(r>2.0) break;
// spherical coords
float theta = acos(w.z/r);
float phi = atan(w.y, w.x);
// scale & rotate the point
float zr = pow(r, power-1.0);
dr = zr * power * dr + 1.0;
float sin_t = sin(power*theta);
w = zr * vec3(sin_t*cos(power*phi),
sin_t*sin(power*phi),
cos(power*theta))
+ p;
}
return 0.5*log(r)*r/dr;
}
// -- Soft shadow by ray‐marching towards light ----------------
float softShadow(vec3 ro, vec3 rd){
float res = 1.0;
float t = 0.02;
for(int i=0; i<50; i++){
float h = mandelbulbDE(ro + rd*t);
if(h<0.001) return 0.0;
res = min(res, 8.0*h/t);
t += clamp(h, 0.02, 0.2);
if(t>20.0) break;
}
return clamp(res, 0.0, 1.0);
}
// -- Ambient Occlusion ---------------------------------------
float ambientOcclusion(vec3 p, vec3 n){
float occ = 0.0;
float sca = 1.0;
for(int i=1; i<=5; i++){
float h = mandelbulbDE(p + n*(0.1*float(i)));
occ += (0.1*float(i) - h)*sca;
sca *= 0.7;
}
return clamp(1.0 - occ, 0.0, 1.0);
}
// -- Estimate normal by gradient --------------------------------
vec3 estimateNormal(vec3 p){
const vec2 e = vec2(1e-3, -1e-3);
return normalize(
e.xyy * mandelbulbDE(p + e.xyy) +
e.yyx * mandelbulbDE(p + e.yyx) +
e.yxy * mandelbulbDE(p + e.yxy) +
e.xxx * mandelbulbDE(p + e.xxx)
);
}
// -- Main Raymarch ----------------------------------------------------
float rayMarch(vec3 ro, vec3 rd, out vec3 p){
float t = 0.0;
for(int i=0; i<MAX_STEPS; i++){
p = ro + rd * t;
float d = mandelbulbDE(p);
if(d < SURF_DIST) return t;
t += d;
if(t >= MAX_DIST) break;
}
return MAX_DIST;
}
// -- Camera setup -----------------------------------------------------
mat3 lookAt(vec3 ro, vec3 ta, float roll){
vec3 forward = normalize(ta-ro);
vec3 up = vec3(0.0, 1.0, 0.0);
vec3 right = normalize(cross(forward, up));
up = cross(right, forward);
return mat3(right, up, -forward);
}
// -- MainImage --------------------------------------------------------
void mainImage(out vec4 fragColor, in vec2 fragCoord){
// Normalized pixel coords (−1…+1)
vec2 uv = (fragCoord - 0.5*iResolution.xy)/iResolution.y;
// Camera
float t = iTime * 0.3;
vec3 camPos = vec3(6.0*sin(t), 2.5*sin(t*0.5), 6.0*cos(t));
vec3 target = vec3(0.0, 0.0, 0.0);
mat3 camMat = lookAt(camPos, target, 0.0);
vec3 rd = camMat * normalize(vec3(uv, -1.2));
// Raymarch
vec3 p;
float dist = rayMarch(camPos, rd, p);
// background
vec3 col = vec3(0.02, 0.04, 0.08) + 0.3*uv.y;
if(dist < MAX_DIST){
// hit!
vec3 n = estimateNormal(p);
// lighting
vec3 lightDir = normalize(vec3(0.5, 0.7, 0.2));
float diff = clamp(dot(n, lightDir), 0.0, 1.0);
float sh = softShadow(p + n*SURF_DIST*2.0, lightDir);
float ao = ambientOcclusion(p, n);
// specular
vec3 viewDir = normalize(camPos - p);
vec3 halfDir = normalize(lightDir + viewDir);
float spec = pow(clamp(dot(n, halfDir), 0.0, 1.0), 32.0);
// color palette
vec3 base = vec3(0.2, 0.6, 1.0);
vec3 accent = vec3(1.0, 0.4, 0.2);
vec3 surfaceCol = mix(base, accent, pow(diff,0.5));
col = surfaceCol * diff * sh * ao
+ vec3(1.2,0.9,0.7)*spec*sh; // glow on edges
}
// tone map & gamma
col = col / (col + vec3(1.0));
col = pow(col, vec3(0.4545));
fragColor = vec4(col,1.0);
}
How it works, briefly:
-
mandelbulbDE()
Implements a classic distance estimator for the Mandelbulb fractal, with a slight time-varying “power” to make the shape breathe.
-
rayMarch()
Steps along the ray until we’re within SURF_DIST of the surface or we exceed MAX_DIST.
-
Soft shadows & AO
Shooting secondary rays for shadowing and ambient occlusion gives a more solid, grounded look.
-
Camera
Orbits around the fractal. Feel free to hook up iMouse to look around interactively.
-
Shading
A mixture of lambert diffuse, Blinn-Phong specular, soft shadows and AO, with a simple two-tone palette (base and accent).
Drop this into Shadertoy (https://www.shadertoy.com/) and press ▶. Enjoy! ✨ Feel free to tweak iteration counts, colors or camera radius for your demo-party entry.