Wave Fun

GLSL shader by scry · created 2026-02-12 · 10s loop · 2 passes

What if time reversed, and then back again...and again..and again?

Tags: 2d, Animation, Waves, Layers, Abstract

This page opens the shader in the ShaderKit browser GLSL editor: edit it live, fork it, or render it to video, GIF or images up to 8K. Also available as a full-screen view and an embeddable player.

Shader source (GLSL)

Common

#define pi acos(-1.)
#define deg pi/180.  //1 degree
#define time iTime*2.*pi/10. //sin(time) loops 10 seconds
#define R iResolution.xy //shorthand
#define ar R.x/R.y //aspect ratio
#define M iMouse //shorthand
#define xm (M.xy/R) //normalized mouse
#define nm ((xm.xy-0.5)*vec2(ar,1.)+0.5) //aspect ratio correction
vec3 cs = vec3(1.,2.,3.);
mat2 r2d(float a) {
    return mat2(cos(a),sin(a),-sin(a),cos(a));
}

// Rotating boundary line for wave direction control
vec2 getBoundaryUV(vec2 uv) {
    uv -= 0.5;
    //uv *= r2d(time);
    uv += cos(time*(1.+(timebar_speed/20.)))*0.5;
    uv += 0.5;
    return uv;
}

Buffer A (iChannel0)

// Hash function for noise
float hash(vec2 p) {
    return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453);
}

// Value noise (2D)
float valueNoise(vec2 p) {
    vec2 i = floor(p);
    vec2 f = fract(p);
    f = f * f * (3.0 - 2.0 * f);
    
    float a = hash(i);
    float b = hash(i + vec2(1.0, 0.0));
    float c = hash(i + vec2(0.0, 1.0));
    float d = hash(i + vec2(1.0, 1.0));
    
    return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 uv = fragCoord.xy / iResolution.xy;
    
    // Read previous frame state: r = height, g = velocity
    vec4 prev = texture(iChannel0, uv);
    
    // Reset for first few frames
    if (iFrame < 3) {
        fragColor = vec4(0.0);
        return;
    }
    
    // Sample neighbors for Laplacian (5-point stencil)
    vec2 texel = 1.0 / iResolution.xy;
    float u_center = prev.r;
    float u_left  = texture(iChannel0, uv + vec2(-texel.x, 0.0)).r;
    float u_right = texture(iChannel0, uv + vec2( texel.x, 0.0)).r;
    float u_up    = texture(iChannel0, uv + vec2(0.0,  texel.y)).r;
    float u_down  = texture(iChannel0, uv + vec2(0.0, -texel.y)).r;
    
    // Laplacian approximation
    float laplacian = (u_left + u_right + u_up + u_down - 4.0 * u_center);
    
    // Wave equation using velocity formulation:
    //   u_tt = c^2 * laplacian(u)
    // Split into first-order:
    //   vel += c^2 * laplacian * dt
    //   pos += vel * dt
    // Setting dt negative reverses all propagation.
    float c2 = 0.1+wave_speed/200.;       // wave speed squared
    c2 *= (wave_speed+20.)/20.;
    float damping = 0.9999995;  // velocity damping per step
    float posDrain = 0.9998; // height slowly relaxes back to zero
    float dt = 1.;        // set to -1.0 to reverse propagation
    
    // Left half propagates forward, right half propagates in reverse
    vec2 rv = getBoundaryUV(uv);
    dt = rv.x < 0.5 ? 1.0 : -1.0;
    
    float u_pos = prev.r;  // height
    float u_vel = prev.g;  // velocity
    
    // Acceleration from wave equation
    float accel = c2 * laplacian;
    
    // Integrate velocity and position (dt sign controls direction)
    float new_vel = (u_vel + accel * dt) * damping;
    float new_pos = u_pos * posDrain + new_vel * dt;
    
    // Mouse input - create disturbance at mouse position
    if (iMouse.z > 0.0) {
        vec2 mouseUV = iMouse.xy / iResolution.xy;
        float dist = length(uv - mouseUV);
        float radius = 0.02;
        if (dist < radius) {
            new_pos += 0.5 * (1.0 - dist / radius);
        }
    }
    
    // Automatic input for first 5 seconds: vertical line from top to bottom
    // Horizontal line drawn from left to right over 5 seconds
    if (iTime < 5.0) {
        float margin = 0.05;
        float progress = iTime / 5.0; // 0 to 1 over 5 seconds
        float lineX = mix(margin, 1.0 - margin, progress); // sweeps left to right
        float lineY = 0.5; // centered vertically
        float lineWidth = 0.02;
        float radius = 0.03;
        float dist = length(uv - vec2(lineX, lineY));
        
        if (dist < radius) {
            new_pos += 0.5 * (1.0 - dist / radius);
        }
    }
    
    // Clamp to prevent instability
    //new_pos = clamp(new_pos, -2.0, 2.0);
    //new_vel = clamp(new_vel, -2.0, 2.0);
    
    fragColor = vec4(new_pos, new_vel, 0.0, 1.0);
}

Image

Not used

More shaders by scry

Browse all public shaders · All shaders by scry · ShaderKit home

Vibe Mode BETA
💰 ~0 credits
Uniforms
FPS: 0
Time: 0
Resolution: 0 x 0

Account

FPS: -- Time: -- --×-- 1x