Here is a specular lighting shader..right now it doesnt include fog.. and the ambient light is hardcoded in the pixel shader.. maybe someone can improve it:
Code:
material mat_specular
{
flags = tangent;
effect =
"
texture texSkin1;
texture texSkin2;
matrix matWorldViewProj;
matrix matWorld;
matrix matWorldView;
vector vecSkill41;
vector vecFog;
vector vecLight;
vector vecDiffuse;
vector vecAmbient;
vector vecSpecular;
technique specular
{
pass p0
{
//load matrices
VertexShaderConstant[0] = <matWorld>; //World Matrix
VertexShaderConstant[4] = <matWorldViewProj>; //World*View*Proj Matrix
//Material properties of object
VertexShaderConstant[9] = <vecLight>; //Diffuse
VertexShaderConstant[16] = <vecSkill41>; //Light Direction
vertexShaderConstant[20] = (.5f,.5f,.5f,.5f);
//Camera Information
VertexShaderConstant[24] = <matWorldView>;
Texture[0] = <texSkin1>;
Texture[1] = <texSkin2>;
VertexShader=
decl
{
stream 0;
float v0[3]; //position
float v3[3]; //normal
float v7[3]; //uv
float v8[3]; //tangent
}
asm
{
vs.1.1
//tranform position
m4x4 oPos,v0,c4
mov oT0,v7 // output uvs
mov oT1,v7 // output uvs
mov oD0,c9 //output light color
//tsb generation
m3x3 r3,v8,c0 //gen normal
m3x3 r5,v3,c0 //gen tangent
//gen binormal via Cross product
mul r0,-r3.zxyw,r5.yzxw;
mad r4,-r3.yzxw,r5.zxyw,-r0;
//specular
m4x4 r2,v0,c24 //transform position
//get a vector toward the camera
add r2,-r2,c24
dp3 r11.x, r2.xyz,r2.xyz //load the square into r11
rsq r11.xyz,r11.x //get the inverse of the square
mul r2.xyz, r2.xyz,r11.xyz //multiply, r0 = -(camera vector)
add r2.xyz,r2.xyz,-c16 //get half angle
//normalize
dp3 r11.x,r2.xyz,r2.xyz //load the square into r1
rsq r11.xyz,r11.x //get the inverse of the square
mul r2.xyz,r2.xyz,r11.xyz //multiply, r2 = HalfAngle
//transform the half angle vector
dp3 r8.x,r3,r2
dp3 r8.y,r4,r2
dp3 r8.z,r5,r2
//half-angle in oD1
mad oD1.xyz,r8.xyz,c20,c20 //mutiply by a half to bias, then add half
};
PixelShader=
asm
{
ps.1.1
def c0,0.1f,0.1f,0.1f,0.1f //ambient value
tex t0 //sample base map
tex t1 //sample normal
dp3 r0,t1_bx2,v1_bx2; //dot(normal,half)
mul r1,r0,r0; //raise it to 32nd power
mul r0,r1,r1;
mul_x4 r1,r1,v0 // modulate light intensity against light color
add r1,r1,c0 //add light
//assemble final color
mul r0,t0,r1
};
}
}
";
}