
uniform sampler2D srctex;
uniform sampler2D noise;
uniform vec2 randpos;
uniform mat4 matrices[4];

void main() {
  vec3 res;
  float alpha;
  
  if (gl_TexCoord[0].y<0.5) {
    res=texture2D(srctex, gl_TexCoord[0].xy*vec2(1.0, 2.0)).rgb;
  } else {
    vec3 pier=texture2D(noise, gl_TexCoord[0].xy*4.0+randpos).rgb;
	  vec4 pos=vec4(texture2D(srctex, (gl_TexCoord[0].xy-vec2(0.0, 0.5))*vec2(1.0, 2.0)).rgb, 1.0);

    if (pier.r<0.5) res = vec3(matrices[0]*pos);
	  else if (pier.r<0.66666) res = vec3(matrices[1]*pos);
	  else if (pier.r<0.83333) res = vec3(matrices[2]*pos);
	  else res = vec3(matrices[3]*pos);
  }

  gl_FragColor = vec4(res, sin(res.x*1.712)*0.5+0.5);
}

