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

/********* pixel shader ********/
void mainPSStuds(VertexOutput IN,
		uniform float3 Contrast,
		uniform float Ks,
		uniform float SpecExpon,
		uniform float3 Lamp0Color,
		uniform float3 Lamp1Color,
		uniform float3 AmbiColor,
		uniform float3 NoiseScale,
	    uniform sampler2D StudsSamp,
		uniform sampler3D NoiseSamp,
		uniform sampler2D NormalSamp,
		uniform sampler2D grassSamp,
		uniform sampler2D GrassNormalSamp,
		uniform float4 FogParams,
		uniform float4 FogColour,
#ifdef GBUFFER		
		out float4 oColor1 : COLOR1,
#endif
		out float4 oColor : COLOR0
)
{
	float3 oo = IN.ObjectNormal.xyz;
	float4 studShade = tex2D(StudsSamp, IN.ModelUV.xy);
	float zdist = IN.ObjectNormal.w;
	float spread = 0.3;					  
	float grass_threshold = 0.95;	
	float NormalRatio = 0.15;
	float2 NormalUV = IN.ModelUV.zw; 
	
	float3 shiftPos = IN.TexPos3D.xyz;
	float3 ns = NoiseScale;
    float noiseval2 = tex3D(NoiseSamp,shiftPos.xyz*ns.x).x * 0.4;
	float noiseval = tex3D(NoiseSamp,shiftPos.zyx*ns.y).x * 0.6;
	noiseval -= noiseval2;
	float noiseval3 = tex3D(NoiseSamp,shiftPos.xyz*ns.z).x * 0.3;
	noiseval += noiseval3;

	float interp = (noiseval - grass_threshold + spread)/2/spread+0.5;
	interp = clamp(interp,0,1);
	
    float3 grassColor = tex2D(grassSamp, NormalUV).xyz;
	float3 dirt = tex2D(NormalSamp,NormalUV).xyz ;
	float3 grassNorm = tex2D(GrassNormalSamp,NormalUV).xyz;
	float3 dirtNorm = float3(0.5,0.5,1);
	float3 tNorm = lerp(grassNorm, dirtNorm, interp) - 0.5;
	
    float3 aWorldBinormal = cross(IN.WorldNormal , IN.WorldTangent);
	float3 NnBump = normalize(tNorm.x*IN.WorldTangent + tNorm.y*aWorldBinormal + tNorm.z*IN.WorldNormal);
    
	float fade = 1.5-abs(zdist);
	if (fade < 0.0)
		fade = 0.0;
	float3 dColor;

	dColor = lerp(grassColor+IN.Color.xyz-float3(0.31,0.43,0.146), dirt, interp*fade);
	
	float3 diffContrib;
	float3 specContrib;

	//Fade out the shader
	ps_shared_lighting(dColor, NnBump, IN.WorldView.xyz, IN.Light0Vec, IN.Light1Vec,
	Lamp0Color, Lamp1Color, AmbiColor, Ks, SpecExpon, diffContrib, specContrib);
	
    diffContrib = lerp(diffContrib, studShade.xyz, studShade.w);
    float3 result = diffContrib + specContrib;
	
	float fogAlpha = clamp( ( FogParams.z - ( IN.WorldView.w * 500.0 ) ) * FogParams.w, 0.0, 1.0 );
	
	oColor = float4( result, 1.0 ); 
	oColor = lerp( FogColour, oColor, fogAlpha );
    
	#ifdef GBUFFER
		oColor1 = packDeferred( lerp( 1.0, IN.WorldView.w, fogAlpha ), diffContrib, specContrib );
	#endif
}

