Hyperbolic tiling

glint · Examples · Tutorials · Gallery

Heptagons, three at every corner, which can only fit in hyperbolic space: drawn in the Poincaré disk by reflecting each point into one fundamental triangle, and slid around with a Möbius transformation.

Hyperbolic tiling: 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;

in vec2 v_texcoord;
out vec4 fragColor;

float hash(float n) { return fract(sin(n * 127.1) * 43758.5453); }
float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
vec2 hash2(float n) { return fract(sin(vec2(n * 127.1, n * 269.5)) * 43758.5453); }
mat2 rot(float a) { float c = cos(a), s = sin(a); return mat2(c, s, -s, c); }
vec3 pal(float t) { return 0.5 + 0.5 * cos(6.28318 * (t + vec3(0.0, 0.33, 0.67))); }

vec2 cmul(vec2 a, vec2 b) { return vec2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); }
vec2 cdiv(vec2 a, vec2 b) { return vec2(a.x * b.x + a.y * b.y, a.y * b.x - a.x * b.y) / dot(b, b); }

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    vec2 z = uv / 0.48;
    float px = 1.5 / (resolution.y * 0.48);
    if (length(z) > 1.0) { fragColor = vec4(vec3(0.03) + 0.1 * smoothstep(px, 0.0, length(z) - 1.0), 1.0); return; }
    // Glide through the plane: a Möbius transformation (a hyperbolic translation).
    vec2 m = 0.35 * vec2(cos(time * 0.2), sin(time * 0.27));
    z = cdiv(z + m, vec2(1.0, 0.0) + cmul(vec2(m.x, -m.y), z));
    // The {7,3} fundamental triangle: angle pi/7 at the centre, pi/3 where the third side (a circle meeting the rim
    // at right angles) crosses the second mirror line.
    float a = 3.14159 / 7.0, b = 3.14159 / 3.0;
    float r = sin(a) / sqrt(cos(b) * cos(b) - sin(a) * sin(a));
    vec2 c = vec2(sqrt(1.0 + r * r), 0.0);
    float flips = 0.0;
    for (int i = 0; i < 40; i++) {
        // Fold the angle into [0, a] (the two mirror lines through the centre).
        float ang = atan(z.y, z.x), len = length(z);
        float k = floor(ang / (2.0 * a));
        ang -= k * 2.0 * a;
        if (ang > a) { ang = 2.0 * a - ang; flips += 1.0; }
        z = len * vec2(cos(ang), sin(ang));
        // Inside the third mirror? Invert in it.
        vec2 w = z - c;
        float l2 = dot(w, w);
        if (l2 >= r * r) break;
        z = c + w * (r * r / l2);
        flips += 1.0;
    }
    float edge = abs(length(z - c) - r);
    vec3 col = mix(vec3(0.9, 0.85, 0.75), vec3(0.2, 0.35, 0.55), mod(flips, 2.0));
    col = mix(col, vec3(0.05), smoothstep(0.02, 0.0, edge));
    col *= 0.6 + 0.4 * smoothstep(0.0, 0.3, length(z));
    fragColor = vec4(col, 1.0);
}

More More GLSL examples

Gyroscope · Moiré · Toon shading · Mandelbox · Wave interference · Lorenz attractor · Voxel world · Snowflakes