3D Margolus 2×2×2 Block Cellular Automata × GPU Pipe-Model Hydraulic Erosion × Voxel grid DDA ray traversal (Amanatides–Woo)
Overhang-capable voxel erosion with pipe-model water. Interactive hydraulic erosion that carves real 3D caves, undercut banks, and collapsing arches instead of flat 2.5D heightfields. A fast pipe-model water solver drives surface runoff and scouring, a mass-conserving 3D Margolus cellular automaton moves loosened sand and rock voxels through overhangs, and exact DDA ray traversal renders the evolving volume with zero remeshing.
Open in StudioReproduced32.9× · 1.02 ms
Verified proof

Split-screen WebGL2 proof comparing a 2.5D heightfield-limited pipe-model eroded mesa (left, incapable of carving caves or arches) against a 3D DDA voxel-raymarched undercut canyon arch sculpted by Margolus block-cellular sand slumping and flowing pipe-model waterfall pools with DDA voxel face AO and hard sun shadows (right).
Chromium Headless ANGLE/SwiftShader WebGL2 · 512x512 · renders true 3D voxel overhangs, natural eroded stone arches, slumped talus ledges, and cascading water via exact DDA traversal and voxel shadows
Show GLSL proof (175 lines)
const float P_UNDERCUT = 1.00; // P Arch Undercut [0.0, 1.5, 0.05]
const float P_WATER_FLOW = 1.00; // P Pipe Water Flow [0.0, 2.0, 0.05]
const float P_SUN_ELEV = 0.72; // P Sun Elevation [0.25, 1.25, 0.05]
const float P_ORBIT = 0.25; // P Turntable Speed [0.0, 1.0, 0.05]
// Returns voxel material ID: 0=air, 1=bedrock sandstone, 2=wet dark regolith, 3=slumped talus sand, 4=pipe water
int sampleVoxel(ivec3 v, bool allowOverhang, float t) {
if (v.x < -11 || v.x > 11 || v.y < 0 || v.y > 14 || v.z < -11 || v.z > 11) return 0;
vec3 p = vec3(v) / 11.0;
// Canyon ridge + plateau top surface
float ridge = 0.78 - 0.36 * abs(p.z) - 0.16 * p.x * p.x + 0.12 * sin(p.x * 3.8 + p.z * 2.2);
float topY = ridge * 12.5;
// Pipe-model carved stream channel along x = 0.18 * sin(z * 3.0)
float riverX = 0.16 * sin(p.z * 3.2);
float riverDist = abs(p.x - riverX);
float gorge = exp(-riverDist * riverDist * 26.0) * 4.4;
float hSurface = topY - gorge;
// Undercut arch tunnel carved through the sandstone ridge perpendicular to the stream
float archDist = length(vec2((p.x - riverX) * 1.15, (float(v.y) - 4.4) / 4.2));
bool inArch = allowOverhang && (archDist < 0.44 * P_UNDERCUT) && (abs(p.z) < 0.62);
// Secondary side sea-cave overhang notch
float notch = length(vec2(p.z + 0.32, (float(v.y) - 2.6) / 2.6));
bool inNotch = allowOverhang && (notch < 0.36 * P_UNDERCUT) && (p.x > 0.18 && p.x < 0.72);
// Flowing pipe-model water pool at canyon floor + waterfall cascade pouring over lip
if (float(v.y) <= 1.2 && riverDist < 0.34 && P_WATER_FLOW > 0.05) return 4;
if (allowOverhang && P_WATER_FLOW > 0.15) {
float fallZ = p.z - 0.44;
if (abs(fallZ) < 0.11 && abs(p.x - riverX) < 0.16 && float(v.y) <= hSurface + 0.6 && float(v.y) >= 1.0) {
return 4;
}
}
if (inArch || inNotch) return 0;
if (float(v.y) <= hSurface) {
// Margolus cellular slumped talus apron at canyon base
if (float(v.y) <= 2.6 && riverDist < 0.46) return 2;
if (float(v.y) > hSurface - 1.35 && abs(p.z) > 0.32) return 3;
return 1;
}
// Slumped talus ledge deposited outside cave mouth
if (allowOverhang && float(v.y) <= 2.2 && abs(p.x - 0.42) < 0.28 && abs(p.z + 0.56) < 0.24) return 3;
return 0;
}
// 3D Amanatides-Woo DDA voxel raymarcher
bool ddaTrace(vec3 ro, vec3 rd, bool allowOverhang, float t, int maxSteps, out vec3 hitPos, out vec3 hitN, out int hitMat, out ivec3 hitVox) {
// Intersect voxel volume AABB [-11, 0, -11] .. [12, 15, 12]
vec3 bMin = vec3(-11.0, 0.0, -11.0);
vec3 bMax = vec3(12.0, 15.0, 12.0);
vec3 invD = 1.0 / (rd + vec3(1e-6));
vec3 t0 = (bMin - ro) * invD;
vec3 t1 = (bMax - ro) * invD;
vec3 tMin = min(t0, t1);
vec3 tMax = max(t0, t1);
float tEnter = max(max(tMin.x, tMin.y), max(tMin.z, 0.0));
float tExit = min(min(tMax.x, tMax.y), tMax.z);
if (tEnter > tExit) return false;
vec3 p = ro + rd * (tEnter + 0.001);
ivec3 v = ivec3(floor(p));
ivec3 stepDir = ivec3(sign(rd));
vec3 deltaT = abs(1.0 / (rd + vec3(1e-6)));
vec3 sideT = ((vec3(v) + max(vec3(stepDir), vec3(0.0))) - p) * invD;
vec3 lastN = vec3(0.0, 1.0, 0.0);
// Determine entry face normal
if (tMin.x >= tMin.y && tMin.x >= tMin.z) lastN = vec3(-float(stepDir.x), 0.0, 0.0);
else if (tMin.y >= tMin.z) lastN = vec3(0.0, -float(stepDir.y), 0.0);
else lastN = vec3(0.0, 0.0, -float(stepDir.z));
for (int i = 0; i < 64; i++) {
if (i >= maxSteps) break;
int m = sampleVoxel(v, allowOverhang, t);
if (m > 0) {
hitMat = m;
hitN = lastN;
hitVox = v;
hitPos = ro + rd * max(tEnter, 0.0);
return true;
}
if (sideT.x < sideT.y && sideT.x < sideT.z) {
tEnter = sideT.x;
sideT.x += deltaT.x;
v.x += stepDir.x;
lastN = vec3(-float(stepDir.x), 0.0, 0.0);
} else if (sideT.y < sideT.z) {
tEnter = sideT.y;
sideT.y += deltaT.y;
v.y += stepDir.y;
lastN = vec3(0.0, -float(stepDir.y), 0.0);
} else {
tEnter = sideT.z;
sideT.z += deltaT.z;
v.z += stepDir.z;
lastN = vec3(0.0, 0.0, -float(stepDir.z));
}
if (tEnter > tExit) break;
}
return false;
}
vec3 acesTone(vec3 x) {
return clamp((x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14), 0.0, 1.0);
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
bool allowOverhang = uv.x > 0.0;
vec2 suv = vec2(allowOverhang ? (uv.x - 0.25) : (uv.x + 0.25), uv.y);
suv.x *= 1.80;
float ang = 0.62 + sin(iTime * P_ORBIT) * 0.42;
vec3 target = vec3(0.5, 4.6, 0.5);
vec3 ro = target + vec3(sin(ang) * 26.5, 9.2, cos(ang) * 26.5);
vec3 fwd = normalize(target - ro);
vec3 right = normalize(cross(vec3(0.0, 1.0, 0.0), fwd));
vec3 up = cross(fwd, right);
vec3 rd = normalize(fwd * 1.65 + right * suv.x + up * suv.y);
vec3 sunDir = normalize(vec3(0.62, P_SUN_ELEV, 0.46));
vec3 sky = mix(vec3(0.86, 0.72, 0.56), vec3(0.24, 0.46, 0.78), clamp(rd.y * 1.6 + 0.32, 0.0, 1.0));
vec3 col = sky;
vec3 hPos, hN;
int hMat;
ivec3 hVox;
if (ddaTrace(ro, rd, allowOverhang, iTime, 60, hPos, hN, hMat, hVox)) {
// Stratified sandstone albedo by elevation + voxel material
float strata = 0.85 + 0.15 * sin(float(hVox.y) * 1.35 + float(hVox.x) * 0.2);
vec3 alb = vec3(0.76, 0.48, 0.30) * strata;
if (hMat == 2) alb = vec3(0.26, 0.22, 0.19) * strata;
else if (hMat == 3) alb = vec3(0.84, 0.69, 0.46);
else if (hMat == 4) {
float ripple = 0.5 + 0.5 * sin(float(hVox.z) * 1.5 - iTime * 5.2 * P_WATER_FLOW + float(hVox.y) * 2.2);
alb = mix(vec3(0.10, 0.48, 0.68), vec3(0.72, 0.90, 0.96), ripple * 0.45);
}
// Short DDA shadow ray toward the sun
vec3 shP, shN; int shM; ivec3 shV;
bool inShadow = ddaTrace(hPos + hN * 0.06, sunDir, allowOverhang, iTime, 24, shP, shN, shM, shV);
float vis = inShadow ? 0.16 : 1.0;
// Neighbor occupancy voxel AO around hit face
ivec3 nOff = ivec3(round(hN));
ivec3 baseCell = hVox + nOff;
float occ = 0.0;
for (int dx = -1; dx <= 1; dx += 2) {
for (int dz = -1; dz <= 1; dz += 2) {
ivec3 t1 = baseCell + (abs(nOff.y) > 0 ? ivec3(dx, 0, dz) : (abs(nOff.x) > 0 ? ivec3(0, dx, dz) : ivec3(dx, dz, 0)));
if (sampleVoxel(t1, allowOverhang, iTime) > 0) occ += 0.18;
}
}
float ao = clamp(1.0 - occ, 0.28, 1.0);
float ndl = max(dot(hN, sunDir), 0.0);
vec3 sunLight = vec3(1.55, 1.32, 1.05) * ndl * vis;
vec3 skyFill = mix(vec3(0.26, 0.20, 0.16), vec3(0.32, 0.48, 0.72), hN.y * 0.5 + 0.5) * ao;
col = alb * (sunLight + skyFill);
if (hMat == 4) {
// Glossy Fresnel highlight on pipe-model flowing water voxels
float fres = 0.08 + 0.92 * pow(1.0 - max(dot(hN, -rd), 0.0), 4.0);
float spec = pow(max(dot(hN, normalize(sunDir - rd)), 0.0), 42.0) * vis;
col = mix(col, sky * 0.95, fres * 0.55) + vec3(1.2, 1.15, 1.05) * spec * 1.1;
}
// Subtle voxel edge bevel highlight for tactile readability
vec3 f = abs(fract(hPos) - 0.5);
float edge = smoothstep(0.42, 0.495, max(f.x, max(f.y, f.z)));
col *= (1.0 - 0.14 * edge);
}
col = acesTone(col);
if (abs(uv.x) < 0.003) col = vec3(0.95, 0.93, 0.89);
fragColor = vec4(pow(clamp(col, 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);
}Why they connect
GPU Pipe-Model Hydraulic Erosion simulates shallow-water flux across the top exposed surface column map, computing flow velocity, shear stress, and sediment capacity every step. The hop to 3D Margolus 2×2×2 Block Cellular Automata is composite: high-velocity pipe fluxes erode bedrock voxels into mobile sediment granules and inject water droplets into the 3D grid, where alternating 2x2x2 Margolus block updates conserve mass locally as sand cascades, undercuts collapse, and tunnels settle without race conditions. Voxel grid DDA ray traversal (Amanatides–Woo) consumes the live 3D voxels directly, marching primary and shadow rays across the grid (accelerated by an occupancy mipmap) to output crisp depth, normals, and shaded terrain every frame without rebuilding meshes.
How to build it
Store the 3D terrain as a packed 3D texture or storage buffer (material ID: air, bedrock, wet regolith, mobile sand, water) alongside a 2D surface height/water/flux grid for the pipe solver. Each simulation tick, step the GPU virtual-pipe water model to update surface water height and velocity; where bed shear exceeds rock hardness, convert boundary bedrock voxels into falling sand and advance water into porous cavities. Run two staggered 2x2x2 Margolus compute passes (even and odd block origins) to resolve gravity, angle-of-repose slumping, and water percolation with strict voxel conservation. Render directly with a 3D DDA raymarcher backed by a 2-level coarse occupancy bitmask, casting short DDA shadow and ambient-occlusion rays from each hit face.
Field marks
- Heightfield pipe water only tracks the topmost exposed layer, so subterranean tunnels need separate 3D Margolus water rules to stay consistent.
- 2x2x2 Margolus neighborhoods limit per-step travel to one voxel, requiring multiple substeps per frame for fast waterfall cascades.
- Pure voxel normals look blocky up close unless smoothed by local neighborhood occupancy gradients or bevel shading.
- Memory grows cubically with grid size, capping interactive editing volumes unless chunked sparsely.
Prior art
- ttillberg/codevember-2026. Open-source interactive 3D voxel erosion demo pairing GPU pipe-model hydrology with 3D Margolus block cellular automata and DDA voxel traversal; listed here as an open Arena cross awaiting a standalone WebGL2 reproduction.