matrix World;
matrix View;
matrix Projection;

float3 LightDir0 = float3(0.f, 1.f, 0.f);
float3 LightDir1 = float3(0.f, -1.f, 0.f);

float3 LightCol0 = float3(1.f, 1.f, 0.8f);
float3 LightCol1 = float3(1.f, 1.f, 0.8f);

struct vsInput
{
	float4 Pos 		: POSITION;
	float4 Color	: COLOR;
	float3 Norm		: NORMAL;
	float2 Tex		: TEXCOORD;
};

struct psInput
{
	float4 Pos 		: SV_POSITION;
	float4 Color	: COLOR;
	float3 Norm		: NORMAL;
	float2 Tex		: TEXCOORD;
	float vDepth	: VDEPTH;
};

float fFarPlane = 20000.f;

psInput vShader(vsInput input)
{
	psInput output;
	
	output.Pos = mul(input.Pos, World);
	output.Pos = mul(output.Pos, View);
	
	output.vDepth = output.Pos.z / fFarPlane;

	output.Pos = mul(output.Pos, Projection);
	output.Norm = mul(input.Norm, (float3x3)World); 
	output.Tex = input.Tex;
	output.Color = input.Color;

	return output;
}

Texture2D Diffuse;
Texture2D MaskTex;

SamplerState LinearSampler
{
	Filter = MIN_MAG_MIP_LINEAR;
	AddressU = Wrap;
	AddressV = Wrap;
};

float4 pShader(psInput input) : SV_Target
{
	float msk = MaskTex.Sample(LinearSampler, input.Tex).r;
	clip(msk - (1.f / 255.f));

	float3 tex = Diffuse.Sample(LinearSampler, input.Tex).rgb;

	input.Norm = normalize(input.Norm);
	float3 diff = saturate(dot(LightDir0, input.Norm)) * LightCol0;
	diff += saturate(dot(LightDir1, input.Norm)) * LightCol1;
	diff += float3(0.2f, 0.2f, 0.2f);

	diff *= tex * input.Color;

	return float4(diff, input.vDepth);
}

DepthStencilState EnableDepth
{
	DepthEnable = true;
	StencilEnable = false;
};

BlendState NoBlend
{
	BlendEnable[0] = false;
};

technique10 StdForward
{
	pass P0
	{
		SetVertexShader(CompileShader(vs_4_0, vShader()));
		SetGeometryShader(NULL);
		SetPixelShader(CompileShader(ps_4_0, pShader()));
		SetDepthStencilState(EnableDepth, 0);
		SetBlendState(NoBlend, float4(0.f,0.f,0.f,0.f), 1);
	}
}