Circuit board

glint · Examples · Tutorials · Gallery

A procedural PCB: every cell picks a trace (straight, bend or via), and pulses of light race along the copper.

Circuit board: 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))); }

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    vec2 p = uv * 12.0 + vec2(time * 0.2, 0.0);
    vec2 id = floor(p), f = fract(p) - 0.5;
    float h = hash(id);
    // Rotate each cell's pattern by a quarter turn or not, so traces join up differently.
    if (h > 0.5) f = vec2(f.y, -f.x);
    float d, along;
    if (fract(h * 7.0) < 0.45) {
        d = abs(f.y);                                  // straight through
        along = f.x;
    } else if (fract(h * 7.0) < 0.85) {
        vec2 q = f - vec2(0.5, 0.5);                   // a bend round a corner
        d = abs(length(q) - 0.5);
        along = atan(q.y, q.x) / 1.5708;
    } else {
        d = length(f) - 0.12;                          // a via
        along = 0.0;
    }
    vec3 col = vec3(0.02, 0.18, 0.08);                 // board
    col += vec3(0.02, 0.06, 0.03) * step(0.5, fract((uv.x + uv.y) * 80.0));
    float trace = smoothstep(0.08, 0.06, d);
    col = mix(col, vec3(0.75, 0.55, 0.2), trace);
    // Pulses travelling along the copper.
    float pulse = smoothstep(0.9, 1.0, fract(along * 0.5 + p.x * 0.05 - time * 0.8 + h * 3.0));
    col += vec3(0.3, 1.0, 0.9) * pulse * trace * 1.5;
    col += vec3(0.1, 0.5, 0.4) * 0.3 * exp(-d * 20.0) * pulse;
    fragColor = vec4(col, 1.0);
}

More More GLSL examples

Gyroid cave · Julia set · Mandelbrot zoom · Lava lamp · Snowfall · Ink in water · Infinite spheres · Night sky