//shadow effect

shared const float3 lightdir < string UIDirectional = "Light Direction"; > = {0, -1, 0};

shared const float4x4 World				: WORLD;
shared const float4x4 ViewProjection	: VIEWPROJECTION;

struct VS_Shadow_Out
{
	float4	pos 	: POSITION;
	//float3	n	: TEXCOORD1;
};

VS_Shadow_Out	VS_Shadow(float3 p : POSITION, float3 n : NORMAL)
{
	VS_Shadow_Out o = (VS_Shadow_Out)0;

	float3 ntemp = mul(n,(float3x3)World);
	float3 mask = (float3)(dot(ntemp,lightdir) > 0);

	float3 ptemp = mul(float4(p,1),(float4x3)World);
	ptemp += (float3)(10000 * mask * lightdir);

	o.pos = mul(float4(ptemp,1),ViewProjection);
	//o.n = ntemp;

	return o;
}

float4 PS_Shadow (VS_Shadow_Out In) : color
{
	//return dot(In.n,lightdir);
	return float4(0,0,0,0.5);
}

//technique DebugShadows
//{
//	pass P0
//	{
//		ZEnable		= True;
//		ZWriteEnable	= False;
//
//		//Don't want to actually draw anything (as fast as possible)
//		Lighting		= False;
//		ShadeMode		= Flat;
//
//		AlphaBlendEnable= True;
//		SrcBlend		= SrcAlpha;
//		DestBlend		= InvSrcAlpha;
//
//		//actual state that matters
//		CullMode			= NONE;
//
//		VertexShader = compile vs_1_1 VS_Shadow();
//	}
//}

technique ShadowTwoSided
{
	//Stencil value
	pass P0
	{
		ZEnable		= True;
		ZFunc		= Less;
		ZWriteEnable	= False;

		StencilEnable	= True;

		StencilRef		= 1;
		StencilMask		= 0xffffffff;
		StencilWriteMask= 0xffffffff;

		StencilFunc		= Always;
		StencilFail		= Keep;
		StencilZFail	= Keep;

		//with 2 stencil mode settings
		TwoSidedStencilMode = True;
		CCW_StencilFunc	= Always;
		CCW_StencilFail	= Keep;
		CCW_StencilZFail= Keep;
        
		//Don't want to actually draw anything (as fast as possible)
		Lighting		= False;
		ShadeMode		= Flat;

		AlphaBlendEnable= True;
		SrcBlend		= Zero;
		DestBlend		= One;

		FogEnable		= False;

		//actual state that matters
		CullMode			= NONE;
		TwoSidedStencilMode = TRUE;

		StencilPass		= INCR;
		CCW_StencilPass	= DECR;

		DepthBias = 0.0000004;

		VertexShader = compile vs_1_1 VS_Shadow();
		//PixelShader = compile ps_2_0 PS_Shadow();
	}
}

technique NormalShadow
{
	//Stencil value
	pass P0
	{
		ZEnable		= True;
		ZFunc		= Less;
		ZWriteEnable	= False;

		StencilEnable	= True;

		StencilRef		= 1;
		StencilMask		= 0xffffffff;
		StencilWriteMask= 0xffffffff;

		StencilFunc		= Always;
		StencilFail		= Keep;
		StencilZFail	= Keep;

		//Don't want to actually draw anything (as fast as possible)
		Lighting		= False;
		ShadeMode		= Flat;

		AlphaBlendEnable= True;
		SrcBlend		= Zero;
		DestBlend		= One;

		DepthBias = 0.0000001;

		//actual state that matters
		CullMode			= CCW;

		StencilPass		= INCR;

		VertexShader = compile vs_1_1 VS_Shadow();
	}

	pass P1
	{
		//actual state that matters
		CullMode			= CW;

		StencilPass		= Decr;

		VertexShader = compile vs_1_1 VS_Shadow();
	}
}

technique Empty
{
	// Must be using an old video card
}