Menger sponge
glint · Examples · Tutorials · Gallery
An infinite fractal sponge built by folding space, with soft shadows, ambient occlusion and a bass-driven hue shift.
Audio-reactive: it moves with music from a microphone, line-in or a file.

An example in glint, a free live shader editor in the browser: the GLSL code floats over the running picture and recompiles as you type.
The code
One pass, in GLSL.
fragment shader
#version 300 es
precision highp float;
uniform float time;
uniform vec2 resolution;
uniform vec2 mouse;
uniform vec3 spectrum;
uniform vec3 spectrumIntegrated;
uniform float beat;
uniform float iterations;
uniform float glow;
in vec2 v_texcoord;
out vec4 fragColor;
mat2 rot(float a) { float c = cos(a), s = sin(a); return mat2(c, -s, s, c); }
// Ray from an orbiting camera: yaw from mouse.x (plus slow drift), pitch from mouse.y.
void orbit(vec2 uv, float dist, float drift, out vec3 ro, out vec3 rd) {
float yaw = (mouse.x - 0.5) * 6.2831 + time * drift;
float pitch = clamp((mouse.y - 0.5) * 2.5, -1.4, 1.4);
ro = vec3(0.0, 0.0, dist);
ro.yz *= rot(pitch);
ro.xz *= rot(yaw);
vec3 fw = normalize(-ro), rt = normalize(cross(fw, vec3(0, 1, 0))), up = cross(rt, fw);
rd = normalize(uv.x * rt + uv.y * up + 1.8 * fw);
}
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); }
// Menger sponge: a box, then carve crosses at ever smaller scales (each iteration is 3x smaller).
vec2 map(vec3 p) {
float d = sdBox(p, vec3(1.0));
float s = 1.0, trap = 1e9;
for (int i = 0; i < 6; i++) {
if (float(i) >= iterations) break;
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), db = max(r.y, r.z), dc = max(r.z, r.x);
float c = (min(da, min(db, dc)) - 1.0) / s;
d = max(d, c);
trap = min(trap, dot(r, r));
}
return vec2(d, trap);
}
vec3 normalAt(vec3 p) {
vec2 e = vec2(0.0005, 0.0);
return normalize(vec3(map(p + e.xyy).x - map(p - e.xyy).x, map(p + e.yxy).x - map(p - e.yxy).x, map(p + e.yyx).x - map(p - e.yyx).x));
}
float softShadow(vec3 ro, vec3 rd) {
float res = 1.0, t = 0.01;
for (int i = 0; i < 40; i++) {
float h = map(ro + rd * t).x;
res = min(res, 12.0 * h / t);
t += clamp(h, 0.005, 0.1);
if (res < 0.002 || t > 4.0) break;
}
return clamp(res, 0.0, 1.0);
}
float ao(vec3 p, vec3 n) {
float occ = 0.0, w = 1.0;
for (int i = 1; i <= 5; i++) {
float h = 0.02 * float(i);
occ += (h - map(p + n * h).x) * w;
w *= 0.7;
}
return clamp(1.0 - 6.0 * occ, 0.0, 1.0);
}
void main() {
vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
vec3 ro, rd;
orbit(uv, 3.2 - 0.3 * sin(time * 0.2), 0.08, ro, rd);
float t = 0.0, steps = 0.0;
vec2 h = vec2(0.0);
for (int i = 0; i < 160; i++) {
h = map(ro + rd * t);
if (h.x < 0.0004 * t || t > 12.0) break;
t += h.x;
steps += 1.0;
}
vec3 bg = mix(vec3(0.02, 0.02, 0.05), vec3(0.15, 0.1, 0.25), uv.y + 0.5);
vec3 col = bg;
if (t < 12.0) {
vec3 p = ro + rd * t, n = normalAt(p);
vec3 l = normalize(vec3(0.6, 0.7, 0.4));
float dif = max(dot(n, l), 0.0) * softShadow(p + n * 0.002, l);
float occ = ao(p, n);
vec3 base = 0.5 + 0.5 * cos(6.2831 * (h.y * 0.05 + spectrumIntegrated.x * 0.1 + vec3(0.0, 0.33, 0.67)));
col = base * (0.15 * occ + dif) + pow(max(dot(reflect(rd, n), l), 0.0), 24.0) * dif * 0.4;
col = mix(col, bg, 1.0 - exp(-0.04 * t * t));
}
// Glow from how hard the march worked: edges light up, more with the kick.
col += vec3(0.5, 0.35, 1.0) * pow(steps / 160.0, 2.0) * glow * (1.0 + 2.0 * beat);
fragColor = vec4(pow(col, vec3(0.4545)), 1.0);
}More Showpieces examples
Gasket lantern · Lava world · Deep glow · Prism · Mandelbulb · Volumetric clouds · Ocean · KIFS tunnel