Möbius ants (after Escher)

glint · Examples · Tutorials · Gallery

Red ants walking a latticed Möbius band: one side, one edge, so each ant goes round twice before it's back where it started. Raymarched, with shadows, engraved.

Möbius ants (after Escher): 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 ants;

in vec2 v_texcoord;
out vec4 fragColor;

const vec3 PAPER = vec3(0.93, 0.89, 0.8);
const vec3 INK = vec3(0.1, 0.09, 0.08);
float hash21(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
float lineSet(vec2 q, float ang, float w) {
    float l = abs(fract(dot(q, vec2(cos(ang), sin(ang)))) - 0.5) * 2.0;
    float aa = 0.18;
    return 1.0 - smoothstep(w - aa, w + aa, l);
}
// 0 = ink, 1 = paper. lum 0..1; ang the main stroke direction.
float engrave(float lum, float ang) {
    vec2 q = gl_FragCoord.xy / (resolution.y / 240.0) / 3.2;
    lum = clamp(lum, 0.0, 1.0);
    float ink = lineSet(q, ang, clamp(0.95 - lum * 1.15, 0.0, 1.0));
    ink = max(ink, lineSet(q, ang + 1.4, clamp(0.55 - lum * 1.3, 0.0, 1.0)));
    ink = max(ink, lineSet(q * 1.3, ang - 0.7, clamp(0.3 - lum * 1.2, 0.0, 1.0)));
    return 1.0 - ink;
}
// Paper with a little grain and a darker edge.
vec3 paper(vec2 st) {
    float g = hash21(floor(gl_FragCoord.xy)) * 0.04;
    float edge = smoothstep(0.75, 1.2, length((st - 0.5) * vec2(1.6, 1.2)));
    return PAPER * (0.98 - g) * (1.0 - edge * 0.25);
}
vec3 ink(float paperAmount, vec2 st) { return mix(INK, paper(st), paperAmount); }
mat2 rot(float a) { float c = cos(a), s = sin(a); return mat2(c, s, -s, c); }

const float R = 1.0, BW = 0.3, BT = 0.035;
const int ANTS = 9;

float sdBox2(vec2 p, vec2 b) { vec2 q = abs(p) - b; return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0); }
float sdCapsule(vec3 p, vec3 a, vec3 b, float r) { vec3 pa = p - a, ba = b - a; return length(pa - ba * clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0)) - r; }
float sdEllipsoid(vec3 p, vec3 r) { float k0 = length(p / r); float k1 = length(p / (r * r)); return k0 * (k0 - 1.0) / k1; }

// The band: a strip with a half twist, cut into a lattice.
float band(vec3 p) {
    float a = atan(p.z, p.x);
    vec2 q = vec2(length(p.xz) - R, p.y);
    q = rot(a * 0.5) * q;
    float d = sdBox2(q, vec2(BW, BT));
    float n = 28.0;
    float u = fract(a / 6.2832 * n) - 0.5;
    float v = q.x / (BW * 2.0 / 3.0);
    float hole = sdBox2(vec2(u * 6.2832 * R / n, (fract(v + 0.5) - 0.5) * BW * 2.0 / 3.0), vec2(0.06, 0.055)) - 0.015;
    return max(d, -hole) * 0.8;
}

// Where ant k is on the band (it walks along, round twice before it's back on the same side), and its frame.
void antFrame(int k, out vec3 pos, out vec3 T, out vec3 N) {
    float a = time * 0.25 + float(k) * 6.2832 * 2.0 / float(ANTS);
    vec2 local = vec2(0.0, BT + 0.035);
    vec2 w = rot(-a * 0.5) * local;
    vec3 radial = vec3(cos(a), 0.0, sin(a));
    pos = radial * (R + w.x) + vec3(0.0, w.y, 0.0);
    T = vec3(-sin(a), 0.0, cos(a));
    vec2 nn = rot(-a * 0.5) * vec2(0.0, 1.0);
    N = normalize(radial * nn.x + vec3(0.0, nn.y, 0.0));
}

float ant(vec3 p, int k) {
    vec3 c, T, N;
    antFrame(k, c, T, N);
    vec3 d = p - c;
    if (dot(d, d) > 0.04) return length(d) - 0.15;
    vec3 B = cross(T, N);
    vec3 l = vec3(dot(d, T), dot(d, N), dot(d, B));
    float s = sdEllipsoid(l - vec3(-0.06, 0.005, 0.0), vec3(0.045, 0.03, 0.032));
    s = min(s, sdEllipsoid(l, vec3(0.025, 0.017, 0.017)));
    s = min(s, length(l - vec3(0.045, 0.004, 0.0)) - 0.02);
    // Six legs stepping in turn, and two feelers.
    for (int i = 0; i < 3; i++) {
        float fi = float(i) - 1.0;
        for (int side = -1; side <= 1; side += 2) {
            float ph = time * 9.0 + fi * 2.1 + float(side) * 1.57;
            vec3 hip = vec3(fi * 0.012, 0.0, float(side) * 0.012);
            vec3 knee = vec3(fi * 0.025 + 0.008 * sin(ph), 0.02, float(side) * 0.04);
            vec3 foot = vec3(fi * 0.04 + 0.015 * sin(ph), -0.034 + 0.008 * max(cos(ph), 0.0), float(side) * 0.055);
            s = min(s, min(sdCapsule(l, hip, knee, 0.004), sdCapsule(l, knee, foot, 0.0035)));
        }
    }
    s = min(s, sdCapsule(l, vec3(0.06, 0.01, 0.006), vec3(0.09, 0.035, 0.025), 0.003));
    s = min(s, sdCapsule(l, vec3(0.06, 0.01, -0.006), vec3(0.09, 0.035, -0.025), 0.003));
    return s;
}

vec2 map(vec3 p) {
    vec2 res = vec2(band(p), 1.0);
    for (int k = 0; k < ANTS; k++) {
        if (float(k) >= ants) break;
        float a = ant(p, k);
        if (a < res.x) res = vec2(a, 2.0);
    }
    return res;
}

vec3 normal(vec3 p) {
    vec2 e = vec2(0.0008, 0.0);
    return normalize(vec3(map(p + e.xyy).x - map(p - e.xyy).x, map(p + e.yxy).x - map(p - e.yxy).x, map(p + e.yyx).x - map(p - e.yyx).x));
}

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    vec2 st = gl_FragCoord.xy / resolution;
    float ca = time * 0.08;
    vec3 ro = vec3(sin(ca) * 2.6, 1.5 + 0.3 * sin(time * 0.1), cos(ca) * 2.6);
    vec3 f = normalize(-ro), r = normalize(cross(vec3(0.0, 1.0, 0.0), f)), u = cross(f, r);
    vec3 rd = normalize(uv.x * r + uv.y * u + 1.6 * f);
    float t = 0.5;
    vec2 h = vec2(0.0);
    for (int i = 0; i < 200; i++) {
        h = map(ro + rd * t);
        if (h.x < 0.0004) break;
        t += h.x;
        if (t > 8.0) { h.y = 0.0; break; }
    }
    vec3 col = paper(st);
    if (h.y > 0.5) {
        vec3 p = ro + rd * t;
        vec3 n = normal(p);
        vec3 L = normalize(vec3(0.5, 0.9, 0.3));
        float dif = max(dot(n, L), 0.0);
        // A quick shadow, so the lattice falls on itself.
        float sh = 1.0, s = 0.01;
        for (int i = 0; i < 32; i++) {
            float d = map(p + n * 0.002 + L * s).x;
            sh = min(sh, 10.0 * d / s);
            s += clamp(d, 0.005, 0.1);
            if (sh < 0.01 || s > 2.0) break;
        }
        float lum = 0.15 + 0.85 * dif * clamp(sh, 0.0, 1.0);
        float rim = pow(1.0 - abs(dot(n, rd)), 3.0);
        col = ink(engrave(lum, 0.8 + n.y), st);
        col = mix(col, INK, smoothstep(0.6, 0.9, rim) * 0.8);
        // The ants are red, as in Escher's print.
        if (h.y > 1.5) col = mix(col, vec3(0.72, 0.12, 0.08) * (0.35 + 0.65 * lum), 0.8);
    }
    fragColor = vec4(col, 1.0);
}

More Art & illusions examples

Drifting honeycomb · Waterfall (after Escher) · Metamorphosis (after Escher) · Mirror sphere (after Escher) · Relativity (after Escher) · Ink vortex