#version 330 core
in vec2 vTex;
out vec4 FragColor;

uniform sampler2D iChannel0;
uniform vec3 iResolution;
uniform float iTime;
uniform float uAlpha;
uniform float uStartDelay;

float random(vec2 st) { // borrowed from Book of Shaders
    return fract(sin(dot(st.xy, vec2(12.9898,78.233))) * 43758.5453123);
}

float movement(vec2 coord) {
  return sin(coord.y + 1.5) * sin(iTime * random(coord)) * 0.025;
}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
  vec2 uv = fragCoord / iResolution.xy;

  // passthrough until startDelay
  if (iTime < uStartDelay)
  {
      fragColor = texture(iChannel0, uv);
      return;
  }

  vec2 coord = uv;
  coord.x -= movement(coord);
  coord.x += 0.1;
  vec4 redVal = texture(iChannel0, coord) * vec4(1.0, 0.0, 0.0, 1.0);
  coord.x -= movement(coord);
  vec4 greenVal = texture(iChannel0, coord) * vec4(0.0, 0.0, 1.0, 1.0);
  coord.x -= movement(coord);
  vec4 blueVal = texture(iChannel0, coord) * vec4(0.0, 1.0, 0.0, 1.0);

  vec4 c = redVal + blueVal + greenVal;

  // mix processed effect over original using uAlpha for consistent blending
  vec3 orig = texture(iChannel0, uv).rgb;
  vec3 outcol = mix(orig, c.rgb, clamp(uAlpha, 0.0, 1.0));
  fragColor = vec4(outcol, 1.0);
}

void main(){ mainImage(FragColor, gl_FragCoord.xy); }
