Moiré

glint · Examples · Tutorials · Gallery

Two sets of fine rings, each just black or white, combined with exclusive or: where they fall in and out of step, big slow patterns appear that neither set has. Try the ring density.

Moiré: 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 density;
uniform float apart;

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 c1 = vec2(-apart * 0.5, 0.0) + 0.1 * vec2(sin(time * 0.3), cos(time * 0.4));
    vec2 c2 = vec2(apart * 0.5, 0.0) + 0.1 * vec2(cos(time * 0.35), sin(time * 0.25));
    float a = step(0.0, sin(length(uv - c1) * density));
    float b = step(0.0, sin(length(uv - c2) * density));
    float x = abs(a - b);                                    // exclusive or
    vec3 col = mix(vec3(0.05, 0.04, 0.08), pal(length(uv) * 0.5 + time * 0.05), x);
    fragColor = vec4(col, 1.0);
}

More More GLSL examples

Candle · Planet · Isometric city · Gyroscope · Toon shading · Mandelbox · Hyperbolic tiling · Wave interference