Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / a shader for normals and lights tiuse for example a starship in space.

Author
Message
Ermes
23
Years of Service
User Offline
Joined: 27th May 2003
Location: ITALIA
Posted: 6th Oct 2011 18:28 Edited at: 6th Oct 2011 18:29
hi, back here after some months.
i see i can't write a single shader because i really don't know a single things about them, even when reading some strange named constant, my mind blow up and i'm start thinking why not an upgrade with the best shaders fitted in. Ok after complaining, i'm looking for this:

i'm developing a new space sim game, my starships when not facing sun, are totally dark... but i think lights like windows, or other lights signal must be visible... so there is a shader with normals and light maps working at the same time?

3 texture to use: diffuse, normals (for bumps) and lights.

thanks!

really donwhearted ... shaders for me are how to make a simple thing in a very very hard and diffucult system.....

SYS 64738
Morcilla
23
Years of Service
User Offline
Joined: 1st Dec 2002
Location: Spain
Posted: 8th Oct 2011 19:53
Hey hello there,

Here you have a shader that does exactly what you want:



It is probably not the best code out there but it works. It is based on a regular normal map shader by Green Gandalf (many thanks GG )
To use it, you must take some things into account:

Texture stages are like follow:

0 - diffuse map
1 - normal map
2 - light map

Now the variables, you must feed:

Feed per frame or as the ship moves:
Light direction: float4 lightDir = {-1.0, 0.0, 0.0, 1.0};

Set only once, as needed, or leave as they are, they have default values:
Ambient light: float4 ambiColor = {0.2, 0.2, 0.2, 1.0};
Light color: float4 lightColor = {1.0, 1.0, 1.0, 1.0};
Specularity level: float specLevel = 0.5;
Specularity factor: float specExpon = 10.0;

Improvements could be made by using the normal map alpha channel as the light map source, that would save a texture. Oh well, current way is easier to implement and easier to have the textures changed.

Just remember:

-Load the object (ajem)
-Load the shader
-Load the textures
-Texture each layer of the object
-Apply the shader to the object
-Set the shader variables if needed
-Refresh the needed shader variables once per loop
-Enjoy the coding all along with the results

Good luck!

Ermes
23
Years of Service
User Offline
Joined: 27th May 2003
Location: ITALIA
Posted: 11th Oct 2011 18:20 Edited at: 11th Oct 2011 18:24
thanks... but

what's this?

Quote: "

Now the variables, you must feed:

Feed per frame or as the ship moves:
Light direction: float4 lightDir = {-1.0, 0.0, 0.0, 1.0};

Set only once, as needed, or leave as they are, they have default values:
Ambient light: float4 ambiColor = {0.2, 0.2, 0.2, 1.0};
Light color: float4 lightColor = {1.0, 1.0, 1.0, 1.0};
Specularity level: float specLevel = 0.5;
Specularity factor: float specExpon = 10.0;

"


i think light direction, if is the sun, must be setted just one time, i guess.

and then... how it is calculated light direction? is a vector? x,y, and z? in some shaders light direction is a vector3 but with odd values. many way to make one thing meaning confusion.

how i can feed the shaders with this variables?
i must say things are easy when you know it. otherwise, they will be not easy, but hard.

the only way i know is: set constant "what is the name of the constant" etc etc....

... i'm hopeless with shaders,... ' can't understand how they work, i see what they do, i don't understand how they are writed.

Morcilla, can you redirect me to a shaders collections here on the board??? i searched "shaders" but stops after 100 results without (i remember there was one) a shader srchive post (why not sticky!), i see "shaders" questions are very common here.

thanks friend.

SYS 64738
Ermes
23
Years of Service
User Offline
Joined: 27th May 2003
Location: ITALIA
Posted: 11th Oct 2011 19:29 Edited at: 11th Oct 2011 19:46
Morcilla, can't belive it for sure... i made it! it works!!!!!

now, please just tell me how to set light direction...
and how calculate it: i've light direction from a sure point, the position of the sun. it has x,y and z.

i think is:

Set Effect Constant vector n.effect ,"lightdir", vector4

then how to set the other variables, writed in the same way:

set effect constant (what?) n.effect,"name of the variable (what the right name?)", (what? a float value, a vector3 or a vector4)

usually use shaders with ctrl+c and ctrl+v,

thanks... really a great thank you morcilla!!!! very nice shader, great mod for light map!

SYS 64738
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 11th Oct 2011 20:39 Edited at: 11th Oct 2011 20:40
The shader is expecting a normalised vector for the light direction

Something like:


If the lighting is on the wrong side of the object with the above code, just subtract the light position from the object position instead.

Ermes
23
Years of Service
User Offline
Joined: 27th May 2003
Location: ITALIA
Posted: 11th Oct 2011 23:34 Edited at: 11th Oct 2011 23:37
ahhh get it! thanks!

and for the other values? ambient, specularity...

SYS 64738
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 11th Oct 2011 23:58
It's all the same idea.

Anything that is defined as float4 should be set using a VECTOR4.
Anything that is defined as float should be set using a float.
Anything that is defined as int should be set using an integer.
Anything that is defined as bool should be set using a boolean.

The above aren't fixed rules, as obviously, passing a float to an int will cause the float to be truncated using DBPro's standard type-conversion rules

The only tweakables that you should set from DBPro should have types of float4, float, int or bool.

Ermes
23
Years of Service
User Offline
Joined: 27th May 2003
Location: ITALIA
Posted: 12th Oct 2011 00:09
ok thanks, now all is working! in dbpro and in my mind.

SYS 64738
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 12th Oct 2011 01:18
Quote: "The only tweakables that you should set from DBPro should have types of float4, float, int or bool."


Or matrix?
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 12th Oct 2011 15:05
Oops, yes matrix too, which is a float4x4, although I can't recall ever needing to do that.

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 12th Oct 2011 20:17
Shadow mapping uses it.
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 12th Oct 2011 21:16
... and that's why I can't recall ever needing it

Morcilla
23
Years of Service
User Offline
Joined: 1st Dec 2002
Location: Spain
Posted: 14th Oct 2011 12:35
@Ermes, glad you got it working

Quote: "i think light direction, if is the sun, must be setted just one time, i guess"

Well, looking at IanM's code, you can tell that the light direction must be updated if the spaceship changes its x,y,z coordinates.

@IanM, @GG, thanks for the plug

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 14th Oct 2011 12:50
Oh yes, thanks.

Login to post a reply

Server time is: 2026-07-11 09:48:54
Your offset time is: 2026-07-11 09:48:54