Truchet weave

glint · Examples · Tutorials · Gallery

Two layers of animated Truchet tiles (random quarter-arcs that join into endless paths), woven over and under with shadows and flowing dashes.

Audio-reactive: it moves with music from a microphone, line-in or a file.

Truchet weave: an audio-reactive 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 vec2 mouse;
uniform vec3 spectrum;
uniform vec3 spectrumIntegrated;
uniform float beat;
uniform float density;

in vec2 v_texcoord;
out vec4 fragColor;

float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }

// One Truchet layer: in each cell pick one of two diagonal orientations and draw two quarter-circle arcs.
// Returns distance to the path (x) and the position along it (y), for dashes that flow.
vec2 truchet(vec2 p, float seed) {
    vec2 id = floor(p), f = fract(p) - 0.5;
    if (hash(id + seed) < 0.5) f.x = -f.x;
    vec2 c = f.x > -f.y ? vec2(0.5) : vec2(-0.5);
    vec2 q = f - c;
    float d = abs(length(q) - 0.5);
    float along = atan(q.y, q.x) / 1.5708;
    // Alternate direction on a checkerboard so the flow is continuous across cells.
    float dir = mod(id.x + id.y, 2.0) * 2.0 - 1.0;
    return vec2(d, along * dir);
}

vec3 layer(vec2 p, float seed, vec3 tint, float t, inout float mask) {
    vec2 tr = truchet(p, seed);
    float w = 0.12;
    float line = smoothstep(w, w - 0.02, tr.x);
    float shadow = smoothstep(w + 0.12, w, tr.x) * 0.6;
    float dash = 0.5 + 0.5 * sin((tr.y + t) * 6.2831 * 2.0);
    vec3 c = tint * (0.55 + 0.45 * dash) * (0.7 + 0.6 * smoothstep(w, 0.0, tr.x));
    c += smoothstep(0.03, 0.0, abs(tr.x - w * 0.5)) * 0.25;
    mask = max(mask, line);
    return c * line - shadow * (1.0 - line) * 0.0 + vec3(-shadow * 0.35) * (1.0 - line);
}

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    uv = mat2(cos(time * 0.03), -sin(time * 0.03), sin(time * 0.03), cos(time * 0.03)) * uv;
    float t = time * 0.4 + spectrumIntegrated.x;
    vec2 p = uv * density + vec2(time * 0.2, 0.0);
    vec3 bg = mix(vec3(0.06, 0.05, 0.1), vec3(0.12, 0.08, 0.2), uv.y + 0.5);
    float m1 = 0.0, m2 = 0.0;
    vec3 lower = layer(p, 1.0, vec3(0.2, 0.7, 1.0), t, m1);
    vec3 upper = layer(p * 1.0 + 0.5, 7.0, vec3(1.0, 0.35, 0.55), -t, m2);
    vec3 col = bg + lower;
    col = mix(col, upper, m2) + upper * (1.0 - m2) * 0.0;
    col += beat * 0.15 * (m1 + m2);
    col *= 1.0 - 0.4 * dot(uv, uv);
    fragColor = vec4(col, 1.0);
}

More Showpieces examples

Mandelbulb · Volumetric clouds · Ocean · KIFS tunnel · Voronoi crystals · Domain warping · Neon city · Galaxy