Phyllotaxis

glint · Examples · Tutorials · Gallery

A sunflower head: seed k at radius √k and angle k × 137.5° (the golden angle), which packs them with no gaps. Nudge the angle a fraction and spiral arms appear and swap.

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

fragment shader

#version 300 es
precision highp float;

uniform float time;
uniform vec2 resolution;

in vec2 v_texcoord;
out vec4 fragColor;

float hash(float n) { return fract(sin(n * 127.1) * 43758.5453); }
float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
vec2 hash2(float n) { return fract(sin(vec2(n * 127.1, n * 269.5)) * 43758.5453); }
mat2 rot(float a) { float c = cos(a), s = sin(a); return mat2(c, s, -s, c); }
vec3 pal(float t) { return 0.5 + 0.5 * cos(6.28318 * (t + vec3(0.0, 0.33, 0.67))); }

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    float golden = 2.39996323 + 0.004 * sin(time * 0.15);      // radians; the true golden angle at the middle of the swing
    float c = 0.0135;
    float r = length(uv);
    // Only seeds near this radius can be close: k is about (r / c)^2.
    float k0 = floor(r * r / (c * c));
    float best = 1e9, bestK = 0.0;
    for (int i = -80; i <= 80; i++) {
        float k = k0 + float(i);
        if (k < 1.0 || k > 1100.0) continue;
        float a = k * golden;
        vec2 p = c * sqrt(k) * vec2(cos(a), sin(a));
        float d = length(uv - p);
        if (d < best) { best = d; bestK = k; }
    }
    vec3 col = vec3(0.05, 0.12, 0.05);
    // Petals round the outside.
    float pa = atan(uv.y, uv.x);
    float petal = smoothstep(0.02, 0.0, r - (0.47 + 0.08 * abs(sin(pa * 13.0))));
    col = mix(col, vec3(1.0, 0.8, 0.1) * (0.7 + 0.3 * abs(sin(pa * 13.0))), petal);
    float seed = smoothstep(c * 0.55, c * 0.4, best) * step(bestK, 1100.0) * step(r, 0.46);
    vec3 seedCol = mix(vec3(0.35, 0.2, 0.05), vec3(0.8, 0.55, 0.1), bestK / 1100.0);
    col = mix(col, seedCol, seed);
    col = mix(col, vec3(0.2, 0.1, 0.02), step(r, 0.46) * (1.0 - seed) * 0.9);
    fragColor = vec4(col, 1.0);
}

More More GLSL examples

Low poly · 10 PRINT · Invaders · Gears · Pong · Jellyfish · Fireflies · Newton fractal