Physarum

glint · Examples · Tutorials · Gallery

Slime-mould simulation: agents sense a chemical trail ahead of them, turn towards it and deposit more, self-organising into living networks.

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

Physarum: 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.

Agents: fragment shader

#version 300 es
precision highp float;

uniform sampler2D feedback;
uniform sampler2D pass2;
uniform vec2 resolution;
uniform int frame;
uniform vec3 spectrum;
uniform float beat;
uniform float sensorAngle;
uniform float turnSpeed;

in vec2 v_texcoord;
out vec4 fragColor;

// Each texel is an agent: xy = position (0..1), z = heading (radians).
float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
float sense(vec2 pos, float a, float dist) { return texture(pass2, fract(pos + dist * vec2(cos(a), sin(a)))).r; }

void main() {
    vec2 id = gl_FragCoord.xy;
    vec4 s = texelFetch(feedback, ivec2(id), 0);
    if (frame < 2) {
        // Start in a ring, facing inwards.
        float a = hash(id) * 6.2831, r = 0.15 + 0.25 * hash(id + 3.0);
        s = vec4(0.5 + r * vec2(cos(a), sin(a)), a + 3.1416, 0.0);
    }
    float dist = 0.012, ang = sensorAngle;
    float F = sense(s.xy, s.z, dist), Lf = sense(s.xy, s.z + ang, dist), Rt = sense(s.xy, s.z - ang, dist);
    float turn = turnSpeed * (1.0 + beat);
    if (F < Lf && F < Rt) s.z += (hash(id + float(frame)) - 0.5) * 2.0 * turn;   // wander
    else if (Lf > Rt) s.z += turn;
    else if (Rt > Lf) s.z -= turn;
    float speed = 0.0012 * (1.0 + spectrum.x * 2.0);
    s.xy = fract(s.xy + speed * vec2(cos(s.z), sin(s.z)));
    fragColor = vec4(s.xyz, 1.0);
}

Deposit: vertex shader

#version 300 es
uniform sampler2D pass0;
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; }
    vec2 p = texelFetch(pass0, ivec2(id % size.x, id / size.x), 0).xy;
    gl_Position = vec4(p * 2.0 - 1.0, 0.0, 1.0);
    gl_PointSize = 1.0;
}

Deposit: fragment shader

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

Trail: fragment shader

#version 300 es
precision highp float;

uniform sampler2D feedback;
uniform sampler2D prevPass;
uniform vec2 resolution;
uniform float decay;

in vec2 v_texcoord;
out vec4 fragColor;

// Diffuse (3x3 blur) and decay the chemical, then add this frame's deposits.
void main() {
    vec2 px = 1.0 / resolution;
    float sum = 0.0;
    for (int y = -1; y <= 1; y++)
        for (int x = -1; x <= 1; x++) sum += texture(feedback, fract(v_texcoord + vec2(x, y) * px)).r;
    float t = sum / 9.0 * decay + texture(prevPass, v_texcoord).r;
    fragColor = vec4(min(t, 8.0), 0.0, 0.0, 1.0);
}

Colour: fragment shader

#version 300 es
precision highp float;

uniform sampler2D prevPass;
uniform float time;
uniform vec3 tint;

in vec2 v_texcoord;
out vec4 fragColor;

void main() {
    float t = texture(prevPass, v_texcoord).r;
    vec3 col = tint * (1.0 - exp(-t * 0.8));
    col += vec3(1.0, 0.95, 0.8) * smoothstep(2.0, 6.0, t) * 0.5;
    col += 0.03 * (0.5 + 0.5 * cos(6.2831 * (v_texcoord.x + time * 0.02 + vec3(0.0, 0.33, 0.67))));
    fragColor = vec4(col, 1.0);
}

More Showpieces examples

Neon city · Galaxy · Fluid · GPU particles · SmoothLife · Glitch · Quaternion Julia · Aurora