
// ------- UNIFORMS (SHADER CONSTS) -------
uniform sampler2D	hu_sSceneTexture0; // pass 1
uniform sampler2D	hu_sSceneTexture1; // pass 2
uniform sampler2D	hu_sDepthTexture0; // pass 1
uniform sampler2D	hu_sDepthTexture1; // pass 2
uniform vec4		hu_vCameraNearFar;

// pass 1
uniform vec4		u_vAlphaDistanceFunction1;
uniform vec4		u_vAlphaDistanceFunctionOutline1;
uniform vec4		u_vOutlineColor1;

// pass 2
uniform vec4		u_vAlphaDistanceFunction2;
//uniform vec4		u_vAlphaDistanceFunctionOutline2;
uniform vec4		u_vOutlineColor2;

// ------- INTERPOLATORS (SHADER INPUTS) -------
varying vec2		v_vTex0UV;
varying vec2		v_vUV0;
varying vec2		v_vUV1;
varying vec2		v_vUV2;
varying vec2		v_vUV3;

// ------- FUNCTIONS ------- 

float LinearizeDepth(float z)
{
	float n = hu_vCameraNearFar.x;
	float f = hu_vCameraNearFar.y;
	return (2.0 * n) / (f + n - z * (f - n));
}

float GetDist(sampler2D sDepth, vec2 vCoords)
{
	float fDepth = texture2D(sDepth, vCoords).x;
	float fLinearDepth = LinearizeDepth(fDepth);
	return fLinearDepth*hu_vCameraNearFar.z;
}

vec4 Blend(vec4 f1, vec4 f2)
{
	//float a = a1 + a2 - a1*a2;
	//f = (f1*a1 + f2*a2*(1.0 - a1))/a; // = (f1*a1 + f2*a2 - f2*a1*a2)/a;
	
	vec4 f;
	float a = f1.a + f2.a - f1.a*f2.a;
	f = vec4((f1.rgb*f1.a + f2.rgb*(f2.a*(1.0 - f1.a)))/a, a);
	//f = vec4((f1.rgb*f1.a + f2.rgb*f2.a - f2.rgb*(f1.a*f2.a))/a, a);
	return f;
}

void main(void)
{
	vec4 f;
	vec4 f1 = texture2D(hu_sSceneTexture0, v_vTex0UV);

	if (f1.b < 0.99)
	{
		vec4 f2 = texture2D(hu_sSceneTexture1, v_vTex0UV);
 		float fDist1 = GetDist(hu_sDepthTexture0, v_vTex0UV);
 		
		bool b1 = f1.a <= texture2D(hu_sSceneTexture0, v_vUV0).a && f1.a <= texture2D(hu_sSceneTexture0, v_vUV1).a && 
 				f1.a <= texture2D(hu_sSceneTexture0, v_vUV2).a && f1.a <= texture2D(hu_sSceneTexture0, v_vUV3).a;
 				
		if (f2.r < 0.99)
		{	// blend both passes
			bool b2 = f2.a <= texture2D(hu_sSceneTexture1, v_vUV0).a && f2.a <= texture2D(hu_sSceneTexture1, v_vUV1).a && 
 					f2.a <= texture2D(hu_sSceneTexture1, v_vUV2).a && f2.a <= texture2D(hu_sSceneTexture1, v_vUV3).a;
		
			float fDist2 = GetDist(hu_sDepthTexture1, v_vTex0UV);

			//float a1 = 0.8;
			//float a2 = 0.8;
			//f = Blend(vec4(f1.rgb, a1), vec4(f2.rgb, a2));

   			if (b2 && b1)
 			{	// inside
 				float a2 = GetGrowingValue(u_vAlphaDistanceFunction2, fDist2);
				float a1 = GetGrowingValue(u_vAlphaDistanceFunction1, fDist1);
 				f = Blend(vec4(f1.rgb, a1), vec4(f2.rgb, a2));
 			}
 			else
  			{	// outline
				if ( !b1 && !b2)
				{
  					float a2 = GetGrowingValue(u_vAlphaDistanceFunctionOutline1, fDist2);
  					f = vec4(u_vOutlineColor2.rgb, u_vOutlineColor2.a*a2);
				}
				else
				if ( !b1)
				{
					f = vec4(1.0, 0.0, 0.0, 1.0);
				}
				else
				{
					f = vec4(0.0, 1.0, 0.0, 1.0);
				}
 			}
		}
		else
		{	// only first pass affects this pixel
 			if (b1)
			{	// inside
				float a = GetGrowingValue(u_vAlphaDistanceFunction1, fDist1);
				f = vec4(f1.rgb, a);
			}
			else
 			{	// outline
 				float a = GetGrowingValue(u_vAlphaDistanceFunctionOutline1, fDist1);
 				f = vec4(u_vOutlineColor1.rgb, u_vOutlineColor1.a*a);
			}
		}
	}
	else
	{
		f = vec4(0.0);
	}

	gl_FragColor = f;
}
