
#ifdef FXCOMPOSER_VERSION // in fxcompser editor
#include "include/common.cgh"
#else
#include "common.cgh"
#endif

/********* pixel shader ********/
float4 dplatePSStuds(BumpVertexOutput IN,
		uniform float Ks,
		uniform float SpecExpon,
		uniform float3 Lamp0Color,
		uniform float3 Lamp1Color,
		uniform float3 AmbiColor,
		uniform sampler2D StudsSamp,
		uniform sampler2D NormalSamp
) : COLOR
{
	float4 studShade = tex2D(StudsSamp, IN.ModelUV.xy);
	float fade = 1-abs(IN.ObjectNormal.w);  // ObjectNormal.w holds z distance from object position to camera
														// *.00555 is division by 180, the fade distance for LOD drawing
														// so that the contribution of shader will fade to 0 at 180
	if(fade < 0)
		fade = 0;
	
	float NormalRatio = 0.15;
	float2 NormalUV = IN.ModelUV.zw; 

	float3 dColor = IN.Color.xyz;
	float3 tNorm = tex2D(NormalSamp,NormalUV).xyz - float3(0.5,0.5,0.5);

    float3 aWorldBinormal = cross(IN.WorldNormal, IN.WorldTangent);
	float3 NnBump = normalize(tNorm.x * IN.WorldTangent +  tNorm.y * aWorldBinormal +  tNorm.z * IN.WorldNormal);
    NnBump *= fade;
	Ks *= fade;
	
	float3 Nn = normalize(lerp(NnBump, IN.WorldNormal, 0.7));
	
	float3 diffContrib;
	float3 specContrib;			
	
	ps_shared_lighting(dColor, Nn, IN.WorldView, 
					IN.Light0Vec, IN.Light1Vec,
					Lamp0Color, Lamp1Color,
					AmbiColor,
					Ks, SpecExpon,
					diffContrib,
					specContrib);
	
    float3 result = lerp(diffContrib, studShade.xyz, studShade.w) + specContrib;
	
	return float4(result+0.01, 1);
}

