in vec2 texCoord;
in vec4 instanceFragmentColor;
out vec4 fragColor;

uniform float time;
uniform sampler2D texture0; // this will be automatically binded in the script to animation's first texture
uniform vec4 color; // this will be automatically binded to color animation variable, defaults to 1,1,1,1

float random(in vec2 p) {
  return fract(cos(dot(p,
      vec2(16.12327, 27.4725))) *
      29322.1543424);
}

void main() {
    vec2 uv = texCoord.xy;
    float t = time*4.0;
    uv.s += (sin(uv.t*20.0 + t*0.7)*0.045)*(uv.s-0.5);
    uv.t += (cos(uv.s*8.0 + t*0.6)*0.045)*(uv.t-0.5);

    fragColor = instanceFragmentColor * color * mix(texture2D(texture0, texCoord),texture2D(texture0,uv),0.2+0.4*random(texCoord*100.0+time/1000.0));
    fragColor.rgb *= 0.45 + 0.55*random(texCoord*floor(time*10.0));
}
