Voxel world
glint · Examples · Tutorials · Gallery
A blocky landscape rendered by stepping a ray through a grid one cell wall at a time (the DDA algorithm, as in Wolfenstein and Minecraft-style engines): exact cubes, no distance field needed.

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;
in vec2 v_texcoord;
out vec4 fragColor;
float hash(float n) { return fract(sin(n * 127.1) * 43758.5453); }
float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
vec2 hash2(float n) { return fract(sin(vec2(n * 127.1, n * 269.5)) * 43758.5453); }
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.28318 * (t + vec3(0.0, 0.33, 0.67))); }
float noise(vec2 p) {
vec2 i = floor(p), f = fract(p);
f = f * f * (3.0 - 2.0 * f);
return mix(mix(hash(i), hash(i + vec2(1.0, 0.0)), f.x), mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), f.x), f.y);
}
float fbm(vec2 p) {
float v = 0.0, a = 0.5;
for (int i = 0; i < 5; i++) { v += a * noise(p); p = p * 2.03 + 17.0; a *= 0.5; }
return v;
}
vec3 cameraRay(vec3 ro, vec3 target, vec2 uv, float zoom) {
vec3 f = normalize(target - ro);
vec3 r = normalize(cross(vec3(0.0, 1.0, 0.0), f));
vec3 u = cross(f, r);
return normalize(uv.x * r + uv.y * u + zoom * f);
}
float ground(vec2 c) { return floor(noise(c * 0.07) * 10.0 + noise(c * 0.21) * 3.0); }
bool solid(vec3 c) { return c.y < ground(c.xz) || (c.y < 3.0); } // below the ground, or water
void main() {
vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
vec3 ro = vec3(time * 3.0, 17.0, time * 2.0);
vec3 rd = cameraRay(ro, ro + vec3(1.0, -0.55, 0.7), uv, 1.3);
vec3 cell = floor(ro), stepv = sign(rd), tDelta = abs(1.0 / rd);
vec3 tMax = (cell + max(stepv, 0.0) - ro) / rd;
int axis = 0;
float t = 0.0;
bool hit = false;
for (int i = 0; i < 200; i++) {
if (solid(cell)) { hit = true; break; }
if (tMax.x < tMax.y && tMax.x < tMax.z) { cell.x += stepv.x; t = tMax.x; tMax.x += tDelta.x; axis = 0; }
else if (tMax.y < tMax.z) { cell.y += stepv.y; t = tMax.y; tMax.y += tDelta.y; axis = 1; }
else { cell.z += stepv.z; t = tMax.z; tMax.z += tDelta.z; axis = 2; }
}
vec3 sky = mix(vec3(0.7, 0.85, 1.0), vec3(0.3, 0.5, 0.9), max(rd.y, 0.0));
vec3 col = sky;
if (hit) {
vec3 p = ro + rd * t;
float g = ground(cell.xz);
bool water = cell.y >= g; // not ground, so it's water
vec3 base;
if (water) base = vec3(0.15, 0.35, 0.7);
else if (axis == 1) base = cell.y > 9.0 ? vec3(0.95) : cell.y < 4.0 ? vec3(0.85, 0.8, 0.55) : vec3(0.3, 0.65, 0.2);
else base = fract(p.y) > 0.8 && cell.y == g - 1.0 && cell.y >= 4.0 ? vec3(0.3, 0.6, 0.2) : vec3(0.45, 0.3, 0.2);
base *= 0.9 + 0.1 * hash(cell.xz + cell.y);
float shade = axis == 1 ? 1.0 : (axis == 0 ? 0.75 : 0.6);
col = mix(base * shade, sky, smoothstep(40.0, 90.0, t));
}
fragColor = vec4(col, 1.0);
}More More GLSL examples
Mandelbox · Hyperbolic tiling · Wave interference · Lorenz attractor · Snowflakes · Maurer rose · Spirograph · Field lines