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 / [STICKY] Learning to write Shaders

Author
Message
Irradic
13
Years of Service
User Offline
Joined: 1st Jul 2006
Location:
Posted: 22nd Jan 2007 05:41
I wasn't able to get it to work, i took a look at the shader code but it was just too much for me.
Honestly I don't know much about the different techniques which can be used to achieve the affect. I just remember that one involves render to texture,up sampling and blurring of the image.
The end result wasn't too great though, rather pixelated and it was not a shader solution.
It's good news that you are coming along with your shader. I'm sure you'll get over the little obstacles in the code. I'm pretty excited about the final product.
Have a good one.
Thanks
Green Gandalf
VIP Member
15
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 22nd Jan 2007 19:45 Edited at: 22nd Jan 2007 23:11
@Irradic

I'm glad you sent me that NVIDIA shader. Although I can't use it directly, it has given me an idea of one way to get the effect you want fairly easily. The downside is that I shall have to write the shader from scratch - but I think it will be worth the attempt.

My impression, now that I've had time to digest the code a bit, is that it is intended to be used with intermediate render targets. That cannot be done at present with DBPro - unless you use set camera to image, etc, with different camera masks (as in some of EVOLVED's water shaders). So, I'm going to see if I can use that method to achieve a DOF effect. The basic idea is to to render the scene in two steps: the first render would create the scene as normal - but with the alpha component containing the depth information per pixel (I don't know yet whether that can be done - if it can't the idea falls flat on its face). Next, the result of that render would be copied to an image using set camera to image and another technique in the effect would blur that image. The depth information would be used to vary the amount of blur according to distance.

Anyway, first things first - let's see if the basic idea can be made to work with a simple shader.

Edit: Looks like the alpha level is not copied to the screen image (Lee apparently suggested some time back that he was considering this feature). That's a pain because another camera and image will be needed to store the depth info. Looks do-able though.

@Everyone

While I'm puzzling over Irradic's request, here's another fun shader for you all to play with. It's a variation of my bump mapping shader. It allows the normal map to be both scrolled and distorted over the surface of the object. This can be used to give moving water and a variety of other effects. The demo uses simple bumps on a flat surface. The result is somewhat reminiscent of the creepy scarab beetles that move under the skin of their victims in "The Mummy" films (indeed that effect could be createdmore accurately with a bit of work on the two main textures).

The effect can be seen if you choose option 3 and rotate the object and light a bit.

Attachments

Login to view attachments
valleyman86
13
Years of Service
User Offline
Joined: 15th Dec 2006
Location:
Posted: 22nd Jan 2007 23:24
Is there a shader that moves a texture over an object. I would like to make my object look magical alot like the shader for the enchanted items in morrowind. I dont know I would really like a shader that puts a electricity type effect around the object (precisely to the objects shape as to make it look part of it) that glows a little but can change colors manually. I cant explain it. heh
Green Gandalf
VIP Member
15
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 22nd Jan 2007 23:42
Quote: "Is there a shader that moves a texture over an object."


Yes, plenty. The one I've just posted does exactly that with the normal map - you can modify the shader to scroll the base texture in exactly the same way.

The two lava demos I posted several posts back also scroll over the object.

You could also look at the glow shaders I've posted earlier in this thread - just change the intensity and colour of the glow.
Irradic
13
Years of Service
User Offline
Joined: 1st Jul 2006
Location:
Posted: 23rd Jan 2007 03:36
I have attached you another example, this time from ATI.
Maybe it will come handy as well. Thanks

Attachments

Login to view attachments
Scraggle
Moderator
16
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 23rd Jan 2007 09:32 Edited at: 23rd Jan 2007 09:37
I had a go at combining DBP's bubble shader with Green Gandalfs reflection shader in an attempt to produce what looks like a chrome bubble.

It's a very difficult task when you don't know how a shader file works and my attempts involved cutting and pasting seemingly related parts of each file into a new file. In the blind hope that the end result might actually do something. Similar in many ways to throwing a jigsaw puzzle in the air and hoping that it will all fall in to place when it lands.

Needless to say it failed!

I would really like to have a shader that performed like this but I would like it so much more if I could make it myself. So, some questions to the shader experts:

Is it possible to combine these two shaders?

Would it be quicker and easier to make a new shader from scratch?

Presumably some of the combined code that I have is conflicting and perhaps canceling out any results but I don't know which.

Actually ... the more I sit here composing questions, the more I realise I haven't a clue what I am doing. So, instead of answering my questions, if someone has time would it be possible to take the two shaders and combine the result to produce a reflective bubble for me? Perhaps with comments so that i can try to work out what is happening?

Thank you

*Skulks off to a corner to lick wounds*

Bubble shader:


Green Gandalfs Reflection Shader:


My pathetic attempt at combining them:


Edit I have just re-read my post and realised what I have done ... I have just asked a very noob 'Gimme Teh Codez' question ... sorry



EVOLVED
17
Years of Service
User Offline
Joined: 9th Feb 2003
Location: unknown
Posted: 23rd Jan 2007 11:13
@Irradic

heres my attempt at a depth of field shader > http://www.vector3r.com/DepthOfField.zip , but trust me it'll be such a pain in the ass to get working in game unless you have some shader knowledge.

It also requires this some where in every shader you use to get the pixel depth of your scene. so every object needs to be shaded(including particles) = pain too.



Conclusion , to much of a pain to look right and to bigger hit on performance.

Attached a screen shot>>

Attachments

Login to view attachments
Green Gandalf
VIP Member
15
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 23rd Jan 2007 11:24 Edited at: 23rd Jan 2007 15:36
@EVOLVED

Quote: "but trust me it'll be such a pain in the ass to get working in game unless you have some shader knowledge.

It also requires this some where in every shader you use to get the pixel depth of your scene. so every object needs to be shaded(including particles) = pain too.
"


I'd agree with that - except that it seems to be a pain even WITH some shader knowledge.

I'm going to keep trying because my preliminary attempts look promising even if slow on my machine - some people have far faster machines than me.

But the real pain is your comment "so every object needs to be shaded" which was the conclusion I was coming to - very annoying if you want normal mapping on some objects, for example. Each shader would need to combine several different effects = big performance hit plus a lot of extra programming.

@Scraggle

I'll try to come to your rescue later (got to go to work now).

Edit: Some hints for now...

1. I don't think you need the "Environment" technique - that was just the way I did the skybox in the demo, so you can delete that technique and the shaders called by it.

2. You probably don't need the pixel shader from the "Bubble" shader - sounds like you want my reflection imaging on the bubble shape, i.e. the vertex shader from "Bubble" and pixel shader from "GG Reflection".

3. The "Bubble" vertex shader will need to calculate any quantities needed by my pixel shader - and you can delete anything NOT needed by my pixel shader (EXCEPT the output position - which is mandatory).

You should end up with a single technique with a single pass using two textures - the "environment" cube map and the object's base texture (I'm assuming you have a cube map to hand ).

The way to understand what you are trying to do is to think of the vertex shader as defining the overall shape of your object by calculating the vertex positions and normals, and the pixel shader as defining what the textures and lighting look like by processing various image info.

The vertex shader can also be used to improve performance by calculating some quantities at the vertex level rather than the pixel level (there are usually far fewer vertices than pixels when you render an object) - the pixel shader will linearly interpolate such quantities before using them (a pixel will be located within a poly of three vertices on screen).

EVOLVED's DOF shader illustrates how a pixel shader can be used as a calculator/computer: just code your calculations as colors in the pixel shader, render the result to the screen, use a DBP camera to grab the image and Hey Presto! you've used your graphics card as a calculator with the results stored as bytes in an image.

Anyway, back to work now. Good luck and let me know how you get on.
Irradic
13
Years of Service
User Offline
Joined: 1st Jul 2006
Location:
Posted: 23rd Jan 2007 17:35
@Evolved

I can only say WOW ! This is incredible ! It automaticaly focuses on what's nearest to the camera. I would have been satisfied with something that doesn't auto focus but blurs everything behind a given distance. I can imagine it will be difficult to implement,
but the effect is surely worth a try.
Thanks !
Green Gandalf
VIP Member
15
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 23rd Jan 2007 18:49 Edited at: 23rd Jan 2007 21:24
Quote: "It automaticaly focuses on what's nearest to the camera"


A mixed blessing surely? If a butterfly wanders into your scene between you and a monster you are trying to shoot, you wouldn't want your target to go out of focus, would you? You might prefer to control the focussing depth yourself - but I'm sure EVOLVED can fix that easily.

Looking forward to trying it myself when I get home.

Edit: Looks good. But 5 fps?? Something to aim for perhaps?
Darksoft
17
Years of Service
User Offline
Joined: 17th Sep 2002
Location: United States
Posted: 23rd Jan 2007 23:01 Edited at: 23rd Jan 2007 23:14
Hey guys,

I just started learning shaders and figured I'd try to do an inverting shader and desaturation. I'm all set with those, but now I'm trying to do Hue changing. I read the article on wikipedia on HSV (or HSB):
http://en.wikipedia.org/wiki/HSV_color_space

I found some sample functions from google. I tried converting them into shader form and they run, but they are not working as intended.

Here is the Hue shader:


In DB I just load an object texture it and do a wrapvalue for 'hue#' and pass it back to the shader via 'set effect constant float'

Its going from yellow to green and then back and forth again. Any ideas how to fix it? Maybe I didn't convert the sample functions properly? This is where I got them:
http://www.cs.rit.edu/~ncs/color/t_convert.html

Here is the sample DBPro code:


See attached for the output (the blue.png is just a rgb(0,0,255) png) its yellow and fades to green and then BLAM back to yellow

Thanks a ton!


Check out my new website - http://www.madcents.com/

Attachments

Login to view attachments
Green Gandalf
VIP Member
15
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 23rd Jan 2007 23:50
@Darksoft

It would be easier if you told us what it's supposed to do - you've only told us what it actually does.

Then we might be able to help.
Darksoft
17
Years of Service
User Offline
Joined: 17th Sep 2002
Location: United States
Posted: 23rd Jan 2007 23:56 Edited at: 24th Jan 2007 00:02
True!

If you hold down shiftkey it should wrapvalue from 0-360 (H of HSV) basically it should switch between hues of each pixel for the full 0-360 range. I hope that makes sense!

It should go from like green to yellow to red to purple to blue smoothly.

Thanks!


Check out my new website - http://www.madcents.com/
Green Gandalf
VIP Member
15
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 24th Jan 2007 00:38
Quote: "It should go from like green to yellow to red to purple to blue smoothly."


That explains a lot, thanks. Will think about it.
Darksoft
17
Years of Service
User Offline
Joined: 17th Sep 2002
Location: United States
Posted: 24th Jan 2007 00:48
You are the man Green Gandalf!


Check out my new website - http://www.madcents.com/
Irradic
13
Years of Service
User Offline
Joined: 1st Jul 2006
Location:
Posted: 24th Jan 2007 06:31
@Green Gandalf

Quote: "If a butterfly wanders into your scene between you and a monster you are trying to shoot, you wouldn't want your target to go out of focus, would you? "

Haha, you are quiet right about that. Our game is in 3rd person view, the camera follows behind the character. In our case a blur that moves with the camera would be sufficient. On the other hand i think in reality only a person with a eye sight defect (near-sighted) would see his surrounding in such a way. Like Eveolved said, it's difficult to achieve a realistic effect.
Are you still working on your version ?
Green Gandalf
VIP Member
15
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 24th Jan 2007 12:02
Quote: "Are you still working on your version ?"


Yes, but not much time to do anything till tomorrow. Will probably look at Darksoft's query today (I think I can reduce his shader to about 2 lines of code and get the effect he wants) and also Scraggle's humble request if he hasn't already sorted it out. Both of those should be quick to deal with.

For your DOF thing, I seem to be moving towards EVOLVED's approach, some details will be different though (I haven't studied his code yet - I'll learn more if I try to get it working myself first). So far, I've got the cameras and images set up correctly with a shader - just need the shaders themselves to do the right thing with the images. I'd like to beat EVOLVED's fps somehow - but I expect to fail on that.

Anyway, off to work now.
Peter H
16
Years of Service
User Offline
Joined: 20th Feb 2004
Location: Witness Protection Program
Posted: 24th Jan 2007 14:25 Edited at: 24th Jan 2007 21:11
Quote: "On the other hand i think in reality only a person with a eye sight defect (near-sighted) would see his surrounding in such a way."

i'm kind of jumping in at the middle (so i may be misunderstanding) but being pretty near-sighted i can tell you that there isn't motion blur, everything is just blurry to start with... (i wear contacts everyday though so it's been awhile )

One man, one lawnmower, plenty of angry groundhogs.
Darksoft
17
Years of Service
User Offline
Joined: 17th Sep 2002
Location: United States
Posted: 24th Jan 2007 16:37
Gandalf, just in case I got you off on the wrong track.

Quote: "I think I can reduce his shader to about 2 lines of code and get the effect he wants"

I do want to be able to do a HSV change not just go from full color green to yellow to red to purple to blue and I need to take the pixel that I started with and slowly convert it as a smooth effect. Blacks stay black, grayscales stay grayscale, but colors can switch anywhere in the spectrum with a 0-360 change in hue.

Thank you for helping me, since its my second shader, I have no idea how to debug them (can you output text onto the texture so you know whats going on?)


Check out my new website - http://www.madcents.com/
Green Gandalf
VIP Member
15
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 24th Jan 2007 18:31 Edited at: 24th Jan 2007 21:02
Quote: "but colors can switch anywhere in the spectrum with a 0-360 change in hue"


You can't do that with only a single variable changing - you can only change one dimension such as "hue", "saturation", "brightness", "red", etc. However, I think I know what you want now.

I've already written a one line pixel shader which cycles through the colour sequence red to yellow to green to cyan to blue to magenta and back to red and I'll post it later when I can test it.

But that assumes the s and v components are fixed - I assume you want to do the same thing using the s and v values from your texture. I'll see if I can do that in just one or two more lines.

Edit:

@Darksoft

Try this in place of your shader - it took 3 more lines (which could be condensed to one if I wanted a long line.

Darksoft
17
Years of Service
User Offline
Joined: 17th Sep 2002
Location: United States
Posted: 24th Jan 2007 22:21 Edited at: 24th Jan 2007 22:57
Green Gandalf:

Thank you that is heading in the right direction. However, What I need to do is take the current hue of the pixel (from the original texture) so you do a conversion in the shader from rgb to hsv (take the hue element and add whatever amount to it) to switch hue of the colors that already exist in the texture.

For instance if say a texture I have has a blue circle in the center and surrounded with red, and say on the 0-360 red = 0, and opposite is (example only) blue which equals 180. if the surrounding pixels would be blue at 180 (the 180 is the only thing needed to pass to the shader. So in the end the center circle is red and the surrounding pixels are now blue at 180.

In that way, all the colors change hues correctly and if I went back to 0 it would equal the original texture 100% (blue circle surrounded by green). If I change the hue to 180 then all the colors would be the opposite color in the hsv spectrum.

I hope I make sense. Thank you again for your generous help!

Here is a pic of what I'm trying to describe:



Also in photoshop here is basically what I want to be able to control for a texture (0-360 slider bar):



Check out my new website - http://www.madcents.com/
Darksoft
17
Years of Service
User Offline
Joined: 17th Sep 2002
Location: United States
Posted: 24th Jan 2007 23:05 Edited at: 24th Jan 2007 23:08
YAY!!

I think I got it! Of course I used Green Gandalf's code and modded it for my liking:



Note:
I know I don't need to calculate the s and v in the function I made since I think Gandalf's does that already.


Check out my new website - http://www.madcents.com/
Green Gandalf
VIP Member
15
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 24th Jan 2007 23:10 Edited at: 24th Jan 2007 23:14
[Edit: Posted following before your last post appeared! Looks like you sorted it out. What I forgot to do was to calculate the starting point in the sequence for each pixel in the texture. Well done.]

Yes, I know. That is what it was supposed to do. It seems there's a bug in my shader.

Inadequate testing, I'm afraid. Sorry. I'll have another look.
Darksoft
17
Years of Service
User Offline
Joined: 17th Sep 2002
Location: United States
Posted: 24th Jan 2007 23:22 Edited at: 24th Jan 2007 23:41
Gandalf, I'm trying to play with it more, but to be honest I'm confused at your out.col line, what is what and why does that work? I would like to be able to change s and v offsets passed into the shader as well so that I may desaturate with the same shader. On that note, how would I go about doing that? I wrote one that can either desaturate 100% or not using the Grayval = 0.3R + 0.59G + 0.11B, but I would like this current one to be able to be able to do both and smoothly go from saturated to desaturated... on the wiki article it says that "define desaturation as the qualitative inverse of saturation" which somehow goes right over my head in a puff of love.

I hope I'm not being a pain!

Thanks again, good work


EDIT: I've changed the 'scale' float4 to drag down the saturation, but It cycles strangly down to desaturated:



Check out my new website - http://www.madcents.com/
Green Gandalf
VIP Member
15
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 25th Jan 2007 01:37
Just a quick comment.

You asked in your code:
Quote: "0.00277778 purposefully put this comment so it will error here -- what is the last number?, am i using the scale wrong? I'm confused! it does make it grayscale though if i set the r g and b to 0 and leave 00277778 there...
"


My code assumes the cycle repeats when the "hue" variable reaches 1 - but you wanted it to repeat at a value of 360, so all I've done is rescale it by 1/360 = 0.00277778 (approximately). The offsets of 0, 0.333333 and 0.6666667 should be untouched because they are needed to ensure the hue rgb components cycle in the right way - I'm not surprised you had problems when you scaled them all down.

The main part of the code for Out.Col is just reproducing the cycle shape shown in the top right of your wikipedia page reference. I deduced it from the following sequence of rgb changes as you go through red to yellow to green to cyan to blue to magenta to red: i.e. (r,g,b) goes through (1,0,0)[red] to (1,1,0)[yellow] to (0,1,0) to (0,1,1) to (0,0,1) to (1,0,1)[magenta] and finally back to (1,0,0)[red]. If you study this sequence you will notice two things:

1. Each colour component cycles through the same sequence of 1's and 0's - but starting at different positions in the sequence.

2. Only one colour component is changing at a time.

3. The transitions between colours follow the sequence: one unchanged at 1, followed by one reduction from 1 to zero, followed by two staying at zero, followed by one increasing from zero to 1 again, and finally one staying at 1 again.

That was three things, sorry! Counting is tricky this time of night.
Darksoft
17
Years of Service
User Offline
Joined: 17th Sep 2002
Location: United States
Posted: 25th Jan 2007 04:26 Edited at: 25th Jan 2007 04:31
Green Gandalf,

Thank you for explaining your code and helping me. The output that I have gotten is rather interesting (desaturation), I kind of like it for a weird effect. I'll have to study the code a bit more to fully grasp what is going on i guess.

I don't follow this code:


first off, I don't know where or what 'saturate' is in the shader, how does that as a function work?

scale.a + scale <-- what does that do? I'm confused I think because scale.a = 0 right? Also what does it do when you add scale when it has four floats in it?

Also very confusing -.5 * 6.0 -1.0 ? lol Maybe its late for my brain too.


Thanks again


Check out my new website - http://www.madcents.com/
Green Gandalf
VIP Member
15
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 25th Jan 2007 13:32 Edited at: 25th Jan 2007 13:34
Ok. I've packed a lot into that one line of code. What I have done is to apply a complicated function to each colour component simultaneously.

The command uses three functions: frac, abs and saturate. The first just returns the fractional part of a number (so you get a value between 0 and 1), the second just returns the absolute value (i.e. removes a negative sign if there is one), the third clamps its argument to the range 0 to 1 (so -0.1 becomes 0, 0.23 stays as it is, and 2.67 becomes 1). The saturate function is often used to form an intermediate colour value - because they are restricted to the range 0 to 1 in shaders (that was not the reason I used it here though).

What the line of code is doing is to build up the shape of the cycle in stages (ignore the scale parts for the moment):

1. first get a function which repeats over a cycle of length 1 (a straight line starting at 0 and increasing to 1 (it helps to draw a plot of the function) as its argument increases from 0 to 1, or from 1 to 2, or from 2 to 3 and so on;

2. next subtract 0.5 from this value - this gives a function whose values start at -0.5 and finish at 0.5 as its argument increases from 0 to 1, etc;

3. next take the absolute value, this gives a "sawtooth" shaped function which starts at 0.5, drops to 0 in the middle of the interval 0 to 1 and then increases to 0.5 again at the end of the interval - this then repeats;

4. this function is then multiplied by 6 so the range is 0 to 3 rather than 0 to 0.5;

5. next the function is reduced by 1 so that it is symmetric around the value 0.5 - it will now range from -1 to 2 in a repeating sawtooth manner;

6. the final step is to cut off the top and bottom of the sawtooth shape using the saturate function - the result is a function which has the shape shown on the Wikipedia page you mentioned, i.e. it starts at 1, stays there for a bit, then drops to 0, stays there for a bit, then increases to 1 again, and stays there for a bit till the argument finishes the cycle at 1, this then repeats;

7. the scale part does two things, it converts the interval length from 1 unit to 360 units using the value scale.a (which is 1/360 - in shaders the colour components are in the order RGBA) and shifts each colour component by a fixed amount along the repeating cycle(look at the Wikipedia diagram again)

8. note that the innermost part of the code (the bit that gets evaluated first) is the expression


this is a float added to a float4, what this does is add the float to each component of the float4 so the result is a float4, i.e. we have processed all four colour components simultaneously (we don't need the fourth, alpha, component on this occasion but it's calculated anyway), the other functions are then applied to the resulting float4, i.e. to each component - it is generally more efficient to do calculations on a vector in a shader rather than write separate lines of code.

Well, you did ask.

It took far longer for me to try to explain this than it did to actually work out and write the code.

I haven't mentioned the very final step (the range and lower bits): these scale the final value in a way which is supposed to give the correct S and V values for the original image input value.

Phew! Time for another coffee.

[Edited silly typo.]
Green Gandalf
VIP Member
15
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 25th Jan 2007 18:42 Edited at: 25th Jan 2007 21:08
@Scraggle

I've combined the wobble and "reflection" shaders for you. See attached zip file for demo and new FX file.

Edit: replaced zip file with "improved" version.

Attachments

Login to view attachments
Scraggle
Moderator
16
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 25th Jan 2007 20:12
Thank you

Is there some code within the fx file that is positioning the object?
I tried to put it in my game but any object with the shader applied to it it was positioned at 0,0,0.

I modified the example code that you provided so that using the arrowkeys will move the object but instead it seems using the arrowkeys scrolls the texture.

Any ideas?



Green Gandalf
VIP Member
15
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 25th Jan 2007 20:54 Edited at: 25th Jan 2007 21:12
Quote: "Is there some code within the fx file that is positioning the object?"


Dammit! That was one of the things I didn't check. It's probably a hangover from the DBP bubble shader. I'll see if there's a quick fix. Supper is imminent so it might be a while.

Edit: No, I tell a lie - it was my rushed changes that were at fault. I've now replaced the zip file a couple of posts back. I've changed the DBP file so you can move the object backwards and forwards with the b and f keys, and corrected the FX file (several errors there I'm ashamed to say).

These changes were even more rushed - supper is being served behind me as I type, so don't be surprised if there are more bugs.

Let me know if there are still problems.
Scraggle
Moderator
16
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 25th Jan 2007 23:01
That's fantastic! Thank you so much

I have put images of the Concentric world into the cube map and it looks pretty impressive.

Thank you



Alfa x
13
Years of Service
User Offline
Joined: 1st Jul 2006
Location: Colombia
Posted: 27th Jan 2007 03:54
Hi, I have a question,
all media that comes with fx composer is license free?
Green Gandalf
VIP Member
15
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 27th Jan 2007 11:22
Quote: "all media that comes with fx composer is license free?"


Best to assume "No" - unless the FX Composer license.pdf file says otherwise. Read it.
Syncaidius
15
Years of Service
User Offline
Joined: 22nd Mar 2005
Location: United Kingdom
Posted: 27th Jan 2007 20:26
@Green Gandalf:
Is it okay to use any of the shaders you've created for other people on this thread?

Im assuming yes, but my assumption may very well be wrong.

dononeton
15
Years of Service
User Offline
Joined: 12th Jun 2004
Location: Tusaloosa, AL : USA
Posted: 27th Jan 2007 20:57

I am using this code to start learning with. How do you add vertex lighting to this? I guess its something like

then add this to myPass


only if it was that easy
Green Gandalf
VIP Member
15
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 27th Jan 2007 21:07 Edited at: 27th Jan 2007 21:30
Quote: "Is it okay to use any of the shaders you've created for other people on this thread?

Im assuming yes, but my assumption may very well be wrong.
"


Yes you may.

@dononeton

I don't think your code will work as it stands. Will amend this post with some changes later.

Edit:
The pixel shader works fine as it is. But the vertex shader doesn't. As a minimum, the vertex shader must output the object's vertex position in screen coordinates. This usually needs to be calculated by multiplying the object's vertex coordinate by a matrix called WorldViewProjection. Once you've done that you can then add other things to adjust lighting, etc. The following code shows how your example might look (before the extra bits for lighting are added):



I tested that in FX Composer by the way.

I've got to cook supper now - but I'll add some simple changes for lighting later for you.
dononeton
15
Years of Service
User Offline
Joined: 12th Jun 2004
Location: Tusaloosa, AL : USA
Posted: 27th Jan 2007 22:16
Thanks Green Gandalf. I am using FX Composer also. I have a few idea's for some shaders but I want to learn some of the basics first. I know about float,float2,float3,float4,pass. I guess thats about it
Green Gandalf
VIP Member
15
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 28th Jan 2007 00:45
@dononeton

Here's a simple example of vertex colouring based on the previous shader - it's not really using lighting as such (that requires a bit more work involving light directions, etc).

Note that I've illustrated two different ways of passing information to and from the two shaders: I've used structures for the vertex shader, and individual variables for the pixel shader. I usually use structures because I find the code easier to follow that way.

Alfa x
13
Years of Service
User Offline
Joined: 1st Jul 2006
Location: Colombia
Posted: 29th Jan 2007 12:47
Thanks GG.
I will check out.
I will use your shaders for sure.

Alfa x
13
Years of Service
User Offline
Joined: 1st Jul 2006
Location: Colombia
Posted: 29th Jan 2007 13:37 Edited at: 29th Jan 2007 13:37
Hi,

this is what the license says:

Quote: "NVIDIA hereby grants to Developer a royalty-free, non-exclusive license to possess and to use the
Materials for both commercial and non-commercial purposes but only in connection with NVIDIA products. Developer
agrees not distribute the Materials or any derivative works created therewith without the express written permission of
an authorized NVIDIA officer or employee."


Quote: "Source Code: Developer shall have the right to modify and create derivative works with the Source Code.
Developer shall own any derivative works (“Derivatives”) it creates to the Source Code, provided that Developer
uses the Materials in accordance with the terms of this Agreement. Developer may distribute the Derivatives,
provided that all NVIDIA copyright notices and trademarks are used properly and the Derivatives include the
following statement: “This software contains source code provided by NVIDIA Corporation.”"



Quote: "Object Code: Developer agrees not to disassemble, decompile or reverse engineer the Object Code versions of any
of the Materials. Developer acknowledges that certain of the Materials provided in Object Code version may
contain third party components that may be subject to restrictions, and expressly agrees not to attempt to modify or
distribute such Materials without first receiving consent from NVIDIA."


To myself is still unclear, if I can use them in my game or not.
But I think yes...
what do you think?.
what is object code and source code?
dononeton
15
Years of Service
User Offline
Joined: 12th Jun 2004
Location: Tusaloosa, AL : USA
Posted: 30th Jan 2007 01:18
Thanks Green Gandalf
That helped. What kinda of data can shaders take, and what are they used for?
Alfa x
13
Years of Service
User Offline
Joined: 1st Jul 2006
Location: Colombia
Posted: 30th Jan 2007 01:33
Hi Green Gandalf.
I just learning shaders.
I was studing your glow shader and have some questions.

1) what this piece of code does?



2) Do you know where can i study all that commands?. There are so many things and I don't know where to start studyng some basic concepts.

3) I have been reading all the post. Thank you for all your support, it has been of great help.
Green Gandalf
VIP Member
15
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 31st Jan 2007 23:34
@Alfa x

You asked:

Quote: "1) what this piece of code does?"





I'll try to explain a bit of background stuff first.

When you look up a texture in a shader you use a command like the following in the pixel shader



The "tex2D" is an HLSL instruction which looks up a 2D texture that's been applied to your object in DBPro - this texture will usually be something like a standard BMP or PNG image, etc. In order to do the look-up the shader needs to know three things: which UV coords to use in the texture, which texture and, finally, how to match "texels" in the texture to the "pixels" on the screen.

The UV coords are provided by the variable "In.UV" - which, in turn, is usually provided to the pixel shader by the vertex shader. The vertex shader calculates these for each vertex - but the pixel shader needs these for each pixel on the screen. Imagine an object's poly (a triangle) being projected onto the screen. The vertex shader gives the required UV coords of the triangle's three vertices -and these are "linearly interpolated" for the pixel shader so it can deduce what the UV coords are for pixels inside the triangle on the screen (it's a sort of averaging process really). It's those interpolated values that get passed to the pixel shader variable "In.UV" in my example.

The other two things - the texture and the texel/pixel matching - are controlled by a "sampler" - the thing called "baseSample" in our example. The sampler specifies which texture to use - called "baseTexture" in your example. This texture must have a declaration in the shader somewhere - this declaration will tell the shader things like what sort of texture it is, i.e. a cube map, volume map, standard texture, etc.

The other lines in the sampler declaration, i.e. "ADDRESSU = CLAMP;", etc, tell the sampler how to process the "texels" in the texture.

The "ADDRESSU" and "ADDRESSV" tell the shader what to do at the edges of the texture. The main texture corresponds to UV values in the range 0 to 1. But, for various reasons, you might supply coordinates outside that range. The value "CLAMP" tells the shader to continue using the colour found at the appropriate edge of the texture. Other values are "WRAP" and "MIRROR" (there may be others) - whose purpose I hope you can guess. In my scrolling lava shaders, for example, you will probably see something like "ADDRESSU=WRAP" in the sampler. This only makes sense for textures which are intended to wrap seamlessly - and these usually need to be specially prepared to look convincing.

Now comes the tricky bit - the last three lines in your example, i.e. "MIPFILTER = LINEAR;", etc.

I'll try to give a rough idea - or at least my understanding of them . So, apologies in advance if some details are wrong.

When an object is textured by DBP I believe it prepares some low resolution versions (MIPMAPS) of the texture. This enables the processor to work more efficiently when objects are a long way from your camera in the 3D scene. For example, imagine an object so far away that it can only fill 2 or 3 pixels on the screen. It would be pointless asking the pixel shader to look-up points in a massive 1024x1024 image that you might have used as your texture. At that distance you probably only need an average of all the pixels - that would be prepared beforehand by DBPro as one of the "MIPMAP" levels. Similarly, as the object gets nearer the camera, you will need a bit more detail. This is done by preparing image resolutions in increasing powers of two, i.e. 1x1, 2x2, 4x4, 8x8, etc (I don't know the exact sequence that's used).

But there's a problem. What do you do when you switch from one MIPMAP image to the next? To avoid an abrupt change in the quality of your image you can the shader to do the transition gradually - the most common way is to specify "MIPFILTER = LINEAR;" as in your example. You can also use "POINT" which uses the nearest MIPMAP (I think) - this will give abrupt changes in quality which might be desirable in some situations. There are other more advanced choices.

The last two lines, e.g. "MINFILTER = LINEAR;", serve a similar purpose - i.e. what to do when the screen pixels fit inside the object's "texels" (i.e the object is very close) or vice versa (i.e. the object is distant). Do you want to take the nearest "texel" to a screen "pixel" - or do you want to take some sort of average (because, for example, your screen pixel falls near the boundary between neighbouring "texels". You specify the first option by using "POINT" and the second by using "LINEAR".

You will usually want "LINEAR" - but not always. I have a screensaver on my PC which has floating balls with text written on them. A friend once asked me why the text looked blurred and out of focus - that was when I discovered the usefulness of the option "POINT" and the purpose of the DBP "SET OBJECT FILTER" command.

You also asked:

Quote: "2) Do you know where can i study all that commands?. There are so many things and I don't know where to start studyng some basic concepts."


I find it very difficult to obtain these sort of details. Some things are explained in the Ultimate Shader Thread 2 on the "Old Stickies" thread in the main DBP list. Other things you can find if you search Google for things like "HLSL Tutorials". The microsoft developer website contains lots of useful info - but you usually need to know the answer before you can find what you want.

You can download the DirectX SDK and that contains lots of useful documentation - but again it's hard work finding the details you want.

I ended up getting hold of some good books. Three I've found useful are:

"Microsoft DirectX 9 Programmable Graphics Pipeline" by Kris Gray.

"Programming Vertex and Pixel Shaders" by Wolfgang Engel.

"Shaders for Game Programmers and Artists" by Sebastien St-Laurent.

I think all three books are rather heavy going for a beginner - but if you have a good computing background they will certainly help - and the last two have lots of nice examples plus CD.

Unfortunately the examples in all three books usually need a bit of modification before they can be used in DBP.

I'd suggest looking on the internet via Google first.

Hope all this helps.

It was only seven lines of code - I'm glad it wasn't a whole shader!!
Alfa x
13
Years of Service
User Offline
Joined: 1st Jul 2006
Location: Colombia
Posted: 1st Feb 2007 18:03 Edited at: 1st Feb 2007 18:17
Hey man,
you are the best .
This information will be very usuful.
You are very detailed and that is good for people like me, that
don't know so much about shaders.
With your help, i discovered there is still a long way to learn
shaders.

In the other hand,what do you think about the license stuff i posted from nvidia three post above?.

Thank you so much.
Green Gandalf
VIP Member
15
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 1st Feb 2007 20:16
@Alfa x

In your post about the nVidia license you said:

Quote: "To myself is still unclear, if I can use them in my game or not."


I agree I'm afraid - especially the sentence:

Quote: "NVIDIA hereby grants to Developer a royalty-free, non-exclusive license to possess and to use the
Materials for both commercial and non-commercial purposes but only in connection with NVIDIA products"


What does "in connection with" mean? OK if you sell it to people for use with nVidia GFX cards? Or only for use with FX Composer or what?

The rest is fairly clear, I think.

You also asked:

Quote: "what is object code and source code? "


"Source code" usually refers to the code that a programmer writes like your DarkBASIC *.dba files or shader *.fx files. So, I think it means that you are free to use one of the FX Composer example FX files and adapt it for your own purposes as long as you include the phrase "This software contains source code provided by NVIDIA Corporation." somewhere in the file. Obviously this is a "grey" area - where do you draw the line? Do you really need to include that statement just because your FX file happens to include the snippet "WorldViewProj : WorldViewProjection"? I rather doubt it - most shaders written by anybody would contain something either identical to or similar to that.

"Object code" usually refers to executables such as *.exe files (and might include *.dll files). For example, the license is specifically prohibiting you from studying the code in the FX Composer program file "FXComposer.exe" itself. This is a reasonable request and shouldn't stop you developing your own games, etc, unless you are trying to do something very dodgy.

@dononeton

You asked:

Quote: "What kinda of data can shaders take, and what are they used for?"


That's a BIG question.

The most important data provided to shaders are the object and texture information. In DBP these are provided by DBP itself when you texture an object and use, for example, the "set object effect" command.

Textures usually contain image data - but can be used as look-up tables for functions that might take some time to calculate in a shader. Remember, as far as a computer is concerned images are just numerically coded information - it is up to the programmer to provide and interpret those numbers.

DBPro will also provide other data in the form of vectors and matrices telling the shader the camera's and object's position or orientation in your scene.

You can also pass information such as real numbers and vectors (i.e. "float" and "float4" in shader jargon). What these numbers do depends entirely on the programmer. For example, a float might contain the intensity of a light, a float4 might represent a light colour or position which is needed in the shader calculations. In DBPro you pass these values using commands such as "set effect constant float" and "set effect constant vector".
Alfa x
13
Years of Service
User Offline
Joined: 1st Jul 2006
Location: Colombia
Posted: 2nd Feb 2007 15:40 Edited at: 2nd Feb 2007 15:49
I really like people in this forum.
your answers are very usuful,
Once again,
thanks.
Alfa x
13
Years of Service
User Offline
Joined: 1st Jul 2006
Location: Colombia
Posted: 2nd Feb 2007 15:41 Edited at: 2nd Feb 2007 15:43
About the license...
I think we need someone else that tells us the answer
Green Gandalf
VIP Member
15
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 12th Feb 2007 12:05
@Irradic (and anyone else interested)

A couple of weeks back you asked whether I was still working on a Depth of Field shader. I've tried a few methods. The fastest achieved an FPS of about 20-30 compared to about 5 for the method used by Evolved's shader - but, really, the visual effect was not very convincing so I've put that to one side for the moment.

However, on the plus side, I realised the same idea could be used to create dynamic mist, fog or smoke and I've posted a shader plus demo of that idea on the main DBPro board.

It achieves an FPS of about 30 on my machine.
Chris K
16
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 1st Mar 2007 14:10
Hello hello!

Been completely busy for the last few months (Job? What is this madness?), but got nothing to do for the next month, so going to be trying to get some beautiful shaders written and working in DBP...

I'll also tidy up and release the volumetric cloud one that's been gathering dust in my Projects folder.

I'll also try and get some shadow mapping or something going...
Got some ideas in this department that I'm desperate to try out.

And then last thing I want to try is hair.

-= Out here in the fields, I fight for my meals =-
Green Gandalf
VIP Member
15
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 1st Mar 2007 19:12
Welcome back!

I was wondering who was going to mess up that nice neat 400 posts on this thread.

... but I guess it is your thread, after all.

Login to post a reply

Server time is: 2020-05-28 23:03:42
Your offset time is: 2020-05-28 23:03:42