Sanctum
glint · Examples · Tutorials · Gallery
The heavy one. A raymarched cathedral of fractal lattice with light shafts through fog (a shadow test at every step), soft shadows from two lights, lit reflections on the floor and gold, then bloom, auto-focus depth of field, chromatic aberration, lens flare and film grain. The orb and the lights follow the music. Lower the resolution if it crawls.
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
3 passes, drawn in this order each frame, in GLSL.
Scene: fragment shader
#version 300 es
precision highp float;
uniform float time;
uniform vec2 resolution;
uniform vec3 spectrum;
uniform float beat;
uniform float beatTime;
uniform int beatCount;
uniform sampler2D fft;
uniform float fog;
in vec2 v_texcoord;
out vec4 fragColor;
// 1 on each kick, fading; without audio, a steady pulse twice a second.
float kick() { return beatCount > 0 ? beat : pow(1.0 - fract(time * 2.0), 4.0); }
// Seconds since the last kick, and which kick it was (the same fallback).
float sinceKick() { return beatCount > 0 ? beatTime : fract(time * 2.0) / 2.0; }
float kickId() { return beatCount > 0 ? float(beatCount) : floor(time * 2.0); }
// One FFT bin, 0..1 across the spectrum (bass on the left).
float bin(float x) { return texture(fft, vec2(x, 0.25)).r; }
// Is anything playing? (Examples fake their input when not.)
bool silent() { return spectrum.x + spectrum.y + spectrum.z < 0.0001; }
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))); }
// ---------------------------------------------------------------- shapes
float sdBox(vec3 p, vec3 b) { vec3 q = abs(p) - b; return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0); }
// An integer hash: stable however far down the nave the camera gets (sin() of large numbers isn't), and no
// repeating pattern (a fract() hash of whole numbers makes moiré rings).
float hash3(vec3 p) {
uvec3 q = uvec3(ivec3(p) + 100000) * uvec3(1597334673u, 3812015801u, 2798796415u);
uint n = (q.x ^ q.y ^ q.z) * 1597334673u;
return float(n >> 8) / 16777216.0;
}
float noise3(vec3 p) {
vec3 i = floor(p), f = fract(p);
f = f * f * (3.0 - 2.0 * f);
return mix(mix(mix(hash3(i), hash3(i + vec3(1, 0, 0)), f.x), mix(hash3(i + vec3(0, 1, 0)), hash3(i + vec3(1, 1, 0)), f.x), f.y),
mix(mix(hash3(i + vec3(0, 0, 1)), hash3(i + vec3(1, 0, 1)), f.x), mix(hash3(i + vec3(0, 1, 1)), hash3(i + vec3(1, 1, 1)), f.x), f.y), f.z);
}
// Set once per frame in main().
float bass, mids, kickNow;
vec3 orbPos;
// Off while testing the orb's own light for shadows (or the orb would shadow itself).
bool withOrb = true;
// A gothic window in a wall's own plane (q.x across the bay, q.y up): the opening is a pointed arch (two circles
// meeting at the top) over a tall rectangle; the stone left in it is a mullion, a transom and a rose window.
float windowOpening(vec2 q) {
float body = max(abs(q.x) - 0.72, abs(q.y - 0.3) - 0.9);
vec2 a = q - vec2(0.0, 1.2);
float arch = max(max(length(a - vec2(-0.5, 0.0)), length(a - vec2(0.5, 0.0))) - 1.22, -a.y);
float open = min(body, arch);
vec2 r = q - vec2(0.0, 1.6);
float ro = length(r);
float sector = 6.28318 / 8.0;
float ang = mod(atan(r.y, r.x) + sector * 0.5, sector) - sector * 0.5;
float rose = min(abs(ro - 0.36) - 0.035, max(abs(ro * sin(ang)) - 0.022, ro - 0.36)); // ring and spokes
rose = min(rose, abs(ro - 0.12) - 0.03);
float mullion = max(abs(q.x) - 0.045, q.y - 1.25);
float transom = max(abs(q.y - 0.45) - 0.035, abs(q.x) - 0.75);
float tracery = min(min(mullion, transom), rose);
return max(open, -tracery);
}
// Distance and material: 1 stone, 2 polished floor, 3 gold, 4 light strips, 5 the orb.
vec2 map(vec3 p) {
vec3 c = p;
c.z = mod(c.z, 4.0) - 2.0; // one bay every 4 units, for ever
vec2 res = vec2(p.y + 1.5, 2.0);
// Walls, each bay with its window cut right through.
float slab = abs(abs(p.x) - 2.75) - 0.2;
float wall = max(max(slab, p.y - 3.4), -windowOpening(vec2(c.z, p.y)));
// Clustered columns between the bays: four shafts, a base and a capital.
vec3 q = vec3(abs(p.x) - 2.35, p.y, abs(c.z) - 2.0);
float shafts = length(abs(q.xz) - 0.1) - 0.075;
float base = sdBox(q - vec3(0.0, -1.35, 0.0), vec3(0.24, 0.15, 0.24)) - 0.02;
float capital = sdBox(q - vec3(0.0, 2.35, 0.0), vec3(0.26, 0.1, 0.26)) - 0.02;
float column = min(min(shafts, base), capital);
// The vault, with an opening in every bay.
float vault = max(abs(p.y - 3.1) - 0.25, -sdBox(c - vec3(0.0, 3.1, 0.0), vec3(1.0, 1.0, 0.8)));
float stone = min(min(wall, column), vault);
if (stone < res.x) res = vec2(stone, 1.0);
// Gold arches over each bay, springing from the capitals.
float arch = length(vec2(length(p.xy - vec2(0.0, 2.45)) - 2.3, c.z)) - 0.06;
arch = max(arch, -(p.y - 2.3));
// Three gold rings spinning round the orb, like a gyroscope.
vec3 o = p - orbPos;
vec3 g1 = o; g1.yz = rot(time * 0.9) * g1.yz;
vec3 g2 = g1; g2.xy = rot(time * 1.3) * g2.xy;
vec3 g3 = g2; g3.xz = rot(time * 0.7) * g3.xz;
float rings = min(min(length(vec2(length(g1.xz) - 0.75, g1.y)) - 0.025, length(vec2(length(g2.xy) - 0.64, g2.z)) - 0.025), length(vec2(length(g3.yz) - 0.54, g3.x)) - 0.025);
float gold = min(arch, rings);
if (gold < res.x) res = vec2(gold, 3.0);
// Light strips along the floor.
float strip = sdBox(vec3(abs(p.x) - 1.6, p.y + 1.49, 0.0), vec3(0.025, 0.012, 1e3));
if (strip < res.x) res = vec2(strip, 4.0);
// The orb.
float orb = length(p - orbPos) - (0.3 + 0.08 * bass + 0.05 * kickNow);
if (withOrb && orb < res.x) res = vec2(orb, 5.0);
return res;
}
vec3 normal(vec3 p) {
vec2 e = vec2(0.0015, 0.0);
return normalize(vec3(map(p + e.xyy).x - map(p - e.xyy).x, map(p + e.yxy).x - map(p - e.yxy).x, map(p + e.yyx).x - map(p - e.yyx).x));
}
// ---------------------------------------------------------------- lighting
const vec3 SUN = normalize(vec3(-0.75, 0.5, 0.3)); // low, from the left: through the windows
const vec3 SUN_COLOUR = vec3(1.0, 0.82, 0.6) * 3.5;
vec3 orbColour() { return mix(vec3(1.0, 0.35, 0.15), vec3(0.3, 0.6, 1.0), 0.5 + 0.5 * sin(kickId() * 0.9)); }
// Soft shadow: how close the ray to the light passes to anything, relative to how far along it is. Between two steps
// the closest approach is estimated from both distances (Inigo Quilez's improved version), and the steps are kept
// short, or the shadow edges come out in bands.
float softShadow(vec3 ro, vec3 rd, float maxT, float k) {
float res = 1.0, t = 0.02, ph = 1e10;
for (int i = 0; i < 80; i++) {
float h = map(ro + rd * t).x;
float y = h * h / (2.0 * ph);
float d = sqrt(max(h * h - y * y, 0.0));
res = min(res, k * d / max(t - y, 0.0001));
ph = h;
t += clamp(h, 0.01, 0.2);
if (res < 0.002 || t > maxT) break;
}
res = clamp(res, 0.0, 1.0);
return res * res * (3.0 - 2.0 * res);
}
float hardShadow(vec3 ro, vec3 rd, float maxT) {
float t = 0.05;
for (int i = 0; i < 18; i++) {
float h = map(ro + rd * t).x;
if (h < 0.01) return 0.0;
t += max(h, 0.1);
if (t > maxT) break;
}
return 1.0;
}
float ambientOcclusion(vec3 p, vec3 n) {
float occ = 0.0, w = 1.0;
for (int i = 1; i <= 5; i++) {
float h = 0.06 * float(i);
occ += (h - map(p + n * h).x) * w;
w *= 0.6;
}
return clamp(1.0 - 2.5 * occ, 0.0, 1.0);
}
vec2 march(vec3 ro, vec3 rd, float maxT, int steps) {
float t = 0.0;
for (int i = 0; i < 200; i++) {
if (i >= steps) break;
vec2 h = map(ro + rd * t);
if (h.x < 0.0007 * t) return vec2(t, h.y);
t += h.x * 0.85;
if (t > maxT) break;
}
return vec2(maxT, 0.0);
}
vec3 albedo(vec3 p, float m) {
if (m < 1.5) return vec3(0.62, 0.57, 0.5) * (0.75 + 0.25 * noise3(p * 6.0)) * (0.85 + 0.15 * noise3(p * 30.0));
if (m < 2.5) return vec3(0.03, 0.03, 0.035);
return vec3(1.0, 0.72, 0.32);
}
vec3 emission(vec3 p, vec3 n, vec3 rd, float m) {
if (m > 4.5) {
// Plasma: swirling noise over the surface, hotter at the rim.
float swirl = noise3((p - orbPos) * 6.0 + vec3(0.0, time * 1.5, 0.0)) * noise3((p - orbPos) * 13.0 - vec3(time, 0.0, 0.0));
float rim = pow(1.0 - max(dot(n, -rd), 0.0), 2.0);
return orbColour() * (0.8 + 3.0 * swirl + 2.0 * rim) * (1.2 + 3.0 * bass + 3.0 * kickNow);
}
if (m > 3.5) return pal(time * 0.05 + kickId() * 0.1) * (2.0 + 6.0 * kickNow + 4.0 * mids);
return vec3(0.0);
}
// Direct light at a surface point: sun (through the lattice), the orb, and some sky.
vec3 light(vec3 p, vec3 n, vec3 rd, float m) {
vec3 e = emission(p, n, rd, m);
if (m > 3.5) return e;
vec3 a = albedo(p, m);
float ao = ambientOcclusion(p, n);
float sunShadow = softShadow(p + n * 0.01, SUN, 12.0, 12.0);
vec3 col = a * SUN_COLOUR * max(dot(n, SUN), 0.0) * sunShadow;
vec3 toOrb = orbPos - p;
float d = length(toOrb);
toOrb /= d;
withOrb = false;
float orbShadow = softShadow(p + n * 0.01, toOrb, d, 8.0);
withOrb = true;
vec3 orbLight = orbColour() * (4.0 + 12.0 * bass + 10.0 * kickNow) / (1.0 + d * d * 0.6);
col += a * orbLight * max(dot(n, toOrb), 0.0) * orbShadow;
col += a * vec3(0.22, 0.24, 0.32) * ao * (0.6 + 0.4 * n.y);
// Highlights (gold is metal: its highlights take its colour).
vec3 h = normalize(SUN - rd), ho = normalize(toOrb - rd);
float shine = m > 2.5 ? 120.0 : 40.0;
vec3 specTint = m > 2.5 ? a : vec3(0.3);
col += specTint * (SUN_COLOUR * pow(max(dot(n, h), 0.0), shine) * sunShadow + orbLight * pow(max(dot(n, ho), 0.0), shine) * orbShadow);
return col;
}
void main() {
vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
bass = silent() ? 0.3 + 0.3 * kick() : spectrum.x;
mids = silent() ? 0.2 : spectrum.y;
kickNow = kick();
float z = time * 1.2;
orbPos = vec3(sin(time * 0.5) * 0.5, 0.2 + 0.25 * sin(time * 0.7), z + 5.0);
// Camera: drifting down the nave, a little shake on the kick.
vec3 ro = vec3(sin(time * 0.21) * 0.7, 0.05 + 0.35 * sin(time * 0.13), z);
ro += (vec3(hash(floor(time * 30.0)), hash(floor(time * 30.0) + 1.0), 0.0) - 0.5) * 0.02 * kickNow;
vec3 f = normalize(orbPos + vec3(0.0, 0.2, 0.0) - ro), r = normalize(cross(vec3(0.0, 1.0, 0.0), f)), u = cross(f, r);
vec3 rd = normalize(uv.x * r + uv.y * u + 1.25 * f);
vec2 hit = march(ro, rd, 40.0, 200);
vec3 col = vec3(0.0);
float t = hit.x;
if (hit.y > 0.5) {
vec3 p = ro + rd * t;
vec3 n = normal(p);
col = light(p, n, rd, hit.y);
// Reflections: the floor is polished stone, the arches gold. The reflected point is lit (and shadowed) too.
if (hit.y > 1.5 && hit.y < 3.5) {
float fres = hit.y > 2.5 ? 1.0 : 0.04 + 0.96 * pow(1.0 - max(dot(n, -rd), 0.0), 5.0);
vec3 tint = hit.y > 2.5 ? albedo(p, 3.0) : vec3(1.0);
// Rough enough to blur slightly: jitter the normal a touch per pixel.
vec3 rn = normalize(n + (vec3(hash(gl_FragCoord.xy + time), hash(gl_FragCoord.yx - time), hash(gl_FragCoord.xy * 1.3)) - 0.5) * 0.03);
vec3 rr = reflect(rd, rn);
vec2 rh = march(p + n * 0.02, rr, 25.0, 110);
vec3 rc = vec3(0.02, 0.025, 0.04);
if (rh.y > 0.5) {
vec3 rp = p + n * 0.02 + rr * rh.x;
rc = light(rp, normal(rp), rr, rh.y) * exp(-rh.x * 0.08);
}
col = mix(col, rc * tint, hit.y > 2.5 ? 0.7 : 0.25 + 0.6 * fres);
}
}
// Light in the air: march the view ray through dusty fog, testing the sun at every step (shafts through the
// lattice and the vault), plus the orb's glow. Jittered per pixel so the steps don't show as bands.
float fogEnd = min(t, 30.0);
int steps = 56;
float dt = fogEnd / float(steps);
float s = dt * hash(gl_FragCoord.xy + fract(time * 7.0) * 100.0);
float density = 0.03 * fog * (1.0 + 0.5 * bass);
float g = 0.6, cosT = dot(rd, SUN);
float phase = (1.0 - g * g) / (4.0 * 3.14159 * pow(1.0 + g * g - 2.0 * g * cosT, 1.5)); // Henyey-Greenstein: bright looking into the sun
vec3 scatter = vec3(0.0);
float transmit = 1.0;
for (int i = 0; i < 56; i++) {
vec3 q = ro + rd * s;
float dust = density * (0.6 + 0.8 * noise3(q * 0.7 + vec3(0.0, -time * 0.2, time * 0.1)));
float sun = hardShadow(q, SUN, 8.0);
float od = length(orbPos - q);
vec3 inLight = SUN_COLOUR * sun * phase * 14.0 + orbColour() * (0.3 + 1.5 * bass + 1.5 * kickNow) / (1.0 + od * od * 4.0);
scatter += transmit * dust * dt * inLight;
transmit *= exp(-dust * dt);
s += dt;
}
col = col * transmit + scatter;
// Depth in alpha (scaled into 0..1) for the lens pass's depth of field.
fragColor = vec4(col, min(t, 40.0) / 40.0);
}Bloom: fragment shader
#version 300 es
precision highp float;
uniform sampler2D pass0;
uniform vec2 resolution;
in vec2 v_texcoord;
out vec4 fragColor;
// Glow: only what's brighter than white, gathered on a golden-angle spiral at two sizes (a tight core, a wide haze).
void main() {
vec2 st = gl_FragCoord.xy / resolution;
vec2 aspect = vec2(resolution.y / resolution.x, 1.0);
vec3 tight = vec3(0.0), wide = vec3(0.0);
for (int i = 0; i < 64; i++) {
float fi = float(i) + 0.5;
vec2 dir = vec2(cos(fi * 2.39996), sin(fi * 2.39996)) * sqrt(fi / 64.0) * aspect;
tight += max(texture(pass0, st + dir * 0.03).rgb - 1.0, 0.0);
wide += max(texture(pass0, st + dir * 0.12).rgb - 1.0, 0.0);
}
fragColor = vec4((tight * 0.6 + wide * 0.4) / 64.0, 1.0);
}Lens: fragment shader
#version 300 es
precision highp float;
uniform sampler2D pass0;
uniform sampler2D pass1;
uniform vec2 resolution;
uniform float time;
uniform float exposure;
uniform float aperture;
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 aces(vec3 x) { return clamp((x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14), 0.0, 1.0); }
void main() {
vec2 st = gl_FragCoord.xy / resolution;
vec2 aspect = vec2(resolution.y / resolution.x, 1.0);
// Cinema bars.
float bars = abs(st.y - 0.5) * 2.0;
if (bars > resolution.x / resolution.y / 2.39 * 1.0 && resolution.x > resolution.y) { fragColor = vec4(0.0, 0.0, 0.0, 1.0); return; }
// Depth of field, focused on whatever's in the middle of the frame (it's usually the orb).
float focus = texture(pass0, vec2(0.5)).a;
float depth = texture(pass0, st).a;
float blur = clamp(abs(depth - focus) / max(depth, 0.01) * 0.005 * aperture, 0.0, 0.008);
// Chromatic aberration: red and blue land a little apart towards the edges.
vec2 fromCentre = st - 0.5;
float ca = 0.004 * dot(fromCentre, fromCentre) * 4.0;
vec3 col = vec3(0.0);
float total = 0.0;
for (int i = 0; i < 48; i++) {
float fi = float(i) + 0.5;
vec2 o = vec2(cos(fi * 2.39996), sin(fi * 2.39996)) * sqrt(fi / 48.0) * blur * aspect * 2.0;
// Don't let sharp foreground bleed into the blur behind it: weight by the sample's own blur.
vec4 c = texture(pass0, st + o);
float w = clamp(abs(c.a - focus) / max(c.a, 0.01) * 0.005 * aperture / max(blur, 0.0005), 0.2, 1.0);
col += vec3(texture(pass0, st + o + fromCentre * ca).r, c.g, texture(pass0, st + o - fromCentre * ca).b) * w;
total += w;
}
col /= total;
// Bloom, a horizontal streak (anamorphic lens), and ghosts mirrored through the middle (lens flare).
vec3 bloom = texture(pass1, st).rgb;
vec3 streak = vec3(0.0);
for (int i = -12; i <= 12; i++) streak += texture(pass1, st + vec2(float(i) * 0.02, 0.0)).rgb * exp(-abs(float(i)) * 0.2);
vec3 ghosts = vec3(0.0);
for (int k = 1; k <= 3; k++) ghosts += texture(pass1, 0.5 - fromCentre * (0.5 + float(k) * 0.35)).rgb * pal(float(k) * 0.3) * 0.25;
col += bloom * 0.7 + streak * vec3(0.3, 0.5, 1.0) * 0.06 + ghosts * 0.6;
col = aces(col * exposure);
col = pow(col, vec3(0.4545));
// Grade: more contrast and colour, cool shadows and warm highlights (the film look).
float luma = dot(col, vec3(0.2126, 0.7152, 0.0722));
col = mix(vec3(luma), col, 1.25);
col = mix(col, col * col * (3.0 - 2.0 * col), 0.6);
col *= mix(vec3(0.85, 1.0, 1.1), vec3(1.1, 1.0, 0.85), smoothstep(0.1, 0.7, luma));
col *= 1.0 - 0.35 * dot(fromCentre, fromCentre) * 2.5; // vignette
col += (hash(gl_FragCoord.xy + fract(time) * 713.0) - 0.5) * 0.035; // film grain
fragColor = vec4(col, 1.0);
}