Drifting honeycomb

glint · Examples · Tutorials · Gallery

A peripheral drift illusion: a honeycomb bending into a channel, with a ball in front. Every cell has a black edge on one side and a white one on the other, and that order makes the still picture seem to slide, more so out of the corner of your eye. Stress makes it move for real.

Drifting honeycomb: 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;
uniform float stress;
uniform float cells;
uniform float depth;

in vec2 v_texcoord;
out vec4 fragColor;

// The cell nearest p (pointy-topped hexagons a unit apart): p relative to its centre.
vec2 hexLocal(vec2 p) {
    const vec2 s = vec2(1.0, 1.7320508);
    vec4 c = floor(vec4(p, p - vec2(0.5, 0.8660254)) / s.xyxy) + 0.5;
    vec4 h = vec4(p - c.xy * s, p - (c.zw + 0.5) * s);
    return dot(h.xy, h.xy) < dot(h.zw, h.zw) ? h.xy : h.zw;
}

// Distance to a hexagon's edge, 0.5 = the cell's own border.
float hexDist(vec2 p) {
    p = abs(p);
    return max(dot(p, vec2(0.5, 0.8660254)), p.x);
}

float fill(float d, float r) {
    float w = fwidth(d) * 0.75 + 1e-4;
    return smoothstep(r + w, r - w, d);
}

// The illusion's whole trick: gold mortar, then a white hexagon nudged one way, a black one nudged the other,
// and the coloured cell on top. The thin crescents left showing are always white-then-black in the same direction.
vec3 honeycomb(vec2 p, vec2 dir, vec3 cell) {
    vec2 q = hexLocal(p);
    vec3 col = vec3(0.72, 0.62, 0.29);
    col = mix(col, vec3(0.98, 0.97, 0.94), fill(hexDist(q + dir * 0.075), 0.37));
    col = mix(col, vec3(0.07, 0.03, 0.09), fill(hexDist(q - dir * 0.075), 0.37));
    col = mix(col, cell, fill(hexDist(q), 0.33));
    return col;
}

// The wall: flat, falling into a channel that narrows towards the middle (an hourglass seen from the front).
const float CX = 0.14;
float width(float y) { return 0.12 + 0.55 * y * y; }
float slopeX(float x, float y) {
    float w = width(y), t = (x - CX) / w;
    return depth * 2.0 * t * exp(-t * t) / w;
}

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    float drift = stress * time;
    vec3 L = normalize(vec3(-0.5, 0.6, 1.0));

    // The ball, seen straight on: its pattern is laid out by angle, so cells shrink towards the rim.
    vec2 bc = vec2(-0.27, 0.02);
    float R = 0.2;
    vec2 b = (uv - bc) / R;
    float r2 = dot(b, b);

    // Arc length across the wall (the honeycomb is painted on the surface, so the channel's steep walls squash it).
    float y = uv.y;
    float s = 0.0;
    const int N = 48;
    float x0 = -1.2, dx = (uv.x - x0) / float(N);
    for (int i = 0; i < N; i++) {
        float hx = slopeX(x0 + (float(i) + 0.5) * dx, y);
        s += sqrt(1.0 + hx * hx) * dx;
    }
    float w = width(y), t = (uv.x - CX) / w;
    float e = exp(-t * t);
    float hx = slopeX(uv.x, y);
    float hy = -depth * e * 2.0 * t * t * (1.1 * y) / w;
    vec3 n = normalize(vec3(-hx, -hy, 1.0));
    vec2 wp = vec2(s - 1.2 + drift * 0.3, y * (1.0 + 0.6 * depth * e)) * cells;
    vec3 col = honeycomb(wp, normalize(vec2(-1.0, 1.0)), vec3(0.44, 0.19, 0.64));
    col *= 0.6 + 0.4 * max(dot(n, L), 0.0);
    col *= 1.0 - 0.35 * depth * e;                                   // the channel's floor is in shade
    // The ball's soft shadow on the wall, down and to the right.
    float sh = length((uv - bc - vec2(0.035, -0.04)) / R);
    col *= 1.0 - 0.45 * smoothstep(1.25, 0.85, sh) * step(1.0, r2);

    if (r2 < 1.0) {
        vec3 bn = vec3(b, sqrt(1.0 - r2));
        float lon = atan(bn.x, bn.z) + drift * 0.5;
        float lat = asin(clamp(bn.y, -1.0, 1.0));
        vec2 bp = vec2(lon, lat) * R * cells * 1.25;
        vec3 ball = honeycomb(bp, normalize(vec2(1.0, -1.0)), vec3(0.52, 0.13, 0.22));
        ball *= 0.35 + 0.65 * max(dot(bn, L), 0.0) + 0.25 * pow(max(dot(reflect(-L, bn), vec3(0, 0, 1)), 0.0), 24.0);
        col = mix(col, ball, smoothstep(1.0, 1.0 - 4.0 * fwidth(r2), r2));
    }
    fragColor = vec4(col, 1.0);
}

More Art & illusions examples

Waterfall (after Escher) · Metamorphosis (after Escher) · Möbius ants (after Escher) · Mirror sphere (after Escher)