Gears
glint · Examples · Tutorials · Gallery
Four meshing gears on a blueprint: each is a circle whose radius bumps out in teeth, and each turns at the speed its neighbour's teeth push it (the ratio of tooth counts), the other way.

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))); }
// Distance to a gear with n teeth and pitch radius R, turned by angle a.
float gear(vec2 p, float R, float n, float a) {
float ang = atan(p.y, p.x) - a;
float tooth = smoothstep(-0.25, 0.25, cos(ang * n)) * 0.045;
float d = length(p) - (R - 0.02 + tooth);
d = max(d, -(length(p) - 0.05)); // axle hole
// Spokes: cut out windows between them.
float s = abs(mod(atan(p.y, p.x) - a, 1.2566) - 0.6283) * length(p);
float window = max(max(length(p) - (R - 0.08), 0.12 - length(p)), 0.04 - s);
return max(d, -window);
}
// The angle of gear B (nB teeth) driven by gear A at angle aA, with B's centre in direction theta from A's: turns
// the other way, nA/nB as fast, phased so a tooth of one always sits in a gap of the other where they touch.
float mesh(float aA, float nA, float nB, float theta) { return -(nA / nB) * aA + theta * (1.0 + nA / nB) + 3.14159 - 3.14159 / nB; }
void main() {
vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
float px = 1.5 / resolution.y;
// Blueprint.
vec2 g = abs(fract(uv * 10.0) - 0.5);
vec3 col = vec3(0.07, 0.2, 0.45) + vec3(0.06) * smoothstep(0.03, 0.0, 0.5 - max(g.x, g.y));
float w = time * 0.8;
// Tooth counts 20, 12, 16, 10; pitch radius proportional; neighbours touch at R1 + R2.
float k = 0.016;
float n1 = 20.0, n2 = 12.0, n3 = 16.0, n4 = 10.0;
vec2 c1 = vec2(-0.3, 0.02), c2 = c1 + (n1 + n2) * k * vec2(cos(0.3), sin(0.3));
vec2 c3 = c2 + (n2 + n3) * k * vec2(cos(-1.2), sin(-1.2)), c4 = c1 + (n1 + n4) * k * vec2(cos(2.4), sin(2.4));
float a1 = w, a2 = mesh(a1, n1, n2, 0.3), a3 = mesh(a2, n2, n3, -1.2), a4 = mesh(a1, n1, n4, 2.4);
float d1 = gear(uv - c1, n1 * k, n1, a1);
float d2 = gear(uv - c2, n2 * k, n2, a2);
float d3 = gear(uv - c3, n3 * k, n3, a3);
float d4 = gear(uv - c4, n4 * k, n4, a4);
float d = min(min(d1, d2), min(d3, d4));
vec3 brass = vec3(0.85, 0.65, 0.3) * (0.8 + 0.2 * uv.y);
col = mix(col, vec3(0.0), smoothstep(0.02, 0.0, d - 0.01) * 0.4); // shadow
col = mix(col, brass, smoothstep(px, 0.0, d));
col = mix(col, brass * 0.6, smoothstep(px, 0.0, abs(d + 0.01) - 0.002)); // bevel
fragColor = vec4(col, 1.0);
}More More GLSL examples
Black hole · Low poly · 10 PRINT · Invaders · Phyllotaxis · Pong · Jellyfish · Fireflies