Sierpinski tetrahedron
glint · Examples · Tutorials · Gallery
A 3D Sierpinski fractal as a distance field: fold space across three planes (so every point lands in one corner), scale up by two, repeat eight times.

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))); }
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 trap;
float map(vec3 p) {
p.xz = rot(time * 0.2) * p.xz;
float s = 1.0;
trap = 1e9;
for (int i = 0; i < 8; i++) {
// Fold into the corner nearest (1, 1, 1): three mirror planes.
if (p.x + p.y < 0.0) p.xy = -p.yx;
if (p.x + p.z < 0.0) p.xz = -p.zx;
if (p.y + p.z < 0.0) p.zy = -p.yz;
p = p * 2.0 - vec3(1.0);
s *= 2.0;
trap = min(trap, dot(p, p));
}
return (length(p) - 1.2) / s;
}
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;
vec3 ro = vec3(5.0 * sin(time * 0.1), 2.5, -5.0 * cos(time * 0.1));
vec3 rd = cameraRay(ro, vec3(0.0), uv, 1.5);
float t = 0.0;
int steps = 0;
for (int i = 0; i < 120; i++) {
float d = map(ro + rd * t);
steps = i;
if (d < 0.0005 * t || t > 8.0) break;
t += d;
}
vec3 col = mix(vec3(0.05, 0.05, 0.08), vec3(0.2, 0.15, 0.25), uv.y + 0.5);
if (t < 8.0) {
vec3 p = ro + rd * t;
vec3 n = normal(p);
map(p);
vec3 base = pal(0.6 + log(trap) * 0.08);
float ao = 1.0 - float(steps) / 120.0;
col = base * (0.2 + 0.8 * max(dot(n, normalize(vec3(0.5, 0.8, -0.4))), 0.0)) * ao;
}
fragColor = vec4(pow(col, vec3(0.4545)), 1.0);
}More More GLSL examples
Digital rain · Bokeh · ASCII · Soap bubbles · CRT television · Lightning · Candle · Planet