/*=============================================================================
	ShadowVolume.hlsl: Shader for rendering a shadow volume.
	Copyright © 2006 Epic Games, Inc. All Rights Reserved.
=============================================================================*/

#include "Common.usf"

float4x4	ViewProjectionMatrix;
float4		LightPosition;
float		BaseExtrusion;

#if USE_HW_INSTANCING
void Main(
	in  float4   InPosition   : POSITION,
	in  float    InExtrusion  : TEXCOORD0,
	in  float4x3 LocalToWorld : BLENDINDICES0,
	out float4   OutPosition  : POSITION
	)
#else
float4x4 LocalToWorld;
void Main(
	in  float4 InPosition  : POSITION,
	in  float  InExtrusion : TEXCOORD0,
	out float4 OutPosition : POSITION
	)
#endif
{
#if USE_HW_INSTANCING
	float3 WorldPosition = MulMatrix(InPosition,LocalToWorld).xyz;
#else
	float3 WorldPosition = MulMatrix(LocalToWorld,InPosition).xyz;
#endif

	float4 ExtrudedVertex = float4(WorldPosition * LightPosition.w - LightPosition.xyz,0);
	float4 Vertex = float4(WorldPosition,1);

	// Offset the unextruded vertex slightly to allow for some mismatch between the shadow volume and the base object.
	// This can happen with GPU skinning vs the CPU skinning used for the skeletal mesh's shadow volume.	
	Vertex.xyz += normalize(ExtrudedVertex.xyz) * BaseExtrusion;

	OutPosition = MulMatrix(ViewProjectionMatrix,InExtrusion > 0.5 ? ExtrudedVertex : Vertex);
}
