KIFS tunnel
glint · Examples · Tutorials · Gallery
Flying down an endless tunnel of kaleidoscopic IFS folds: domain repetition along z, rotations tied to the music, volumetric glow.
Audio-reactive: it moves with music from a microphone, line-in or a file.

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 vec2 mouse;
uniform vec3 spectrum;
uniform vec3 spectrumIntegrated;
uniform float beat;
uniform float twist;
uniform float speed;
in vec2 v_texcoord;
out vec4 fragColor;
mat2 rot(float a) { float c = cos(a), s = sin(a); return mat2(c, -s, s, c); }
// Kaleidoscopic IFS (Knighty's method): mirror-fold into one wedge, rotate, scale about an offset, repeat.
// Each step doubles the detail; the distance is scaled back down at the end.
float kifs(vec3 p) {
const float scale = 2.0;
vec3 offset = vec3(1.0, 1.1, 0.8);
mat2 r1 = rot(0.4 + twist * 0.15 + spectrumIntegrated.y * 0.15);
mat2 r2 = rot(0.25 + spectrum.x * 0.4);
for (int i = 0; i < 7; i++) {
p = abs(p);
if (p.x < p.y) p.xy = p.yx;
if (p.x < p.z) p.xz = p.zx;
if (p.y < p.z) p.yz = p.zy;
p.xy *= r1;
p.yz *= r2;
p = p * scale - offset * (scale - 1.0);
if (p.z < -0.5 * offset.z * (scale - 1.0)) p.z += offset.z * (scale - 1.0);
}
vec3 q = abs(p) - vec3(1.0);
return (length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0)) * pow(scale, -7.0);
}
float map(vec3 p) {
p.xy *= rot(p.z * 0.05 * twist);
// An endless lattice of crystals (about 3 units across), one per 7-unit cell.
vec3 q = mod(p, 7.0) - 3.5;
return kifs(q);
}
void main() {
vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
float z = time * speed + spectrumIntegrated.x * 3.0;
vec3 ro = vec3(0.0, 0.0, z); // on the lattice edges, between crystals (cells are centred at 3.5 mod 7)
vec3 rd = normalize(vec3(uv, 1.2));
rd.xy *= rot(sin(time * 0.2) * 0.3 + (mouse.x - 0.5));
float t = 0.0, glow = 0.0;
float d = 0.0;
for (int i = 0; i < 110; i++) {
d = map(ro + rd * t);
glow += 0.01 / (0.02 + abs(d));
if (d < 0.001 || t > 30.0) break;
t += d * 0.8;
}
vec3 hue = 0.5 + 0.5 * cos(6.2831 * (t * 0.03 + z * 0.02 + vec3(0.0, 0.33, 0.67)));
// Lit facets where the ray hit, glow from near misses in a second hue, fog with distance.
vec3 hue2 = 0.5 + 0.5 * cos(6.2831 * (t * 0.05 + 0.5 + vec3(0.0, 0.33, 0.67)));
vec3 col = vec3(0.0);
if (d < 0.01) {
vec3 p = ro + rd * t;
vec2 e = vec2(0.002, 0.0);
vec3 n = 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)));
vec3 l = normalize(vec3(0.4, 0.8, -0.5));
float dif = max(dot(n, l), 0.0);
float spec = pow(max(dot(reflect(rd, n), l), 0.0), 20.0);
float rim = pow(1.0 - max(dot(n, -rd), 0.0), 3.0);
col = hue * (0.12 + 0.9 * dif) + spec * 0.8 + rim * hue2 * 0.6;
col *= exp(-t * 0.06);
}
col += hue2 * glow * 0.004 * (1.0 + beat * 1.5);
col = 1.0 - exp(-col * 1.3);
fragColor = vec4(col, 1.0);
}More Showpieces examples
Menger sponge · Mandelbulb · Volumetric clouds · Ocean · Truchet weave · Voronoi crystals · Domain warping · Neon city