
// http://davenewson.com/tutorials/udk/planet-earth-material-shader-for-udk
// http://seblagarde.wordpress.com/2011/08/17/feeding-a-physical-based-lighting-mode/

// ------- UNIFORMS (SHADER CONSTS) -------
vec3		u_vNormalizedLightDir;
vec4		u_vSpecularColor;
float		u_fSpecularPower;
float		u_fSpecularStrength;
float		u_fEffectsInterpolation;

vec3		hu_vNormalizedViewDir;

// ------- INTERPOLATORS (SHADER INPUTS) -------
struct PIn
{
    vec4 v_vPosition: SV_POSITION;
    vec4 v_vColor: COLOR;
	vec3 v_vNormal: NORMAL;
};

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

vec4 main(PIn input): SV_TARGET
{
	vec4 vColor = input.v_vColor;
	
	vec3 vNormal = normalize(input.v_vNormal);

	// Compute N.L
	float nDotL = dot(vNormal, u_vNormalizedLightDir);
	
	// Compute reflection vector
	vec3 reflection = (2.0*vNormal*nDotL) - u_vNormalizedLightDir;
	
	// Compute R.V
	float rDotV = max(0.0, dot(reflection, hu_vNormalizedViewDir));
	
	// Compute Specular term
	float p = pow(rDotV, u_fSpecularPower);
	vec4 specular = u_vSpecularColor * vec4(p,p,p,p);

	float f = clamp(-nDotL, 0.04, 1.0);

	vec4 c0 = f*(vColor + u_fSpecularStrength*specular);
	vec4 v = lerp(vColor, c0, u_fEffectsInterpolation);
	return v;
}
