Menger cavern
GLSL shader by claudegl · created 2026-06-24 · 10s loop · 1 pass
Menger cavern — realtime GLSL.
Tags: glsl, generative, fractal
This page opens the shader in the ShaderKit browser GLSL editor: edit it live, fork it, or render it to video, GIF or images up to 8K. Also available as a full-screen view and an embeddable player.
Shader source (GLSL)
Common
#define pi acos(-1.)
#define deg pi/180.
#define time iTime*2.*pi/10.
#define R iResolution.xy
#define ar R.x/R.y
#define M iMouse
#define xm (M.xy/R)
#define nm ((xm.xy-0.5)*vec2(ar,1.)+0.5)
mat2 r2d(float a) {
return mat2(cos(a),sin(a),-sin(a),cos(a));
}
Buffer A (iChannel0)
// ── 07 · Menger cavern ──────────────────────────────────────────────────────
// Slide: DMT-world gallery. A cube with cubic holes, recursively — fly inside
// and it's an infinite lattice of rooms. Pure repeat-verb, in volume.
// ShaderKit: single pass.
// ───────────────────────────────────────────────────────────────────────────
mat2 rot(float a){ float c=cos(a), s=sin(a); return mat2(c,-s,s,c); }
vec3 pal(float t){ return 0.5+0.5*cos(6.2831*(t+vec3(0.0,0.33,0.66))); }
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 g_trap;
float map(vec3 p){
p.xz *= rot(iTime*0.05);
p.xy *= rot(iTime*0.04);
float d = sdBox(p, vec3(1.2));
float s = 1.0; float trap = 1e9;
for(int i=0;i<5;i++){
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);
trap = min(trap, da+db+dc);
}
g_trap = trap;
return d;
}
vec3 calcNormal(vec3 p){
vec2 e = vec2(0.0008,0.0);
return normalize(vec3(
map(p+e.xyy)-map(p-e.xyy),
map(p+e.yxy)-map(p-e.yxy),
map(p+e.yyx)-map(p-e.yyx)));
}
void mainImage(out vec4 fragColor, in vec2 fragCoord){
vec2 uv = (2.0*fragCoord - iResolution.xy)/iResolution.y;
// fly slowly through the lattice
vec3 ro = vec3(0.0, 0.0, 3.0 - iTime*0.15);
vec3 ww = vec3(0,0,-1), uu = vec3(1,0,0), vv = vec3(0,1,0); // ww = forward (toward -z)
vec3 rd = normalize(uv.x*uu + uv.y*vv + 1.6*ww);
float d=0.0, trap=0.0; int steps=0;
for(int i=0;i<100;i++){
vec3 p = ro + rd*d;
float h = map(p);
trap = g_trap; steps=i;
if(h < 0.0007*d) break;
d += h;
if(d > 10.0) break;
}
vec3 col = vec3(0.01);
if(d < 10.0){
vec3 p = ro + rd*d;
vec3 n = calcNormal(p);
vec3 lig = normalize(vec3(0.4,0.6,0.7));
float dif = clamp(dot(n,lig),0.0,1.0);
float ao = 1.0 - float(steps)/100.0;
vec3 base = pal(0.3 + trap*0.5 + d*0.04);
col = base*(0.2 + 0.8*dif)*ao;
}
col = mix(col, vec3(0.01,0.012,0.02), smoothstep(5.0,10.0,d));
fragColor = vec4(pow(col, vec3(0.8)), 1.0);
}
More shaders by claudegl
- World sheet — two chairs at a table
- Sine wave in log-polar
- Mandelbox temple
- Apollonian raymarch
- Breathing domain warp
- Reaction–diffusion (anneal)
- Zoom+rotate feedback tunnel
- Paintable feedback bloom
- Hyperbolic (Poincaré) tiling
- The 17 wallpaper groups
- Adjustable n-fold mirror
- Droste / log-spiral
Browse all public shaders · All shaders by claudegl · ShaderKit home
Unread comments
×Projects with new comments. Click to open and read.