Quaternion Julia
glint · Examples · Tutorials · Gallery
A 4D Julia set sliced into 3D and raymarched: quaternion iteration with an analytic distance estimate, the constant drifting through 4D space.
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;
in vec2 v_texcoord;
out vec4 fragColor;
mat2 rot(float a) { float c = cos(a), s = sin(a); return mat2(c, -s, s, c); }
// Ray from an orbiting camera: yaw from mouse.x (plus slow drift), pitch from mouse.y.
void orbit(vec2 uv, float dist, float drift, out vec3 ro, out vec3 rd) {
float yaw = (mouse.x - 0.5) * 6.2831 + time * drift;
float pitch = clamp((mouse.y - 0.5) * 2.5, -1.4, 1.4);
ro = vec3(0.0, 0.0, dist);
ro.yz *= rot(pitch);
ro.xz *= rot(yaw);
vec3 fw = normalize(-ro), rt = normalize(cross(fw, vec3(0, 1, 0))), up = cross(rt, fw);
rd = normalize(uv.x * rt + uv.y * up + 1.8 * fw);
}
vec4 qmul(vec4 a, vec4 b) { return vec4(a.x * b.x - dot(a.yzw, b.yzw), a.x * b.yzw + b.x * a.yzw + cross(a.yzw, b.yzw)); }
vec4 qsq(vec4 a) { return vec4(a.x * a.x - dot(a.yzw, a.yzw), 2.0 * a.x * a.yzw); }
vec4 constantC() {
float t = time * 0.15 + spectrumIntegrated.x * 0.2;
return 0.45 * cos(vec4(0.5, 3.9, 1.4, 1.1) + t * vec4(1.2, 1.7, 1.3, 2.5)) - vec4(0.3, 0.0, 0.0, 0.0);
}
// Distance estimate: 0.5 * |z| * log|z| / |dz|, iterating z -> z^2 + c and its derivative dz -> 2 z dz.
vec2 julia(vec3 p, vec4 c) {
vec4 z = vec4(p, 0.0);
vec4 dz = vec4(1.0, 0.0, 0.0, 0.0);
float trap = 1e9;
for (int i = 0; i < 11; i++) {
dz = 2.0 * qmul(z, dz);
z = qsq(z) + c;
trap = min(trap, dot(z, z));
if (dot(z, z) > 16.0) break;
}
float r = length(z);
return vec2(0.25 * r * log(r) / length(dz), trap);
}
void main() {
vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
vec3 ro, rd;
orbit(uv, 3.0, 0.12, ro, rd);
vec4 c = constantC();
float t = 0.0;
vec2 h = vec2(0.0);
for (int i = 0; i < 150; i++) {
h = julia(ro + rd * t, c);
if (h.x < 0.0005 * t || t > 8.0) break;
t += h.x;
}
vec3 col = vec3(0.02, 0.02, 0.04) * (1.0 - length(uv) * 0.5);
if (t < 8.0) {
vec3 p = ro + rd * t;
vec2 e = vec2(0.001 * t, 0.0);
vec3 n = normalize(vec3(julia(p + e.xyy, c).x - julia(p - e.xyy, c).x, julia(p + e.yxy, c).x - julia(p - e.yxy, c).x, julia(p + e.yyx, c).x - julia(p - e.yyx, c).x));
vec3 l = normalize(vec3(0.6, 0.8, -0.4));
vec3 base = 0.5 + 0.5 * cos(6.2831 * (sqrt(h.y) * 0.6 + vec3(0.6, 0.7, 0.8)));
float dif = max(dot(n, l), 0.0);
float rim = pow(1.0 - max(dot(n, -rd), 0.0), 4.0);
col = base * (0.2 + 0.8 * dif) + pow(max(dot(reflect(rd, n), l), 0.0), 40.0) * 0.6 + rim * vec3(0.5, 0.6, 1.0) * (0.5 + beat);
}
fragColor = vec4(pow(col, vec3(0.4545)), 1.0);
}More Showpieces examples
GPU particles · Physarum · SmoothLife · Glitch · Aurora · Caustics · Glass orbs · Four to the floor