Fireflies

glint · Examples · Tutorials · Gallery

A summer night: trees and hills as silhouettes, a moon, and forty fireflies drifting on noise paths, each blinking on its own rhythm.

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

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 = p * 2.03 + 17.0; a *= 0.5; }
    return v;
}

void main() {
    vec2 st = gl_FragCoord.xy / resolution;
    float aspect = resolution.x / resolution.y;
    vec2 p = vec2(st.x * aspect, st.y);
    vec3 col = mix(vec3(0.08, 0.1, 0.2), vec3(0.01, 0.02, 0.06), st.y);
    vec2 moon = vec2(aspect * 0.8, 0.8);
    col += vec3(0.9, 0.9, 0.8) * (smoothstep(0.052, 0.05, length(p - moon)) + 0.05 / (length(p - moon) * 8.0 + 0.3) * 0.3);
    col += step(0.997, hash(floor(gl_FragCoord.xy))) * 0.4 * step(0.5, st.y);
    // Hills and trees.
    float hill = 0.22 + 0.06 * noise(vec2(p.x * 2.0, 0.0));
    float treeLine = hill + 0.18 * pow(noise(vec2(p.x * 12.0, 1.0)), 2.0) + 0.1 * step(0.7, noise(vec2(p.x * 5.0, 3.0))) * noise(vec2(p.x * 30.0, 5.0));
    if (p.y < treeLine) col = vec3(0.01, 0.015, 0.02);
    for (int i = 0; i < 40; i++) {
        float fi = float(i);
        vec2 pos = vec2(noise(vec2(fi * 3.1, time * 0.15)) * aspect * 1.2 - 0.1, noise(vec2(fi * 7.3, time * 0.12 + 5.0)) * 0.55 + 0.05);
        float blink = smoothstep(0.1, 1.0, sin(time * (1.0 + hash(fi)) + fi * 2.7));
        float d = length(p - pos);
        col += vec3(0.8, 1.0, 0.3) * blink * (0.00012 / (d * d + 0.0001) + smoothstep(0.004, 0.0, d));
    }
    fragColor = vec4(col, 1.0);
}

More More GLSL examples

Gears · Phyllotaxis · Pong · Jellyfish · Newton fractal · Burning ship · Perfect loop · Piano overlay