	
// Phong parameters
float ambient;
float4 ambientColor;
float4 diffuseColor;
float4 specularColor;
float  specularExp;

// Depth of field parameters
float focusDistance;
float nearFocusPlaneDistance;
float farFocusPlaneDistance;
float farClamp;

float ComputeDepthBlur(float depth)
{
	float f;
	
	if (depth < focusDistance)
	{
		f = (depth - focusDistance) / (focusDistance - nearFocusPlaneDistance);
	}
	else
	{
		f = (depth - focusDistance) / (farFocusPlaneDistance - focusDistance);
		
		f = clamp (f, 0, farClamp);
	}
	
	return f * 0.5 + 0.5;
}

float4 Main(float3 normal : TEXCOORD0, 
			float3 lightDir : TEXCOORD1,
			float3 viewDir : TEXCOORD2,
			float depth : TEXCOORD3) : COLOR
{			
	float3 nNormal = normalize(normal);
	float3 nLightDir = normalize(lightDir);
	float3 nHalfDir = normalize(nLightDir + normalize(viewDir));
	
	float diffuse = saturate(dot(nNormal, nLightDir));
	
	float specular = pow(saturate(dot(nNormal, nHalfDir)), 25);
	
	float4 color = ambient * ambientColor + diffuse * diffuseColor + specularColor * specular;
	
	color.a = ComputeDepthBlur(depth);
	
	return color;
}
