Can anyone help me change this simple diffuse shader so it works with a dynamic light instead of the sun?
I know I need to use "vecLightPos[1]" to create the eqivalent of "vecSunDir", but how?
Thanks for any help!
Code:
/***********************************************************************************************
/ Copyright 2006 by Taco Cohen. All rights reserved
/***********************************************************************************************/
/***********************************************************************************************
/ Global Variables:
/***********************************************************************************************/
// Tweakables:
static const float AmbientIntensity = 1.0f; // The intensity of the ambient light.
static const float DiffuseIntensity = 1.0f; // The intensity of the diffuse light.
static const float4 SunColor = {0.9f, 0.9f, 0.5f, 1.0f}; // Color vector of the sunlight.
// Application fed data:
const float4x4 matWorldViewProj; // World*view*projection matrix.
const float4x4 matWorld; // World matrix.
const float4 vecAmbient; // Ambient color, passed by the engine.
const float4 vecSunDir; // The sun direction vector.
texture entSkin1; // Color map.
sampler ColorMapSampler = sampler_state // Color map sampler.
{
Texture = <entSkin1>;
AddressU = Clamp;
AddressV = Clamp;
};
/***********************************************************************************************
/ Vertex Shader:
/***********************************************************************************************/
void DiffuseVS(in float4 InPos: POSITION,
in float3 InNormal: NORMAL,
in float2 InTex: TEXCOORD0,
out float4 OutPos: POSITION,
out float2 OutTex: TEXCOORD0,
out float3 OutNormal: TEXCOORD1)
{
// Transform the vertex from object space to clip space:
OutPos = mul(InPos, matWorldViewProj);
// Transform the normal from object space to world space:
OutNormal = normalize(mul(InNormal,matWorld));
// Pass the texture coordinate to the pixel shader:
OutTex = InTex;
}
/***********************************************************************************************
/ Pixel Shader:
/***********************************************************************************************/
float4 DiffusePS( in float2 InTex: TEXCOORD0,
in float3 InNormal: TEXCOORD1) : COLOR
{
// Calculate the ambient term:
float4 Ambient = AmbientIntensity * vecAmbient;
// Calculate the diffuse term:
float4 Diffuse = DiffuseIntensity * saturate(dot(-vecSunDir, normalize(InNormal)));
Diffuse *= SunColor;
// Fetch the pixel color from the color map:
float4 Color = tex2D(ColorMapSampler, InTex);
// Calculate final color:
return (Ambient + Diffuse) * Color;
}
/***********************************************************************************************
/ Technique:
/***********************************************************************************************/
technique DiffuseTechnique
{
pass P0
{
VertexShader = compile vs_2_0 DiffuseVS();
PixelShader = compile ps_2_0 DiffusePS();
}
}