Lorenz attractor

glint · Examples · Tutorials · Gallery

The butterfly of chaos theory: two hundred particles, each following the Lorenz equations from its own start (integrated in the vertex shader) and drawn with a trail, all settling onto the same two wings.

Lorenz attractor: 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

One pass, in GLSL.

vertex shader

#version 300 es
uniform mat4 mvp;
uniform float time;

out vec3 v_color;

float hash(float n) { return fract(sin(n * 127.1) * 43758.5453); }

void main() {
    // 200 particles, each drawn as a trail of 60 points a few steps apart.
    float particle = floor(float(gl_VertexID) / 60.0), k = mod(float(gl_VertexID), 60.0);
    vec3 p = vec3(hash(particle), hash(particle + 1.0), hash(particle + 2.0)) * 10.0 - 5.0 + vec3(0.0, 0.0, 25.0);
    // The first 300 steps fall onto the attractor; then the trail's head moves on with time (looping every 5 s).
    int n = 300 + int(mod(time * 40.0 + hash(particle + 3.0) * 200.0, 200.0)) + int(k) * 3;
    float dt = 0.008, speed = 0.0;
    for (int i = 0; i < 700; i++) {
        if (i >= n) break;
        vec3 d = vec3(10.0 * (p.y - p.x), p.x * (28.0 - p.z) - p.y, p.x * p.y - 8.0 / 3.0 * p.z);
        p += d * dt;
        speed = length(d);
    }
    vec3 q = (p - vec3(0.0, 0.0, 25.0)) * 0.035;
    gl_Position = mvp * vec4(q.x, q.z, q.y, 1.0);
    gl_PointSize = 1.5;
    v_color = mix(vec3(0.1, 0.3, 1.0), vec3(1.0, 0.4, 0.2), clamp(speed / 150.0, 0.0, 1.0)) * 0.6 * (k / 60.0);   // brightest at the head
}

fragment shader

#version 300 es
precision highp float;

in vec3 v_color;
out vec4 fragColor;

void main() { fragColor = vec4(v_color, 1.0); }

More More GLSL examples

Toon shading · Mandelbox · Hyperbolic tiling · Wave interference · Voxel world · Snowflakes · Maurer rose · Spirograph