|
Normalmapping shader (ps1.1)
#50902
08/06/05 16:26
08/06/05 16:26
|
Joined: Mar 2001
Posts: 3,298 Beverly, Massachusetts
Rhuarc
OP
Expert
|
OP
Expert
Joined: Mar 2001
Posts: 3,298
Beverly, Massachusetts
|
Code:
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.htmlEntskin1 - diffuse map Entskin2 - normal map Supports 1 point light. -Rhuarc
|
|
|
Re: Normalmapping shader (ps1.1)
[Re: Matt_Aufderheide]
#50906
08/08/05 00:09
08/08/05 00:09
|
Joined: Sep 2004
Posts: 1,214 Austin, Texas
Josh_Arldt
Senior Developer
|
Senior Developer
Joined: Sep 2004
Posts: 1,214
Austin, Texas
|
Thanks Rhuarc. This is cool! The light seems to have to be placed far away from the model for the light to shine on it... I have the light attached to a camera. Here's a picture of the bumpmapping so everyone can see what it looks like in the A6.
|
|
|
Re: Normalmapping shader (ps1.1)
[Re: ello]
#50911
08/08/05 14:02
08/08/05 14:02
|
Joined: Mar 2005
Posts: 80
SirSven
Junior Member
|
Junior Member
Joined: Mar 2005
Posts: 80
|
Can somebody help me? I get always the error that the model has the same color like my background and fog! I have one dynamic light next to the model!
|
|
|
|