/*=============================================================================
	SimpleElementPixelShader.hlsl: Pixel shader for drawing simple elements.
	Copyright 1998-2010 Epic Games, Inc. All Rights Reserved.
=============================================================================*/

#include "Common.usf"

sampler2D _Texture;

half4 TextureComponentReplicate;
half4 TextureComponentReplicateAlpha;

void ReplicateRChannel(inout float4 BaseColor)
{
#if SM4_PROFILE
	//In DX10, doing a texture lookup from a G8 texture gives float4(value,0,0,0), so we need to replicate it to the other channels.  
	//On all other platforms, the value is replicated automatically.
	if(any(TextureComponentReplicate))
	{
		BaseColor = dot(BaseColor,TextureComponentReplicate);
	}
#endif
	BaseColor.a = dot(BaseColor,TextureComponentReplicateAlpha);
}

void Main(
	in float2 TextureCoordinate : TEXCOORD0,
	in float4 Color : TEXCOORD1,
	out float4 OutColor : COLOR0
	)
{
	float4 BaseColor = tex2D(_Texture,TextureCoordinate);
	ReplicateRChannel(BaseColor);
	OutColor = RETURN_COLOR(BaseColor * Color);	
}

half Gamma;

void GammaMain(
	in float2 TextureCoordinate : TEXCOORD0,
	in float4 Color : TEXCOORD1,
	out float4 OutColor : COLOR0
	)
{
	float4 BaseColor = tex2D(_Texture,TextureCoordinate);
	ReplicateRChannel(BaseColor);
	OutColor = BaseColor * Color;
	if( Gamma != 1.0 )
	{
		// Gamma correct the output color.
		OutColor.rgb = pow(saturate(OutColor.rgb),Gamma);	
	}
	OutColor = RETURN_COLOR(OutColor);
}

float ClipRef;

void GammaMaskedMain(
	in float2 TextureCoordinate : TEXCOORD0,
	in float4 Color : TEXCOORD1,
	out float4 OutColor : COLOR0
	)
{
	float4 BaseColor = tex2D(_Texture,TextureCoordinate);
	ReplicateRChannel(BaseColor);
	clip(BaseColor.a - ClipRef);
	OutColor = BaseColor * Color;
	if( Gamma != 1.0 )
	{
		// Gamma correct the output color.
		OutColor.rgb = pow(saturate(OutColor.rgb),Gamma);	
	}
	OutColor = RETURN_COLOR(OutColor);
}


/** the width to smooth the edge the texture */
float SmoothWidth;
/** toggles drop shadow rendering */
bool EnableShadow;
/** 2d vector specifying the direction of shadow */
float2 ShadowDirection;
/** Color of the shadowed pixels */
float4 ShadowColor;
/** the width to smooth the edge the shadow of the texture */
float ShadowSmoothWidth;
/** toggles glow rendering */
bool EnableGlow;
/** base color to use for the glow */
float4 GlowColor;
/** outline glow outer radius */
float2 GlowOuterRadius;
/** outline glow inner radius */
float2 GlowInnerRadius;

/**
* Distance field rendering of textured tile.
* Alpha value represents distance to silhouette edge. A value of 0.5 is exactly on the edge.
*
* Based on the following paper:
* http://www.valvesoftware.com/publications/2007/SIGGRAPH2007_AlphaTestedMagnification.pdf
*/
void GammaDistanceFieldMain(
	in float2 TextureCoordinate : TEXCOORD0,
	in float4 Color : TEXCOORD1,
	out float4 OutColor : COLOR0
	)
{
	float4 BaseTexture = tex2D(_Texture,TextureCoordinate);
	ReplicateRChannel(BaseTexture);
	float BaseDist = BaseTexture.a;
	// smooth range between SmoothWidth texels around edge
	float4 BaseColor = float4(Color.rgb, smoothstep(0.5-SmoothWidth, 0.5+SmoothWidth, BaseDist));
	
	if( EnableShadow )
	{
		// sample texture with shadow direction offset
		float4 ShadowTexture = tex2D(_Texture,TextureCoordinate + ShadowDirection);
		ReplicateRChannel(ShadowTexture);
		float ShadowDist = ShadowTexture.a;
		// smooth range between ShadowSmoothWidth texels around edge
		float4 ShadowResult = ShadowColor * smoothstep(0.5-ShadowSmoothWidth, 0.5+ShadowSmoothWidth, ShadowDist);
		BaseColor = lerp(ShadowResult,BaseColor,BaseColor.a);
	}

	if (EnableGlow)
	{
		// testing glow rendering
		//GlowOuterRadius = float2(0.3,0.45);
		//GlowInnerRadius = float2(0.45,0.5);	

		if (BaseDist >= GlowOuterRadius[0] && BaseDist <= GlowInnerRadius[1])
		{
			float OutlineFactor=0.0;
			if (BaseDist <= GlowOuterRadius[1])
			{
				OutlineFactor = smoothstep(GlowOuterRadius[0],GlowOuterRadius[1],BaseDist);
			}
			else
			{
				OutlineFactor = smoothstep(GlowInnerRadius[1],GlowInnerRadius[0],BaseDist);			
			}
			BaseColor = lerp(BaseColor,GlowColor,OutlineFactor);
		}
	}
 	
	clip(BaseColor.a - ClipRef);
	OutColor = BaseColor;
	OutColor.a *= Color.a;
	if( Gamma != 1.0 )
	{
		// Gamma correct the output color.
		OutColor.rgb = pow(saturate(OutColor.rgb),Gamma);	
	}	
	OutColor = RETURN_COLOR(OutColor);
}

