97

Menger Sponge

An infinitely zooming 3D fractal built with SDFs and raymarching.

60 FPS WebGL Shader

GLSL Fragment Shader

#version 300 es
precision highp float; uniform vec2 u_resolution; uniform vec2 u_mouse; uniform float u_time; out vec4 f;
mat2 rot(float a){float s=sin(a),c=cos(a);return mat2(c,-s,s,c);}
float sdBox(vec3 p, vec3 b){vec3 d=abs(p)-b;return length(max(d,0.)) + min(max(d.x,max(d.y,d.z)),0.);}
float map(vec3 p) {
    float d = sdBox(p,vec3(1.));
    float s = 1.0;
    for(int m=0; m<4; m++){
        vec3 a = mod(p*s, 2.0)-1.0;
        s *= 3.0;
        vec3 r = abs(1.0 - 3.0*abs(a));
        float da = max(r.x,r.y);
        float db = max(r.y,r.z);
        float dc = max(r.z,r.x);
        float c = (min(da,min(db,dc))-1.0)/s;
        d = max(d,c);
    }
    return d;
}
void main() {
    vec2 uv = (gl_FragCoord.xy-.5*u_resolution)/u_resolution.y;
    vec3 ro = vec3(0,0,-2.5); vec3 rd = normalize(vec3(uv,1));
    vec2 m = (u_mouse-.5)*3.; rd.yz*=rot(m.y); rd.xz*=rot(m.x); ro.yz*=rot(m.y); ro.xz*=rot(m.x);
    float t=0.; for(int i=0;i<100;i++){float d=map(ro+rd*t);if(d<0.001||t>10.)break;t+=d;}
    f = vec4(vec3(1.-t*0.2)*vec3(0.8,0.9,1.0), 1.0);
}

Properties

#glsl#fractals#menger
Author: Math Art Core Target: 60 FPS
ESC
↑↓ Navigate Select
101 Mathematical Artworks

Support the Project

Keep mathematical creative coding alive & open source

Or via Direct Payoneer ($0 Fee)
Payoneer Customer ID: $0 Fee Direct
24076084

💡 Payoneer app: Go to Pay → Pay to recipient → Enter ID 24076084.

Thank you for supporting generative mathematical animations! ✨