//--- Standard Automatic Parameters
float4x4 worldViewProjection 	: WORLDVIEWPROJECTION;
float4x4 world 					: WORLD;
float3 eyePos  					: EYEPOS;

//--- Manual Parameter
float3 lightPos <string type="entity3d";>;

//--- Material emissive is used to set the object color
float4 color : EMISSIVE;
texture2D outlineTex : TEXTURE;

sampler OutlineSampler = sampler_state
{
	texture = <outlineTex>;
	Minfilter = LINEAR;
	Magfilter = LINEAR;
	AddressU = CLAMP;
	AddressV = CLAMP;
};

struct VS_OUTPUT
{
	float4 pos 		: POSITION;
	float3 eye 		: TEXCOORD0;
	float3 normal	: TEXCOORD1;
	float3 light	: TEXCOORD2;		
};

VS_OUTPUT VS( float3 inPos	: POSITION,
			  float3 inNorm	: NORMAL)
{
	VS_OUTPUT outVS = (VS_OUTPUT) 0;
	outVS.pos = mul( float4(inPos,1) , worldViewProjection);
	
	// transform position in world space
	inPos = mul(float4(inPos,1), world);
	
	// transform normal in world space 
	outVS.normal = mul( float4(inNorm,0) , world);
	
	// compute vertex->eye
	outVS.eye = normalize(eyePos - inPos);
	
	// compute vertex->light
	outVS.light = normalize(lightPos - inPos);
		
	return outVS;
}

float4 PS ( float3 eye 		: TEXCOORD0,
			float3 normal 	: TEXCOORD1,
			float3 light 	: TEXCOORD2 ) : COLOR
{
	float3 norm = normalize(normal);
	
	// lookup in the outline texture depending on the angle between
	// the normal and vertex->eye
	float4 outline = tex1D(OutlineSampler, 1 - dot(norm,normalize(eye)));
	
	// compute lighting depending on the angle between
	// the normal and vertex->light
	float lighting = (dot(normalize(light),norm) * 0.5 + 1.0);
	
	// combine the outline and the light with the color
	return outline * lighting * color;
}

technique tech
{
	pass p
	{
		VertexShader = compile vs_1_1 VS();
		PixelShader = compile ps_2_0 PS();
	}
}
