GPU particles

glint · Examples · Tutorials · Gallery

About 100k particles simulated on the GPU: positions live in a float texture, a curl-noise flow field moves them, and a vertex pass draws them with glowing trails.

Audio-reactive: it moves with music from a microphone, line-in or a file.

GPU particles: an audio-reactive 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

4 passes, drawn in this order each frame, in GLSL.

Simulate: fragment shader

#version 300 es
precision highp float;

uniform sampler2D feedback;
uniform vec2 resolution;
uniform vec2 mouse;
uniform vec3 mouseButtons;
uniform float time;
uniform int frame;
uniform vec3 spectrum;
uniform float beat;
uniform float flow;

in vec2 v_texcoord;
out vec4 fragColor;

// Each texel is one particle: xy = position (-1..1), zw = velocity.
float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
float noise(vec3 p) {
    vec3 i = floor(p), f = fract(p);
    f = f * f * (3.0 - 2.0 * f);
    float n = dot(i, vec3(1.0, 57.0, 113.0));
    vec4 a = fract(sin(vec4(n, n + 1.0, n + 57.0, n + 58.0)) * 43758.5453);
    vec4 b = fract(sin(vec4(n + 113.0, n + 114.0, n + 170.0, n + 171.0)) * 43758.5453);
    vec4 m = mix(a, b, f.z);
    return mix(mix(m.x, m.y, f.x), mix(m.z, m.w, f.x), f.y);
}
// Curl of a noise potential: a swirling, divergence-free flow (particles never bunch up into sinks).
vec2 curl(vec2 p, float t) {
    vec2 e = vec2(0.01, 0.0);
    float n1 = noise(vec3(p + e.yx, t)), n2 = noise(vec3(p - e.yx, t));
    float n3 = noise(vec3(p + e.xy, t)), n4 = noise(vec3(p - e.xy, t));
    return vec2(n1 - n2, -(n3 - n4)) / (2.0 * e.x);
}

void main() {
    vec2 id = gl_FragCoord.xy;
    vec4 s = texelFetch(feedback, ivec2(id), 0);
    float r = hash(id + float(frame) * 0.013);
    if (frame < 2 || r < 0.002 || abs(s.x) > 1.2 || abs(s.y) > 1.2) {
        // (Re)spawn somewhere random with no velocity.
        s = vec4(hash(id * 1.7 + float(frame)) * 2.0 - 1.0, hash(id * 3.1 + float(frame)) * 2.0 - 1.0, 0.0, 0.0);
    }
    vec2 pos = s.xy, vel = s.zw;
    vec2 f = curl(pos * 1.6, time * 0.1) * 0.0006 * flow * (1.0 + spectrum.x * 3.0 + beat * 4.0);
    vec2 m = (mouse * 2.0 - 1.0);
    if (mouseButtons.x > 0.5) f += normalize(m - pos) * 0.0004;
    vel = vel * 0.94 + f;
    pos += vel;
    fragColor = vec4(pos, vel);
}

Draw: vertex shader

#version 300 es
uniform sampler2D pass0;
uniform vec2 resolution;
out vec3 v_color;
void main() {
    ivec2 size = textureSize(pass0, 0);
    int id = gl_VertexID;
    if (id >= size.x * size.y) { gl_Position = vec4(2.0, 2.0, 0.0, 1.0); return; }
    vec4 s = texelFetch(pass0, ivec2(id % size.x, id / size.x), 0);
    float speed = length(s.zw) * 200.0;
    gl_Position = vec4(s.x * resolution.y / resolution.x, s.y, 0.0, 1.0);
    gl_PointSize = 1.5;
    v_color = mix(vec3(0.15, 0.4, 1.0), vec3(1.0, 0.45, 0.2), clamp(speed, 0.0, 1.0)) * 0.25;
}

Draw: fragment shader

#version 300 es
precision highp float;
in vec3 v_color;
out vec4 fragColor;
void main() { fragColor = vec4(v_color, 1.0); }

Trails: fragment shader

#version 300 es
precision highp float;

uniform sampler2D feedback;
uniform sampler2D prevPass;
uniform float trail;

in vec2 v_texcoord;
out vec4 fragColor;

void main() {
    vec3 prev = texture(feedback, v_texcoord).rgb * trail;
    fragColor = vec4(prev + texture(prevPass, v_texcoord).rgb, 1.0);
}

Tone: fragment shader

#version 300 es
precision highp float;

uniform sampler2D prevPass;

in vec2 v_texcoord;
out vec4 fragColor;

void main() {
    vec3 c = texture(prevPass, v_texcoord).rgb;
    fragColor = vec4(1.0 - exp(-c * 1.2), 1.0);
}

More Showpieces examples

Domain warping · Neon city · Galaxy · Fluid · Physarum · SmoothLife · Glitch · Quaternion Julia