uniform sampler2D normalTex;
uniform sampler2D depthTex;
uniform float near;
uniform float far;
varying vec2 texCoord;

float xOff = 5.0/1280.0;
float yOff = 5.0/720.0;

void main() {
	vec3 color = vec3(1.0);
	
	vec4 normalColor = texture2D(normalTex,texCoord);
	vec4 adjacentNormals[4];
	adjacentNormals[0] = texture2D(normalTex,texCoord+vec2(xOff,0));
	adjacentNormals[1] = texture2D(normalTex,texCoord-vec2(xOff,0));
	adjacentNormals[2] = texture2D(normalTex,texCoord+vec2(0,yOff));
	adjacentNormals[3] = texture2D(normalTex,texCoord-vec2(0,yOff));
	
	float depth = (2.0 * near) / (far + near - (texture2D(depthTex,texCoord).r*2.0-1.0) * (far - near));
	float adjacentDepth[4];
	adjacentDepth[0] = (2.0 * near) / (far + near - (texture2D(depthTex,texCoord+vec2(xOff,0)).r*2.0-1.0) * (far - near));
	adjacentDepth[1] = (2.0 * near) / (far + near - (texture2D(depthTex,texCoord-vec2(xOff,0)).r*2.0-1.0) * (far - near));
	adjacentDepth[2] = (2.0 * near) / (far + near - (texture2D(depthTex,texCoord+vec2(0,yOff)).r*2.0-1.0) * (far - near));
	adjacentDepth[3] = (2.0 * near) / (far + near - (texture2D(depthTex,texCoord-vec2(0,yOff)).r*2.0-1.0) * (far - near));
	
	for (int i=0; i<4; i++)
	{
		if (distance(normalColor.xyz, adjacentNormals[i].xyz) > 0.4)
		{
			color = vec3(0);
		}
		if (distance(depth, adjacentDepth[i]) > 0.075)
		{
			color = vec3(0);
		}
	}
	gl_FragColor = vec4(color, 1.0);
} 