Deep Zoom
A dive into the Mandelbrot set, from the whole set down to a view 10²⁷ times smaller, toward a point where three arms of the set meet. A float keeps about seven digits, so adding each pixel's tiny offset to the center stops telling neighbors apart after about five powers of ten, and the picture turns to blocks. Here each pixel follows only its difference from the center's own orbit, worked out ahead, and the dive goes on sharp to the end. A slider brings the plain sum back on the left of the screen, to watch it fail beside the way that works, and a Shepard tone steps down a semitone each time the picture comes back whole.
Teaches The Mandelbrot set zoomed until float runs out, and how to go further
Builds on Flowing Color Field, Fractal Glow (complex square, complex multiply), Wood & Marble (hash11)
Watch
The idea
The Mandelbrot set is the simplest loop in this series that never runs out of picture. Each pixel is a complex number c; start at z = 0, square z and add c, over and over, and see whether z flies off. Fractal Glow ran the same loop with a fixed c and a moving start, and drew Julia sets; here the start is fixed at 0 and c is the pixel. The points that never fly off are the set. Its edge has new detail at every scale, so a zoom into it can go on forever.
A GPU can't follow it forever. A 32-bit float keeps 24 binary digits, about seven decimal ones, and the zoom's center here is c = −0.1011 + 0.9563i. Near 0.956 a float can only step by 2⁻²⁴, about 6·10⁻⁸. Zoom in until one pixel is smaller than that step, and the plain sum c = center + offset rounds neighboring pixels to the same c: they all get the same color, and the picture turns to blocks. At 720 rows that happens about 4.8 powers of ten in.
The way past it is to never add the tiny offset to the big center at all. One orbit, the center's own, is known ahead with as many digits as it needs. Every pixel follows only how far its own z is from that orbit. That difference is small, and a float keeps all seven digits of a small number however small it is, so the dive here goes on to a view 10²⁷ times smaller than the whole set, sharp all the way. This is perturbation, and it is how deep zooms into the set are made.
The center is chosen so the picture never runs thin: a Misiurewicz point, where three arms of the set meet. Its orbit lands on a fixed point after four steps, and near it the set repeats itself each 1.328 times closer, turned by about 120°. The zoom takes a minute and does not loop. It is built in four steps, each a shader you can run on its own.
Stage 1 · The set
Each pixel's place, q, runs from −0.5 to 0.5 up the picture (wider across), turned by turn · 0.15 radians a second, and c = START + q · VIEW puts the middle of the picture at −0.6 on the plane and makes it 3 units tall, enough for the whole set. A complex number is a pair (x, y), and csqr squares one: (x² − y², 2xy), its length squared and its angle doubled.
The loop is z = csqr(z) + c, up to 320 times, stopping when |z| passes 256. Points that stop escaped, and the number of steps they took, n, says how near the set they are. As in Fractal Glow, n + 1 − log2(log2|z|) joins the whole steps into a smooth count: far out, each step squares |z|, so log2(log2|z|) grows by one a step. That count, less 3, over 22, raised to 0.8, is the place in the kiln palette from Flowing Color Field, so the space just outside the set glows hottest. Points that never escape are the set itself, near-black. heat slides both along the palette.
The sound for now is a low drone on A, 55 Hz with the E above it at 82.5 Hz.
stage-1-the-set.metal
// Stage 1 · The set
// Each pixel is a complex number c. Start at z = 0 and square and add c
// over and over: if z flies off, c is outside the Mandelbrot set. The
// smooth count of steps it took colors the pixel through the kiln
// palette; the points that never leave are near-black.
// @param turn 0.5 -2 2 How fast the view turns
// @param heat 0.5 0 1 Slides the palette from deep ember to white-hot
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
// The middle of the whole set.
constant float2 START = float2(-0.6, 0.0);
// The view's height, in units of the plane.
constant float VIEW = 3.0;
// The most times a point is squared.
constant int STEPS = 320;
// A complex number is a pair (x, y), x + iy, with i * i = -1.
// Squaring one doubles its angle and squares its length.
float2 csqr(float2 z) {
return float2(z.x * z.x - z.y * z.y, 2.0 * z.x * z.y);
}
// The kiln palette from Flowing Color Field.
float3 kiln(float t) {
float3 c0 = float3(0.020, 0.012, 0.020);
float3 c1 = float3(0.280, 0.040, 0.030);
float3 c2 = float3(0.880, 0.260, 0.050);
float3 c3 = float3(1.000, 0.680, 0.200);
float3 c4 = float3(1.000, 0.970, 0.840);
t = clamp(t, 0.0, 1.0);
float3 c = mix(c0, c1, smoothstep(0.00, 0.22, t));
c = mix(c, c2, smoothstep(0.22, 0.50, t));
c = mix(c, c3, smoothstep(0.50, 0.76, t));
return mix(c, c4, smoothstep(0.76, 1.00, t));
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float a = p.turn * 0.15 * u.time;
float2 q = (uv - 0.5) * u.resolution / u.resolution.y;
q = float2(cos(a) * q.x - sin(a) * q.y, sin(a) * q.x + cos(a) * q.y);
float2 c = START + q * VIEW;
float2 z = float2(0.0);
float n = 0.0;
bool escaped = false;
for (int i = 0; i < STEPS; i++) {
z = csqr(z) + c;
if (dot(z, z) > 256.0 * 256.0) { escaped = true; break; }
n += 1.0;
}
float3 col = kiln(0.03 + (p.heat - 0.5) * 0.2);
if (escaped) {
// Far out, each step squares |z|, so log2(log2|z|) grows by one
// a step: taking it away joins the steps into a smooth slope.
float smoothN = n + 1.0 - log2(log2(length(z)));
float level = 0.95 * pow(clamp((smoothN - 3.0) / 22.0, 0.0, 1.0), 0.8);
col = kiln(level + (p.heat - 0.5) * 0.5);
}
return float4(col, 1.0);
}
// ---- The sound ----------------------------------------------------------
// A low drone on A, with its fifth.
float2 sound(float t, constant Params& p) {
float drone = sin(TAU * 55.0 * t) + 0.4 * sin(TAU * 82.5 * t);
drone *= 0.12;
float fade = smoothstep(0.0, 1.5, t) * (1.0 - smoothstep(58.5, 60.0, t));
return tanh(float2(drone) * 1.5) * 0.85 * fade * p.volume;
}Stage 2 · Diving in
Now the view shrinks. deepDecades says how deep the zoom is, in powers of ten: deepest · smoothstep(0, 60, t · speed), slow to start, steady in the middle and slowing to a stop at deepest after 60 seconds at speed 1. The view is 3 · 10^−d tall. Each pixel's offset from the zoom's center is dc = q · height, and c = CENTER + dc.
The view starts on the whole set, not on the center, so dc also carries (START − CENTER) · 10^−2d: at depth 0 that puts the middle of the picture back at −0.6, and it shrinks twice as fast as the view, so the picture glides onto the center as it dives.
Deeper, every pixel takes more steps to escape. Near the center each step multiplies a pixel's distance from the center's orbit by 1.328, so every power of ten deeper costs PER_DECADE = 1 / log10(1.328), about 8.1, more steps. rel takes them away from the smooth count before the color is picked, so each level of the zoom is colored like the one before. Here deepest is 4: the view ends 10,000 times smaller than the whole set, which a float can still draw.
stage-2-diving-in.metal55 new lines since stage 1
// Stage 2 · Diving in
// The view shrinks a power of ten at a time toward one point where
// three arms meet: c = center + offset. Deeper, every pixel takes more
// steps to fly off, so the count is measured from what that depth costs
// and each level looks alike.
// @param speed 1 0 2 How fast the zoom dives: 1 takes the whole minute
// @param deepest 4 4 30 How deep the zoom goes, in powers of ten
// @param turn 0.5 -2 2 How fast the view turns
// @param heat 0.5 0 1 Slides the palette from deep ember to white-hot
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
// Where the zoom starts: the middle of the whole set.
constant float2 START = float2(-0.6, 0.0);
// The view's height when the zoom starts, in units of the plane.
constant float VIEW = 3.0;
// The most times a point is squared.
constant int STEPS = 320;
// The center, a Misiurewicz point where three arms meet.
constant float2 CENTER = float2(-0.101096364, 0.956286511);
// What each power of ten deeper costs: 1 / log10(1.328) steps.
constant float PER_DECADE = 8.111;
// A complex number is a pair (x, y), x + iy, with i * i = -1.
// Squaring one doubles its angle and squares its length.
float2 csqr(float2 z) {
return float2(z.x * z.x - z.y * z.y, 2.0 * z.x * z.y);
}
// The kiln palette from Flowing Color Field.
float3 kiln(float t) {
float3 c0 = float3(0.020, 0.012, 0.020);
float3 c1 = float3(0.280, 0.040, 0.030);
float3 c2 = float3(0.880, 0.260, 0.050);
float3 c3 = float3(1.000, 0.680, 0.200);
float3 c4 = float3(1.000, 0.970, 0.840);
t = clamp(t, 0.0, 1.0);
float3 c = mix(c0, c1, smoothstep(0.00, 0.22, t));
c = mix(c, c2, smoothstep(0.22, 0.50, t));
c = mix(c, c3, smoothstep(0.50, 0.76, t));
return mix(c, c4, smoothstep(0.76, 1.00, t));
}
// Depth in powers of ten: slow, steady, then still.
float deepDecades(float t, constant Params& p) {
return p.deepest * smoothstep(0.0, 60.0, t * p.speed);
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float d = deepDecades(u.time, p);
float height = VIEW * pow(10.0, -d);
float a = p.turn * 0.15 * u.time;
float2 q = (uv - 0.5) * u.resolution / u.resolution.y;
q = float2(cos(a) * q.x - sin(a) * q.y, sin(a) * q.x + cos(a) * q.y);
// Each pixel's offset from the center.
float2 dc = q * height + (START - CENTER) * pow(10.0, -2.0 * d);
float2 c = CENTER + dc;
float2 z = float2(0.0);
float n = 0.0;
bool escaped = false;
for (int i = 0; i < STEPS; i++) {
z = csqr(z) + c;
if (dot(z, z) > 256.0 * 256.0) { escaped = true; break; }
n += 1.0;
}
float3 col = kiln(0.03 + (p.heat - 0.5) * 0.2);
if (escaped) {
float smoothN = n + 1.0 - log2(log2(length(z)));
// Take away what the depth costs: every level is colored alike.
float rel = smoothN - PER_DECADE * d;
float level = 0.95 * pow(clamp((rel - 3.0) / 22.0, 0.0, 1.0), 0.8);
col = kiln(level + (p.heat - 0.5) * 0.5);
}
return float4(col, 1.0);
}
// ---- The sound ----------------------------------------------------------
// Every three levels of 1.328 the picture comes back whole. Each time,
// a Shepard tone steps down a semitone: octaves of one note under a
// fixed curve of loudness, so it seems to fall forever.
// Powers of ten per whole return: 3 * log10(1.328).
constant float RETURN = 0.36987;
float hash11(float n) {
return fract(sin(n * 12.9898) * 43758.5453);
}
// Noise with nothing above about `rate` Hz in it, from hashes of the time.
float deepNoise(float t, float rate) {
float x = t * rate;
float i = floor(x), f = fract(x);
f = f * f * (3.0 - 2.0 * f);
return mix(hash11(i), hash11(i + 1.0), f) * 2.0 - 1.0;
}
// When the zoom reaches depth d: smoothstep turned inside out.
float deepWhen(float d, constant Params& p) {
float s = clamp(d / p.deepest, 0.0, 1.0);
float x = 0.5 - sin(asin(1.0 - 2.0 * s) / 3.0);
return 60.0 * x / max(p.speed, 1e-3);
}
// Shepard tone k: eight octaves of one note, loudest near 440 Hz.
float deepShepard(float k, float age) {
float x = fract(-k / 12.0);
float s = 0.0;
for (int o = 0; o < 8; o++) {
float pos = float(o) + x;
float f = 27.5 * exp2(pos);
float w = exp(-(pos - 4.0) * (pos - 4.0) / 3.4);
s += w * sin(TAU * f * age);
}
float env = (1.0 - exp(-age * 120.0)) * exp(-age * 3.5);
return s * env;
}
float2 sound(float t, constant Params& p) {
float d = deepDecades(t, p);
float now = floor(d / RETURN);
float2 notes = float2(0.0);
// The step just passed and the seven before it, still ringing.
for (int back = 0; back < 8; back++) {
float k = now - float(back);
if (k < 1.0) continue;
float age = t - deepWhen(k * RETURN, p);
if (age < 0.0 || age > 3.0) continue;
float pan = 0.5 + 0.3 * sin(k * 2.0944);
notes += float2(1.0 - pan, pan) * deepShepard(k, age) * 0.3;
}
// Wind, as loud as the zoom is fast.
float rate = (deepDecades(t + 0.05, p) - deepDecades(t - 0.05, p)) * 10.0;
float wind = clamp(rate / 1.2, 0.0, 1.0);
float2 air = float2(deepNoise(t, 700.0), deepNoise(t + 5.3, 700.0));
air += 0.5 * float2(deepNoise(t, 2300.0), deepNoise(t + 2.1, 2300.0));
air *= 0.05 * wind;
// A low drone on A, with its fifth.
float drone = sin(TAU * 55.0 * t) + 0.4 * sin(TAU * 82.5 * t);
drone *= 0.12;
float fade = smoothstep(0.0, 1.5, t) * (1.0 - smoothstep(58.5, 60.0, t));
return tanh((notes + air + drone) * 1.5) * 0.85 * fade * p.volume;
}3 lines from stage 1 that are gone
float2 c = START + q * VIEW;
⋯
float level = 0.95 * pow(clamp((smoothN - 3.0) / 22.0, 0.0, 1.0), 0.8);
⋯
return tanh(float2(drone) * 1.5) * 0.85 * fade * p.volume;Stage 3 · The wall
The same plain sum, taken deeper: deepest is 8, and speed is 6, so the dive takes 10 seconds instead of 60 and the wall comes soon. Near y ≈ 0.956 the floats are 2⁻²⁴ apart. Once a pixel, 3 · 10^−d / rows, is smaller than that, several rows of pixels round to one c. wall works out where: log10(3 · 2^24 / rows), 4.84 powers of ten at 720 rows, 4.67 at 1080. Across, x ≈ −0.101 is in a range of floats eight times finer (2⁻²⁷), so the blocks come out about eight times taller than they are wide, turned with the view.
deepGauge draws a bar along the bottom: 30 powers of ten across the middle three-fifths of the picture, a tick every five, filled up to the present depth, gold before the wall and orange past it, with a cream mark at wall. It is 6 pixels of a 720-line picture tall, scaled with the picture. Press Run it live and watch the fill: at 720 rows it passes the mark at about 5.7 seconds, by 6.5 the arms break up into blocks, by 7 the blocks are plain, and by 10 the picture is a few broad bands of flat color. The 4-second clip here stops before any of that.
stage-3-the-wall.metal16 new lines since stage 2
// Stage 3 · The wall
// A float keeps about seven digits. Near 0.956 they round to steps of
// 2^-24, so once a pixel is smaller than that step, neighbors get the
// same c and the picture turns to blocks. A gauge counts the powers of
// ten and marks the wall.
// @param speed 6 0 6 How fast the zoom dives: 1 takes the whole minute
// @param deepest 8 4 30 How deep the zoom goes, in powers of ten
// @param turn 0.5 -2 2 How fast the view turns
// @param heat 0.5 0 1 Slides the palette from deep ember to white-hot
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
// Where the zoom starts: the middle of the whole set.
constant float2 START = float2(-0.6, 0.0);
// The view's height when the zoom starts, in units of the plane.
constant float VIEW = 3.0;
// The most times a point is squared.
constant int STEPS = 320;
// The center, a Misiurewicz point where three arms meet.
constant float2 CENTER = float2(-0.101096364, 0.956286511);
// What each power of ten deeper costs: 1 / log10(1.328) steps.
constant float PER_DECADE = 8.111;
// A complex number is a pair (x, y), x + iy, with i * i = -1.
// Squaring one doubles its angle and squares its length.
float2 csqr(float2 z) {
return float2(z.x * z.x - z.y * z.y, 2.0 * z.x * z.y);
}
// The kiln palette from Flowing Color Field.
float3 kiln(float t) {
float3 c0 = float3(0.020, 0.012, 0.020);
float3 c1 = float3(0.280, 0.040, 0.030);
float3 c2 = float3(0.880, 0.260, 0.050);
float3 c3 = float3(1.000, 0.680, 0.200);
float3 c4 = float3(1.000, 0.970, 0.840);
t = clamp(t, 0.0, 1.0);
float3 c = mix(c0, c1, smoothstep(0.00, 0.22, t));
c = mix(c, c2, smoothstep(0.22, 0.50, t));
c = mix(c, c3, smoothstep(0.50, 0.76, t));
return mix(c, c4, smoothstep(0.76, 1.00, t));
}
// Depth in powers of ten: slow, steady, then still.
float deepDecades(float t, constant Params& p) {
return p.deepest * smoothstep(0.0, 60.0, t * p.speed);
}
// A gauge: depth out of 30 powers of ten, a tick every five, and a
// cream mark at the wall.
float3 deepGauge(float2 uv, float3 col, float d, float wall,
constant Uniforms& u) {
// In pixels of a 720-line picture.
float y = (uv.y - 0.04) * 720.0;
float x = (uv.x - 0.2) / 0.6;
if (abs(y) > 3.0 || x < 0.0 || x > 1.0) return col;
float at = x * 30.0;
float3 bar = at < d ? kiln(at < wall ? 0.72 : 0.5) : kiln(0.12);
float apart = 0.02 * u.resolution.x;
float tick = abs(at - 5.0 * floor(at / 5.0 + 0.5)) * apart;
bar = mix(bar, kiln(0.3), step(tick, 0.6) * step(1.0, abs(y)));
float mark = abs(at - wall) * apart;
bar = mix(bar, kiln(0.97), 1.0 - smoothstep(0.6, 1.6, mark));
return mix(col, bar, 0.85);
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float d = deepDecades(u.time, p);
float height = VIEW * pow(10.0, -d);
float a = p.turn * 0.15 * u.time;
float2 q = (uv - 0.5) * u.resolution / u.resolution.y;
q = float2(cos(a) * q.x - sin(a) * q.y, sin(a) * q.x + cos(a) * q.y);
// Each pixel's offset from the center.
float2 dc = q * height + (START - CENTER) * pow(10.0, -2.0 * d);
float2 c = CENTER + dc;
float2 z = float2(0.0);
float n = 0.0;
bool escaped = false;
for (int i = 0; i < STEPS; i++) {
z = csqr(z) + c;
if (dot(z, z) > 256.0 * 256.0) { escaped = true; break; }
n += 1.0;
}
float3 col = kiln(0.03 + (p.heat - 0.5) * 0.2);
if (escaped) {
float smoothN = n + 1.0 - log2(log2(length(z)));
// Take away what the depth costs: every level is colored alike.
float rel = smoothN - PER_DECADE * d;
float level = 0.95 * pow(clamp((rel - 3.0) / 22.0, 0.0, 1.0), 0.8);
col = kiln(level + (p.heat - 0.5) * 0.5);
}
// The wall: the depth where a pixel shrinks below float's step near
// the center, 2^-24.
float wall = log2(VIEW * 16777216.0 / u.resolution.y) * 0.30103;
col = deepGauge(uv, col, d, wall, u);
return float4(col, 1.0);
}
// ---- The sound ----------------------------------------------------------
// Every three levels of 1.328 the picture comes back whole. Each time,
// a Shepard tone steps down a semitone: octaves of one note under a
// fixed curve of loudness, so it seems to fall forever.
// Powers of ten per whole return: 3 * log10(1.328).
constant float RETURN = 0.36987;
float hash11(float n) {
return fract(sin(n * 12.9898) * 43758.5453);
}
// Noise with nothing above about `rate` Hz in it, from hashes of the time.
float deepNoise(float t, float rate) {
float x = t * rate;
float i = floor(x), f = fract(x);
f = f * f * (3.0 - 2.0 * f);
return mix(hash11(i), hash11(i + 1.0), f) * 2.0 - 1.0;
}
// When the zoom reaches depth d: smoothstep turned inside out.
float deepWhen(float d, constant Params& p) {
float s = clamp(d / p.deepest, 0.0, 1.0);
float x = 0.5 - sin(asin(1.0 - 2.0 * s) / 3.0);
return 60.0 * x / max(p.speed, 1e-3);
}
// Shepard tone k: eight octaves of one note, loudest near 440 Hz.
float deepShepard(float k, float age) {
float x = fract(-k / 12.0);
float s = 0.0;
for (int o = 0; o < 8; o++) {
float pos = float(o) + x;
float f = 27.5 * exp2(pos);
float w = exp(-(pos - 4.0) * (pos - 4.0) / 3.4);
s += w * sin(TAU * f * age);
}
float env = (1.0 - exp(-age * 120.0)) * exp(-age * 3.5);
return s * env;
}
float2 sound(float t, constant Params& p) {
float d = deepDecades(t, p);
float now = floor(d / RETURN);
float2 notes = float2(0.0);
// The step just passed and the seven before it, still ringing.
for (int back = 0; back < 8; back++) {
float k = now - float(back);
if (k < 1.0) continue;
float age = t - deepWhen(k * RETURN, p);
if (age < 0.0 || age > 3.0) continue;
float pan = 0.5 + 0.3 * sin(k * 2.0944);
notes += float2(1.0 - pan, pan) * deepShepard(k, age) * 0.3;
}
// Wind, as loud as the zoom is fast.
float rate = (deepDecades(t + 0.05, p) - deepDecades(t - 0.05, p)) * 10.0;
float wind = clamp(rate / 1.2, 0.0, 1.0);
float2 air = float2(deepNoise(t, 700.0), deepNoise(t + 5.3, 700.0));
air += 0.5 * float2(deepNoise(t, 2300.0), deepNoise(t + 2.1, 2300.0));
air *= 0.05 * wind;
// A low drone on A, with its fifth.
float drone = sin(TAU * 55.0 * t) + 0.4 * sin(TAU * 82.5 * t);
drone *= 0.12;
float fade = smoothstep(0.0, 1.5, t) * (1.0 - smoothstep(58.5, 60.0, t));
return tanh((notes + air + drone) * 1.5) * 0.85 * fade * p.volume;
}Stage 4 · A reference orbit
Never add the tiny offset to a big number. Call the center's orbit Z and the pixel's z = Z + dz. The center steps as Z' = Z² + C and the pixel as z' = z² + C + dc; take one from the other and
dz' = 2 Z dz + dz² + dc
Every term in it is small when dz and dc are, so it never rounds the offset away. cmul multiplies two complex numbers (the lengths multiply and the angles add), and the loop steps dz with deepRef(m) for Z, then forms z = deepRef(m) + dz only to test for escape and to color.
deepRef is the center's orbit, worked out ahead to 50 digits and rounded to float: 0, the center itself, then two more points, and from the fourth step on the fixed point −0.3276 + 0.5778i, where it stays. So the reference is never stepped in float, and no error builds up along it. At that fixed point 2Z is a turn of about 120° and a stretch of 1.328: that is the factor the whole zoom repeats by, and where PER_DECADE comes from.
Here deepest is 27. Even at 30, the slider's end, a pixel is about 10⁻³³ of the plane, and a float still holds all seven digits of a number that small (floats reach down to about 10⁻³⁸). speed is 2, so in this stage the dive reaches 27 at 30 seconds and holds: the gauge's fill passes the cream mark at about 8 seconds at 720 rows, and the picture stays sharp.
stage-4-a-reference-orbit.metal15 new lines since stage 3
// Stage 4 · A reference orbit
// Never add the tiny offset to a big number. The center's own orbit Z
// is known ahead, and each pixel follows only its difference dz from
// it. A small number keeps all seven digits however small it is, so the
// zoom goes past the wall.
// @param speed 2 0 2 How fast the zoom dives: 1 takes the whole minute
// @param deepest 27 4 30 How deep the zoom goes, in powers of ten
// @param turn 0.5 -2 2 How fast the view turns
// @param heat 0.5 0 1 Slides the palette from deep ember to white-hot
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
// Where the zoom starts: the middle of the whole set.
constant float2 START = float2(-0.6, 0.0);
// The view's height when the zoom starts, in units of the plane.
constant float VIEW = 3.0;
// The most times a point is squared.
constant int STEPS = 320;
// The center, a Misiurewicz point where three arms meet.
constant float2 CENTER = float2(-0.101096364, 0.956286511);
// What each power of ten deeper costs: 1 / log10(1.328) steps.
constant float PER_DECADE = 8.111;
// A complex number is a pair (x, y), x + iy, with i * i = -1.
// Squaring one doubles its angle and squares its length.
float2 csqr(float2 z) {
return float2(z.x * z.x - z.y * z.y, 2.0 * z.x * z.y);
}
// Multiplying: the angles add, the lengths multiply.
float2 cmul(float2 a, float2 b) {
return float2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
}
// The kiln palette from Flowing Color Field.
float3 kiln(float t) {
float3 c0 = float3(0.020, 0.012, 0.020);
float3 c1 = float3(0.280, 0.040, 0.030);
float3 c2 = float3(0.880, 0.260, 0.050);
float3 c3 = float3(1.000, 0.680, 0.200);
float3 c4 = float3(1.000, 0.970, 0.840);
t = clamp(t, 0.0, 1.0);
float3 c = mix(c0, c1, smoothstep(0.00, 0.22, t));
c = mix(c, c2, smoothstep(0.22, 0.50, t));
c = mix(c, c3, smoothstep(0.50, 0.76, t));
return mix(c, c4, smoothstep(0.76, 1.00, t));
}
// Depth in powers of ten: slow, steady, then still.
float deepDecades(float t, constant Params& p) {
return p.deepest * smoothstep(0.0, 60.0, t * p.speed);
}
// The center's orbit, worked out ahead to 50 digits: from step 4 on it
// sits on a fixed point, so it is never stepped.
float2 deepRef(int n) {
if (n == 1) return float2(-0.101096364, 0.956286511);
if (n == 2) return float2(-1.005359780, 0.762932333);
if (n == 3) return float2(0.327586179, -0.577756453);
if (n >= 4) return float2(-0.327586179, 0.577756453);
return float2(0.0, 0.0);
}
// A gauge: depth out of 30 powers of ten, a tick every five, and a
// cream mark at the wall.
float3 deepGauge(float2 uv, float3 col, float d, float wall,
constant Uniforms& u) {
// In pixels of a 720-line picture.
float y = (uv.y - 0.04) * 720.0;
float x = (uv.x - 0.2) / 0.6;
if (abs(y) > 3.0 || x < 0.0 || x > 1.0) return col;
float at = x * 30.0;
float3 bar = at < d ? kiln(at < wall ? 0.72 : 0.5) : kiln(0.12);
float apart = 0.02 * u.resolution.x;
float tick = abs(at - 5.0 * floor(at / 5.0 + 0.5)) * apart;
bar = mix(bar, kiln(0.3), step(tick, 0.6) * step(1.0, abs(y)));
float mark = abs(at - wall) * apart;
bar = mix(bar, kiln(0.97), 1.0 - smoothstep(0.6, 1.6, mark));
return mix(col, bar, 0.85);
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float d = deepDecades(u.time, p);
float height = VIEW * pow(10.0, -d);
float a = p.turn * 0.15 * u.time;
float2 q = (uv - 0.5) * u.resolution / u.resolution.y;
q = float2(cos(a) * q.x - sin(a) * q.y, sin(a) * q.x + cos(a) * q.y);
// dc is this pixel's offset from the center: tiny, but kept whole.
// The view starts on the whole set and drifts onto the center.
float2 dc = q * height + (START - CENTER) * pow(10.0, -2.0 * d);
float2 z = float2(0.0);
float2 dz = float2(0.0);
int m = 0;
float n = 0.0;
bool escaped = false;
for (int i = 0; i < STEPS; i++) {
// The pixel's z is Z + dz and Z' = Z^2 + C, so
// dz' = 2 Z dz + dz^2 + dc: nothing big is added.
dz = 2.0 * cmul(deepRef(m), dz) + csqr(dz) + dc;
m = min(m + 1, 4);
z = deepRef(m) + dz;
if (dot(z, z) > 256.0 * 256.0) { escaped = true; break; }
n += 1.0;
}
float3 col = kiln(0.03 + (p.heat - 0.5) * 0.2);
if (escaped) {
float smoothN = n + 1.0 - log2(log2(length(z)));
// Take away what the depth costs: every level is colored alike.
float rel = smoothN - PER_DECADE * d;
float level = 0.95 * pow(clamp((rel - 3.0) / 22.0, 0.0, 1.0), 0.8);
col = kiln(level + (p.heat - 0.5) * 0.5);
}
// The wall: the depth where a pixel shrinks below float's step near
// the center, 2^-24.
float wall = log2(VIEW * 16777216.0 / u.resolution.y) * 0.30103;
col = deepGauge(uv, col, d, wall, u);
return float4(col, 1.0);
}
// ---- The sound ----------------------------------------------------------
// Every three levels of 1.328 the picture comes back whole. Each time,
// a Shepard tone steps down a semitone: octaves of one note under a
// fixed curve of loudness, so it seems to fall forever.
// Powers of ten per whole return: 3 * log10(1.328).
constant float RETURN = 0.36987;
float hash11(float n) {
return fract(sin(n * 12.9898) * 43758.5453);
}
// Noise with nothing above about `rate` Hz in it, from hashes of the time.
float deepNoise(float t, float rate) {
float x = t * rate;
float i = floor(x), f = fract(x);
f = f * f * (3.0 - 2.0 * f);
return mix(hash11(i), hash11(i + 1.0), f) * 2.0 - 1.0;
}
// When the zoom reaches depth d: smoothstep turned inside out.
float deepWhen(float d, constant Params& p) {
float s = clamp(d / p.deepest, 0.0, 1.0);
float x = 0.5 - sin(asin(1.0 - 2.0 * s) / 3.0);
return 60.0 * x / max(p.speed, 1e-3);
}
// Shepard tone k: eight octaves of one note, loudest near 440 Hz.
float deepShepard(float k, float age) {
float x = fract(-k / 12.0);
float s = 0.0;
for (int o = 0; o < 8; o++) {
float pos = float(o) + x;
float f = 27.5 * exp2(pos);
float w = exp(-(pos - 4.0) * (pos - 4.0) / 3.4);
s += w * sin(TAU * f * age);
}
float env = (1.0 - exp(-age * 120.0)) * exp(-age * 3.5);
return s * env;
}
float2 sound(float t, constant Params& p) {
float d = deepDecades(t, p);
float now = floor(d / RETURN);
float2 notes = float2(0.0);
// The step just passed and the seven before it, still ringing.
for (int back = 0; back < 8; back++) {
float k = now - float(back);
if (k < 1.0) continue;
float age = t - deepWhen(k * RETURN, p);
if (age < 0.0 || age > 3.0) continue;
float pan = 0.5 + 0.3 * sin(k * 2.0944);
notes += float2(1.0 - pan, pan) * deepShepard(k, age) * 0.3;
}
// Wind, as loud as the zoom is fast.
float rate = (deepDecades(t + 0.05, p) - deepDecades(t - 0.05, p)) * 10.0;
float wind = clamp(rate / 1.2, 0.0, 1.0);
float2 air = float2(deepNoise(t, 700.0), deepNoise(t + 5.3, 700.0));
air += 0.5 * float2(deepNoise(t, 2300.0), deepNoise(t + 2.1, 2300.0));
air *= 0.05 * wind;
// A low drone on A, with its fifth.
float drone = sin(TAU * 55.0 * t) + 0.4 * sin(TAU * 82.5 * t);
drone *= 0.12;
float fade = smoothstep(0.0, 1.5, t) * (1.0 - smoothstep(58.5, 60.0, t));
return tanh((notes + air + drone) * 1.5) * 0.85 * fade * p.volume;
}2 lines from stage 3 that are gone
float2 c = CENTER + dc;
⋯
z = csqr(z) + c;Stage 5 · Deep Zoom
The finished shader lights the edge and brings back the plain sum to compare.
- A glowing edge. Alongside
z, the loop keepsder, how fastzmoves as the pixel moves:2 · z · der + pxeach step, withpxone pixel in units of the plane, soderis measured in pixels. Then0.5 · |z| · log|z| / |der|is the distance estimate from Fractal Glow, in pixels, and within a few of them the edge glows:exp(−dist / (1 + 4 · glow)), its strength0.2 + 0.6 · glow. Atspeed1 the dive takes the whole minute again. - The plain way, on the left. Pixels with
uv.x < naivetakez = csqr(z) + cwithc = CENTER + dc, as in stage 3, and the rest take the reference orbit. A thin cream line marks where one ends and the other begins. At 0, the default, the whole screen dives the deep way.
the kiln palette and shade(), from the finished shader
// The kiln palette from Flowing Color Field.
float3 kiln(float t) {
float3 c0 = float3(0.020, 0.012, 0.020);
float3 c1 = float3(0.280, 0.040, 0.030);
float3 c2 = float3(0.880, 0.260, 0.050);
float3 c3 = float3(1.000, 0.680, 0.200);
float3 c4 = float3(1.000, 0.970, 0.840);
t = clamp(t, 0.0, 1.0);
float3 c = mix(c0, c1, smoothstep(0.00, 0.22, t));
c = mix(c, c2, smoothstep(0.22, 0.50, t));
c = mix(c, c3, smoothstep(0.50, 0.76, t));
return mix(c, c4, smoothstep(0.76, 1.00, t));
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float d = deepDecades(u.time, p);
float height = VIEW * pow(10.0, -d);
// One pixel, in units of the plane.
float px = height / u.resolution.y;
float a = p.turn * 0.15 * u.time;
float2 q = (uv - 0.5) * u.resolution / u.resolution.y;
q = float2(cos(a) * q.x - sin(a) * q.y, sin(a) * q.x + cos(a) * q.y);
// dc is this pixel's offset from the center: tiny, but kept whole.
// The view starts on the whole set and drifts onto the center.
float2 dc = q * height + (START - CENTER) * pow(10.0, -2.0 * d);
// On the left, the plain way: one sum, rounded to seven digits.
bool plain = uv.x < p.naive;
float2 c = CENTER + dc;
float2 z = float2(0.0);
float2 dz = float2(0.0);
// How fast z moves as the pixel moves, measured in pixels.
float2 der = float2(0.0);
int m = 0;
float n = 0.0;
bool escaped = false;
for (int i = 0; i < STEPS; i++) {
der = 2.0 * cmul(z, der) + float2(px, 0.0);
if (plain) {
z = csqr(z) + c;
} else {
// The pixel's z is Z + dz and Z' = Z^2 + C, so
// dz' = 2 Z dz + dz^2 + dc: nothing big is added.
dz = 2.0 * cmul(deepRef(m), dz) + csqr(dz) + dc;
m = min(m + 1, 4);
z = deepRef(m) + dz;
}
if (dot(z, z) > 256.0 * 256.0) { escaped = true; break; }
n += 1.0;
}
float3 col = kiln(0.03 + (p.heat - 0.5) * 0.2);
if (escaped) {
float r = length(z);
float smoothN = n + 1.0 - log2(log2(r));
// Take away what the depth costs: every level is colored alike.
float rel = smoothN - PER_DECADE * d;
float level = 0.95 * pow(clamp((rel - 3.0) / 22.0, 0.0, 1.0), 0.8);
// How far this pixel is from the set, in pixels, from |z| and how
// fast it grew. Within a few pixels of the edge, it glows.
float dist = 0.5 * r * log(r) / max(length(der), 1e-30);
float edge = exp(-dist / (1.0 + 4.0 * p.glow));
level = max(level, edge * (0.2 + 0.6 * p.glow));
col = kiln(level + (p.heat - 0.5) * 0.5);
}
// A thin cream line where the plain side ends.
float split = abs(uv.x - p.naive) * u.resolution.x;
if (p.naive > 0.0 && p.naive < 1.0) {
col = mix(col, kiln(0.9), 1.0 - smoothstep(0.5, 1.5, split));
}
// The wall: the depth where a pixel shrinks below float's step near
// the center, 2^-24.
float wall = log2(VIEW * 16777216.0 / u.resolution.y) * 0.30103;
col = deepGauge(uv, col, d, wall, u);
return float4(col, 1.0);
}deep-zoom.metal, the whole file18 new lines since stage 4
// Ray Kiln · Deep Zoom
//
// @episode Deep Zoom
// @length 60
// @variation 30
// @variations 1,2,3,6,7,9,11,12
// @still 40
// @teaches The Mandelbrot set zoomed until float runs out, and how to go further
// @category Fragment
// @tags mandelbrot set, complex numbers, fractals, smooth iteration count, floating point, perturbation, misiurewicz points, self-similarity, distance estimation, procedural sound
// @final Deep Zoom | Light the edge of the set by its distance, and bring back the plain sum on the left to see where it fails.
//
// A zoom into the Mandelbrot set, from the whole set down to a view
// 10^27 times smaller. It dives toward c = -0.1011 + 0.9563i, where
// three arms of the set meet. A float holds about seven digits, so the
// plain sum c = center + offset stops telling neighboring pixels apart
// near 10^-5 and the picture turns to blocks. To go further, each pixel
// keeps only its small difference from one reference orbit, the orbit
// of the center, which is known ahead: dz' = 2 Z dz + dz^2 + dc. A small
// number keeps all its digits in float, however small it is. The center
// is a Misiurewicz point: its orbit lands on a repelling fixed point
// after four steps, so the set near it repeats each 1.328 times closer,
// turned by 120 degrees, and the picture never runs out.
// Each @param line below becomes a slider in the host app and a row in the page's parameters table:
// // @param name default min max description
// @param speed 1 0 2 How fast the zoom dives: 1 takes the whole minute
// @param deepest 27 4 30 How deep the zoom goes, in powers of ten
// @param turn 0.5 -2 2 How fast the view turns
// @param naive 0 0 1 How much of the screen, from the left, adds the plain way
// @param glow 0.5 0 1 How brightly the edge of the set glows
// @param heat 0.5 0 1 Slides the palette from deep ember to white-hot
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
// Where the zoom starts: the middle of the whole set.
constant float2 START = float2(-0.6, 0.0);
// The view's height when the zoom starts, in units of the plane.
constant float VIEW = 3.0;
// The most times a point is squared.
constant int STEPS = 320;
// The center, a Misiurewicz point where three arms meet.
constant float2 CENTER = float2(-0.101096364, 0.956286511);
// What each power of ten deeper costs: 1 / log10(1.328) steps.
constant float PER_DECADE = 8.111;
// A complex number is a pair (x, y), x + iy, with i * i = -1.
// Squaring one doubles its angle and squares its length.
float2 csqr(float2 z) {
return float2(z.x * z.x - z.y * z.y, 2.0 * z.x * z.y);
}
// Multiplying: the angles add, the lengths multiply.
float2 cmul(float2 a, float2 b) {
return float2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
}
// The kiln palette from Flowing Color Field.
float3 kiln(float t) {
float3 c0 = float3(0.020, 0.012, 0.020);
float3 c1 = float3(0.280, 0.040, 0.030);
float3 c2 = float3(0.880, 0.260, 0.050);
float3 c3 = float3(1.000, 0.680, 0.200);
float3 c4 = float3(1.000, 0.970, 0.840);
t = clamp(t, 0.0, 1.0);
float3 c = mix(c0, c1, smoothstep(0.00, 0.22, t));
c = mix(c, c2, smoothstep(0.22, 0.50, t));
c = mix(c, c3, smoothstep(0.50, 0.76, t));
return mix(c, c4, smoothstep(0.76, 1.00, t));
}
// Depth in powers of ten: slow, steady, then still.
float deepDecades(float t, constant Params& p) {
return p.deepest * smoothstep(0.0, 60.0, t * p.speed);
}
// The center's orbit, worked out ahead to 50 digits: from step 4 on it
// sits on a fixed point, so it is never stepped.
float2 deepRef(int n) {
if (n == 1) return float2(-0.101096364, 0.956286511);
if (n == 2) return float2(-1.005359780, 0.762932333);
if (n == 3) return float2(0.327586179, -0.577756453);
if (n >= 4) return float2(-0.327586179, 0.577756453);
return float2(0.0, 0.0);
}
// A gauge: depth out of 30 powers of ten, a tick every five, and a
// cream mark at the wall.
float3 deepGauge(float2 uv, float3 col, float d, float wall,
constant Uniforms& u) {
// In pixels of a 720-line picture.
float y = (uv.y - 0.04) * 720.0;
float x = (uv.x - 0.2) / 0.6;
if (abs(y) > 3.0 || x < 0.0 || x > 1.0) return col;
float at = x * 30.0;
float3 bar = at < d ? kiln(at < wall ? 0.72 : 0.5) : kiln(0.12);
float apart = 0.02 * u.resolution.x;
float tick = abs(at - 5.0 * floor(at / 5.0 + 0.5)) * apart;
bar = mix(bar, kiln(0.3), step(tick, 0.6) * step(1.0, abs(y)));
float mark = abs(at - wall) * apart;
bar = mix(bar, kiln(0.97), 1.0 - smoothstep(0.6, 1.6, mark));
return mix(col, bar, 0.85);
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float d = deepDecades(u.time, p);
float height = VIEW * pow(10.0, -d);
// One pixel, in units of the plane.
float px = height / u.resolution.y;
float a = p.turn * 0.15 * u.time;
float2 q = (uv - 0.5) * u.resolution / u.resolution.y;
q = float2(cos(a) * q.x - sin(a) * q.y, sin(a) * q.x + cos(a) * q.y);
// dc is this pixel's offset from the center: tiny, but kept whole.
// The view starts on the whole set and drifts onto the center.
float2 dc = q * height + (START - CENTER) * pow(10.0, -2.0 * d);
// On the left, the plain way: one sum, rounded to seven digits.
bool plain = uv.x < p.naive;
float2 c = CENTER + dc;
float2 z = float2(0.0);
float2 dz = float2(0.0);
// How fast z moves as the pixel moves, measured in pixels.
float2 der = float2(0.0);
int m = 0;
float n = 0.0;
bool escaped = false;
for (int i = 0; i < STEPS; i++) {
der = 2.0 * cmul(z, der) + float2(px, 0.0);
if (plain) {
z = csqr(z) + c;
} else {
// The pixel's z is Z + dz and Z' = Z^2 + C, so
// dz' = 2 Z dz + dz^2 + dc: nothing big is added.
dz = 2.0 * cmul(deepRef(m), dz) + csqr(dz) + dc;
m = min(m + 1, 4);
z = deepRef(m) + dz;
}
if (dot(z, z) > 256.0 * 256.0) { escaped = true; break; }
n += 1.0;
}
float3 col = kiln(0.03 + (p.heat - 0.5) * 0.2);
if (escaped) {
float r = length(z);
float smoothN = n + 1.0 - log2(log2(r));
// Take away what the depth costs: every level is colored alike.
float rel = smoothN - PER_DECADE * d;
float level = 0.95 * pow(clamp((rel - 3.0) / 22.0, 0.0, 1.0), 0.8);
// How far this pixel is from the set, in pixels, from |z| and how
// fast it grew. Within a few pixels of the edge, it glows.
float dist = 0.5 * r * log(r) / max(length(der), 1e-30);
float edge = exp(-dist / (1.0 + 4.0 * p.glow));
level = max(level, edge * (0.2 + 0.6 * p.glow));
col = kiln(level + (p.heat - 0.5) * 0.5);
}
// A thin cream line where the plain side ends.
float split = abs(uv.x - p.naive) * u.resolution.x;
if (p.naive > 0.0 && p.naive < 1.0) {
col = mix(col, kiln(0.9), 1.0 - smoothstep(0.5, 1.5, split));
}
// The wall: the depth where a pixel shrinks below float's step near
// the center, 2^-24.
float wall = log2(VIEW * 16777216.0 / u.resolution.y) * 0.30103;
col = deepGauge(uv, col, d, wall, u);
return float4(col, 1.0);
}
// ---- The sound ----------------------------------------------------------
// Every three levels of 1.328 the picture comes back whole. Each time,
// a Shepard tone steps down a semitone: octaves of one note under a
// fixed curve of loudness, so it seems to fall forever.
// Powers of ten per whole return: 3 * log10(1.328).
constant float RETURN = 0.36987;
float hash11(float n) {
return fract(sin(n * 12.9898) * 43758.5453);
}
// Noise with nothing above about `rate` Hz in it, from hashes of the time.
float deepNoise(float t, float rate) {
float x = t * rate;
float i = floor(x), f = fract(x);
f = f * f * (3.0 - 2.0 * f);
return mix(hash11(i), hash11(i + 1.0), f) * 2.0 - 1.0;
}
// When the zoom reaches depth d: smoothstep turned inside out.
float deepWhen(float d, constant Params& p) {
float s = clamp(d / p.deepest, 0.0, 1.0);
float x = 0.5 - sin(asin(1.0 - 2.0 * s) / 3.0);
return 60.0 * x / max(p.speed, 1e-3);
}
// Shepard tone k: eight octaves of one note, loudest near 440 Hz.
float deepShepard(float k, float age) {
float x = fract(-k / 12.0);
float s = 0.0;
for (int o = 0; o < 8; o++) {
float pos = float(o) + x;
float f = 27.5 * exp2(pos);
float w = exp(-(pos - 4.0) * (pos - 4.0) / 3.4);
s += w * sin(TAU * f * age);
}
float env = (1.0 - exp(-age * 120.0)) * exp(-age * 3.5);
return s * env;
}
float2 sound(float t, constant Params& p) {
float d = deepDecades(t, p);
float now = floor(d / RETURN);
float2 notes = float2(0.0);
// The step just passed and the seven before it, still ringing.
for (int back = 0; back < 8; back++) {
float k = now - float(back);
if (k < 1.0) continue;
float age = t - deepWhen(k * RETURN, p);
if (age < 0.0 || age > 3.0) continue;
float pan = 0.5 + 0.3 * sin(k * 2.0944);
notes += float2(1.0 - pan, pan) * deepShepard(k, age) * 0.3;
}
// Wind, as loud as the zoom is fast.
float rate = (deepDecades(t + 0.05, p) - deepDecades(t - 0.05, p)) * 10.0;
float wind = clamp(rate / 1.2, 0.0, 1.0);
float2 air = float2(deepNoise(t, 700.0), deepNoise(t + 5.3, 700.0));
air += 0.5 * float2(deepNoise(t, 2300.0), deepNoise(t + 2.1, 2300.0));
air *= 0.05 * wind;
// A low drone on A, with its fifth.
float drone = sin(TAU * 55.0 * t) + 0.4 * sin(TAU * 82.5 * t);
drone *= 0.12;
float fade = smoothstep(0.0, 1.5, t) * (1.0 - smoothstep(58.5, 60.0, t));
return tanh((notes + air + drone) * 1.5) * 0.85 * fade * p.volume;
}1 line from stage 4 that are gone
float smoothN = n + 1.0 - log2(log2(length(z)));The sound
The sound follows the zoom's depth. Three levels of 1.328 make 2.34 times closer and three turns of 120°, a whole turn, so every 0.36987 powers of ten (RETURN, 3 · log10(1.328)) the picture comes back whole. Each time, a new note starts: a Shepard tone, eight octaves of one note from 27.5 Hz up, under a fixed bell of loudness centered on 440 Hz. Each note is a semitone below the one before, the octaves sliding down under the bell, so after twelve the tone is back where it started. The 27 powers of ten hold 72 of them. Each rises in 10 milliseconds and fades away over the next three seconds. deepWhen turns the smoothstep inside out to find the moment the zoom passed each one, so each note starts exactly on its moment, and up to eight ring at once. Each sits at one of three places from left to right, in turn, as the picture turns by 120°.
Under the notes is wind, two bands of noise made by hashing the time and smoothing between the hashes, about 700 and 2,300 times a second, as loud as the zoom is fast: silent at the start and the end, loudest in the middle of the minute. Under that is the drone of the first stage. Everything goes through tanh, fades in over the first 1.5 seconds and out over the last 1.5, so the first and last samples are 0.
Over the 60 seconds at the defaults the peak is −6.9 dBFS and the RMS −19.0 dBFS.
New in Ray Kiln
Zoom depth. The playground's Zoom now goes ten million times in, and says where 32-bit floats run out. Its button gives the zoom plainly up to ×1,000 (×2.5, ×240) and as a power of ten past it, ×10 with the power raised. While zooming, a note under the picture works out the wall for the canvas you have. The playground zooms by moving uv, so one pixel to the next moves uv by 1 / (pixels × zoom), while a float between 0.5 and 1 can only step by 2⁻²⁴. Where the two meet, neighboring pixels get the same uv: about ×13,107 across 1,280 pixels, about ×44,739 across 375. The note says how far that is from where you are, and, past it, how many pixels across and down share one uv, and that a shader that scales its own coordinates about a point goes much deeper. It is free and live only.
The two walls are not the same. The playground's is in uv, about ×10⁴ on screen, and it is a wall of the playground's zoom, whatever the shader does. This episode's own wall is in the plane, where the view is about 10⁻⁵ of the whole set, and perturbation takes the shader past it to 10⁻²⁷, because it never adds the small offset to the big center. To see the playground's, open the set, zoomed ×3,000 onto the edge of the big black heart, paused and with turn at 0 so it holds still. Stage 1's colors don't depend on a depth, so it shows the edge plainly at any zoom. Wheel in where the cream threads meet the black. Until about ×10⁴ the note says how much deeper floats can go; past it, the smooth edge turns into a staircase of blocks, and the note says how many pixels across and down share one uv (on a canvas 976 pixels wide, about 6 × 3 at ×10⁵). The finished shader, with Zoom off, dives twenty-two powers of ten further than that and stays sharp.
Try this
- Set
naiveto 0.5: the left half of the screen takes the plain sum. At about 17 seconds (at 720 rows) it turns grainy and then breaks into tall blocks while the right half stays sharp, and by 24 seconds the left half is one flat color. - Set
naiveto 1: the whole picture breaks into blocks at about 17 to 18 seconds, just after the gauge's fill passes the cream mark. - Set
deepestto 4 andnaiveto 1: the zoom stops before the wall, and even the plain sum stays sharp to the end. - Set
speedto 2: the dive reaches its deepest at 30 seconds and holds there, turning, for the second half of the minute. At 0 it never leaves the whole set, which only turns. - Set
glowto 0 for bare threads, or 1 for thick, glowing arms. - Set
heatto 1: the background lifts from near-black to a dark red, and the arms go white-hot.
Parameters
Pick a parameter and drag the slider. Each frame is the same moment of the shader, with only that one setting changed.
| Parameter | Default | Range | What it does |
|---|---|---|---|
speed | 1 | 0 to 2 | How fast the zoom dives: 1 takes the whole minute |
deepest | 27 | 4 to 30 | How deep the zoom goes, in powers of ten |
turn | 0.5 | -2 to 2 | How fast the view turns |
naive | 0 | 0 to 1 | How much of the screen, from the left, adds the plain way |
glow | 0.5 | 0 to 1 | How brightly the edge of the set glows |
heat | 0.5 | 0 to 1 | Slides the palette from deep ember to white-hot |
volume | 0.8 | 0 to 1 | Loudness of the sound |
More from this shader
The same shader 8 more ways: each from a different moment, with different settings, one after another. Each chapter below says which settings moved most, then lists them all.
Wallpapers
A still from the shader, rendered at full size for a desktop or a phone screen. Free to use as your wallpaper.
Clips to post
Fifteen seconds of the shader with its sound, sized for posting: square for feeds, vertical for stories and short videos. Free to post, ideally with a link to this page.
Made from this episode
The full source
This is the whole episode: the picture, the parameters and the sound.
deep-zoom.metal
// Ray Kiln · Deep Zoom
//
// @episode Deep Zoom
// @length 60
// @variation 30
// @variations 1,2,3,6,7,9,11,12
// @still 40
// @teaches The Mandelbrot set zoomed until float runs out, and how to go further
// @category Fragment
// @tags mandelbrot set, complex numbers, fractals, smooth iteration count, floating point, perturbation, misiurewicz points, self-similarity, distance estimation, procedural sound
// @final Deep Zoom | Light the edge of the set by its distance, and bring back the plain sum on the left to see where it fails.
//
// A zoom into the Mandelbrot set, from the whole set down to a view
// 10^27 times smaller. It dives toward c = -0.1011 + 0.9563i, where
// three arms of the set meet. A float holds about seven digits, so the
// plain sum c = center + offset stops telling neighboring pixels apart
// near 10^-5 and the picture turns to blocks. To go further, each pixel
// keeps only its small difference from one reference orbit, the orbit
// of the center, which is known ahead: dz' = 2 Z dz + dz^2 + dc. A small
// number keeps all its digits in float, however small it is. The center
// is a Misiurewicz point: its orbit lands on a repelling fixed point
// after four steps, so the set near it repeats each 1.328 times closer,
// turned by 120 degrees, and the picture never runs out.
// Each @param line below becomes a slider in the host app and a row in the page's parameters table:
// // @param name default min max description
// @param speed 1 0 2 How fast the zoom dives: 1 takes the whole minute
// @param deepest 27 4 30 How deep the zoom goes, in powers of ten
// @param turn 0.5 -2 2 How fast the view turns
// @param naive 0 0 1 How much of the screen, from the left, adds the plain way
// @param glow 0.5 0 1 How brightly the edge of the set glows
// @param heat 0.5 0 1 Slides the palette from deep ember to white-hot
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
// Where the zoom starts: the middle of the whole set.
constant float2 START = float2(-0.6, 0.0);
// The view's height when the zoom starts, in units of the plane.
constant float VIEW = 3.0;
// The most times a point is squared.
constant int STEPS = 320;
// The center, a Misiurewicz point where three arms meet.
constant float2 CENTER = float2(-0.101096364, 0.956286511);
// What each power of ten deeper costs: 1 / log10(1.328) steps.
constant float PER_DECADE = 8.111;
// A complex number is a pair (x, y), x + iy, with i * i = -1.
// Squaring one doubles its angle and squares its length.
float2 csqr(float2 z) {
return float2(z.x * z.x - z.y * z.y, 2.0 * z.x * z.y);
}
// Multiplying: the angles add, the lengths multiply.
float2 cmul(float2 a, float2 b) {
return float2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
}
// The kiln palette from Flowing Color Field.
float3 kiln(float t) {
float3 c0 = float3(0.020, 0.012, 0.020);
float3 c1 = float3(0.280, 0.040, 0.030);
float3 c2 = float3(0.880, 0.260, 0.050);
float3 c3 = float3(1.000, 0.680, 0.200);
float3 c4 = float3(1.000, 0.970, 0.840);
t = clamp(t, 0.0, 1.0);
float3 c = mix(c0, c1, smoothstep(0.00, 0.22, t));
c = mix(c, c2, smoothstep(0.22, 0.50, t));
c = mix(c, c3, smoothstep(0.50, 0.76, t));
return mix(c, c4, smoothstep(0.76, 1.00, t));
}
// Depth in powers of ten: slow, steady, then still.
float deepDecades(float t, constant Params& p) {
return p.deepest * smoothstep(0.0, 60.0, t * p.speed);
}
// The center's orbit, worked out ahead to 50 digits: from step 4 on it
// sits on a fixed point, so it is never stepped.
float2 deepRef(int n) {
if (n == 1) return float2(-0.101096364, 0.956286511);
if (n == 2) return float2(-1.005359780, 0.762932333);
if (n == 3) return float2(0.327586179, -0.577756453);
if (n >= 4) return float2(-0.327586179, 0.577756453);
return float2(0.0, 0.0);
}
// A gauge: depth out of 30 powers of ten, a tick every five, and a
// cream mark at the wall.
float3 deepGauge(float2 uv, float3 col, float d, float wall,
constant Uniforms& u) {
// In pixels of a 720-line picture.
float y = (uv.y - 0.04) * 720.0;
float x = (uv.x - 0.2) / 0.6;
if (abs(y) > 3.0 || x < 0.0 || x > 1.0) return col;
float at = x * 30.0;
float3 bar = at < d ? kiln(at < wall ? 0.72 : 0.5) : kiln(0.12);
float apart = 0.02 * u.resolution.x;
float tick = abs(at - 5.0 * floor(at / 5.0 + 0.5)) * apart;
bar = mix(bar, kiln(0.3), step(tick, 0.6) * step(1.0, abs(y)));
float mark = abs(at - wall) * apart;
bar = mix(bar, kiln(0.97), 1.0 - smoothstep(0.6, 1.6, mark));
return mix(col, bar, 0.85);
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float d = deepDecades(u.time, p);
float height = VIEW * pow(10.0, -d);
// One pixel, in units of the plane.
float px = height / u.resolution.y;
float a = p.turn * 0.15 * u.time;
float2 q = (uv - 0.5) * u.resolution / u.resolution.y;
q = float2(cos(a) * q.x - sin(a) * q.y, sin(a) * q.x + cos(a) * q.y);
// dc is this pixel's offset from the center: tiny, but kept whole.
// The view starts on the whole set and drifts onto the center.
float2 dc = q * height + (START - CENTER) * pow(10.0, -2.0 * d);
// On the left, the plain way: one sum, rounded to seven digits.
bool plain = uv.x < p.naive;
float2 c = CENTER + dc;
float2 z = float2(0.0);
float2 dz = float2(0.0);
// How fast z moves as the pixel moves, measured in pixels.
float2 der = float2(0.0);
int m = 0;
float n = 0.0;
bool escaped = false;
for (int i = 0; i < STEPS; i++) {
der = 2.0 * cmul(z, der) + float2(px, 0.0);
if (plain) {
z = csqr(z) + c;
} else {
// The pixel's z is Z + dz and Z' = Z^2 + C, so
// dz' = 2 Z dz + dz^2 + dc: nothing big is added.
dz = 2.0 * cmul(deepRef(m), dz) + csqr(dz) + dc;
m = min(m + 1, 4);
z = deepRef(m) + dz;
}
if (dot(z, z) > 256.0 * 256.0) { escaped = true; break; }
n += 1.0;
}
float3 col = kiln(0.03 + (p.heat - 0.5) * 0.2);
if (escaped) {
float r = length(z);
float smoothN = n + 1.0 - log2(log2(r));
// Take away what the depth costs: every level is colored alike.
float rel = smoothN - PER_DECADE * d;
float level = 0.95 * pow(clamp((rel - 3.0) / 22.0, 0.0, 1.0), 0.8);
// How far this pixel is from the set, in pixels, from |z| and how
// fast it grew. Within a few pixels of the edge, it glows.
float dist = 0.5 * r * log(r) / max(length(der), 1e-30);
float edge = exp(-dist / (1.0 + 4.0 * p.glow));
level = max(level, edge * (0.2 + 0.6 * p.glow));
col = kiln(level + (p.heat - 0.5) * 0.5);
}
// A thin cream line where the plain side ends.
float split = abs(uv.x - p.naive) * u.resolution.x;
if (p.naive > 0.0 && p.naive < 1.0) {
col = mix(col, kiln(0.9), 1.0 - smoothstep(0.5, 1.5, split));
}
// The wall: the depth where a pixel shrinks below float's step near
// the center, 2^-24.
float wall = log2(VIEW * 16777216.0 / u.resolution.y) * 0.30103;
col = deepGauge(uv, col, d, wall, u);
return float4(col, 1.0);
}
// ---- The sound ----------------------------------------------------------
// Every three levels of 1.328 the picture comes back whole. Each time,
// a Shepard tone steps down a semitone: octaves of one note under a
// fixed curve of loudness, so it seems to fall forever.
// Powers of ten per whole return: 3 * log10(1.328).
constant float RETURN = 0.36987;
float hash11(float n) {
return fract(sin(n * 12.9898) * 43758.5453);
}
// Noise with nothing above about `rate` Hz in it, from hashes of the time.
float deepNoise(float t, float rate) {
float x = t * rate;
float i = floor(x), f = fract(x);
f = f * f * (3.0 - 2.0 * f);
return mix(hash11(i), hash11(i + 1.0), f) * 2.0 - 1.0;
}
// When the zoom reaches depth d: smoothstep turned inside out.
float deepWhen(float d, constant Params& p) {
float s = clamp(d / p.deepest, 0.0, 1.0);
float x = 0.5 - sin(asin(1.0 - 2.0 * s) / 3.0);
return 60.0 * x / max(p.speed, 1e-3);
}
// Shepard tone k: eight octaves of one note, loudest near 440 Hz.
float deepShepard(float k, float age) {
float x = fract(-k / 12.0);
float s = 0.0;
for (int o = 0; o < 8; o++) {
float pos = float(o) + x;
float f = 27.5 * exp2(pos);
float w = exp(-(pos - 4.0) * (pos - 4.0) / 3.4);
s += w * sin(TAU * f * age);
}
float env = (1.0 - exp(-age * 120.0)) * exp(-age * 3.5);
return s * env;
}
float2 sound(float t, constant Params& p) {
float d = deepDecades(t, p);
float now = floor(d / RETURN);
float2 notes = float2(0.0);
// The step just passed and the seven before it, still ringing.
for (int back = 0; back < 8; back++) {
float k = now - float(back);
if (k < 1.0) continue;
float age = t - deepWhen(k * RETURN, p);
if (age < 0.0 || age > 3.0) continue;
float pan = 0.5 + 0.3 * sin(k * 2.0944);
notes += float2(1.0 - pan, pan) * deepShepard(k, age) * 0.3;
}
// Wind, as loud as the zoom is fast.
float rate = (deepDecades(t + 0.05, p) - deepDecades(t - 0.05, p)) * 10.0;
float wind = clamp(rate / 1.2, 0.0, 1.0);
float2 air = float2(deepNoise(t, 700.0), deepNoise(t + 5.3, 700.0));
air += 0.5 * float2(deepNoise(t, 2300.0), deepNoise(t + 2.1, 2300.0));
air *= 0.05 * wind;
// A low drone on A, with its fifth.
float drone = sin(TAU * 55.0 * t) + 0.4 * sin(TAU * 82.5 * t);
drone *= 0.12;
float fade = smoothstep(0.0, 1.5, t) * (1.0 - smoothstep(58.5, 60.0, t));
return tanh((notes + air + drone) * 1.5) * 0.85 * fade * p.volume;
}Running it in your own project
Every Ray Kiln shader is the same shape. The standalone download above is the shader with the small wrapper around it that the Ray Kiln host adds, so it compiles with the ordinary Metal compiler and runs in your own app. It defines three entry points: rk_vertex (a full-screen triangle), rk_fragment (calls the shader's shade) and, for shaders with sound, the compute kernel rk_sound.
To draw it, pass the uniforms at fragment buffer 0 and the parameter values at buffer 1, in the order they are declared:
Drawing it, in Swift
struct Uniforms {
var resolution: SIMD2<Float>
var time: Float
var timeDelta: Float
var mouse: SIMD2<Float>
var frame: UInt32
var pad: UInt32 = 0
}
let library = try device.makeLibrary(source: standaloneSource, options: nil)
let descriptor = MTLRenderPipelineDescriptor()
descriptor.vertexFunction = library.makeFunction(name: "rk_vertex")
descriptor.fragmentFunction = library.makeFunction(name: "rk_fragment")
descriptor.colorAttachments[0].pixelFormat = .bgra8Unorm
let pipeline = try device.makeRenderPipelineState(descriptor: descriptor)
// Each frame, inside a render pass:
var uniforms = Uniforms(resolution: size, time: time, timeDelta: dt, mouse: mouse, frame: frame)
var params: [Float] = [1, 27, 0.5, 0, 0.5, 0.5, 0.8] // the @param defaults, in the order they are declared
encoder.setRenderPipelineState(pipeline)
encoder.setFragmentBytes(&uniforms, length: MemoryLayout<Uniforms>.stride, index: 0)
encoder.setFragmentBytes(¶ms, length: params.count * MemoryLayout<Float>.stride, index: 1)
encoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3)Time is in seconds, mouse is in pixels from the bottom left, and uv in the shader runs 0 to 1 with (0, 0) at the bottom left. Write the output as sRGB: the shader's numbers go to the screen as they are.
Built on later
More in Fractals
Press ? for keyboard shortcuts.


