Torchlit dungeon
glint · Examples · Tutorials · Gallery
A pixel dungeon room lit by four wall torches whose flames flare with the music, and a slime hopping across the flagstones on every kick.
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 vec3 spectrum;
uniform float beat;
uniform float beatTime;
uniform int beatCount;
uniform sampler2D fft;
uniform float bpm;
uniform float beatPhase;
uniform float barPhase;
in vec2 v_texcoord;
out vec4 fragColor;
#define PI 3.14159265
// 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); }
float sinceKick() { return beatCount > 0 ? beatTime : fract(time * 2.0) / 2.0; }
float kickId() { return beatCount > 0 ? float(beatCount) : floor(time * 2.0); }
float bin(float x) { return texture(fft, vec2(clamp(x, 0.0, 1.0), 0.25)).r; }
// With no sound in, gentle stand-ins so a visual is never empty: a moving spectrum, a bass that follows the pulse.
float silent() { return step(spectrum.x + spectrum.y + spectrum.z, 0.02); }
float level(float x) { return mix(bin(x), 0.3 + 0.25 * sin(time * 2.3 + x * 17.0) * sin(time * 1.1 + x * 5.0) + 0.15 * kick(), silent()); }
float bass() { return mix(spectrum.x, 0.15 + 0.25 * kick(), silent()); }
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(vec2 p) { return fract(sin(vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3)))) * 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))); }
float noise(vec2 p) {
vec2 i = floor(p), f = fract(p);
f = f * f * (3.0 - 2.0 * f);
return mix(mix(hash(i), hash(i + vec2(1.0, 0.0)), f.x), mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), f.x), f.y);
}
float fbm(vec2 p) {
float v = 0.0, a = 0.5;
for (int i = 0; i < 5; i++) { v += a * noise(p); p = rot(0.5) * p * 2.02; a *= 0.5; }
return v;
}
float PS() { return max(1.0, floor(resolution.y / 120.0)); }
vec2 vpix() { return floor(gl_FragCoord.xy / PS()); }
vec2 vsize() { return floor(resolution / PS()); }
float scan() { return 0.82 + 0.18 * step(0.5, fract(gl_FragCoord.y / PS())); }
float beatDur() { return beatCount > 0 && bpm > 1.0 ? 60.0 / bpm : 0.5; }
void main() {
float Z = max(1.0, floor(vsize().y / 120.0));
vec2 P = floor(vpix() / Z), V = floor(vsize() / Z);
float wallY = V.y - 24.0;
vec3 base;
vec2 t = floor(P / vec2(12.0, 10.0)), f = P - t * vec2(12.0, 10.0);
if (P.y >= wallY) {
float row = floor((P.y - wallY) / 5.0);
float sx = P.x + mod(row, 2.0) * 6.0;
base = vec3(0.34, 0.31, 0.38) * (0.75 + 0.35 * hash(vec2(floor(sx / 12.0), row)));
if (mod(P.y - wallY, 5.0) < 1.0 || mod(sx, 12.0) < 1.0) base = vec3(0.1, 0.09, 0.12);
} else {
base = vec3(0.4, 0.36, 0.32) * (0.7 + 0.35 * hash(t));
if (f.x < 1.0 || f.y < 1.0) base = vec3(0.13, 0.12, 0.11);
if (hash(t + 9.0) > 0.8 && abs(f.x - f.y * 1.1 - 1.0) < 0.6) base *= 0.45;
if (hash(t + 3.0) > 0.94 && length(f - vec2(6.0, 5.0)) < 1.5) base = vec3(1.0, 0.8, 0.2) * (0.6 + 0.6 * kick());
}
// The slime: hops on each kick, squashes when it lands.
float prog = clamp(sinceKick() / beatDur(), 0.0, 1.0);
vec2 sp = vec2(floor(V.x * 0.5 + V.x * 0.32 * sin(time * 0.37)), floor(V.y * 0.3 + V.y * 0.12 * sin(time * 0.23)));
float hop = 4.0 * prog * (1.0 - prog) * 12.0;
float squash = 0.35 * max(0.0, 1.0 - prog * 6.0);
vec2 q = (P - sp - vec2(0.0, floor(hop))) / vec2(7.0 * (1.0 + squash), 6.0 * (1.0 - squash));
bool slime = length(q) < 1.0 && q.y > -0.15;
bool shadow = length((P - sp) / vec2(7.0, 2.0)) < 1.0;
if (shadow && !slime) base *= 0.5;
float light = 0.12;
vec3 glow = vec3(0.0);
for (int i = 0; i < 4; i++) {
float fi = float(i);
vec2 tp = vec2(floor(V.x * (0.125 + 0.25 * fi)), wallY + 8.0);
float fl = 0.8 + 0.15 * sin(time * 13.0 + fi * 4.0) * sin(time * 7.3 + fi) + 0.4 * level(0.05 + 0.2 * fi) + 0.3 * kick();
vec2 d = P - tp;
light += fl / (1.0 + dot(d, d) / (900.0 * fl * fl)) * 0.75;
if (abs(d.x) <= 1.0 && d.y >= -6.0 && d.y < 0.0) glow = vec3(0.35, 0.2, 0.1);
float fh = 7.0 * fl;
if (d.y >= 0.0 && d.y < fh && abs(d.x) < 2.5 * (1.0 - d.y / fh) + 0.5 * sin(d.y + time * 20.0)) glow = mix(vec3(1.0, 0.95, 0.5), vec3(1.0, 0.35, 0.05), d.y / fh) * 1.4;
}
vec3 col = base * light * vec3(1.0, 0.75, 0.5);
if (slime) {
col = vec3(0.2, 0.9, 0.35) * (0.35 + 0.6 * light) + vec3(0.6, 1.0, 0.6) * step(0.7, q.y) * step(q.x, -0.2) * 0.6;
if (abs(q.y - 0.35) < 0.15 && abs(abs(q.x) - 0.35) < 0.12) col = vec3(0.02);
}
if (glow != vec3(0.0)) col = glow;
fragColor = vec4(col * scan(), 1.0);
}More DJ visuals examples
Starry lake · Bomb maze · Light cycles · Dive bombers · Mode 7 kart · Vector balls · Shadebobs · Voxel landscape