Wave interference

glint · Examples · Tutorials · Gallery

Two sources making circular waves: where crests meet they add (bright), where a crest meets a trough they cancel (dark). Move them apart or change the wavelength.

Wave interference: 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 wavelength;
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 s1 = vec2(-apart * 0.5, 0.05 * sin(time * 0.3)), s2 = vec2(apart * 0.5, -0.05 * sin(time * 0.3));
    float k = 6.28318 / wavelength;
    float d1 = length(uv - s1), d2 = length(uv - s2);
    float h = sin(k * d1 - time * 4.0) / sqrt(d1 + 0.05) + sin(k * d2 - time * 4.0) / sqrt(d2 + 0.05);
    vec3 col = h > 0.0 ? mix(vec3(0.02, 0.03, 0.06), vec3(0.2, 0.7, 1.0), h * 0.35) : mix(vec3(0.02, 0.03, 0.06), vec3(1.0, 0.3, 0.2), -h * 0.35);
    col += vec3(1.0) * (smoothstep(0.012, 0.008, d1) + smoothstep(0.012, 0.008, d2));
    fragColor = vec4(col, 1.0);
}

More More GLSL examples

Moiré · Toon shading · Mandelbox · Hyperbolic tiling · Lorenz attractor · Voxel world · Snowflakes · Maurer rose