Bump specular wiht sun ?

Posted By: TheExpert

Bump specular wiht sun ? - 12/29/05 21:36

Anyone could give us some day a simple bump shader taking account of the sun
of the level defined in map properties ?
Posted By: Matt_Aufderheide

Re: Bump specular wiht sun ? - 12/30/05 07:22

This shader will do specular and diffuse normal mapping from the sun. Put color map+specmap in alpha channel in skin 1, normal map in skin 2.

define the material like this:
material mat_normalmap
{
flags=tangent;
}

after you load you level do this in a function:
effect_load(mat_normalmap,"normalmap.fx");
mat_normalmap.skill1=float(sun_pos.x);
mat_normalmap.skill2=float(sun_pos.z);
mat_normalmap.skill3=float(sun_pos.y);

then make an fx file called normalmap.fx with this code:

Code:
 
// -------------------------------------------------------------
// Diffuse and specular shader for models
// -------------------------------------------------------------
//Only does sun light, no dynamic lights..

float4x4 matWorldViewProj;
float4x4 matWorld;
float4 vecViewPos;
float4 vecFog;
float4 vecSkill1;

float4 ambientcolor={0.3,0.3,0.3,0.0}; //can also use vecLight here for true ambient color
float4 suncolor={0.8,0.8,0.7,0.0}; //define the suncolor here, couls al so pass it fomr the script in a mtl skill

texture entSkin1; //this is the color map with specular map in alpha ..32 bit
texture entSkin2; //this is the normal map .. 24 bit

sampler ColorMapSampler = sampler_state
{
Texture = <entSkin1>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = wrap;
AddressV = wrap;
};


sampler BumpMapSampler = sampler_state
{
Texture = <entSkin2>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = wrap;
AddressV = wrap;
};


///////////////////////////////////////////////////////////////////////////////////////////////////////////////
//sunlight
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

// -------------------------------------------------------------
// Output channels
// -------------------------------------------------------------
struct VS_OUTPUT1
{
float4 Pos : POSITION;
float2 Tex : TEXCOORD0;

float3 View4 : TEXCOORD3;

float3 Sun : TEXCOORD1;
float Fog : FOG;

};

// -------------------------------------------------------------
// vertex shader function (input channels)
// -------------------------------------------------------------
VS_OUTPUT1 VS_PASS1(float4 Pos : POSITION, float2 texcoord0 : TEXCOORD0, float3 Normal : NORMAL, float3 Tangent : TEXCOORD2 )
{
VS_OUTPUT1 Out = (VS_OUTPUT1)0;
Out.Pos = mul(Pos, matWorldViewProj); // transform Position

// compute the 3x3 tranform matrix
// to transform from world space to tangent space
float3x3 worldToTangentSpace;
worldToTangentSpace[0] = mul(Tangent, matWorld);
worldToTangentSpace[1] = mul(cross(Tangent, Normal), matWorld);
worldToTangentSpace[2] = mul(Normal, matWorld);

Out.Tex = texcoord0.xy;

float3 PosWorld = mul(Pos, matWorld);

// float ofog = 1 - (distance(PosWorld, vecViewPos) - vecFog.x) * (vecFog.z);
// Out.Fog = ofog;
Out.Fog = 10000;


float3 Viewer4 = PosWorld - vecViewPos;
Out.View4 = mul(worldToTangentSpace, -Viewer4); // V

//sunlight
Out.Sun.xyz = mul(worldToTangentSpace, vecSkill1); // L

return Out;
}


struct PS_INPUT1
{
float2 Tex : TEXCOORD0;
float3 View4 : TEXCOORD3;
float3 Sun : TEXCOORD1;
float Fog : FOG;
};

// -------------------------------------------------------------
// Pixel Shader (input channels):output channel
// -------------------------------------------------------------
float4 PS_PASS1( PS_INPUT1 psInStruct ):COLOR
{

float4 color = tex2D(ColorMapSampler, psInStruct.Tex); // fetch color map
float3 bumpNormal = 2 * (tex2D(BumpMapSampler, psInStruct.Tex) - 0.5); // fetch bump map
bumpNormal=normalize(bumpNormal);

float3 ViewDir4 = normalize(psInStruct.View4);

//sunlight
float3 Sun = normalize(psInStruct.Sun);
float4 diff7 = saturate(dot(bumpNormal, Sun)); // diffuse component
float3 Reflect7 = normalize(2 * diff7 * bumpNormal - Sun); // R
float4 spec7 = pow(saturate(dot(Reflect7, ViewDir4)), 15);
float shadow7 = saturate(4 * diff7);

return
(
(ambientcolor * color) + //ambient

((shadow7* (color * diff7 + (spec7*color.w))) * suncolor)
);
}

// -------------------------------------------------------------
// techniques//
// -------------------------------------------------------------
technique two_pass
{

pass P0
{
alphablendenable=false;
zenable=true;
zwriteenable=true;


//compile shaders
VertexShader = compile vs_1_1 VS_PASS1();
PixelShader = compile ps_2_0 PS_PASS1();
}



}


Posted By: TheExpert

Re: Bump specular wiht sun ? - 12/30/05 12:46

Thanks a lot Matt

Caus the existing ones i found worked with fixed light, and not
good for a global lightening.

thanks again.
Posted By: Logimator

Re: Bump specular wiht sun ? - 12/30/05 14:46

Excellent Shader The Bump-Mapping is one of the best i've seen. Thubs up for Matt again

@Matt, the only thing is, the Shader has Problems with transparent models which are in front of the shaded model. Do you think, this could be fixed ?
Posted By: Matt_Aufderheide

Re: Bump specular wiht sun ? - 12/30/05 18:24

make sure that the entities you have this material assigned to have my.transparency=off and my.flare=off...

becasue it has a tga with alpha channel, the engine will automatically sort it as a transparent entity, when it shouldn't be.. so set those flags off in the action and it will be fine.
Posted By: Logimator

Re: Bump specular wiht sun ? - 12/31/05 18:08

Again Thumps up. Working now, looking perfect ! Matt's the Shader Guru

@Matt, do you have the same Shader working for Dynamic-Lights also ?
Posted By: Matt_Aufderheide

Re: Bump specular wiht sun ? - 01/01/06 02:39

yes, you can try the Ultimate Lighting Shader, or buy the Sphere Engine which is better.. it has the shaders all ready, even if you dont use the other features you can use the model normal mapping shader with dynamic lights.
Posted By: TheExpert

Re: Bump specular wiht sun ? - 01/01/06 20:31

For my part i'll never buy things that are free in open source 3D engines,
and that you can have directly !!
Posted By: zefor

Re: Bump specular wiht sun ? - 01/07/06 13:37

Not seeing bump or specular. do I need to assign an action? something like this?


Code:
 
action bumpout
{
my.material=mat_normalmap;
}



Still no result. I have color map with specular in alpha channel in skin1, saved as a 32 bit tga. I have my normal map in skin2 saved as 24 bit tga. ???

also tried this action
Code:
 action tryout
{
my.transparent = off;
my.flare = off;
}



Still a no go.
Posted By: Matt_Aufderheide

Re: Bump specular wiht sun ? - 01/07/06 17:13

make sure you have this stuff in a function that loads after the level is loaded

effect_load(mat_normalmap,"normalmap.fx");
mat_normalmap.skill1=float(sun_pos.x);
mat_normalmap.skill2=float(sun_pos.z);
mat_normalmap.skill3=float(sun_pos.y);

you have to save the shader code in a file called normalmap.fx, and this file must be in your game's root directory, not in a subfolder.
Posted By: zefor

Re: Bump specular wiht sun ? - 01/07/06 19:48

I have this at the bottom of main


Code:
 
function load_shaders2()
{

effect_load(mat_normalmap,"normalmap.fx");
mat_normalmap.skill1=float(sun_pos.x);
mat_normalmap.skill2=float(sun_pos.z);
mat_normalmap.skill3=float(sun_pos.y);
}



I also have normalmap.fx in my root directory. Are actions not necessary? if not how do you make them not transparent, because the transparency flag is already unchecked? I only know how to do this with an action.
Posted By: Matt_Aufderheide

Re: Bump specular wiht sun ? - 01/07/06 20:26

yes actions are needed, both to assign a material, and to set various flags..

and of course you have to call this function somewhere after you load the level..
Posted By: zefor

Re: Bump specular wiht sun ? - 01/08/06 01:52

Ok, here is what I have. my models look like the side of the models that are AWAY from the light in other shaders. Dont know if that makes sense??? Its like they are not getting the sun light.
Posted By: zefor

Re: Bump specular wiht sun ? - 01/09/06 20:25

Do I need to change anything with the sun in the map properties? Tried this and still no result. I know I am missing something so simple, I'll kick myself later.
Posted By: Thomas_Nitschke

Re: Bump specular wiht sun ? - 01/11/06 14:54

Maybe you could replace this
Code:

function load_shaders2(){
effect_load(mat_normalmap,"normalmap.fx");
mat_normalmap.skill1=float(sun_pos.x);
mat_normalmap.skill2=float(sun_pos.z);
mat_normalmap.skill3=float(sun_pos.y);}



with this:
Code:

function load_shaders2(){
effect_load(mat_normalmap,"normalmap.fx");
while(1){
mat_normalmap.skill1=float(sun_pos.x);
mat_normalmap.skill2=float(sun_pos.z);
mat_normalmap.skill3=float(sun_pos.y);
wait(1);}
}



Just in case the sun_pos is modified during runtime, that'd probably already do the trick
Posted By: zefor

Re: Bump specular wiht sun ? - 01/11/06 21:08

Here is what I have:
Code:

material mat_normalmap
{
flags=tangent;
}



Is under the "Includes" in the main script.
I have the rest of the code placed at the bottom like so:
Code:

/////////////////////////////////////////////////////////////////
//INCLUDE <debug.wdl>;


function load_shaders2()
{
effect_load(mat_normalmap,"normalmap.fx");
while(1){
mat_normalmap.skill1=float(sun_pos.x);
mat_normalmap.skill2=float(sun_pos.z);
mat_normalmap.skill3=float(sun_pos.y);
wait(1);}
}



I am using a model with colormap (with specular in alpha) in skin1 32bit and normal map in skin2 24 bit. Used this model successfully with ultimate lighting shader. I do have normalmap.fx in my work folder. still no bump or spec, I am hoping I just placed these in the wrong section of script and someone will be able to point out where they "should" go. Thank you.
Posted By: Thomas_Nitschke

Re: Bump specular wiht sun ? - 01/12/06 15:00

Right below the load_shaders2 function, you should place an action that can be assigned to the model, otherwise of course the model won't do anything.
I know this is a rather stupid reminder but just in case you forgot it...
Posted By: Matt_Aufderheide

Re: Bump specular wiht sun ? - 01/12/06 15:20

Where in your script do you call this function?
Posted By: zefor

Re: Bump specular wiht sun ? - 01/12/06 16:50

I have read this post a hundred times, and somehow, in my stupidity, I was calling the function ABOVE the level load. Now it is BELOW where it belongs and its working. Thank you again matt, for opening my eyes. I knew it would be something stupid that I missed. I rated you a star.

By the way, Nice looking shader matt. And not a bad framerate too. Best framerate I've had with shaders
Posted By: zefor

Re: Bump specular wiht sun ? - 01/13/06 22:24

This shader only seems to work for me with templates, and only if I dont have camera1stperson commented out. I dont have a problem using templates, but I dont want a 1st person veiw. Any suggestions?
Posted By: lofwyr

Re: Bump specular wiht sun ? - 02/05/06 09:55

Hey, thanks for such great code, I have a small problem getting a model to work when I need to set CullMod=None (for double sided poly). If I add

CullMod = None;

to the technique two_pass routine then I lose the bumpmapping effect.

Any ideas?
Posted By: Matt_Aufderheide

Re: Bump specular wiht sun ? - 02/05/06 12:15

well first its "cullmode" not cullmod..

But otherwise cullmode wont affect bump mapping.. it must be something else..
Posted By: lofwyr

Re: Bump specular wiht sun ? - 02/05/06 20:06

Hi Matt

Thanks for the reply, when I add cullmode=None it stops working, without it the model gets bump and spec just fine. So if it's not the culling mode itself that creates the problem it copuld perhaps be some other issue with a double sided model.

With out the cullmode statement the model is rendered as single sided so it works, when cullmode is added only then is the model instructed to render double sided, so that's my reasoning.

I'll hit google and see if I can understand this a bit more.

Thanks for such a great script.

Cheers.
Posted By: TheGame

Not working - 02/22/06 09:25

i tried the code with A6 latest demo and even with A6.2 , still it dosen't working, please help me out
Thankyou
© 2024 lite-C Forums