Mirror sphere (after Escher)

glint · Examples · Tutorials · Gallery

A mirror ball in a room tiled with interlocking creatures, the whole room curved round its surface, your own viewpoint a smudge at its centre: Escher's self-portrait in a sphere, ray-traced and engraved.

Mirror sphere (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 tiles;

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

// A mirror ball in a room whose walls are tiled with interlocking creatures; the ball shows the whole room bent
// round it, as in Escher's self-portrait in a sphere. Everything ray-traced exactly (a sphere, a box).
const vec3 ROOM = vec3(4.0, 2.2, 4.0);
const vec3 BALL = vec3(0.0, -1.2, 0.0);
const float BR = 1.0;
const vec3 LAMP = vec3(-2.5, 1.8, -1.5);

float hitSphere(vec3 ro, vec3 rd) {
    vec3 oc = ro - BALL;
    float b = dot(oc, rd), c = dot(oc, oc) - BR * BR, h = b * b - c;
    return h < 0.0 ? -1.0 : -b - sqrt(h);
}

// From inside the room: where the ray meets a wall (and that wall's normal).
float hitRoom(vec3 ro, vec3 rd, out vec3 n) {
    vec3 t = (sign(rd) * ROOM - ro) / rd;
    float tm = min(min(t.x, t.y), t.z);
    n = -sign(rd) * step(t.xyz, t.yzx) * step(t.xyz, t.zxy);
    return tm;
}

// The wall pattern: warped squares (lizards, nearly), light and dark.
float pattern(vec2 p) {
    p *= tiles;
    vec2 q = p;
    for (int i = 0; i < 5; i++) q = p - 0.08 * vec2(sin(6.2832 * q.y) + 0.4 * sin(12.566 * q.y + 1.0), sin(6.2832 * q.x) + 0.4 * sin(12.566 * q.x + 2.0));
    vec2 f = fract(q);
    float parity = mod(floor(q.x) + floor(q.y), 2.0);
    float edge = min(min(f.x, 1.0 - f.x), min(f.y, 1.0 - f.y));
    float eye = length(f - vec2(0.7, 0.66));
    float v = parity > 0.5 ? 0.95 : 0.55;
    v = mix(v, 0.05, smoothstep(0.03, 0.01, edge));
    v = mix(v, 1.0 - v, step(eye, 0.06) * step(0.03, eye));
    return v;
}

// Light at a point on a wall or the floor: the lamp, the ball's shadow, and the pattern (the floor is a chequer).
float roomLum(vec3 p, vec3 n) {
    vec2 w = abs(n.y) > 0.5 ? p.xz : abs(n.x) > 0.5 ? p.zy : p.xy;
    float base = n.y > 0.5 ? (mod(floor(p.x * 1.2) + floor(p.z * 1.2), 2.0) > 0.5 ? 0.85 : 0.45) : pattern(w * 0.5);
    if (n.y < -0.5) base = 0.7;
    vec3 toL = LAMP - p;
    float dl = length(toL);
    toL /= dl;
    float lit = max(dot(n, toL), 0.0) * 2.5 / (1.0 + dl * dl * 0.08);
    float shadow = hitSphere(p + n * 0.01, toL) > 0.0 ? 0.25 : 1.0;
    return base * (0.5 + lit * shadow * 0.8);
}

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    vec2 st = gl_FragCoord.xy / resolution;
    float a = time * 0.07;
    vec3 ro = vec3(sin(a) * 2.8, -0.2 + 0.4 * sin(time * 0.05), cos(a) * 2.8);
    vec3 f = normalize(BALL + vec3(0.0, 0.3, 0.0) - 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.3 * f);
    float lum;
    float ang = 0.9;
    float ts = hitSphere(ro, rd);
    if (ts > 0.0) {
        // In the ball: the reflected room (and the camera's own position, a dark smudge).
        vec3 p = ro + rd * ts;
        vec3 n = normalize(p - BALL);
        vec3 rr = reflect(rd, n);
        vec3 wn;
        float tw = hitRoom(p, rr, wn);
        lum = roomLum(p + rr * tw, wn) * 0.9;
        float self = length(cross(normalize(ro - p), rr));
        lum = mix(lum, 0.05, smoothstep(0.08, 0.02, self));
        // Highlight from the lamp.
        lum += pow(max(dot(rr, normalize(LAMP - p)), 0.0), 200.0) * 2.0;
        lum *= 0.85 + 0.15 * pow(1.0 - abs(dot(n, rd)), 2.0);
        ang = 0.9;
    } else {
        vec3 wn;
        float tw = hitRoom(ro, rd, wn);
        lum = roomLum(ro + rd * tw, wn);
        ang = abs(wn.y) > 0.5 ? 0.3 : abs(wn.x) > 0.5 ? 1.2 : 2.0;
    }
    vec3 col = ink(engrave(lum, ang), st);
    // The ball's outline.
    vec3 oc = ro - BALL;
    float b = dot(oc, rd), c = dot(oc, oc) - BR * BR;
    float miss = (b * b - c) / (BR * BR);
    col = mix(col, INK, smoothstep(0.012, 0.0, abs(miss)) * 0.9);
    fragColor = vec4(col, 1.0);
}

More Art & illusions examples

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