Rain on the window
glint · Examples · Tutorials · Gallery
Raindrops running down glass in front of blurred city lights: each drop is a little lens showing a sharp, flipped view of what's behind.

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;
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))); }
// City lights behind the glass: soft discs (bokeh), blurrier with larger blur.
vec3 lights(vec2 uv, float blur) {
vec3 col = mix(vec3(0.02, 0.03, 0.06), vec3(0.08, 0.05, 0.1), uv.y + 0.5);
for (int i = 0; i < 40; i++) {
float fi = float(i);
vec2 p = (hash2(fi) - 0.5) * vec2(1.8, 0.9) + vec2(sin(time * 0.1 + fi) * 0.02, -0.1);
float r = 0.03 + 0.05 * hash(fi + 3.0);
float d = length(uv - p);
col += pal(0.05 + hash(fi + 9.0) * 0.25) * 1.1 * smoothstep(r + blur, r - blur * 0.5, d) * (0.4 + 0.6 * hash(fi + 5.0));
}
return col;
}
// One layer of drops: returns the refraction offset (xy) and how much drop is here (z).
vec3 drops(vec2 uv, float scale, float seed) {
vec2 grid = vec2(3.0, 1.0) * scale;
vec2 p = uv * grid;
float col = floor(p.x);
float speed = 0.3 + hash(col + seed) * 0.4;
p.y += time * speed + hash(col * 3.7 + seed) * 10.0;
vec2 id = floor(p), f = fract(p) - 0.5;
float h = hash(id + seed);
// The drop slides down its cell with a little wobble.
float y = -fract(time * speed * 0.5 + h) + 0.5;
vec2 at = vec2((h - 0.5) * 0.6 + sin(y * 12.0 + h * 6.0) * 0.05, y);
vec2 d = (f - at) / grid * grid.y;
d.x *= 1.3;
float drop = smoothstep(0.08, 0.05, length(d));
// A trail of little drops above it.
vec2 tf = vec2(f.x - at.x, fract(f.y * 6.0) - 0.5);
float trail = smoothstep(0.035, 0.02, length(tf / vec2(1.0, 6.0) * vec2(1.3, 1.0))) * step(at.y, f.y) * smoothstep(0.5, at.y, f.y);
float m = max(drop, trail * 0.8 * step(0.6, h)) * step(0.25, h);
return vec3(d * 2.5 * m, m);
}
void main() {
vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
vec3 a = drops(uv, 4.0, 1.0), b = drops(uv * 1.7 + 0.3, 6.0, 7.0);
vec3 dr = a.z > b.z ? a : b;
vec3 blurry = lights(uv, 0.06) * 0.8;
vec3 sharp = lights(uv - dr.xy * 3.0, 0.008);
// Through a drop: the sharp view, plus some of the glow (a drop gathers light from all round).
vec3 col = mix(blurry, sharp * 1.3 + blurry * 0.7 + 0.03, dr.z);
// A bright rim where the drop's edge catches the light.
col += vec3(0.6, 0.7, 0.8) * smoothstep(0.2, 0.6, dr.z) * smoothstep(1.0, 0.6, dr.z) * 0.25;
// The glass itself: a faint cold tint and misted edges.
col = mix(col, vec3(0.12, 0.14, 0.18), 0.15 * (1.0 - dr.z));
fragColor = vec4(pow(col, vec3(0.9)), 1.0);
}More More GLSL examples
2D distance field playground · Gyroid cave · Julia set · Mandelbrot zoom