/*=============================================================================
	PointLightPixelShader.hlsl: Point light pixel shader.
	Copyright 1998-2010 Epic Games, Inc. All Rights Reserved.
=============================================================================*/

#define NEEDS_LIGHTMAP_COORDINATE STATICLIGHTING_TEXTUREMASK

#include "Common.usf"
#include "Material.usf"
#include "VertexFactory.usf"
#include "DynamicLightingCommon.usf"

void Main(
	in FVertexFactoryInterpolants FactoryInterpolants,
	in float3 TangentLightVector : TEXCOORD4,
	in float4 WorldLightVector : TEXCOORD5,
	in float4 CameraVectorOrVertexColor : TEXCOORD6,
	in float4 PixelPosition : TEXCOORD7,
	OPTIONAL_FacingSign
	OPTIONAL_PixelShaderScreenPosition
	out float4 OutColor : COLOR0
	)
{
	FMaterialPixelParameters MaterialParameters = GetMaterialPixelParameters(FactoryInterpolants);
	CalcMaterialParameters(MaterialParameters,FacingSign,CameraVectorOrVertexColor,PixelPosition,TangentLightVector);

#if STATICLIGHTING_TEXTUREMASK
	#if STATICLIGHTING_SIGNEDDISTANCEFIELD
		half TransitionDistance = tex2D(ShadowTexture,GetLightMapCoordinate(FactoryInterpolants)).x;
		half ShadowFactor = (TransitionDistance + DistanceFieldParameters.x) * DistanceFieldParameters.y;
		half LightMask = pow(saturate(ShadowFactor), DistanceFieldParameters.z); 
	#else
		half LightMask = tex2D(ShadowTexture,GetLightMapCoordinate(FactoryInterpolants)).r; 
	#endif
#elif STATICLIGHTING_VERTEXMASK
		half	LightMask = WorldLightVector.w;
#else
		half	LightMask = 1;
#endif

	// clip and early out if outside of the light radius or masked
	OutColor = float4(0,0,0,0);
	float LightDist = dot(WorldLightVector.xyz,WorldLightVector.xyz);
	float LightAmount = (1 - LightDist) * LightMask;
	clip(LightAmount);

#if !PS3 && !SM4_PROFILE
	// only emit the branch attribute on xbox, D3D platforms have too many restrictions. (tex2D can't be used inside the if)
	BRANCH_XBOX
	if( LightAmount > 0 ) 
#endif
	{
		GetMaterialClipping(MaterialParameters, PixelShaderScreenPosition.xy);

		half3 CombinedShadowFactor = LightMask.xxx;
		if (bReceiveDynamicShadows)
		{
			#if MATERIALBLENDING_SOLID || MATERIALBLENDING_MASKED || TRANSLUCENCY_INHERIT_DOMINANT_SHADOWS_FROM_OPAQUE
				CombinedShadowFactor *= GetLightAttenuation(MaterialParameters.ScreenPosition).rgb;
			#elif TRANSLUCENCY_RECEIVE_DYNAMIC_SHADOWS_FROM_STATIC
				float DynamicShadowFactor = CalculateForwardDynamicShadowing(MaterialParameters.ScreenPosition);
				CombinedShadowFactor *= DynamicShadowFactor;
			#endif
		}

		OutColor.rgb =
			GetMaterialPointLightTransfer(MaterialParameters,WorldLightVector.xyz,FalloffExponent,CombinedShadowFactor,true) *
			LightColor;

		half Opacity = GetMaterialOpacity(MaterialParameters);

#if MATERIALBLENDING_TRANSLUCENT
		OutColor.a = Opacity;
#elif MATERIALBLENDING_ADDITIVE || MATERIALBLENDING_SOFTMASKED
		OutColor.rgb = OutColor.rgb * Opacity;
		OutColor.a = 0.0f;
#else
		OutColor.a = 0.0f;
#endif
	}
	OutColor = RETURN_COLOR(OutColor);
}
