Gyroid cave
glint · Examples · Tutorials · Gallery
Flying through a gyroid, the endless minimal surface: a cave of smooth tunnels that branch forever, lit by your own headlamp.

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))); }
vec2 path(float z) { return vec2(sin(z * 0.3) * 1.5, cos(z * 0.23) * 1.2); }
float map(vec3 p) {
float g = abs(dot(sin(p * 1.3), cos(p.zxy * 1.3))) / 1.3 - 0.12;
// Keep a tunnel open around the camera's path.
float tunnel = 0.9 - length(p.xy - path(p.z));
return max(g, tunnel) * 0.8;
}
vec3 normal(vec3 p) {
vec2 e = vec2(0.002, 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 main() {
vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
float z = time * 1.5;
vec3 ro = vec3(path(z), z);
vec3 target = vec3(path(z + 1.0), z + 1.0);
vec3 f = normalize(target - ro), r = normalize(cross(f, vec3(0.0, 1.0, 0.0))), u = cross(r, f);
vec3 rd = normalize(mat3(r, u, f) * vec3(uv, 1.2));
float t = 0.0;
int steps = 0;
for (int i = 0; i < 100; i++) {
float d = map(ro + rd * t);
steps = i;
if (d < 0.001 || t > 20.0) break;
t += d;
}
vec3 col = vec3(0.0);
if (t < 20.0) {
vec3 p = ro + rd * t;
vec3 n = normal(p);
float lamp = max(dot(n, -rd), 0.0);
float ao = 1.0 - float(steps) / 100.0;
col = pal(p.z * 0.02 + 0.55) * (0.15 + 0.85 * lamp) * ao;
col += pow(lamp, 30.0) * 0.4;
}
col = mix(col, vec3(0.02, 0.03, 0.05), 1.0 - exp(-t * 0.12));
fragColor = vec4(pow(col, vec3(0.8)), 1.0);
}More More GLSL examples
Rain on the window · 2D distance field playground · Julia set · Mandelbrot zoom · Lava lamp · Circuit board