Hard RaymarchAnimated
Fractal: Mandelbulb
A slowly turning 3D fractal, shaded by how many iterations it took to escape.
About the Fractal: Mandelbulb shader
A slowly turning 3D fractal, shaded by how many iterations it took to escape. Ray marching draws 3D scenes inside a pixel shader by walking a ray through a distance function. No triangles needed.
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 952 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
- Distance estimator
uses pow, log, dr- Spin
a plain value or condition- Camera
uses normalize- March
a plain value or condition- Color
uses lerp · colours float3(0.1,0.2,0.4), float3(1.0,0.7,0.4)- Background
colours float3(0.02, 0.02, 0.05)
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 fractal: mandelbulb shader
- Sebastian Lague's fractal video: the "distance estimator" gives a safe step size for a shape with infinite detail.
- Per iteration: r = length(z); if (r > 2.0) break; theta = acos(z.z / r) * 8; phi = atan2(z.y, z.x) * 8; dr = pow(r, 7.0) * 8.0 * dr + 1.0; z = pow(r, 8.0) * float3(sin(theta) * cos(phi), sin(phi) * sin(theta), cos(theta)) + p. Return 0.5 * log(r) * r / dr.
Techniques used
- Polar coordinates: angle and distance from a center, for spirals, rings and radial effects.
- Ray marching with a signed distance field: each pixel walks a ray through a distance function until it hits a surface.
- Rotation: rotate coordinates with sine and cosine.
- Sine waves: smooth oscillation for waves, pulses and motion over time.
- lerp (mix): blend between two colors or values.
Fractal: Mandelbulb 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: map()
float map(float3 p0)
{
float a = time * 0.3;
float3 p = p0;
p.xz = mul(float2x2(cos(a), -sin(a), sin(a), cos(a)), p.xz);
float3 z = p;
float dr = 1.0;
float r = 0.0;
for (int i = 0; i < 6; i++)
{
r = length(z);
if (r > 2.0) break;
float theta = acos(clamp(z.z / r, -1.0, 1.0)) * 8.0;
float phi = atan2(z.y, z.x) * 8.0;
dr = pow(r, 7.0) * 8.0 * dr + 1.0;
z = pow(r, 8.0) * float3(sin(theta) * cos(phi), sin(phi) * sin(theta), cos(theta)) + p;
}
return 0.5 * log(r) * r / dr;
} - 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 2: 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, -2.6);
float3 rd = normalize(float3(p, 1.6)); - 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 3: Build the pattern
Still inside frag()
float t = 0.0;
float steps = 0.0; Part 4: The main loop
Still inside frag()
for (int i = 0; i < 80; i++)
{
float d = map(ro + rd * t);
t += d;
steps += 1.0;
if (d < 0.002 || t > 6.0) break;
} - The loop runs 80 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 5: Choose the colours
Still inside frag()
float3 col = float3(0.02, 0.02, 0.05); - float3(r, g, b) is a colour: each channel from 0 (none) to 1 (full).
Part 6: Choose the colours (continued)
Still inside frag()
if (t < 6.0)
{
float ao = 1.0 - steps / 80.0;
col = lerp(float3(0.1, 0.2, 0.4), float3(1.0, 0.7, 0.4), ao);
} - The code inside the braces only runs when the condition is true.
- lerp(a, b, t) blends from a to b. With a mask as t it paints one colour over another.
- float3(r, g, b) is a colour: each channel from 0 (none) to 1 (full).
Part 7: 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 map(float3 p0)
{
float a = time * 0.3;
float3 p = p0;
p.xz = mul(float2x2(cos(a), -sin(a), sin(a), cos(a)), p.xz);
float3 z = p;
float dr = 1.0;
float r = 0.0;
for (int i = 0; i < 6; i++)
{
r = length(z);
if (r > 2.0) break;
float theta = acos(clamp(z.z / r, -1.0, 1.0)) * 8.0;
float phi = atan2(z.y, z.x) * 8.0;
dr = pow(r, 7.0) * 8.0 * dr + 1.0;
z = pow(r, 8.0) * float3(sin(theta) * cos(phi), sin(phi) * sin(theta), cos(theta)) + p;
}
return 0.5 * log(r) * r / dr;
}
float4 frag(float2 uv)
{
float2 p = (uv - 0.5) * float2(aspect, 1.0) * 2.0;
float3 ro = float3(0.0, 0.0, -2.6);
float3 rd = normalize(float3(p, 1.6));
float t = 0.0;
float steps = 0.0;
for (int i = 0; i < 80; i++)
{
float d = map(ro + rd * t);
t += d;
steps += 1.0;
if (d < 0.002 || t > 6.0) break;
}
float3 col = float3(0.02, 0.02, 0.05);
if (t < 6.0)
{
float ao = 1.0 - steps / 80.0;
col = lerp(float3(0.1, 0.2, 0.4), float3(1.0, 0.7, 0.4), ao);
}
return float4(col, 1.0);
} GLSL Shadertoy, three.js, OpenGL
float map(vec3 p0)
{
float a = time * 0.3;
vec3 p = p0;
p.xz = (p.xz * mat2(cos(a), -sin(a), sin(a), cos(a)));
vec3 z = p;
float dr = 1.0;
float r = 0.0;
for (int i = 0; i < 6; i++)
{
r = length(z);
if (r > 2.0) break;
float theta = acos(clamp(z.z / r, -1.0, 1.0)) * 8.0;
float phi = atan(z.y, z.x) * 8.0;
dr = pow(r, 7.0) * 8.0 * dr + 1.0;
z = pow(r, 8.0) * vec3(sin(theta) * cos(phi), sin(phi) * sin(theta), cos(theta)) + p;
}
return 0.5 * log(r) * r / dr;
}
vec4 frag(vec2 uv)
{
vec2 p = (uv - 0.5) * vec2(aspect, 1.0) * 2.0;
vec3 ro = vec3(0.0, 0.0, -2.6);
vec3 rd = normalize(vec3(p, 1.6));
float t = 0.0;
float steps = 0.0;
for (int i = 0; i < 80; i++)
{
float d = map(ro + rd * t);
t += d;
steps += 1.0;
if (d < 0.002 || t > 6.0) break;
}
vec3 col = vec3(0.02, 0.02, 0.05);
if (t < 6.0)
{
float ao = 1.0 - steps / 80.0;
col = mix(vec3(0.1, 0.2, 0.4), vec3(1.0, 0.7, 0.4), ao);
}
return vec4(col, 1.0);
} WGSL WebGPU, Bevy
fn map(p0: vec3f) -> f32
{
let a = time * 0.3;
var p = p0;
let pxz = (p.xz * mat2x2f(cos(a), -sin(a), sin(a), cos(a))); p.x = pxz.x; p.z = pxz.y;
var z = p;
var dr = 1.0;
var r = 0.0;
for (var i = 0; i < 6; i++)
{
r = length(z);
if (r > 2.0) { break; }
let theta = acos(clamp(z.z / r, -1.0, 1.0)) * 8.0;
let phi = atan2(z.y, z.x) * 8.0;
dr = pow(r, 7.0) * 8.0 * dr + 1.0;
z = pow(r, 8.0) * vec3f(sin(theta) * cos(phi), sin(phi) * sin(theta), cos(theta)) + p;
}
return 0.5 * log(r) * r / dr;
}
fn frag(uv: vec2f) -> vec4f
{
let p = (uv - 0.5) * vec2f(aspect, 1.0) * 2.0;
let ro = vec3f(0.0, 0.0, -2.6);
let rd = normalize(vec3f(p, 1.6));
var t = 0.0;
var steps = 0.0;
for (var i = 0; i < 80; i++)
{
let d = map(ro + rd * t);
t += d;
steps += 1.0;
if (d < 0.002 || t > 6.0) { break; }
}
var col = vec3f(0.02, 0.02, 0.05);
if (t < 6.0)
{
let ao = 1.0 - steps / 80.0;
col = mix(vec3f(0.1, 0.2, 0.4), vec3f(1.0, 0.7, 0.4), ao);
}
return vec4f(col, 1.0);
} Godot Godot 4 shading language
shader_type canvas_item;
float map(vec3 p0)
{
float a = TIME * 0.3;
vec3 p = p0;
p.xz = (p.xz * mat2(cos(a), -sin(a), sin(a), cos(a)));
vec3 z = p;
float dr = 1.0;
float r = 0.0;
for (int i = 0; i < 6; i++)
{
r = length(z);
if (r > 2.0) break;
float theta = acos(clamp(z.z / r, -1.0, 1.0)) * 8.0;
float phi = atan(z.y, z.x) * 8.0;
dr = pow(r, 7.0) * 8.0 * dr + 1.0;
z = pow(r, 8.0) * vec3(sin(theta) * cos(phi), sin(phi) * sin(theta), cos(theta)) + p;
}
return 0.5 * log(r) * r / dr;
}
vec4 frag(vec2 uv)
{
vec2 p = (uv - 0.5) * vec2(aspect, 1.0) * 2.0;
vec3 ro = vec3(0.0, 0.0, -2.6);
vec3 rd = normalize(vec3(p, 1.6));
float t = 0.0;
float steps = 0.0;
for (int i = 0; i < 80; i++)
{
float d = map(ro + rd * t);
t += d;
steps += 1.0;
if (d < 0.002 || t > 6.0) break;
}
vec3 col = vec3(0.02, 0.02, 0.05);
if (t < 6.0)
{
float ao = 1.0 - steps / 80.0;
col = mix(vec3(0.1, 0.2, 0.4), vec3(1.0, 0.7, 0.4), ao);
}
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));
} Metal iOS, macOS
float map(float3 p0)
{
float a = time * 0.3;
float3 p = p0;
p.xz = (p.xz * float2x2(cos(a), -sin(a), sin(a), cos(a)));
float3 z = p;
float dr = 1.0;
float r = 0.0;
for (int i = 0; i < 6; i++)
{
r = length(z);
if (r > 2.0) break;
float theta = acos(clamp(z.z / r, -1.0, 1.0)) * 8.0;
float phi = atan2(z.y, z.x) * 8.0;
dr = pow(r, 7.0) * 8.0 * dr + 1.0;
z = pow(r, 8.0) * float3(sin(theta) * cos(phi), sin(phi) * sin(theta), cos(theta)) + p;
}
return 0.5 * log(r) * r / dr;
}
float4 frag(float2 uv)
{
float2 p = (uv - 0.5) * float2(aspect, 1.0) * 2.0;
float3 ro = float3(0.0, 0.0, -2.6);
float3 rd = normalize(float3(p, 1.6));
float t = 0.0;
float steps = 0.0;
for (int i = 0; i < 80; i++)
{
float d = map(ro + rd * t);
t += d;
steps += 1.0;
if (d < 0.002 || t > 6.0) break;
}
float3 col = float3(0.02, 0.02, 0.05);
if (t < 6.0)
{
float ao = 1.0 - steps / 80.0;
col = mix(float3(0.1, 0.2, 0.4), float3(1.0, 0.7, 0.4), ao);
}
return float4(col, 1.0);
} 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 codingQuestions about this shader
How do I make a fractal: Mandelbulb shader?
A slowly turning 3D fractal, shaded by how many iterations it took to escape. A good first step: sebastian Lague's fractal video: the "distance estimator" gives a safe step size for a shape with infinite detail. 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 fractal: Mandelbulb shader use?
It is built from polar coordinates, ray marching with a signed distance field, rotation, sine waves, lerp (mix). Each one is a small idea you can reuse in other effects.
Does this fractal: Mandelbulb 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.