Normalmapping shader (ps1.1)

Posted By: Rhuarc

Normalmapping shader (ps1.1) - 08/06/05 16:26

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.html

Entskin1 - diffuse map
Entskin2 - normal map

Supports 1 point light.

-Rhuarc
Posted By: BoH_Havoc

Re: Normalmapping shader (ps1.1) - 08/06/05 18:52

good job [Rhuarc]

exspecially giving the source of the shader is a good idea as shader-n00bs like myself can try to understand the steps of how to convert a shader to be used with 3dgs
Posted By: nightshade

Re: Normalmapping shader (ps1.1) - 08/07/05 10:22

Don't works :-(, to many Errors :'(. But screens lookin' very nice!
Posted By: Matt_Aufderheide

Re: Normalmapping shader (ps1.1) - 08/07/05 22:48

you say it supports a point light.. how do you calculate attenuation? as far as i can see it doesnt have any at all. It seems you need another pass with an attenuation factor.
Posted By: Josh_Arldt

Re: Normalmapping shader (ps1.1) - 08/08/05 00:09

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.


Posted By: oronll

Re: Normalmapping shader (ps1.1) - 08/08/05 03:29

why cant anyone get GS sunlight to work with these shaders?
Posted By: ello

Re: Normalmapping shader (ps1.1) - 08/08/05 08:59

what is the extern... for??
Posted By: nightshade

Re: Normalmapping shader (ps1.1) - 08/08/05 12:47

ello, why It doesn't worked?
Posted By: ello

Re: Normalmapping shader (ps1.1) - 08/08/05 13:25

could be many causes for that. wrong copying, wrong engine version
Posted By: SirSven

Re: Normalmapping shader (ps1.1) - 08/08/05 14:02

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!


Posted By: Rhuarc

Re: Normalmapping shader (ps1.1) - 08/08/05 17:31

Quote:

what is the extern... for??




It's not *needed*, however I put it there for readability.
Quote:

A global variable marked extern is an external input to the shader. To set an extern variable, use any of the set methods ("SetVertexShaderConstantX") or any of the ID3DXConstantTable interface methods.

A global variable that is not declared to be either static or extern is assumed to be extern. You cannot declare a global variable with both extern and static.



-MSDN

Quote:

you say it supports a point light.. how do you calculate attenuation? as far as i can see it doesnt have any at all. It seems you need another pass with an attenuation factor.



Good point, I have not added attenuation to the shader- this is a direct port of the one linked to in the first post. If I find a few minutes sometime this week I will throw in attenuation. I have another ps1.1 normalmapping shader I coded a while back from scratch that uses attenuation and specular, I will probably just copy some source from that over.

-Rhuarc
Posted By: Steempipe

Re: Normalmapping shader (ps1.1) - 08/08/05 18:40

Quote:

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!






You need to code Fog output in the shader.
Posted By: broozar

Re: Normalmapping shader (ps1.1) - 08/08/05 20:35

damn, i still didnīt manage to integrate it in my scripts. i really need a scripter...
Posted By: Josh_Arldt

Re: Normalmapping shader (ps1.1) - 08/10/05 01:38

This would be perfect for me if the bumps were dynamic.
How hard would it be for me to do that?
Also if it could work on shaded blocks...
What would I have to do?
BTW how can I make it more bumpy? it barely shows up on most textures...
© 2024 lite-C Forums