The Menger Sponge code approach

The Menger Sponge is usually drawn in 3D, but can it be procedurally rendered?

17th October 2026

A 3D shape with infinite surface area and zero volume should be impossible yet, at the limiting case of iterations, it is true. Just look below to see one rendered in 3D.

I have always been fascinated by geometry and fractals since first seeing the Mandelbrot set back in the Triassic period. As 3d libraries have become more and more accessible, it has been possible to render 3d environments entirely 'in browser' using tools like Three.js, a personal favourite.

How do you build a Menger Sponge?

These geometries are incredibly simple to define, a little harder to code. For the cube, you start by naming the eight vertices 1 to 8 and assign x, y & z coordinates to each. You then choose a random number from 1 to 8 representing a vertex and plot a point halfway between your current position and the randomly selected vertex. Repeat a bunch of times and you create the Menger Sponge.

The sponge was first described by the Austrian mathematician Karl Menger in 1926, and it can also be built step by step. Take a solid cube and slice it into 27 smaller cubes, like a Rubik's cube. Remove the cube from the centre of each face, plus the one at the very heart, leaving 20 behind. Then do exactly the same to each of those 20 cubes, and again, and again, forever.

How can a shape have zero volume but infinite surface area?

The result is gloriously strange. With every step the sponge loses volume, heading towards nothing at all, while its surface area grows without limit. Look at any face and you will see another famous fractal, the Sierpinski carpet. Mathematicians give the sponge a fractal dimension of around 2.73, so it is more than a surface but not quite a solid.

The arithmetic behind this is surprisingly simple. Each step keeps 20 of every 27 small cubes, so the volume is multiplied by 20/27, roughly three quarters, every time. Keep multiplying by three quarters and the volume shrinks towards zero. Meanwhile, every cube you remove opens up new tunnel walls, adding more surface than it takes away, so the surface area keeps on climbing. The odd dimension of 2.73 comes from the same numbers: the sponge is made of 20 copies of itself, each a third of the size.

What is the Sierpinski carpet?

The Sierpinski carpet is the flat version of the sponge. Take a square, divide it into nine smaller squares, remove the middle one and repeat for the eight that remain. Its fractal dimension is about 1.89, and every face of a Menger Sponge is one. It is a close relative of the Sierpinski gasket, which follows the same idea with triangles instead of squares.

Why is a Menger Sponge so hard to render?

With Three.js, it is possible to render a 3D environment and then navigate it in real time.

The catch is that the numbers grow alarmingly fast. Each level multiplies the number of cubes by 20, so level three needs 8,000 cubes and level four needs 160,000. That is why the version below stops where it does.

Each cube is drawn from 12 triangles, so a level four sponge would need almost two million of them. Most of those are hidden faces squashed between neighbouring cubes that nobody will ever see, so a smarter renderer can skip them and only draw the outside walls and tunnel linings. Techniques like that make deeper levels possible, but they add a good deal of code, which is why I kept this version simple. If you are new to Three.js, start with our guide to animating a cube in Three.js.

Has anyone built a real Menger Sponge?

They have, and with remarkable patience. Origami artist Jeannine Mosely folded a level three sponge from tens of thousands of business cards. In 2014, the MegaMenger project went further, with volunteers at sites around the world each building part of a giant level four sponge, also from business cards. It is one of the few fractals you can actually walk around.

Here is a Menger Sponge

Menger Sponge

How do we go about rendering these in 3d using Three.js? Like this...

Note: This cube has been left simplified to render it quickly on all browsers. Several minutes pass at the next level of complexity. Drag to rotate it, and zoom in to explore the tunnels running right through the middle.

For more fractals in the browser, explore the Mandelbrot and Julia sets.