Old Normalmapping Shader

From GameStudio Wiki

Jump to: navigation, search

Here's Mats vs20ps20 Normalmapping Shader. I think this is already on Acknex Unlimited but I though I might as well put it here too. I modified it slightly to use the real lightrange rather than a preset value which may or not be useful.

WARNING: Dont bother running this on a GeforceFX card unless you want single digit framerate. Also no fallbacks.


EntSkin1: Entity skin

EntSkin2: Normal Map

 // -------------------------------------------------------------
 // Diffuse and specular shader for models
 // -------------------------------------------------------------
 
 
 float4x4 matWorldViewProj;
 float4x4 matWorld;
 float4 vecLightPos[8]; //light position
 float4 vecLightColor[8]; //light position
 float4 vecViewPos;
 
 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;
 
 };
 
 // -------------------------------------------------------------
 // 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 = 0.0025; //light range here!!!!!!!!!!!!!!!!! 
 
 float LightRange = 1/vecLightPos[1].w;//THESE ARE THE CHANGES I MADE 
 
 float LightRange2 = 1/vecLightPos[2].w;
 
 //light 1
 float3 Light1 = PosWorld - vecLightPos[1] ;
 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[2] ;
 Out.Light2.xyz = mul(worldToTangentSpace, -Light2); // L 
 
 float3 Viewer2 = PosWorld - vecViewPos;
 Out.View2 = mul(worldToTangentSpace, -Viewer2); // V
 
 Out.Att2 = Light2 * LightRange2; // 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; 
 
 };
 
 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));
 
 return (
 (0.1 * color) + //ambient
 ((shadow1 * (color * diff1 + (spec1*gloss.w)) * (1 -Attenuation1))*vecLightColor[1])+
 ((shadow2 * (color * diff2 + (spec2*gloss.w)) * (1 -Attenuation2))*vecLightColor[2])
 );
 }
 
 
 
 // -------------------------------------------------------------
 // techniques//
 // -------------------------------------------------------------
 technique two_pass
 {
 pass P0
 {
 alphablendenable=false;
 srcblend=zero;
 
 VertexShader = compile vs_2_0 VS_PASS0();
 PixelShader = compile ps_2_0 PS_PASS0();
 }
 
 
 
 }
Personal tools