Apollonian
glint · Examples · Tutorials · Gallery
Flying through an Apollonian fractal: space folded into a repeating cube and inverted in a sphere, eight times over. Bend changes the inversion (small changes, big differences).

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;
uniform float bend;
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) {
float s = 1.0;
trap = 1e10;
for (int i = 0; i < 8; i++) {
p = -1.0 + 2.0 * fract(0.5 * p + 0.5); // fold into -1..1 (repeat space)
float r2 = dot(p, p);
trap = min(trap, r2);
float k = bend / r2; // invert in a sphere
p *= k;
s *= k;
}
return 0.25 * abs(p.y) / s;
}
void main() {
vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
float a = time * 0.05;
vec3 ro = vec3(1.6 * cos(a), 1.2 + 0.4 * sin(a * 2.0), 1.6 * sin(a));
vec3 rd = cameraRay(ro, vec3(0.0, 0.9, 0.0), uv, 1.3);
float t = 0.01;
int steps = 0;
for (int i = 0; i < 160; i++) {
float d = map(ro + rd * t);
steps = i;
if (d < 0.0002 * t || t > 8.0) break;
t += d;
}
vec3 col = vec3(0.02, 0.02, 0.03);
if (t < 8.0) {
map(ro + rd * t);
// Colour from how close the orbit came to the centre; ambient occlusion from how many steps it took.
vec3 base = mix(vec3(1.0, 0.7, 0.35), vec3(0.3, 0.5, 0.9), clamp(trap * 1.5, 0.0, 1.0));
float ao = 1.0 - float(steps) / 160.0;
col = base * ao * ao;
col = mix(col, vec3(0.02, 0.02, 0.03), 1.0 - exp(-t * 0.4));
}
fragColor = vec4(pow(col, vec3(0.4545)), 1.0);
}More More GLSL examples
Stained glass · DNA helix · Clock · Desert dunes · Digital rain · Bokeh · ASCII · Soap bubbles