Metamorphosis (after Escher)

glint · Examples · Tutorials · Gallery

Squares drifting across the paper slowly bend into interlocking creatures (eyes, mouths, scales), still fitting together perfectly: the plane is warped by something with the tiles' own period, which is how Escher designed his.

Metamorphosis (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 speed;

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

// Squares that bend into interlocking creatures as they travel: warping the plane by something with the grid's own
// period keeps every tile the same shape and still fitting its neighbours (which is how Escher drew his).
vec2 warp(vec2 p, float a) {
    return p + a * vec2(sin(6.2832 * p.y) + 0.45 * sin(12.566 * p.y + 1.3) + 0.25 * sin(6.2832 * (p.x + p.y)),
                         sin(6.2832 * p.x) + 0.45 * sin(12.566 * p.x + 2.1) - 0.25 * sin(6.2832 * (p.x - p.y)));
}

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    vec2 st = gl_FragCoord.xy / resolution;
    // Across the picture: plain squares on the left, creatures on the right.
    float a = smoothstep(-0.85, 0.75, uv.x) * 0.07;
    vec2 p = uv * 5.0 + vec2(-time * speed * 0.35, 0.0);
    // Invert the warp (a few fixed-point steps): which tile is this point in?
    vec2 q = p;
    for (int i = 0; i < 6; i++) q = p - (warp(q, a) - q);
    vec2 cell = floor(q), f = fract(q);
    float parity = mod(cell.x + cell.y, 2.0);
    // Light creatures and dark ones, like day and night.
    float lum = parity > 0.5 ? 0.9 : 0.22;
    vec3 col = ink(engrave(lum, parity > 0.5 ? 0.6 : 2.2), st);
    // The outlines, sharp at any size.
    vec2 e = min(f, 1.0 - f);
    float edge = min(e.x, e.y) / max(fwidth(q.x), 1e-4);
    col = mix(col, INK, smoothstep(2.5, 1.0, edge));
    // As the tiles turn into creatures they get eyes and a mouth line, then wing or scale strokes.
    float alive = smoothstep(0.02, 0.06, a);
    vec2 eye = f - vec2(0.72, 0.68);
    float eyeD = length(eye) / max(fwidth(q.x), 1e-4);
    col = mix(col, parity > 0.5 ? INK : PAPER, alive * smoothstep(9.0, 7.0, eyeD));
    col = mix(col, parity > 0.5 ? PAPER : INK, alive * smoothstep(3.5, 2.0, eyeD));
    float mouth = abs(f.y - 0.45 - 0.08 * sin(f.x * 6.0)) / max(fwidth(q.y), 1e-4);
    col = mix(col, parity > 0.5 ? INK : PAPER, alive * smoothstep(1.6, 0.6, mouth) * step(0.55, f.x));
    float scales = abs(fract((f.x + f.y * 0.5) * 5.0) - 0.5) / max(fwidth(q.x) * 5.0, 1e-4);
    col = mix(col, parity > 0.5 ? INK : PAPER, smoothstep(0.06, 0.07, a) * smoothstep(1.5, 0.5, scales) * step(f.x, 0.5) * 0.7);
    fragColor = vec4(col, 1.0);
}

More Art & illusions examples

Drifting honeycomb · Waterfall (after Escher) · Möbius ants (after Escher) · Mirror sphere (after Escher) · Relativity (after Escher) · Ink vortex