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
Dar13
15
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 17th Mar 2012 23:00
That works great! Thanks James H. Have one on me !

mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 18th Mar 2012 11:56
Anyone, who knows (or had example) how to rotate whole object using shader (and preferably using direction vector) please answer.
Brendy boy
18
Years of Service
User Offline
Joined: 17th Jul 2005
Location: Croatia
Posted: 19th Mar 2012 00:46
Quote: "Anyone, who knows (or had example) how to rotate whole object using shader (and preferably using direction vector) please answer. "

set a rotation vector with set effect constant vector command.
Using that vector construct rotation transform matrix
You also need to set the coordinates of a point around which you want to rotate (also using set effect constant vector).
Use that point to construct translation transform matrix. Multiply that matrix with the above rotation matrix and then use the result matrix to transform the vertex position inside the vertex shader

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 19th Mar 2012 02:15
It would be better to calculate the rotation matrix in DBPro and use set effect constant matrix.
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 19th Mar 2012 09:50 Edited at: 19th Mar 2012 09:51
Thank you for instrictions. My idea was to rotate plain object (torch glow) to camera view. I thought, that shader will do it faster, than DB's "rotation to camera". Although, if I use shader to rotate object, will it replace any DB rotation\movement\scale commands?
Brendy boy
18
Years of Service
User Offline
Joined: 17th Jul 2005
Location: Croatia
Posted: 19th Mar 2012 10:13
Quote: "I thought, that shader will do it faster, than DB's "rotation to camera". Although, if I use shader to rotate object, will it replace any DB rotation\movement\scale commands? "

it wont be. It is only one 4 matrix multiplications on the cpu and if done in a shader you need separate shader for every object and you'll need to set separate angle rotations and rotation origin for every object. All that will cause more slowdown then if done on the cpu.

Quote: " Although, if I use shader to rotate object, will it replace any DB rotation\movement\scale commands? "

It depends. If you done it like i described above then yes. f you want to keep db rotation/movement/scale then you'll have to pass object world matrix to the shader and multiply vertex position with that matrix and after that multiply it with rotation matrix.

If you were doing that in c++, for large amount of object maybe it would be faster but in dbpro every object will need separate shader and that will make the whole thing slower so i wouldn't advise you to go the shader route

mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 19th Mar 2012 10:58
Thank you, Brendy boy! I will follow your advice.
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 19th Mar 2012 16:53
I beg a pardon for my extraactivity

Can shader get an object position? I mean it's pivot point?
Brendy boy
18
Years of Service
User Offline
Joined: 17th Jul 2005
Location: Croatia
Posted: 19th Mar 2012 20:30
Quote: "Can shader get an object position? I mean it's pivot point?"

No, you have to send it to the shader using set effect constant vector command

Dar13
15
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 19th Mar 2012 23:45 Edited at: 19th Mar 2012 23:50
Well, I'm back!
I'm now trying to combine three textures into one while alpha blending it all together. I have one texture, my base texture, that I want everything else added onto(always visible). Then I have another texture that I'm going to edit via direct memory access(aka memblocks) that's going to have its own alpha. The last texture is a scroll texture(the same as last time) that also has its own alpha. I have everything down except that the base layer and the changing layer aren't adding their colors like they should(despite my math saying they should...).

Here's the relevant piece of code:


And here's the whole shader file:


Again, thanks for any help!

EDIT:screenie of the issue


Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 20th Mar 2012 02:17
What should be purple (i.e. you've circled three colours and four edges, which are you talking about?)?

What are your three source textures?

What are the values of blend, diffuse, etc?

Does your shader compile? (It doesn't by the way. For one thing the technique has been commented out. For another, no values have been passed to the call of BlendPS. )
Dar13
15
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 20th Mar 2012 02:36
Base texture:


The scroll texture:


The other texture is a transparent texture that is added to dynamically. It is also 512x512.

Sorry, forgot the values of the variables.
blend = 0.08
diffuse = 1 1 1 .5

Disclaimer: I'm using Ogre3D for my graphics engine. It handles HLSL differently in certain ares, mainly syntax(like not requiring(nor wanting) techniques for shaders in DX9). This shader does work in Ogre by the way.

I edited the picture to show what I meant via Photobucket, should be updated in my other post.

mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 20th Mar 2012 08:11
@Brendy boy
How about using smth like difference between local space [-1;1] and worldspace?
Brendy boy
18
Years of Service
User Offline
Joined: 17th Jul 2005
Location: Croatia
Posted: 20th Mar 2012 08:48 Edited at: 20th Mar 2012 08:54
Quote: "@Brendy boy
How about using smth like difference between local space [-1;1] and worldspace? "

Only input data that vertex shader receives is data of a single vertex ->position, uv, normal so there is no difference you can calculate to get object origin.

What you can do is this. Put your object origin into normal data of all of it's vertices and that way you can retrieve than in a shader. Of course, if you have many objects which move constantly and have lots of vertices that method would be to slow.


There is one other possibility. I'll have to investigate the dbpro source code to see if this is possible.
Maybe there's some object data that dbpro sends to every shader automaticaly (such as camera position, object/limb world matrix). If that is the case then there is a way to retrieve object origin, but as i said i'm not sure. Maybe IanM knows more

EDIT:
you can put this in your shader at the beggining:
matrix mWld : World;

and tehn in vertex shader extract the translation vector from that shader to get the origin.
matrix mWld should contain object world matrix.
i'm not sure if that will work.

Have a look at the shaders that are in compiler/effects folder.
They have some matrices and vector defined at the beggining and they use some sort of sematics that could indicate that dbpro sends some object and camera data to the shader

mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 20th Mar 2012 09:30 Edited at: 20th Mar 2012 09:31
Thanks a lot! I will try!
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 20th Mar 2012 15:53
Quote: "I edited the picture to show what I meant via Photobucket, should be updated in my other post."


Thanks. That's much clearer. However, I'm still not sure what you're trying to do. For example, where is the red component of your purple region coming from? Which image causes that dark red disk in your first screenshot?

It should be easy to rewrite the shader so it conforms with the standard HLSL used by DBPro. I've no idea what syntax Ogre uses.
Dar13
15
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 20th Mar 2012 23:40
In my program, via memblocks, I added a red(1 0 0 1 in HLSL float4 form) circle at 256,256. That's where the red is coming from.

What I'm trying to do is have the base texture color blend with the overlay texture(the texture that's edited during the program's execution) and then alpha blend the resulting texture with the scroll texture.
Here's what I want it to look like:


I'd be more than happy to convert it from its current form to DBPro compatible HLSL, but I have no idea how. No matter where I looked, I couldn't find a place that explained what to put as arguments in the technique section.

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 21st Mar 2012 01:34
Quote: "No matter where I looked, I couldn't find a place that explained what to put as arguments in the technique section."


I won't attempt that now - it's too close to midnight here and strange things start to happen to my posts around that hour.

Your explanation of the red circle helps a lot and I think it should be simple to fix. I've got a couple of things on at the moment but I'll get to this as soon as I can - later this week unless someone else beats me to it. Several people here should be able to help you with this one though.
James H
16
Years of Service
User Offline
Joined: 21st Apr 2007
Location: St Helens
Posted: 21st Mar 2012 23:47 Edited at: 22nd Mar 2012 00:02
@ Dar13 - This a good enough starting point?



The end result doesnt match your final image, but am not sure if you used photo shop for it or is it a screenie from ogres result? Also the images I downloaded are they original ones? I had to create new alpha layer for them from b&w mask just to get the result looking similar to your final image. Also "I added a red(1 0 0 1 in HLSL float4 form) circle at 256,256" the pixels that arent red - whats their alpha value - same as red pixels or different?

EDIT: attached some screenies showing differences between textures and also the 5 textures Ive been using

Attachments

Login to view attachments
Dar13
15
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 22nd Mar 2012 03:40
I used Photoshop for the mockup, since I couldn't get Ogre to cooperate. The images I uploaded are the exact same files I use for the shader. The overlay image is (0,0,0,0) except where changed(like the red circle).

It's close, James H, but your code still doesn't blend the colors like it does in my 'goal' image. The alpha blending is done spot-on though. This color blending stuff is really frustrating.

James H
16
Years of Service
User Offline
Joined: 21st Apr 2007
Location: St Helens
Posted: 24th Mar 2012 02:00 Edited at: 24th Mar 2012 02:04
Well I made an obvious error where I put *blend, I still cant get the images provided to act correctly - if I open the base image for example in texture maker it has a different apearance to when I open it in paint or infranview or dx dds texture tool
As for alpha blending Im not sure this is alpha blending as such because when I turn on alpahblending state in shader, transparencies become apparent, so I left it to its default state - remove rems in shader files for respective projects(in new upload) to see
attatched is a rar file with 3 screenies, 2 DS project files and the exported files for those projects in two folders. One screenie is the base texture showing different results when loaded into four image tools. The other 2 are DS results first using provided images, second using altered images and some changes in the shader file. As you will be able to see with the two DS project files, DS does not show any transparencies on the object when alphablending is enabled which is different to how they are affected in DBP
Anyway 2nd DS project is much nearer to your goal image, I dont really know enough about shaders to be able to improve this to using the images you provided without showing all those extra differences that different image/texture tools produce, as I said earlier Im not good at these either so I just altered the images in the 2nd project. The first DS project is using provided images.

Attachments

Login to view attachments
Dar13
15
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 25th Mar 2012 02:43
Made some progress. I took JamesH's most recent code(thank you!):
and turned it into this:
and it works! Kinda.

The shader now blends the colors properly, but since now I can only see that color blending when the scroll texture scrolls over the combined colors. Screenie attached to show what I mean:


I have no clue what to do from here.

mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 6th Apr 2012 09:43
I have an idea. This thread is a huge mess, it hard to see what topics are discussied here. Or even several different issues mixed in a row.

...Maybe we should start a separate shader forum?
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 6th Apr 2012 12:47
That sounds like a good suggestion.
david w
18
Years of Service
User Offline
Joined: 18th Dec 2005
Location: U.S.A. Michigan
Posted: 8th Apr 2012 07:34
I have to agree that this thread is a huge mess. You have my vote in this matter.
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 17th Apr 2012 09:27
I don't know who has rights to do such thing
The Weeping Corpse
12
Years of Service
User Offline
Joined: 19th Sep 2011
Location: United Kingdom
Posted: 19th Apr 2012 12:33 Edited at: 19th Apr 2012 12:34
I agree. I'm just about to embark on my shader learning adventure and this thread probably has all the answers I need. I understand the concept of what shaders do and I know what I want to achieve with them but I simple dont know where to start looking lol.
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 22nd Apr 2012 23:28 Edited at: 22nd Apr 2012 23:31
Well, great idea about separate forum will die here, between dozens of posts...

Hi there, I have two issues:
1. Can it be possible to render depth from four cameras into one RGBA texture?
2. In S.T.A.L.K.E.R. game there was plenty (>8) of spot lights from characters. I can do one spotlight. How to bo many? I think it's somehow connected with deferred shading.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 25th Apr 2012 15:07
Quote: "1. Can it be possible to render depth from four cameras into one RGBA texture?"


Not easily. It's much easier to use the same camera rendered several times as in the attached demo (another way is to use several passes in the shader).

Attachments

Login to view attachments
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 25th Apr 2012 17:17 Edited at: 25th Apr 2012 18:14
Thanks, I'll see your demo ASAP, from your description I think it's even more than I expected.

edit: About spotlights - I've figured out how to.
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 25th Apr 2012 18:35
Just tested your demo - exept a little typo at line 10 it's perfect!
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 25th Apr 2012 19:01
Well spotted.

That was a deliberate oversight. I'd been experimenting with IanM's version of the rgb command and forgot to put it back to the usual rgb(0, 0, 0) - makes no difference to the demo though.

In ten years time nobody will be able to find that demo ...
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 25th Apr 2012 22:38 Edited at: 25th Apr 2012 22:44
Ten years Maybe we just should start a "vote for" thread to collect votes for new subforum?

Quote: "I'd been experimenting with "

That "typo" was easy to "fix", but sometimes somebody uploads snippet that requires some external dll's, even paid
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 28th Apr 2012 18:50 Edited at: 28th Apr 2012 19:50
post deleted, problem solved

«It's the Shader, shader me this, shader me that»
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 29th Apr 2012 20:11 Edited at: 29th Apr 2012 20:16
I have a problem with specular lighting.



On screenshot you can see sphere (1) with 50% self-illum. and specular.
Specular lighting on this sphere looks same as in my shader with pow(dot(normal,half view vector),25)

Sphere (2) is sphere (1) rendered in max.
How to smooth normals?

«It's the Shader, shader me this, shader me that»

Attachments

Login to view attachments
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 29th Apr 2012 22:32
I've figured out strange thing:

bad effect:
VS: OUT.nor=normalize(mul(IN.nor,world));
PS: float3 nor=IN.nor;

good effect:
VS: OUT.nor=normalize(mul(IN.nor,world));
PS: float3 nor=normalize(IN.nor);

why I should normalize twice?!

«It's the Shader, shader me this, shader me that»
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 30th Apr 2012 00:03 Edited at: 30th Apr 2012 00:04
Because the vectors are linearly interpolated for use in the pixel shader.

On a curved surface the normals at the three corners of a poly will be pointing in slightly different directions (e.g. see vertices v1 and v2 in diagram below) so a linearly interpolated normal used in the pixel shader for points between the two vertices will be slightly too short. This difference in length can be visually noticeable on curved surfaces (they should all be length 1 - i.e. "normalised").

As an extreme example consider two normals n1 = (0, 1, 0) and n2 = (0, 0, 1) at two corners of a poly. A point midway between the two corners will have the average of these passed to it in the pixel shader, i.e. n = (0, 0.5, 0.5). The interpolated normal n has length sqrt(0.5x0.5 + 0.5x0.5) = sqrt(0.5) = 0.707. If that is used in lighting the result will be too dark. The correct normalised normal should be n = (0, 0.707, 0.707) (to three decimal places).

That's why per pixel lighting is so much better visually than per vertex lighting (when done correctly).

Attachments

Login to view attachments
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 30th Apr 2012 10:55
Thanks!

My current version is:

VS: OUT.nor=mul(IN.nor,world);
PS: float3 nor=normalize(IN.nor);

Is it correct? Visually looks ok.

«It's the Shader, shader me this, shader me that»
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 30th Apr 2012 14:21
Looks OK to me.
Dar13
15
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 11th Jul 2012 05:38
I figured out my shader, finally.

The pixel shader looks like this:

I kind of just flailed about until I got the right formula. The part that really made it work was the 'invTex = 1 / tex2D(texture0,input.uv);'.

mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 20th Jul 2012 09:15
Hi! I have two simple questions:
1. How to get exact RGB from texture
2. If world position is stored in RGBA32, what value of 1 DBP world unit? And -1?

«Socks are sexy. You should wear them!» — Bronies
david w
18
Years of Service
User Offline
Joined: 18th Dec 2005
Location: U.S.A. Michigan
Posted: 20th Jul 2012 21:36
@Dar13 What exactly is your shader suppose to be doing? Just a description would be ok about what your trying to accomplish.

Thanks
Dar13
15
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 21st Jul 2012 03:36
What I want is to have three textures, two that are actual textures that blend additively(one is programmatically altered) and another that is blended multiplicatively(I think that's what it's called) on top of the earlier blended textures. The pixel shader I posted above accomplishes all that, though I've had to edit one of the textures to get the right effect.

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 23rd Jul 2012 17:18
Quote: "How to get exact RGB from texture"




or



where, I hope, the meaning of myTexSample and myUV should be obvious.

Quote: "If world position is stored in RGBA32, what value of 1 DBP world unit? And -1?"


World position is a float3 vector whereas 1 and -1 are presumably float1s so I can't answer your question without further information. Has the world position been scaled before being coded as RGBA32? What formula was used?
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 24th Jul 2012 12:14
@Green Gandalf
1)
If texture is white then result of
Quote: "float3 RGB = tex2D(myTexSample, myUV).rgb;"

will be (255,255,255)?

2)
World position is just like that

VS out.pos=mul(in.pos,worldviewproj);

PS return float4(in.pos.xyz,1)

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 24th Jul 2012 23:43
Quote: "If texture is white then result of

Quote: "float3 RGB = tex2D(myTexSample, myUV).rgb;"
will be (255,255,255)?"


I doubt it. Colour values are in the range 0 to 1 for shaders, so full white would probably be (1.0, 1.0, 1.0).

Quote: "2)
World position is just like that

VS out.pos=mul(in.pos,worldviewproj);

PS return float4(in.pos.xyz,1)"


Two comments there: (1) I've no idea what variable "in.pos" corresponds to. It might be the same as out.pos from your vertex shader but not necessarily. (2) Assuming it is the same variable, then that isn't world position. It's the view projection version of the world position. If you need world position then you should be using



with an appropriate semantic in the output structure which you haven't provided.
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 25th Jul 2012 09:27
1) I just need to calculate [color*256]. OMG my brain is melted this summer
2) Yeah, I made a mistake. And that is good! I mean, now I can clearly describe what I want to know:

a) VS out.pos=mul(in.pos,world);
RGB 32 float values for xyz will be exact as in DBP i.e.(1200.5,5000.0,-1000.93)?

b) VS out.pos=mul(in.pos,worldviewproj);
How coords are transformed in this case? For example, we have camera at (0,0,0) and object at (10,10,10).
I don't clearly understand what is that wvp matrix really do.

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 25th Jul 2012 16:48
Quote: "a) VS out.pos=mul(in.pos,world);
RGB 32 float values for xyz will be exact as in DBP i.e.(1200.5,5000.0,-1000.93)?"


I don't know. I also don't know how those values are read in to a shader. I can only suggest you do some Googling combined with some experimentation.

Quote: "I don't clearly understand what is that wvp matrix really do."


Loosely speaking, the world matrix converts the model's internal coordinates into their corresponding world coordinates. The view matrix converts those values into their values relative to the camera's location and orientation. The projection matrix converts those values into screen space coordinates so that a 2D projection can be performed. The world view projection matrix is the result of applying all three transformations. Further details can be found in many on-line articles such as:

Direct3D9 transformation matrices
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 26th Jul 2012 09:04
Thanks, Green Gandalf!

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 26th Jul 2012 13:41
Quote: "I don't know. I also don't know how those values are read in to a shader. I can only suggest you do some Googling combined with some experimentation."


I'm hoping to write a short demo to clarify that issue. I don't have any experience of RGBA32 textures but I have an idea of a simple way to test their use in DBPro. Watch this space.

Login to post a reply

Server time is: 2024-04-18 23:08:54
Your offset time is: 2024-04-18 23:08:54