Posted By: Rhuarc
Normalmapping shader (ps1.1) - 08/06/05 16:26
Code:
Converted from: http://www.monitorstudios.biz/bcloward/resources_shaders.html
Entskin1 - diffuse map
Entskin2 - normal map
Supports 1 point light.
-Rhuarc
extern float4x4 matWorldViewProj;
extern float4x4 matWorldInv;
extern float4x4 matWorld;
float4 vecLightPos[8];
float4 vecLightColor[8];
float4 vecAmbient;
float4 vecDiffuse;
texture entSkin1; // diffuse
texture entSkin2; // normals
sampler2D diffuseSampler = sampler_state
{
Texture = <entSkin1>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};
sampler2D normalSampler = sampler_state
{
Texture = <entSkin2>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};
// vertex input
struct VertexIn {
float4 Position : POSITION; //in object space
float2 UV : TEXCOORD0; //in object space
float4 Normal : NORMAL; //in object space
float3 T : TEXCOORD2; //in object space
};
//vertex output
struct VertexOut {
float4 HPosition : POSITION;
float2 texCoord0 : TEXCOORD0;
float2 texCoord1 : TEXCOORD1;
float3 LightVector : TEXCOORD2;
};
//vertex shader*************************************
VertexOut DiffuseBumpVS(VertexIn IN)
{
//create the vertex out struct
VertexOut OUT;
OUT.texCoord0 = IN.UV; //pass coords for diffuse map
OUT.texCoord1 = IN.UV; //pass coords for normal map
// compute the 3x3 tranform from tangent space to object space
float3x3 objToTangentSpace;
objToTangentSpace[0] = IN.T;
objToTangentSpace[1] = cross(IN.T, IN.Normal);
objToTangentSpace[2] = IN.Normal;
//put the vert position in world space
float4 worldSpacePos = mul(IN.Position, matWorld);
//cast a ray to the light
float4 normLightVec = mul(vecLightPos[1], matWorld) - worldSpacePos;
//transform the light vector to object space
float3 objnormLightVec = mul(normLightVec, matWorldInv).xyz;
objnormLightVec = normalize(objnormLightVec);
// transform light vector from object space to tangent space and pass it as a color
OUT.LightVector = 0.5 * mul(objToTangentSpace, objnormLightVec) + 0.5;
// transform position to projection space
OUT.HPosition = mul(IN.Position, matWorldViewProj);
return OUT;
}
//pixel shader*************************************
float4 myps(VertexOut IN):COLOR
{
float4 col;
//get the color from the diffuse texture
float4 texColor = tex2D(diffuseSampler, IN.texCoord0);
//get the color from the normal map and convert to normal
float4 bumpNormal = (2 * (tex2D(normalSampler, IN.texCoord1)-0.5));
//unpack the light vector to [-1,1]
float3 lightVector = 2* (IN.LightVector - 0.5);
//compute the angle to the light and clamp at zero
float bump = max(dot(bumpNormal,lightVector),0);
//compute final color (diffuse + ambient)
float4 diffuse = texColor * bump * vecLightColor[1];
float4 ambient = texColor * vecAmbient*0.5;
col = diffuse + ambient;
col.a = 1.0;
return col;
}
technique diffuseBump
{
pass p0
{
ZEnable = true;
ZWriteEnable = true;
CullMode = CCW;
VertexShader = compile vs_1_1 DiffuseBumpVS();
PixelShader = compile ps_1_1 myps();
}
}
Converted from: http://www.monitorstudios.biz/bcloward/resources_shaders.html
Entskin1 - diffuse map
Entskin2 - normal map
Supports 1 point light.
-Rhuarc