Waterfall (after Escher)

glint · Examples · Tutorials · Gallery

The impossible triangle as an aqueduct: three beams that can't meet, meeting, with water running round them for ever, always downhill, and spilling off a corner onto a mill wheel. Ray-cast, then drawn like a lithograph.

Waterfall (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 flow;

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); }

// Three beams along x, then y, then z, seen exactly along (1,1,1): the last one ends where the first begins, so the
// three make a closed triangle that can't exist. Where they meet, the first beam is put in front to finish the trick.
const float L = 2.0, W = 0.32;

vec2 boxHit(vec3 ro, vec3 rd, vec3 lo, vec3 hi, out vec3 n) {
    vec3 inv = 1.0 / rd;
    vec3 t0 = (lo - ro) * inv, t1 = (hi - ro) * inv;
    vec3 tmin = min(t0, t1), tmax = max(t0, t1);
    float tn = max(max(tmin.x, tmin.y), tmin.z), tf = min(min(tmax.x, tmax.y), tmax.z);
    n = -sign(rd) * step(tmin.yzx, tmin.xyz) * step(tmin.zxy, tmin.xyz);
    return vec2(tn, tf);
}

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    vec2 st = gl_FragCoord.xy / resolution;
    vec3 d = -normalize(vec3(1.0));
    vec3 right = normalize(cross(d, vec3(0.0, 0.0, 1.0)));
    vec3 up = cross(right, d);
    vec3 centre = vec3(L, L, L) * 0.5 + vec3(0.0, 0.0, -0.75) + (-0.15 * right - 0.16 * up) * 4.6;
    vec3 ro = centre + (uv.x * right + uv.y * up) * 4.6 - d * 10.0;
    // The beams (boxes), and which way the water runs along each.
    vec3 lo[3], hi[3], dirs[3];
    lo[0] = vec3(-W, -W, -W); hi[0] = vec3(L + W, W, W); dirs[0] = vec3(1.0, 0.0, 0.0);
    lo[1] = vec3(L - W, -W, -W); hi[1] = vec3(L + W, L + W, W); dirs[1] = vec3(0.0, 1.0, 0.0);
    lo[2] = vec3(L - W, L - W, -W); hi[2] = vec3(L + W, L + W, L - W * 0.4); dirs[2] = vec3(0.0, 0.0, 1.0);
    float best = 1e9, bestT = 0.0;
    int which = -1;
    vec3 bn = vec3(0.0);
    for (int i = 0; i < 3; i++) {
        vec3 n;
        vec2 h = boxHit(ro, d, lo[i], hi[i], n);
        if (h.x < h.y && h.x > 0.0) {
            // The impossible join: near the first beam's start, the first beam wins over the third.
            float rank = h.x;
            vec3 rel = ro + d * h.x;
            vec3 perp = rel - dot(rel, d) * d;
            if (i == 0 && length(perp) < W * 1.9) rank -= 100.0;
            if (rank < best) { best = rank; bestT = h.x; which = i; bn = n; }
        }
    }
    vec3 col = paper(st);
    // A mill wheel turned by the waterfall, under the triangle's left-hand corner.
    vec3 fallTop3 = vec3(L + W, 0.0, -W);
    vec2 fallTop = vec2(dot(fallTop3 - centre, right), dot(fallTop3 - centre, up)) / 4.6;
    vec2 wc = uv - (fallTop + vec2(0.1, -0.33));
    float wr = length(wc);
    float spokes = abs(sin(atan(wc.y, wc.x) * 6.0 + time * flow * 1.5));
    float wheel = smoothstep(0.008, 0.0, abs(wr - 0.16)) + smoothstep(0.006, 0.0, abs(wr - 0.12)) + step(wr, 0.12) * smoothstep(0.08, 0.0, spokes) + smoothstep(0.022, 0.015, wr) + smoothstep(0.03, 0.0, abs(wr - 0.14)) * step(0.7, fract(atan(wc.y, wc.x) * 3.82 + time * flow * 0.24)) * 0.8;
    col = mix(col, INK, clamp(wheel, 0.0, 1.0) * 0.85);
    // The waterfall: off that corner, onto the wheel.
    float fx = uv.x - fallTop.x - 0.02;
    if (abs(fx) < 0.045 && uv.y < fallTop.y && uv.y > fallTop.y - 0.19) {
        float streaks = 0.5 + 0.5 * sin(fx * 220.0 + sin(uv.y * 30.0) * 2.0);
        float fall = fract(uv.y * 5.0 + time * flow * 2.0);
        col = mix(col, ink(engrave(0.55 + 0.4 * streaks * fall, 1.57), st), 0.9);
    }
    if (which >= 0) {
        vec3 p = ro + d * bestT;
        // Three tones of stone for the three directions a face can point.
        vec3 an = abs(bn);
        float lum = an.z > 0.5 ? 0.95 : an.x > 0.5 ? 0.66 : 0.42;
        float ang = an.z > 0.5 ? 0.5 : an.x > 0.5 ? 2.1 : 1.0;
        // Brick courses on the stone.
        vec3 q = p * 6.0;
        float brick = smoothstep(0.08, 0.0, abs(fract(q.x + q.y + q.z) - 0.5) - 0.42);
        col = ink(engrave(lum, ang), st) * (1.0 - brick * 0.25);
        // Water in a channel along the face that looks up, running round and round (and therefore downhill all the way).
        vec3 dir = dirs[which];
        bool channel = dot(bn, vec3(0.0, 0.0, 1.0)) > 0.5 || (which == 2 && dot(bn, vec3(1.0, 0.0, 0.0)) > 0.5);
        vec3 side = abs(cross(dir, bn));
        float across = dot(p - (lo[which] + hi[which]) * 0.5, side);
        if (channel && abs(across) < W * 0.62) {
            float along = dot(p, dir);
            float ripple = sin(along * 30.0 - time * flow * 8.0 + sin(across * 40.0) * 1.5);
            col = mix(col, ink(engrave(0.75 + 0.2 * ripple, 0.2), st), 0.95);
            col = mix(col, INK, smoothstep(W * 0.56, W * 0.62, abs(across)) * 0.9);
        }
        // Edges of every face inked.
        vec3 local = (p - lo[which]) / (hi[which] - lo[which]);
        vec3 e = min(local, 1.0 - local) * (hi[which] - lo[which]);
        float edge = min(min(an.x > 0.5 ? 9.0 : e.x, an.y > 0.5 ? 9.0 : e.y), an.z > 0.5 ? 9.0 : e.z);
        col = mix(col, INK, smoothstep(0.02, 0.008, edge));
    }
    fragColor = vec4(col, 1.0);
}

More Art & illusions examples

Drifting honeycomb · Metamorphosis (after Escher) · Möbius ants (after Escher) · Mirror sphere (after Escher) · Relativity (after Escher)