Fluid
glint · Examples · Tutorials · Gallery
A real-time fluid: semi-Lagrangian advection and a pressure solve that keeps it swirling, with orbiting emitters that pulse on the beat. Click to push the fluid.
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
3 passes, drawn in this order each frame, in GLSL.
Velocity: 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 force;
in vec2 v_texcoord;
out vec4 fragColor;
// State: xy = velocity (pixels per frame), z = pressure.
vec4 S(vec2 p) { return texture(feedback, p / resolution); }
vec2 emitter(float i, float t) { return resolution * (0.5 + 0.3 * vec2(cos(t * (0.3 + 0.1 * i) + i * 2.1), sin(t * (0.4 + 0.07 * i) + i * 1.3))); }
void main() {
vec2 p = gl_FragCoord.xy;
if (frame < 2) { fragColor = vec4(0.0); return; }
vec4 c = S(p);
// Advect: fetch the state from where this fluid came from.
vec4 a = S(p - c.xy);
vec4 L = S(p - vec2(1, 0)), R = S(p + vec2(1, 0)), B = S(p - vec2(0, 1)), T = S(p + vec2(0, 1));
float div = 0.5 * ((R.x - L.x) + (T.y - B.y));
// One Jacobi step per frame for the pressure, then remove its gradient: the flow stays (nearly) divergence-free.
float pr = (L.z + R.z + B.z + T.z - div) * 0.25;
vec2 v = a.xy - 0.5 * vec2(R.z - L.z, T.z - B.z);
// Emitters: small jets circling the screen, stronger with the bass and on each kick.
for (int i = 0; i < 3; i++) {
float fi = float(i);
vec2 e = emitter(fi, time);
vec2 dir = normalize(emitter(fi, time + 0.1) - e);
float k = exp(-dot(p - e, p - e) / (resolution.y * resolution.y * 0.0006));
v += dir * k * force * (0.4 + spectrum.x * 2.0 + beat * 3.0);
}
// Mouse: push outwards from the pointer while held.
vec2 m = mouse * resolution;
if (mouseButtons.x > 0.5) v += normalize(p - m + 1e-4) * 3.0 * exp(-dot(p - m, p - m) / (resolution.y * resolution.y * 0.002));
v *= 0.998;
if (p.x < 2.0 || p.y < 2.0 || p.x > resolution.x - 2.0 || p.y > resolution.y - 2.0) v = vec2(0.0);
fragColor = vec4(clamp(v, -8.0, 8.0), pr, 1.0);
}Dye: fragment shader
#version 300 es
precision highp float;
uniform sampler2D feedback;
uniform sampler2D pass0;
uniform vec2 resolution;
uniform float time;
uniform int frame;
uniform vec3 spectrum;
uniform float beat;
in vec2 v_texcoord;
out vec4 fragColor;
vec2 emitter(float i, float t) { return resolution * (0.5 + 0.3 * vec2(cos(t * (0.3 + 0.1 * i) + i * 2.1), sin(t * (0.4 + 0.07 * i) + i * 1.3))); }
void main() {
vec2 p = gl_FragCoord.xy;
if (frame < 2) { fragColor = vec4(0.0, 0.0, 0.0, 1.0); return; }
vec2 v = texture(pass0, p / resolution).xy;
vec3 dye = texture(feedback, (p - v) / resolution).rgb * 0.994;
for (int i = 0; i < 3; i++) {
float fi = float(i);
vec2 e = emitter(fi, time);
float k = exp(-dot(p - e, p - e) / (resolution.y * resolution.y * 0.0004));
vec3 c = 0.5 + 0.5 * cos(6.2831 * (fi / 3.0 + time * 0.05 + vec3(0.0, 0.33, 0.67)));
dye += c * k * (0.18 + 0.4 * beat + spectrum.y * 0.3);
}
fragColor = vec4(min(dye, vec3(4.0)), 1.0);
}Display: fragment shader
#version 300 es
precision highp float;
uniform sampler2D prevPass;
uniform sampler2D pass0;
uniform vec2 resolution;
in vec2 v_texcoord;
out vec4 fragColor;
void main() {
vec3 dye = texture(prevPass, v_texcoord).rgb;
// Shade by velocity: a little highlight where the flow is fast.
float speed = length(texture(pass0, v_texcoord).xy);
vec3 col = 1.0 - exp(-dye * 1.5);
col += vec3(0.08, 0.1, 0.15) * smoothstep(0.0, 4.0, speed);
fragColor = vec4(col, 1.0);
}More Showpieces examples
Voronoi crystals · Domain warping · Neon city · Galaxy · GPU particles · Physarum · SmoothLife · Glitch