uniform sampler2D depthpacked,normal,colorMap;
uniform float s,rad,min_normaldist,min_depthdist,depth_mult,amount;
float unpackFloatFromVec3i(const vec3 value){
	const vec3 bitSh = vec3( 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0);
	return(dot(value, bitSh));
}
float getdepth(vec2 xy){
	float depth;
	vec3 col=texture2D(depthpacked,xy).xyz;
	if(col.x==0.0 && col.y==0.0 && col.z==0.0)
		depth=1000000000.0;
	else
		depth=unpackFloatFromVec3i(col);
	return depth*depth_mult*.0001;
}
vec3 rand3(vec2 co) {
	vec3 ret;
	ret.x=sin(cos(dot(co.xy*2.0 ,vec2(121.9898,788.2333))) * 437158.5453);
	ret.y=cos(sin(dot(co.xy*2.0 ,vec2(1111.9898,78.2333))) * 431158.5453);
	ret.z=sin(sin(dot(co.xy*2.0 ,vec2(11.9898,78.2333))) * 431158.5453);
	return ret;
}
void main() {
	int i;
	vec2 uv=gl_TexCoord[0].xy,newuv;
	vec3 uvadd;
	float depth=getdepth(uv),depthdist;
	float d,a=1.0;
	vec3 normalXYZ=(texture2D(normal,uv).xyz),normalnew;
	for(i=1;i<12;i++){
		uvadd=.01*s*(normalXYZ-.5)+rand3(uv+vec2(i))*rad/40.0;
		newuv=uv+(uvadd.xy*float(i)/100000.0)/depth;
		d=getdepth(newuv);
		depthdist=(depth-d);
		if(depthdist>0.0 && newuv.x > 0.0001 && newuv.x < 0.999 && newuv.y > 0.0001 && newuv.y < 0.999)
			if(depthdist<.000005*min_depthdist){
			normalnew=(texture2D(normal,newuv).xyz);
			if(distance(normalXYZ,normalnew)>min_normaldist)
				a-=(amount*.1)/(1.0+depthdist);
		}
	}

	gl_FragColor = vec4(a);
	// if(uv.x>0.5)
		//gl_FragColor = texture2D(normal,uv);
	//else
		//gl_FragColor = texture2D(depthpacked,uv);
}
