Time for action – HLSL for lighting
To implement HLSL lighting, perform the following steps:
1. In the declarations area of the
Terrain.fx
effect file, aftertextureSampler
is declared, add the following variable declarations:float3 lightDirection; float4 lightColor; float lightBrightness;
2. In the
VertexShaderInput
struct definition, add a new struct component to pass in the normal associated with the vertex as follows:float3 Normal : NORMAL0;
3. In the
VertexShaderOutput
struct definition, add a new struct component to hold the calculated lighting value for the vertex as follows:float4 LightingColor : COLOR0;
4. In the
VertexShaderFunction()
function, add the following lines of code before the line that readsreturn output;
:float4 normal = normalize(mul(input.Normal, World)); float lightLevel = dot(normal, lightDirection); output.LightingColor = saturate( lightColor * lightBrightness * lightLevel);
5. Replace the current
PixelShaderFunction()
method with the following:float4 PixelShaderFunction...