Stained glass

glint · Examples · Tutorials · Gallery

A gothic window of Voronoi cells with lead between them (the exact distance to each cell's border), lit by a slow sweep of sunlight.

Stained glass: a GLSL shader

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))); }

vec2 hash22(vec2 p) { return fract(sin(vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3)))) * 43758.5453); }
vec2 point(vec2 cell) { return 0.5 + 0.4 * sin(time * 0.3 + 6.28318 * hash22(cell)); }

// Distance to the nearest cell border, and which cell (Inigo Quilez's two-pass method).
vec3 voronoi(vec2 x) {
    vec2 n = floor(x), f = fract(x), mg = vec2(0.0), mr = vec2(0.0);
    float md = 8.0;
    for (int j = -1; j <= 1; j++) for (int i = -1; i <= 1; i++) {
        vec2 g = vec2(float(i), float(j));
        vec2 r = g + point(n + g) - f;
        float d = dot(r, r);
        if (d < md) { md = d; mr = r; mg = g; }
    }
    // Second pass: distance to each bisector between the nearest point and its neighbours.
    md = 8.0;
    for (int j = -2; j <= 2; j++) for (int i = -2; i <= 2; i++) {
        vec2 g = mg + vec2(float(i), float(j));
        vec2 r = g + point(n + g) - f;
        if (dot(mr - r, mr - r) > 0.00001) md = min(md, dot(0.5 * (mr + r), normalize(r - mr)));
    }
    return vec3(md, n + mg);
}

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    // The window: a rectangle with a pointed arch on top (two circles meeting).
    vec2 w = uv - vec2(0.0, -0.1);
    float arch = max(length(w - vec2(-0.2, 0.1)), length(w - vec2(0.2, 0.1))) - 0.62;
    float rect = max(abs(w.x) - 0.42, abs(w.y + 0.2) - 0.3);
    float window = w.y > 0.1 ? arch : rect;
    vec3 v = voronoi(uv * 7.0);
    vec3 glass = pow(pal(hash(v.yz) * 0.8 + 0.1), vec3(2.0)) * 1.3;
    // Uneven old glass: brightness varies within each pane.
    glass *= 0.75 + 0.25 * sin(dot(uv, vec2(40.0, 23.0)) + hash(v.yz) * 6.0);
    float sun = 0.5 + 0.9 * smoothstep(0.4, 0.0, abs(uv.x + uv.y * 0.5 - sin(time * 0.2) * 0.8));
    float lead = smoothstep(0.03, 0.06, v.x);
    vec3 col = glass * sun * lead;
    col = mix(col, vec3(0.02), smoothstep(-0.01, 0.0, window));
    // Stone around it, with a lead frame along the edge.
    vec3 stone = vec3(0.18, 0.16, 0.14) * (0.7 + 0.3 * hash(floor(uv * vec2(8.0, 16.0))));
    col = window > 0.03 ? stone * (0.6 + 0.4 * smoothstep(0.03, 0.1, window)) : col;
    fragColor = vec4(col, 1.0);
}

More More GLSL examples

Infinite spheres · Night sky · Dithered retro · Chrome balls · DNA helix · Clock · Desert dunes · Apollonian