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
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 20th Mar 2011 01:53 Edited at: 20th Mar 2011 02:38
Quote: "Still not sure about the tangent space problem."


I think I've solved that problem now - well partially at least. My original version forgot to renormalize the XY components of the vector2 used to offset the UV coordinates. The basic idea of the tangent space method is to move a fixed distance in UV space in each screen frame, i.e. the length of the offset "scroll * downDir" as used in the new version should be constant for all directions. With that correction the original idea looks a lot better on spheres - but only if you rotate the sphere around its Y axis. There's still a problem if you rotate the sphere around the X or Z axes.

If you use the objects' UV coordinates as in this demo then you are more or less bound to get problems near the poles of the sphere which you'll see if you rotate around the X axis. What I'm not quite sure about is why there are problems when you rotate around the Z axis but not the Y axis. I suspect it's something to do with the scrolling getting "out of step" as you cross a poly boundary but I'm still not sure. It's possible I need to change a sign somewhere. Something to think about.

It still seems that the other approach that I posted yesterday is better.

Edit!!

Just realised what the main problem is. Nothing to do with DBPro or shader bugs or smoothing along poly edges. It's a simple logical error in my reasoning.

The method I was trying to use is fundamentally flawed and the reason becomes obvious when you mark out the spheres polys on to a (0,1) x (0,1) unit square. Imagine rotating the sphere about 70 degrees around its Z axis and then mark each poly with the sphere's down direction. Note that the direction will be different for each poly. You'll then see that moving each poly a fixed amount in that direction results in the texture "tearing". I can't see any way around this so I don't see much choice other than something like the triplanar method I posted on the 18th. I've deleted the new download as it doesn't achieve anything.
pictionaryjr
15
Years of Service
User Offline
Joined: 12th Mar 2009
Location:
Posted: 8th Apr 2011 08:49
I've got a few questions regarding the fact that I have never used a shader before.

1)Whats the best way to develop shaders for DBP. I looked up FX Composer but it appears it is no longer supported.
2)What are the benefits of shaders
3)How do you implement them into DBP

I know these are broad just random generalizations, but i read the first 2 pages of this thread and didn't find anything usable really. Thank you ahead of time for the help

nruser
16
Years of Service
User Offline
Joined: 22nd Dec 2007
Location: Serbia
Posted: 9th Apr 2011 03:53
i have been looking on shaders in ultimate shader pack and i decided i want to use evolved's shadow mapping, normal mapping sm1.4 and cube lighting sm1.4, but there is a problem because every shader has its own normal mapping technique, what i wanted is to use shadow mapping shader just for shadow projection, normal mapping shader just for that so i can use it with any other shader, and cubic lighting just for that purpose. Does anyone know how to achive this?
Edson
13
Years of Service
User Offline
Joined: 21st Dec 2010
Location: Brasil
Posted: 16th May 2011 17:18
how to make a file for comand:

cheate pixel shader from file

My project is Phenadlax!!!
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 17th May 2011 19:59
You can use a text editor. Vertex and pixel shader files as used in that and its sister command are usually saved with the extenders .vsh and .psh respectively. There are two simple examples in the Dark Basic Professional/Help/Examples/Basic3D folder.

However, hardly anyone uses those commands and file types - and they seem to require the shader to be written in asm rather than HLSL. I suggest you use the ".fx" file type which can be created using a text editor or special purpose shader software. "Fx" files are loaded using commands like load effect and load camera effect. There are many examples scattered around this thread.
Edson
13
Years of Service
User Offline
Joined: 21st Dec 2010
Location: Brasil
Posted: 21st May 2011 15:36
Someone uses the nvidia fx composer?

I've been using. But some in darkbasic Do not Work!

What do I do?

My project is Phenadlax!!!
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 21st May 2011 16:15
Quote: "What do I do?"


A specific question and example would be a good start.
Edson
13
Years of Service
User Offline
Joined: 21st Dec 2010
Location: Brasil
Posted: 22nd May 2011 02:40
The darkbasic execute all effects of nvidia fx composer?

My project is Phenadlax!!!
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 22nd May 2011 13:24
I doubt it. Give an example that you're having trouble with.
Edson
13
Years of Service
User Offline
Joined: 21st Dec 2010
Location: Brasil
Posted: 22nd May 2011 20:50
This shader does not work on dark basic.

it creates the image effect.

My project is Phenadlax!!!

Attachments

Login to view attachments
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 23rd May 2011 02:22 Edited at: 23rd May 2011 02:23
Yes it does:

Attachments

Login to view attachments
Edson
13
Years of Service
User Offline
Joined: 21st Dec 2010
Location: Brasil
Posted: 23rd May 2011 13:46
but, not function in my project! how to make it?

You can send me your code?

Darkbasic suport directx10?

My project is Phenadlax!!!
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 23rd May 2011 14:41 Edited at: 23rd May 2011 14:44
Quote: "but, not function in my project! how to make it?"


That's the whole point. You need:

1. the right kind of image
2. the right kind of object
3. the right settings for all the constants, i.e. the shader "tweakables":



You can change them in the shader (if you compare that snippet with the original shader you'll see I changed a couple of things - but not necessarily for the better ) or, better still, you can change them in your DBPro code in the usual way. You'll need to experiment (and study the shader code carefully) to get it working the way you want.

Here's another screenshot:



DBPro project for producing that screenshot attached.

Attachments

Login to view attachments
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 23rd May 2011 14:43
New screenshot for previous post.

Attachments

Login to view attachments
Edson
13
Years of Service
User Offline
Joined: 21st Dec 2010
Location: Brasil
Posted: 23rd May 2011 15:54
ok, allright.
Thank for help.
You have a pdf or doc that I can learn this process?

post it here!

My project is Phenadlax!!!
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 23rd May 2011 17:04
I don't have any documentation other than the references given in Chris K's first post on this thread unfortunately.

You can probably find some useful tutorials by doing a Google web search for HLSL tutorials.

Some of the concepts needed are discussed in various places on this thread but it's hard to find what you need as the information is scattered all over the place (a bit like my study ).
Edson
13
Years of Service
User Offline
Joined: 21st Dec 2010
Location: Brasil
Posted: 23rd May 2011 17:13
ok, your intellect for darkbasic and shader are good and fast.
you have a project of game?

My project is Phenadlax!!!
david w
18
Years of Service
User Offline
Joined: 18th Dec 2005
Location: U.S.A. Michigan
Posted: 23rd May 2011 22:48
This is a modificiation of GG's terrain shader. Can someone check to see if I am outputing the normals correctly.

nruser
16
Years of Service
User Offline
Joined: 22nd Dec 2007
Location: Serbia
Posted: 29th May 2011 14:45
can someone tell me how to fix this problem i wanted to replicate doom 3 texturing but when i finished all textures and applied them to the model, i get this result



all textures are 256x256 and are only half of the face like in doom 3.

this is the shader


adn this is the code



btw can someone modify this shader to use more then one light?
noobnerd
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location:
Posted: 23rd Jun 2011 00:05
i have almost no clue on how to make shaders so before i start learning i need to know if they are up to what i need. Can i use shaders to make 2D shockwave effects? Like distorting or so? Or is there some way better method?
cyril
17
Years of Service
User Offline
Joined: 6th Aug 2007
Location: 7 miles away from big ben
Posted: 27th Jun 2011 19:16
Quote: "i have almost no clue on how to make shaders so before i start learning i need to know if they are up to what i need. Can i use shaders to make 2D shockwave effects? Like distorting or so? Or is there some way better method? "


This might be a long shot, but I believe that if you use the darkshader's heathaze.dbs on like a disk like mesh, making sure the each triangle is uv mapped with the top of the uv being at the top of each triangle (being at the centre of the disk) and then have a wave like distortion texture moving a reasonable speed might be able to produce the same effect.

The other method would most likely be a memblock alteration, but I have no clue how that would work.
Max P
14
Years of Service
User Offline
Joined: 23rd Jan 2010
Location:
Posted: 2nd Jul 2011 21:25 Edited at: 2nd Jul 2011 21:27
Hi,
I'm having some problems with editing Evolved's parallax shader.
What I'm trying to do is adding a lightmap to it. Everything works fine but the lightmap uses the wrong uv's. The uv data of the object is correct and the lightmap works when the shader is not applied. I was hoping you guys could help me edit the shader so the lightmap works. There is a screenshot attached showing the problem.

Here is the shader:

Attachments

Login to view attachments
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 2nd Jul 2011 22:12
Quote: "I was hoping you guys could help me edit the shader so the lightmap works."


The short answer is "Yes."

However, there are a few things to think about first ():

1. Do you really need 6 lights AND a lightmap?

2. You might not need ambient light as that could be included in the lightmap (unless you want it to change with time on a daily cycle for example). In other words, I guess the light map should be used in place of the ambient light.

3. Which UV stage coordinates belong to the lightmap? The shader only uses the coordinates on stage 0 but your image suggests the lightmap might be using a different set on stage 1, in which case those need to be added to the shader.
Max P
14
Years of Service
User Offline
Joined: 23rd Jan 2010
Location:
Posted: 2nd Jul 2011 22:29 Edited at: 2nd Jul 2011 22:32
Maybe 6 lights is alot, but it can be handy sometimes
But if 6 lights is a problem, having less is ok.
For the stages:
0: diffuse map
1: normal map
2: height map
3: lightmap (I wanted this in the last stage, wich is 8 if I'm right, but I don't know how to do that)
And your right, I don't need the ambient. The only reason it's there is because it was in Evolved's shader

My guess was that the NewUv should be replaced with the uv's of the lightmap stage:
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 3rd Jul 2011 01:17
Quote: "For the stages:
0: diffuse map
1: normal map
2: height map
3: lightmap ("


No. That's a common cause of confusion. Those are texture stages which may or may not correspond to the stage UV data. If your object is constructed for use with a lightmap then it should have two sets of UV data - typically stage 0 for the main texture(s) and stage 1 for the lightmap (or vice versa depending on software used to create the lightmapping).

Texture stages merely refer to the different textures that may be used when rendering any given pixel - the stage UV data tell the shader how those textures are to be read. For example a standard parallax shader might have three texture stages but be applied to an object which has only one set of UV data (EVOLVED's shader assumes that for example).

You can find out how many sets of UV data you have from the object's FVF format. One way of finding this is the somewhat circuitous object->mesh->memblock route, e.g.



You then need to compare the fvf format value with the values listed in the Help for the convert object fvf command. Phew! I'm sure there's a more direct way but it's late (or I'm getting old and tired ).

A quicker solution is trial error. I'll come back to this tomorrow when I'm awake.
Max P
14
Years of Service
User Offline
Joined: 23rd Jan 2010
Location:
Posted: 3rd Jul 2011 12:25 Edited at: 3rd Jul 2011 13:06
The uv data for the stage 0-2 are the same, the stages 3-8 are from the lightmap. I copy the uv's for the stages 3-8 manually from the liightmapped object.
First I used LM Add Shaded Light Map Object, but it turned out the wrong way, I thought that it might not set the right uv's, so thats why I copy them manually.

For the fvf: When loading the object I convert the fvf with this line:


So the uv data is right, the shader just uses the wrong stage, right?

Edit
I tried adding these lines in the shader to make it work:

This in the vertex shader:


And this one in the pixel shader:


I thought it would pick the uv from stage 8, but the object becomes invisible now..
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 3rd Jul 2011 13:52 Edited at: 3rd Jul 2011 13:54
[Dammit! Why does the PC have to crash just after I've composed a long detailed reply - but before I hit the Post Message button??? I'll try again and save as I go. ]

Quote: "The uv data for the stage 0-2 are the same, the stages 3-8 are from the lightmap."


Why do you need all those extra sets of UV data? You probably just need two sets, one for the main textures and one for the lightmap. Also, I suspect you can have at most 8 texture stages and ditto for the sets of UV data, i.e. stages 0 to 7. [Not 100% sure about that though.]

Quote: "So the uv data is right, the shader just uses the wrong stage, right?"


I've no idea. Where's your code for filling all those extra sets of UV data? The convert object fvf command simply creates the necessary structures, retaining the old data when it already exists. In any case, you probably only need TWO sets of UV data, i.e. stages 0 and 1.

Quote: "I thought it would pick the uv from stage 8, but the object becomes invisible now.. "


Does your shader compile?

[Actually, just done some tests and much to my surprise it may compile (or at least not fail for the reason I suspected ) so the problem is probably the lack of sensible UV data. The new sets will default to zero unless you have code to fill them with the right values as noted above.]
Max P
14
Years of Service
User Offline
Joined: 23rd Jan 2010
Location:
Posted: 3rd Jul 2011 14:01 Edited at: 3rd Jul 2011 14:04
This is my code for copying the stages:
The only reason for filling all the stages was because I didn't know wich stages were needed, wich I do know now.

edit Changed the code so it only uses 2 stages now.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 3rd Jul 2011 14:15 Edited at: 3rd Jul 2011 14:16
OK. I suggest you reduce that to the two needed sets and then see what happens. In fact are you sure you need to convert the fvf at all? I only mentioned that command yesterday for its Help file which contains the necessary information about vertex structure.

Your original structures:



need to be replaced by something like



Then the bit of code that used ambient light needs to be replaced with something like



I don't think you need the extra complication of your earlier snippet, i.e.



unless you know the alpha values are less than 1. Your code plays safe though - which isn't a bad idea since colour defaults vary from one graphics card to another.

You need to pass the LM data via the vertex shader as you noted before.

If that doesn't work try swapping the roles of UV stages 0 and 1. Lightmaps are sometimes applied to stage 0.
Max P
14
Years of Service
User Offline
Joined: 23rd Jan 2010
Location:
Posted: 3rd Jul 2011 14:22 Edited at: 5th Jul 2011 13:16
Thanks for the code, I'll try it out tomorrow.
I have a 24h party now at a friend cause we all passsed our exams

edit Tested your code and everything is working now
Thanks for your help!
(Screenshot of the working shader attached)

Attachments

Login to view attachments
Max P
14
Years of Service
User Offline
Joined: 23rd Jan 2010
Location:
Posted: 5th Jul 2011 14:19
Hi again, I was wondering if it is possible to adjust an objects vertices based on the applied texture, and if this is possible if you are still able to get the new vertex positions for collision?
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 5th Jul 2011 14:38 Edited at: 5th Jul 2011 14:57
Quote: "I was wondering if it is possible to adjust an objects vertices based on the applied texture"


Yes it is - but only for Shader Model 3 and above. I'm sure someone posted a demo of this on the forum a while back - might have been Dark Coder or Diggsey. I'll edit this post if I find it.

Edit Here it is:

Dark Coder's Water Shader Demo

Quote: "and if this is possible if you are still able to get the new vertex positions for collision?"


I suspect not but I'm not sure. I doubt it's a DBPro limitation since I think it would require the application to retrieve position information from the graphics card which I don't think is possible.

I suppose it could be done the hard way by using a separate render to image and coding the position info as colour (a bit like the way shadow mapping stores depth info into a texture). Your application could then read that image for the desired info. I rather doubt it would be feasible in real time though.
Max P
14
Years of Service
User Offline
Joined: 23rd Jan 2010
Location:
Posted: 5th Jul 2011 16:26
Thanks for the info and the demo, too bad it isn't possible to get the positions
At least I can learn a lot from the demo
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 5th Jul 2011 19:56
Did you get the lightmapping working?
Max P
14
Years of Service
User Offline
Joined: 23rd Jan 2010
Location:
Posted: 5th Jul 2011 19:58
Yup, see my 3rd post back. Only it seems that the normal mapping and height mapping isn't working now
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 5th Jul 2011 22:10
Quote: "Yup, see my 3rd post back"


Great! (I didn't check for the edit. )

Quote: "Only it seems that the normal mapping and height mapping isn't working now"


That shouldn't be affected. Post the current version of the shader and I'll have a closer look. Which technique are you using?
Max P
14
Years of Service
User Offline
Joined: 23rd Jan 2010
Location:
Posted: 5th Jul 2011 22:16
The technique is automaticaly applied by the engine, depending on the number of lights. I tested it and the shader is using the right technique and all the lights are passed to the shader.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 6th Jul 2011 01:34
Ok. I'll take a look tomorrow and see if it works with some sample media.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 6th Jul 2011 13:08 Edited at: 6th Jul 2011 13:11
Quote: "I'll take a look tomorrow and see if it works with some sample media."


What a pleasant surprise.

The shader worked fine first time without modification. As far as I can see you made all the necessary changes correctly. I attach the media and code I used. I hope the code is self-explanatory.

I have only three comments:

1. I restored the Heightvec value which Evolved had - and made it adjustable from the demo.

2. I'm not convinced the ambient light has been used sensibly in my demo. I've used the default value in the shader. It would make more sense to set it to full white since my lightmap includes ambient light. You can easily change that if necessary.

3. I'm not convinced of the logic of this line (repeated in each light's code):



I would expect the dynamic lighting to be added to the lightmap's value:



but I've left your version in case you had a good reason to do that.

Here's a screenshot using the additive version (the attached demo uses your multiplicative, darker, version):

Attachments

Login to view attachments
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 6th Jul 2011 13:11
Lightmap screenshot for previous post.

Attachments

Login to view attachments
Max P
14
Years of Service
User Offline
Joined: 23rd Jan 2010
Location:
Posted: 6th Jul 2011 14:38
Thanks for the demo, it runs fine. The only reason for the multiplicative version was because I thought it was the right way, I'm using the additive version now
It still isn't working in my engine, but I'm sure it is easy to solve. I think it is applying the wrong texture somewhere or there is an invalid id, or something like that
Thanks for your help!
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 6th Jul 2011 14:57
Quote: "I think it is applying the wrong texture somewhere or there is an invalid id, or something like that"


That would be my guess too. Post a simplified demo (no exe though)if you're still stuck and I'll take a look.

Another thing to check is that you've spelt all the shader constants correctly when you set them in DBPro - they will just be ignored if you misspell them or get the case wrong.
mr Handy
17
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 27th Jul 2011 14:16 Edited at: 29th Jul 2011 17:52
Hi, i need help with UV.

THIS POST WAS MOVED TO ITS OWN THREAD, where you can find full description and source code.

mr Handy
17
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 9th Aug 2011 13:53 Edited at: 9th Aug 2011 13:56
Hi, guys, I need help again.

Example:

Screen space quad has world normals texture (normals shown)
I need to get purple dots coordinates in screen space (x,y)

I beg you help me with this, I can't find anything helpful in the web.

Main problem is that normals are float3 and coords are float2...

Attachments

Login to view attachments
revenant chaos
Valued Member
17
Years of Service
User Offline
Joined: 21st Mar 2007
Location: Robbinsdale, MN
Posted: 9th Aug 2011 14:28 Edited at: 9th Aug 2011 19:55
Does this help?
mr Handy
17
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 9th Aug 2011 19:22
sp.xy is absolute screen coordinates? sample of purple dot will be like tex2D(purpledot,sp.xy) ?
revenant chaos
Valued Member
17
Years of Service
User Offline
Joined: 21st Mar 2007
Location: Robbinsdale, MN
Posted: 9th Aug 2011 19:59
Oops, I posted that too quickly. Re-read my code snippet.
Quote: "sp.xy is absolute screen coordinates?"
Yep
Quote: "sample of purple dot will be like tex2D(purpledot,sp.xy)?"
The point will at least be in the right direction screen-wise, at a distance which is dependent on the the normal's length (so not necessarily as far away as your picture suggests).
mr Handy
17
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 9th Aug 2011 20:28
thanks! it worked!
nruser
16
Years of Service
User Offline
Joined: 22nd Dec 2007
Location: Serbia
Posted: 16th Aug 2011 13:03
can someone look into the problem i had a while back in the post with a screen shot of the head, that really makes me angry, if i must i will post required media but i need this problem fixed?
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 16th Aug 2011 16:40
I saw your earlier post but didn't understand the problem. I still don't - and, yes, media would help.

Login to post a reply

Server time is: 2024-11-23 22:01:21
Your offset time is: 2024-11-23 22:01:21