|
HLSL Normal mapping for PS_1_4!! (works GREAT!)
#35109
10/17/04 12:11
10/17/04 12:11
|
Joined: Mar 2001
Posts: 3,298 Beverly, Massachusetts
Rhuarc
OP
Expert
|
OP
Expert
Joined: Mar 2001
Posts: 3,298
Beverly, Massachusetts
|
Screenshot:
Just plug this into a script, and assign normalMap_ent to the entity .
Enjoy!
EDIT: I'm going to add specular and dynamic lights (only one possible) soon and update this post with the changes )
UPDATE: Added Specular, change lines in red. Specular value ranges from 0-1, 1 being little specular, 0 being full specular.
Code:
/**************************************************************/
// HLSL NormalMapping Shader
// -------------------------
// By: Daniel "Rhuarc" Niezgocki
// Requires: []VS 1.1
// []PS 1.4
// Skin1: Texture
// Skin2: Normal Map
//
// Usage: Free to use, credit is nice, but not required.
/**************************************************************/
function normalMap_init
{
mat_inverse(mtl.matrix, matWorld);
}
material normalMap_fx {
flags=tangent;
event = normalMap_init;
effect="
// VertexShader ------------------
float4x4 matWorldViewProj;
float4x4 matWorld;
float4 vecSunPos;
float4 vecViewPos;
float4x4 matMtl;
texture entSkin1;
texture entSkin2;
sampler base_map = sampler_state
{
texture = (entSkin1);
};
sampler bump_map = sampler_state
{
texture = (entSkin2);
};
struct VS_INPUT_STRUCT
{
float4 position : POSITION;
float3 normal : NORMAL;
float3 texcoord0 : TEXCOORD0;
float3 tangent : TEXCOORD2;
};
struct VS_OUTPUT_STRUCT
{
float4 position : POSITION;
float2 bump_map : TEXCOORD0;
float3 light_angle : TEXCOORD1;
};
VS_OUTPUT_STRUCT mainVS( VS_INPUT_STRUCT vsInStruct )
{
VS_OUTPUT_STRUCT vsOutStruct;
vsOutStruct.position = mul( vsInStruct.position, matWorldViewProj );
float3 position = mul( matWorld, vsInStruct.position );
vsOutStruct.bump_map = vsInStruct.texcoord0;
float3 light_normal_vector = mul( vsInStruct.normal, matWorld );
vsOutStruct.light_angle = light_normal_vector;
return vsOutStruct;
};
// PixelShader -------------------
float bumpiness = 0.5;
float specular = 0.5;
float4 vecAmbient;
float4 vecDiffuse;
float4 vecSpecular;
float vecViewDir;
struct PS_INPUT_STRUCT
{
float2 bump_map : TEXCOORD0;
float3 light_angle : TEXCOORD1;
};
struct PS_OUTPUT_STRUCT
{
float4 color0 : COLOR0;
};
PS_OUTPUT_STRUCT mainPS( PS_INPUT_STRUCT psInStruct )
{
PS_OUTPUT_STRUCT psOutStruct;
float3 base = tex2D( base_map, psInStruct.bump_map );
float3 bump = tex2D( bump_map, psInStruct.bump_map );
// Uncompress vectors ([0, 1] -> [-1, 1])
float3 lightVectorFinal = (psInStruct.light_angle - 0.5);
float3 bumpNormalVectorFinal = bumpiness * (bump - 0.5);
// Compute diffuse factor
float diffuse = dot(bumpNormalVectorFinal, lightVectorFinal);
psOutStruct.color0.rgb = ( diffuse * base + + max(diffuse-specular,0));
psOutStruct.color0.a = 1.0f;
return psOutStruct;
};
technique normalMap
{
pass P0
{
VertexShader = compile vs_1_1 mainVS();
PixelShader = compile ps_1_4 mainPS();
}
}
";
}
action normalMap_ent
{
my.material = normalMap_fx;
}
Last edited by [Rhuarc]; 10/17/04 22:25.
|
|
|
Re: HLSL Normal mapping for PS_1_4!! (works GREAT!)
[Re: Rhuarc]
#35110
10/17/04 12:43
10/17/04 12:43
|
Joined: Oct 2002
Posts: 815 NY USA
Red Ocktober
Developer
|
Developer
Joined: Oct 2002
Posts: 815
NY USA
|
wow... lookin' good Rhu...
lemme test it out and compare it with the code you put up previously in the level thread...
oh yeah, THANKS for sharing
--Mike
|
|
|
Re: HLSL Normal mapping for PS_1_4!! (works GREAT
[Re: Red Ocktober]
#35113
10/17/04 13:25
10/17/04 13:25
|
Joined: Mar 2001
Posts: 3,298 Beverly, Massachusetts
Rhuarc
OP
Expert
|
OP
Expert
Joined: Mar 2001
Posts: 3,298
Beverly, Massachusetts
|
Updated shader above with specular... It's cheap, but it works . 0.9 seems to be the best value for the specular variable... -Rhuarc
|
|
|
Re: HLSL Normal mapping for PS_1_4!! (works GREAT
[Re: Rhuarc]
#35114
10/17/04 15:39
10/17/04 15:39
|
Joined: Jan 2002
Posts: 1,276 trapped in a paper bag
Drew
Serious User
|
Serious User
Joined: Jan 2002
Posts: 1,276
trapped in a paper bag
|
Thats great, thanks! works very well!! REALLY appreciate the contribution.
can it use level ambient color in the future? also, point light and/or object light sources would be great.. then ill work on a flashlight effecct, hopefully the light source could be attached, moving a specular over the objects...I did it with the older dx8 normal map code, but the light would get stuck on an ebject...
Thanks again!
Last edited by Drew; 10/17/04 16:11.
|
|
|
Re: HLSL Normal mapping for PS_1_4!! (works GREAT
[Re: Drew]
#35115
10/17/04 20:00
10/17/04 20:00
|
Joined: May 2004
Posts: 164 Germany
DARKLORD
Member
|
Member
Joined: May 2004
Posts: 164
Germany
|
|
|
|
Re: HLSL Normal mapping for PS_1_4!! (works GREAT
[Re: Drew]
#35116
10/17/04 22:27
10/17/04 22:27
|
Joined: Mar 2001
Posts: 3,298 Beverly, Massachusetts
Rhuarc
OP
Expert
|
OP
Expert
Joined: Mar 2001
Posts: 3,298
Beverly, Massachusetts
|
Quote:
Thats great, thanks! works very well!! REALLY appreciate the contribution. can it use level ambient color in the future? also, point light and/or object light sources would be great.. then ill work on a flashlight effecct, hopefully the light source could be attached, moving a specular over the objects...I did it with the older dx8 normal map code, but the light would get stuck on an ebject... Thanks again!
I'm glad it works well for you . I'm gonna look into the ambient lighting as well as work on those object lightsources.
Quote:
Whats with skin3?
Ah, I still had it in there from trying it with level textures...
|
|
|
|