
void receiverVP(
	float4 position		: POSITION,
	float4 normal		: NORMAL,
	out float4 outPos			: POSITION,
	out float4 outShadowUV		: TEXCOORD0,
	float3 oPosition: TEXCOORD1,
	uniform float4x4 world,
	uniform float4x4 worldViewProj,
	uniform float4x4 texViewProj

	)
{
	float4 worldPos = mul(world, position);
	outPos = mul(worldViewProj, position);
	oPosition=position;
	outShadowUV = mul(texViewProj, worldPos);

	
}

void receiverFP(
	float4 position			: TEXCOORD1,
	float4 shadowUV			: TEXCOORD0,
	//float4 vertexColour		: COLOR,

	uniform sampler2D shadowMap : register(s0),
	uniform sampler2D cookieMap : register(s1),
	uniform sampler2D noiseMap	: register(s2),
	uniform float inverseShadowmapSize,
	uniform float3 lightColor ,
	uniform float fixedDepthBias,
	uniform float gradientClamp,
	uniform float gradientScaleBias,
	uniform float shadowFuzzyWidth,
	uniform float Time,
	
	out float4 result		: COLOR)
{

	shadowUV = shadowUV / shadowUV.w;
	float centerdepth = tex2D(shadowMap, shadowUV.xy).x;
    
    // gradient calculation
  	float pixeloffset = inverseShadowmapSize;
    float4 depths = float4(
    	tex2D(shadowMap, shadowUV.xy + float2(-pixeloffset, 0)).x,
    	tex2D(shadowMap, shadowUV.xy + float2(+pixeloffset, 0)).x,
    	tex2D(shadowMap, shadowUV.xy + float2(0, -pixeloffset)).x,
    	tex2D(shadowMap, shadowUV.xy + float2(0, +pixeloffset)).x);

	float2 differences = abs( depths.yw - depths.xz );
	float gradient = min(gradientClamp, max(differences.x, differences.y));
	float gradientFactor = gradient * gradientScaleBias;

	// visibility function
	float depthAdjust = gradientFactor + (fixedDepthBias * centerdepth);
	float finalCenterDepth = centerdepth + depthAdjust;


	// use depths from prev, calculate diff
	depths += depthAdjust.xxxx;
	float final = (finalCenterDepth > shadowUV.z) ? 1.0f : 0.0f;
	final += (depths.x > shadowUV.z) ? 1.0f : 0.0f;
	final += (depths.y > shadowUV.z) ? 1.0f : 0.0f;
	final += (depths.z > shadowUV.z) ? 1.0f : 0.0f;
	final += (depths.w > shadowUV.z) ? 1.0f : 0.0f;
	
	//here we load the light masks, the noise uv are animated.
	float cookie  = tex2D(cookieMap, shadowUV.xy).x;
	float noise	  = tex2D(noiseMap, shadowUV.xy*2.0f+Time*0.05f).x;

    result.rgb=lightColor;
	result.a = final;
	
	//computation of a simple fallof
	float fallof=saturate((1-shadowUV.z));
	
	result.a=final*fallof*cookie*noise;
	
	
}



