Posted By: TWO
[Supply] Matt's NormalMapping shader with 3 lights - 03/18/07 11:02
Frank_G wanted to have matt's normalmapping shader with more than 2 lights, so here is it. I added the 3rd light and an ambient parameter you can change to prevent a total black modell. I also fixed that the shader ignored the first light (because matt used vecLighPos[1] instead of vecLightPos[0] as first)
Because of the additional registers I had the change the shader version target from 2.0 to 3.0; Maybe a guru can remove some registers, then it would be possible to have even more lights.
Anyone could post this shader in the wiki.
Code:
Because of the additional registers I had the change the shader version target from 2.0 to 3.0; Maybe a guru can remove some registers, then it would be possible to have even more lights.
Anyone could post this shader in the wiki.
Code:
// -------------------------------------------------------------
// Diffuse and specular shader for models
// -------------------------------------------------------------
// [18/3/06 Bloodline] - Added ambient and a 3rd light
float4x4 matWorldViewProj;
float4x4 matWorld;
float4 vecLightPos[8]; //light position
float4 vecLightColor[8]; //light color
float4 vecViewPos;
// Change ambient here
float4 Ambient = { 0.6f, 0.6f, 0.6f, 1.0f };
texture entSkin1; //this is the color map
texture entSkin2; //this is the normal map
sampler sColorMap = sampler_state
{
Texture = <entSkin1>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = wrap;
AddressV = wrap;
};
sampler sBumpMap = sampler_state
{
Texture = <entSkin2>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = wrap;
AddressV = wrap;
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
//first pass
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// -------------------------------------------------------------
// Output channels
// -------------------------------------------------------------
struct VS_OUTPUT0
{
float4 Pos : POSITION;
float2 Tex : TEXCOORD0;
float3 Light1 : TEXCOORD2;
float3 View1 : TEXCOORD3;
float3 Att1 : TEXCOORD4;
float3 Light2 : TEXCOORD5;
float3 View2 : TEXCOORD6;
float3 Att2 : TEXCOORD7;
float3 Light3 : TEXCOORD8;
float3 View3 : TEXCOORD9;
float3 Att3 : TEXCOORD10;
};
// -------------------------------------------------------------
// vertex shader function (input channels)
// -------------------------------------------------------------
VS_OUTPUT0 VS_PASS0(float4 Pos : POSITION, float2 texcoord0 : TEXCOORD0, float3 Normal : NORMAL, float3 Tangent : TEXCOORD2 )
{
VS_OUTPUT0 Out = (VS_OUTPUT0)0;
Out.Pos = mul(Pos, matWorldViewProj); // transform Position
// compute the 3x3 tranform matrix
// to transform from world space to tangent space
float3x3 worldToTangentSpace;
worldToTangentSpace[0] = mul(Tangent, matWorld);
worldToTangentSpace[1] = mul(cross(Tangent, Normal), matWorld);
worldToTangentSpace[2] = mul(Normal, matWorld);
Out.Tex = texcoord0.xy;
float3 PosWorld = mul(Pos, matWorld);
float LightRange = 1/vecLightPos[0].w;
float LightRange2 = 1/vecLightPos[1].w;
float LightRange3 = 1/vecLightPos[2].w;
//light 1
float3 Light1 = PosWorld - vecLightPos[0] ;
Out.Light1.xyz = mul(worldToTangentSpace, -Light1); // L
float3 Viewer1 = PosWorld - vecViewPos;
Out.View1 = mul(worldToTangentSpace, -Viewer1); // V
Out.Att1 = Light1 * LightRange; // Point light
//light 2
float3 Light2 = PosWorld - vecLightPos[1] ;
Out.Light2.xyz = mul(worldToTangentSpace, -Light2); // L
float3 Viewer2 = PosWorld - vecViewPos;
Out.View2 = mul(worldToTangentSpace, -Viewer2); // V
Out.Att2 = Light2 * LightRange2; // Point light
//light 3
float3 Light3 = PosWorld - vecLightPos[2] ;
Out.Light3.xyz = mul(worldToTangentSpace, -Light3); // L
float3 Viewer3 = PosWorld - vecViewPos;
Out.View3 = mul(worldToTangentSpace, -Viewer3); // V
Out.Att3 = Light3 * LightRange3; // Point light
return Out;
}
struct PS_INPUT0
{
float2 Tex : TEXCOORD0;
float3 Light1 : TEXCOORD2;
float3 View1 : TEXCOORD3;
float3 Att1 : TEXCOORD4;
float3 Light2 : TEXCOORD5;
float3 View2 : TEXCOORD6;
float3 Att2 : TEXCOORD7;
float3 Light3 : TEXCOORD8;
float3 View3 : TEXCOORD9;
float3 Att3 : TEXCOORD10;
};
float4 PS_PASS0( PS_INPUT0 psInStruct ):COLOR
{
float4 color = tex2D(sColorMap, psInStruct.Tex); // fetch color map
float3 bumpNormal = 2 * (tex2D(sBumpMap, psInStruct.Tex) - 0.5); // fetch bump map
float4 gloss = tex2D( sBumpMap, psInStruct.Tex );
//light1
float3 LightDir1 = normalize(psInStruct.Light1);
float3 ViewDir1 = normalize(psInStruct.View1);
float4 diff1 = saturate(dot(bumpNormal, LightDir1)); // diffuse component
float shadow1 = saturate(4 * diff1);
float3 Reflect1 = normalize(2 * diff1 * bumpNormal - LightDir1); // R
float4 spec1 = pow(saturate(dot(Reflect1, ViewDir1)), 15);
float4 Attenuation1 = saturate(dot(psInStruct.Att1, psInStruct.Att1));
//light2
float3 LightDir2 = normalize(psInStruct.Light2);
float3 ViewDir2 = normalize(psInStruct.View2);
float4 diff2 = saturate(dot(bumpNormal, LightDir2)); // diffuse component
float shadow2 = saturate(4 * diff2);
float3 Reflect2 = normalize(2 * diff2 * bumpNormal - LightDir2); // R
float4 spec2 = pow(saturate(dot(Reflect2, ViewDir2)), 15);
float4 Attenuation2 = saturate(dot(psInStruct.Att2, psInStruct.Att2));
//light3
float3 LightDir3 = normalize(psInStruct.Light3);
float3 ViewDir3 = normalize(psInStruct.View3);
float4 diff3 = saturate(dot(bumpNormal, LightDir3)); // diffuse component
float shadow3 = saturate(4 * diff3);
float3 Reflect3 = normalize(2 * diff3 * bumpNormal - LightDir3); // R
float4 spec3 = pow(saturate(dot(Reflect3, ViewDir3)), 15);
float4 Attenuation3 = saturate(dot(psInStruct.Att3, psInStruct.Att3));
return (
(0.1 * color + 0.2 * Ambient) +
((shadow1 * (color * diff1 + (spec1*gloss.w)) * (1 -Attenuation1))*vecLightColor[0])+
((shadow2 * (color * diff2 + (spec2*gloss.w)) * (1 -Attenuation2))*vecLightColor[1])+
((shadow3 * (color * diff3 + (spec3*gloss.w)) * (1 -Attenuation3))*vecLightColor[2])
);
}
// -------------------------------------------------------------
// techniques//
// -------------------------------------------------------------
technique two_pass
{
pass P0
{
alphablendenable=false;
srcblend=zero;
VertexShader = compile vs_3_0 VS_PASS0();
PixelShader = compile ps_3_0 PS_PASS0();
}
}