uniform sampler2D Texture;
uniform sampler2D RandomTexture;
uniform vec2 Scale;
void main()
{
vec2 xy = gl_TexCoord[0].xy;
vec4 px = texture2D(Texture, xy);
vec2 scaledUV = xy * Scale;
vec2 cell = floor(scaledUV);
vec2 offset = scaledUV - cell;
vec2 randomUV, randomPlusOffset;
vec4 random, image;
float priority = -1.0;
for (int i = -1; i <= 0; i++) {
for (int j = -1; j <= 0; j++) {
vec2 cell_t = cell + vec2(i, j);
vec2 offset_t = offset - vec2(i, j);
randomUV = cell_t.xy * vec2(0.037, 0.119);
random = texture2D(RandomTexture, randomUV);
randomPlusOffset = offset_t - random.xy;
randomPlusOffset.x = clamp(0.0,1.0,randomPlusOffset.x);
randomPlusOffset.y = clamp(0.0,1.0,randomPlusOffset.y);
image = texture2D(Texture, randomPlusOffset);
if (random.a > priority && image.a > 0.0) {
gl_FragColor = image;
priority = random.w;
} else {
gl_FragColor = vec4(0.0);
//discard;
}
}
}
}