@LancerSolurus:
1) I'm setting the light direction to (1, 0, 1), it's a directional light (no position).
2) Not sure what you mean exactly
Here is the entire effect file:
float4x4 World;
float4x4 View;
float4x4 Projection;
// Light
float3 lightDirection; // Directional light
float4 lightDiffuse;
float4 lightAmbient;
float4 lightSpecular;
// Material
float4 materialDiffuse;
float4 materialAmbient;
float4 materialSpecular;
float materialPower;
float4 globalAmbient;
// Camera position
float3 cameraPos;
// Texture
texture colorMapTexture;
sampler2D colorMap = sampler_state
{
Texture = <colorMapTexture>;
MagFilter = Linear;
MinFilter = Anisotropic;
MipFilter = Linear;
MaxAnisotropy = 16;
};
struct VS_INPUT
{
float3 Pos : POSITION;
float2 UV : TEXCOORD0;
float3 Normal : NORMAL;
};
struct VS_OUTPUT
{
float4 Pos : POSITION;
float3 worldPos : TEXCOORD1;
float2 UV : TEXCOORD0;
float3 Normal : NORMAL;
};
//-----------------------------------------------------------------------
// Vertex shader function
//-----------------------------------------------------------------------
VS_OUTPUT VS( VS_INPUT IN )
{
VS_OUTPUT Out;
Out = (VS_OUTPUT)0;
float4x4 WVP = mul(World, mul(View, Projection));
Out.Pos = mul(float4(IN.Pos, 1.0f), WVP);
Out.UV = IN.UV;
Out.Normal = mul(IN.Normal, (float3x3)World).xyz;
Out.worldPos = mul(float4(IN.Pos, 1.0f), World).xyz;
return Out;
}
//-----------------------------------------------------------------------
// Pixel shader function
//-----------------------------------------------------------------------
float4 PS( VS_OUTPUT IN ) : COLOR
{
float3 viewDir = normalize(cameraPos - IN.worldPos);
float3 lightDir = normalize(-lightDirection);
float3 halfVector = normalize(lightDir + viewDir);
float3 n = normalize(IN.Normal);
float3 h = normalize(halfVector);
float3 l = normalize(lightDir);
float nDotL = saturate(dot(n, l));
float nDotH = saturate(dot(n, h));
float power = (nDotL <= 0.0f) ? 0.0f : pow(nDotH, materialPower);
float4 FinalDiffuse = materialDiffuse * lightDiffuse;
float4 FinalSpecular = materialSpecular * lightSpecular;
float4 color = (materialAmbient + globalAmbient) + (FinalDiffuse * nDotL) + (FinalSpecular * power);
return color * tex2D(colorMap, IN.UV);
}
//-----------------------------------------------------------------------------
// Techniques.
//-----------------------------------------------------------------------------
technique DirectionalLight
{
pass
{
AlphaBlendEnable = false;
ZEnable = true;
VertexShader = compile vs_3_0 VS();
PixelShader = compile ps_3_0 PS();
}
}