Relativity (after Escher)

glint · Examples · Tutorials · Gallery

Stairways for three different downs, landings and arches repeated through space: walk any flight and the others are walls and ceilings. Drawn in his crosshatched style, fading into the paper.

Relativity (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 drift;

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

// Stairways for three different downs: the same flight of stairs, landing and arch, laid out along each axis in turn,
// repeated through space. Walk any of them and the others are walls or ceilings.
float sdBox(vec3 p, vec3 b) { vec3 q = abs(p) - b; return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0); }

// One flight: eight steps rising along x (up y), 1.2 wide in z, each a solid block, a landing at the top, two
// pillars and an arch beyond.
float flight(vec3 q) {
    float run = 3.0, rise = 3.0, dx = run / 8.0, dy = rise / 8.0;
    float d = 1e9;
    for (int i = 0; i < 8; i++) {
        float fi = float(i);
        vec3 c = vec3((fi + 0.5) * dx, (fi + 1.0) * dy * 0.5 + (fi * dy - 0.45) * 0.5, 0.0);
        d = min(d, sdBox(q - c, vec3(dx * 0.5, ((fi + 1.0) * dy - (fi * dy - 0.45)) * 0.5, 0.6)));
    }
    d = min(d, sdBox(q - vec3(run + 0.75, rise - 0.12, 0.0), vec3(0.75, 0.12, 0.6)));
    d = min(d, sdBox(q - vec3(run + 1.35, rise * 0.5 - 0.6, 0.45), vec3(0.12, rise * 0.5 + 0.5, 0.12)));
    d = min(d, sdBox(q - vec3(run + 1.35, rise * 0.5 - 0.6, -0.45), vec3(0.12, rise * 0.5 + 0.5, 0.12)));
    d = min(d, max(sdBox(q - vec3(run + 1.35, -0.4, 0.0), vec3(0.12, 0.3, 0.6)), -(length((q - vec3(run + 1.35, -0.75, 0.0)).yz) - 0.42)));
    return d;
}

float cell(vec3 p) {
    float d = flight(p - vec3(-2.2, -1.5, 0.0));
    d = min(d, flight((p - vec3(0.6, -1.0, -2.0)).yzx));
    d = min(d, flight((p - vec3(-1.0, 1.8, 1.0)).zxy));
    return d;
}

float map(vec3 p) {
    vec3 q = mod(p + 6.0, 12.0) - 6.0;
    return cell(q);
}

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

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    vec2 st = gl_FragCoord.xy / resolution;
    float tt = time * drift;
    // From the open space between the repeats, looking in, drifting round.
    vec3 ro = vec3(6.0 + 1.5 * sin(tt * 0.05), 5.5 + tt * 0.08, 6.5 + 1.5 * cos(tt * 0.04));
    vec3 ta = vec3(-0.5, 0.5 + tt * 0.08, 0.0);
    vec3 fw = normalize(ta - ro), rt = normalize(cross(vec3(0.0, 1.0, 0.0), fw)), up = cross(fw, rt);
    vec2 tw = rot(0.15 * sin(tt * 0.05)) * uv;
    vec3 rd = normalize(tw.x * rt + tw.y * up + 1.6 * fw);
    float t = 0.0;
    bool hit = false;
    for (int i = 0; i < 160; i++) {
        float h = map(ro + rd * t);
        if (h < 0.001 * t) { hit = true; break; }
        t += h;
        if (t > 40.0) break;
    }
    vec3 col = paper(st);
    if (hit) {
        vec3 p = ro + rd * t;
        vec3 n = normal(p);
        // Flat tones by face direction, as in the print (light from one side), hatching only in the darkest, inked
        // contours, and everything fading into the paper with distance.
        vec3 an = abs(n);
        float lum = an.x > 0.6 ? 0.92 : an.y > 0.6 ? 0.68 : 0.4;
        float fade = exp(-max(t - 6.0, 0.0) * 0.1);
        vec3 tone = mix(PAPER, INK, (1.0 - lum) * 0.75);
        if (lum < 0.5) tone = mix(tone, ink(engrave(lum + 0.25, 0.78), st), 0.6);
        col = mix(paper(st), tone, fade);
        float e = length(normal(p + rd * 0.015) - n) + length(normal(p - rd * 0.015) - n);
        col = mix(col, INK, smoothstep(0.25, 0.6, e) * fade);
    }
    fragColor = vec4(col, 1.0);
}

More Art & illusions examples

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