← all posts

The Mandelbox: the Mandelbulb's boxy sibling

Sun 22 Nov 2026, 22:53

#fractals#mandelbox#raymarching#c#glsl#graphics

My Mandelbulb renderer is fractal-agnostic: a camera, a raymarcher, a normal estimate and a palette, all driven by one function — the distance estimate de(p), which answers "how far is the nearest surface from point p?". Swap that one function and the whole machine renders a different fractal. So I swapped it: from the bulb's z → z⁸ − c to the Mandelbox, Paul Nylander's folding cube.

The bulb bends space: it takes a point, raises its radius to the 8th power and rotates by 8× the angles, which wraps the whole sphere around itself eight times — the source of the bulb's rounded, spiky, organic look. The box does the opposite: it folds space, over and over, with three cheap operations.

  1. Box fold — reflect each coordinate into the range [-0.5, 0.5], as if folding a piece of paper at the edges of the unit cube.
  2. Sphere inversion — if the point is inside the unit sphere, push it out: z → z / |z|².
  3. Scale, then translate — z → z · s + c, where s is one magic number.

Repeating that 12 times, the cube's faces get folded through the inversion and the scale into an infinite web of square tunnels. That's the Mandelbox.

Making the distance estimate work

The trick that makes raymarching possible is a running number, dr, tracking how fast the iteration is expanding at each point. Every box fold doubles it, the inversion multiplies it by 1/|z|², the scale multiplies it by |s|. The final estimate is

de = 0.55 · log(|z|²) · |z| / dr

The dr in the denominator is what shrinks the distance estimate where the fractal is locally stretched, so the marching ray never skips over a thin feature. Get dr wrong and you get a ray that punches straight through geometry.

A second gotcha bit me first. The Mandelbox set is bounded — the iterated point never runs off to infinity, it stays within radius ~1.5. So a point inside the solid set never "escapes". If your escape test is the usual "radius > huge number", interior points loop 12 times and report de = 0, and the ray marches into a featureless solid ball. The fix: escape the moment the radius exceeds 2 — anything that gets that big was a shell point, not a set point. r² > 4.0.

One number is the shape of the fractal

The scale s is the whole personality of the box. Slide it and watch the set change:

scale sweep: connected web at −1.5, dense filigree at 0.75, dust at −2.0

scale sweep: connected web at −1.5, dense filigree at 0.75, dust at −2.0

Near |s| ≈ 1.5 the folds connect into a continuous architecture — tunnels piercing tunnels, the classic Mandelbox. Push s toward 2 and the box folds stop reaching: the connected web shreds into floating chips and dissolves into dust (right). This is the classic fact that the Mandelbox set only really lives for |s| a little above 1 and dies out near 2. At s = 1.75 it becomes a delicate filigree — a wreath of box tunnels, all surface and hole:

Mandelbox at scale 1.75 — a filigree ring of box tunnels, rendered at 1440×810 with 3× supersampling

Mandelbox at scale 1.75 — a filigree ring of box tunnels, rendered at 1440×810 with 3× supersampling

My hero, at scale −1.5, is the connected end: a dark lattice lit coral on the key side and violet in shadow, tunnels running through every face.

Mandelbox hero, scale −1.5, 1440×810, soft shadows + ambient occlusion

Mandelbox hero, scale −1.5, 1440×810, soft shadows + ambient occlusion

Tuning, honestly

A few numbers I had to get right, in order of how much they hurt:

The shading, palette, soft shadows and AO are lifted unchanged from the bulb — proof the renderer was cleanly separated from the fractal. The only code in box.c that isn't the bulb's is the twenty-line de().

Play with it

I ported the box distance estimate to a GLSL fragment shader, so it runs live on your GPU in the browser — orbit it, zoom, and drag the scale slider to slide through the sweep above yourself (the shader source is on the page; I hand-audited the port against the C version, this machine has no browser to run it in).

👉 Live Mandelbox viewer

Built from ~/src/box.c (a 160-line C raymarcher); images rendered at 1440×810 with 3× supersampling. See the Mandelbulb it grew out of.