SmoothLife

glint · Examples · Tutorials · Gallery

A continuous Game of Life (Rafler's SmoothLife): cells integrate a disc and a ring around them, and smooth birth/death rules grow gliding, splitting organisms. Click to seed.

SmoothLife: 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

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

Life: fragment shader

#version 300 es
precision highp float;

uniform sampler2D feedback;
uniform vec2 resolution;
uniform vec2 mouse;
uniform vec3 mouseButtons;
uniform int frame;

in vec2 v_texcoord;
out vec4 fragColor;

float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }

const float ri = 3.0, ra = 9.0;               // inner disc radius, outer ring radius
const float b1 = 0.278, b2 = 0.365;           // birth interval
const float d1 = 0.267, d2 = 0.445;           // survival interval
const float alphaN = 0.028, alphaM = 0.147;   // smoothness

float sigma1(float x, float a, float alpha) { return 1.0 / (1.0 + exp(-(x - a) * 4.0 / alpha)); }
float sigma2(float x, float a, float b) { return sigma1(x, a, alphaN) * (1.0 - sigma1(x, b, alphaN)); }
float sigmaM(float x, float y, float m) { float w = sigma1(m, 0.5, alphaM); return x * (1.0 - w) + y * w; }
float transition(float n, float m) { return sigma2(n, sigmaM(b1, d1, m), sigmaM(b2, d2, m)); }

void main() {
    vec2 p = gl_FragCoord.xy;
    if (frame < 2 || (mouseButtons.x > 0.5 && length(p - mouse * resolution) < 12.0)) {
        // Random blobs to start.
        float r = hash(floor(p / 12.0) + float(frame));
        fragColor = vec4(step(0.7, r), 0.0, 0.0, 1.0);
        return;
    }
    ivec2 size = textureSize(feedback, 0);
    float mSum = 0.0, mArea = 0.0, nSum = 0.0, nArea = 0.0;
    for (int y = -9; y <= 9; y++)
        for (int x = -9; x <= 9; x++) {
            float d = length(vec2(x, y));
            if (d > ra + 0.5) continue;
            float v = texelFetch(feedback, (ivec2(p) + ivec2(x, y) + size) % size, 0).r;
            // Anti-aliased membership of the disc and the ring.
            float inDisc = clamp(ri + 0.5 - d, 0.0, 1.0);
            float inRing = clamp(ra + 0.5 - d, 0.0, 1.0) * (1.0 - inDisc);
            mSum += v * inDisc;
            mArea += inDisc;
            nSum += v * inRing;
            nArea += inRing;
        }
    float m = mSum / mArea, n = nSum / nArea;
    float cur = texelFetch(feedback, ivec2(p), 0).r;
    // Smooth time step towards the rule's target.
    float next = clamp(cur + 0.25 * (2.0 * transition(n, m) - 1.0), 0.0, 1.0);
    fragColor = vec4(next, cur, 0.0, 1.0);
}

Colour: fragment shader

#version 300 es
precision highp float;

uniform sampler2D prevPass;
uniform float time;

in vec2 v_texcoord;
out vec4 fragColor;

void main() {
    vec2 s = texture(prevPass, v_texcoord).rg;
    float grow = s.r - s.g;   // growing vs shrinking
    vec3 col = mix(vec3(0.02, 0.03, 0.08), vec3(0.3, 0.9, 0.8), s.r);
    col += vec3(1.0, 0.4, 0.2) * max(grow, 0.0) * 6.0 + vec3(0.3, 0.3, 1.0) * max(-grow, 0.0) * 6.0;
    fragColor = vec4(col, 1.0);
}

More Showpieces examples

Galaxy · Fluid · GPU particles · Physarum · Glitch · Quaternion Julia · Aurora · Caustics