Reaction–diffusion

glint · Examples · Tutorials · Gallery

Gray-Scott chemistry in two ping-ponging simulation passes plus a colouring pass. Click to paint, Alt+R to reseed.

Reaction–diffusion: 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

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

Sim A: fragment shader

#version 300 es
precision highp float;

uniform sampler2D pass1;
uniform sampler2D noise;
uniform vec2 resolution;
uniform vec2 mouse;
uniform vec3 mouseButtons;
uniform int frame;
uniform float feed;
uniform float kill;

in vec2 v_texcoord;
out vec4 fragColor;

vec2 at(ivec2 p) {
    ivec2 s = textureSize(pass1, 0);
    return texelFetch(pass1, (p + s) % s, 0).rg;
}

void main() {
    ivec2 p = ivec2(gl_FragCoord.xy);
    // Seed: all A, with a scatter of B blobs. Rewind (Alt+R) to reseed.
    if (frame < 2) {
        // Sparse 12-pixel squares of B (one noise texel per square), plus one in the middle.
        float n = texelFetch(noise, ivec2(gl_FragCoord.xy / 12.0) % 256, 0).r;
        vec2 c = gl_FragCoord.xy / resolution - 0.5;
        float b = step(0.96, n) + step(length(c), 0.04);
        fragColor = vec4(1.0, min(b, 1.0), 0.0, 1.0);
        return;
    }
    vec2 c = at(p);
    vec2 lap = -c
        + 0.2 * (at(p + ivec2(1, 0)) + at(p - ivec2(1, 0)) + at(p + ivec2(0, 1)) + at(p - ivec2(0, 1)))
        + 0.05 * (at(p + ivec2(1, 1)) + at(p - ivec2(1, 1)) + at(p + ivec2(1, -1)) + at(p - ivec2(1, -1)));
    float a = c.x, b = c.y, abb = a * b * b;
    a += 1.0 * lap.x - abb + feed * (1.0 - a);
    b += 0.5 * lap.y + abb - (kill + feed) * b;
    // Paint with the mouse (click and drag on the output; hide the code with Ctrl+. first).
    if (mouseButtons.x > 0.5 && length(gl_FragCoord.xy - mouse * resolution) < 6.0) b = 1.0;
    fragColor = vec4(clamp(vec2(a, b), 0.0, 1.0), 0.0, 1.0);
}

Sim B: fragment shader

#version 300 es
precision highp float;

uniform sampler2D prevPass;
uniform sampler2D noise;
uniform vec2 resolution;
uniform vec2 mouse;
uniform vec3 mouseButtons;
uniform int frame;
uniform float feed;
uniform float kill;

in vec2 v_texcoord;
out vec4 fragColor;

vec2 at(ivec2 p) {
    ivec2 s = textureSize(prevPass, 0);
    return texelFetch(prevPass, (p + s) % s, 0).rg;
}

void main() {
    ivec2 p = ivec2(gl_FragCoord.xy);
    // Seed: all A, with a scatter of B blobs. Rewind (Alt+R) to reseed.
    if (frame < 2) {
        // Sparse 12-pixel squares of B (one noise texel per square), plus one in the middle.
        float n = texelFetch(noise, ivec2(gl_FragCoord.xy / 12.0) % 256, 0).r;
        vec2 c = gl_FragCoord.xy / resolution - 0.5;
        float b = step(0.96, n) + step(length(c), 0.04);
        fragColor = vec4(1.0, min(b, 1.0), 0.0, 1.0);
        return;
    }
    vec2 c = at(p);
    vec2 lap = -c
        + 0.2 * (at(p + ivec2(1, 0)) + at(p - ivec2(1, 0)) + at(p + ivec2(0, 1)) + at(p - ivec2(0, 1)))
        + 0.05 * (at(p + ivec2(1, 1)) + at(p - ivec2(1, 1)) + at(p + ivec2(1, -1)) + at(p - ivec2(1, -1)));
    float a = c.x, b = c.y, abb = a * b * b;
    a += 1.0 * lap.x - abb + feed * (1.0 - a);
    b += 0.5 * lap.y + abb - (kill + feed) * b;
    // Paint with the mouse (click and drag on the output; hide the code with Ctrl+. first).
    if (mouseButtons.x > 0.5 && length(gl_FragCoord.xy - mouse * resolution) < 6.0) b = 1.0;
    fragColor = vec4(clamp(vec2(a, b), 0.0, 1.0), 0.0, 1.0);
}

Colour: fragment shader

#version 300 es
precision highp float;

uniform sampler2D prevPass;
uniform vec2 resolution;
uniform vec3 colorA;
uniform vec3 colorB;

in vec2 v_texcoord;
out vec4 fragColor;

void main() {
    vec2 s = texture(prevPass, v_texcoord).rg;
    float v = smoothstep(0.1, 0.35, s.y);
    // Cheap relief from the gradient of B.
    vec2 e = 1.0 / resolution;
    float dx = texture(prevPass, v_texcoord + vec2(e.x, 0)).g - texture(prevPass, v_texcoord - vec2(e.x, 0)).g;
    float dy = texture(prevPass, v_texcoord + vec2(0, e.y)).g - texture(prevPass, v_texcoord - vec2(0, e.y)).g;
    vec3 col = mix(colorA, colorB, v) * (0.8 + 4.0 * (dx - dy));
    fragColor = vec4(col, 1.0);
}

More Starters examples

Shadertoy style · Plasma · Tunnel · Mandelbrot & Julia · Game of Life · Instanced cubes · Wireframe waves · Bloom