off
ikonic>
OUT 0 B

Glass

GLSL 3D Fractals - Menger Sponge

The Menger Sponge is the 3D generalization of the Sierpinski carpet, discovered by Karl Menger in 1926. It's a fascinating geometric fractal with paradoxical properties: infinite surface area yet zero volume, making it a perfect example of a fractal dimension between 2 and 3.

Interactive Demo

Move your mouse to rotate the camera and explore the infinite detail. Scroll to adjust the iteration count and see how the structure becomes more complex:

Mathematical Foundation

Construction Process

The Menger Sponge is constructed recursively:

  1. Start: A cube
  2. Divide: Split into 3×3×3 = 27 smaller cubes
  3. Remove: Remove the center cube and the 6 face-center cubes (7 total)
  4. Repeat: Apply the same process to the remaining 20 cubes
  5. Infinite: Continue ad infinitum

Distance Estimation

We use a fold-based approach for efficient computation:

float sdBox(vec3 p, vec3 b) {
    vec3 q = abs(p) - b;
    return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0);
}

float mengerSpongeDE(vec3 pos, int iterations) {
    vec3 p = pos;
    float scale = 1.0;
    
    // GLSL ES 1.0 needs a constant loop bound; `iterations` still sets the depth
    for (int i = 0; i < 8; i++) {
        if (i >= iterations) break;
        // Fold into first octant
        p = abs(p);
        
        // Sort the axes so the cross-shaped hole is folded the same way every time
        if (p.x < p.y) p.xy = p.yx;
        if (p.x < p.z) p.xz = p.zx;
        if (p.y < p.z) p.yz = p.zy;
        
        // Scale by 3 and centre on the corner sub-cube
        p = p * 3.0 - 2.0;
        scale *= 3.0;
        
        // Remove the center box of the smallest axis
        if (p.z < -1.0) p.z += 2.0;
    }
    
    return sdBox(p, vec3(1.0)) / scale;
}

Fractal Dimension

The Menger Sponge has a fractal dimension of:

$$D = \frac{\log(20)}{\log(3)} \approx 2.727$$

This is between 2 (surface) and 3 (volume), reflecting its paradoxical nature. The dimension is calculated using the scaling factor (3) and the number of copies (20) that remain after each iteration.

Properties

  • Infinite surface area: Each iteration multiplies the surface area
  • Zero volume: The removed cubes eventually remove all volume
  • Self-similarity: Every part looks like the whole
  • Hausdorff dimension: Approximately 2.727

Visual Characteristics

  • Geometric precision: Sharp edges and corners
  • Infinite detail: Zooming in reveals the same pattern
  • Symmetry: Cubic symmetry in all three axes
  • Complexity: Each iteration adds 20× more detail

Optimization

The fold-based approach is highly efficient:

  • No recursion: Iterative algorithm
  • Fast convergence: Distance estimate is accurate
  • GPU-friendly: Parallelizable per pixel

Applications

  • Mathematical visualization: Teaching fractal geometry
  • Architectural inspiration: Self-similar structures
  • Game environments: Procedural geometric worlds
  • Art: Geometric abstract art

References

Related Articles