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
AtomR
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Portugal
Posted: 20th Mar 2009 18:56 Edited at: 20th Mar 2009 19:09
Let me try.
Shaders are "programs" that apply effects to objects pixels and vertexes "pixel shaders and vertex shaders". With these u can implement real time shadows, reflectios etc etc. Every time the object will be drawn to the screen the shader program will be run for each pixel and each vertex it has to draw. Anyone correct me if any info is wrong.
For instance a shader that applies a light to an object will usually have constants to hold light direction, light color etc. The coder creates the constant inside the shader but we have to be able to access that when applying a shader to our object.

So lets try with GG's normal map shaders he posted a few posts above. Between all that advanced 3d math that allows the shader to make those cool effects he has

Quote: "
float4 lightDir = {-1.0f, 0.0f, 0.0f, 1.0f};
float4 ambiColor = {0.2f, 0.2f, 0.2f, 1.0f};
float4 lightColor = {1.0f, 1.0f, 1.0f, 1.0f};
float specLevel = 0.5;
float specExpon=10.0;
"

which means that you can change this inside your darkbasic program to values of your chosing.

lightDir is a vector 4 type constant because it holds four values. Same for ambiColor and lightColor. For these color constants the vector holds the Red, Green, Blue and Alpha components. For the lightDir is a vector that has x, y, z and the last component is the length of the vector. specLevel and specExpon are floats so to put this shader in your scene you would have to do the following.

And there u have an object with an effect applied. Does not mean you will get the results you want but its applied. If u want to change the light direction u need to create a vector4 type of constant inside your dbp program and pass it to the shader.

So,after loading the shader, u do

And for each of the other vector4 constants u'd have to do the same.

The best thing is to know well the shader your gonna use, so u know what constants you need to alter. But if you have a shader and u don't know what constants need to be altered you can use the command
PERFORM CHECKLIST FOR EFFECT VALUES
And use the checklist commands to see what these values are.

I hope i wasn't too confusing and that i answered a few of your doubts. I've been trying to wrap my head around shaders since i started playing around with DBPro and they are still a very big mystery to me too

Take care
AtomR
Monk
15
Years of Service
User Offline
Joined: 25th Sep 2008
Location: Standing in the snow =D
Posted: 20th Mar 2009 21:24 Edited at: 20th Mar 2009 21:38
Thanks very much Atom, Ive only started dabbling in shaders because im bored of my graphics and Ive heard about all the cool stuff shaders can do.

Is it as simple as that? Set object effect.... I thought it was way more complicated than that

Ive recently tried using "GG Blended Bump Mapping Shader v4" but to be fair, Im not entirely sure its best one to be using, but its a start...
Anyway, when I apply it to a set of objects that make up a terrain, it appears fuzzy. Is that down to one of the constants, a texture, or am I missing something?

(see attached, I dunno how to post images successfully yet...)

Last question =),


No matter what values I change the set vector 4 to, from 4 to 1004, and 1 to 1001, the outcome never seems to change, and that perplexes me... Any help is welcome

Cheers for clearing up the first part!

Oh yeah, is it possible to use two or more shaders, say parallax abd bump mapping shaders on the same object at the same time?

Attachments

Login to view attachments
AtomR
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Portugal
Posted: 21st Mar 2009 12:23 Edited at: 21st Mar 2009 12:24
U can only use one shader per object so if uy want parallax and bump on the same object then the one shader must apply both techniques to the object.

Altho Evolved seems to be able to apply any shader he wants to his objects but i'm not sure how he does it. I don't think he's applying more then 1 shader per object but it sure looks like it.

On another note, the way i add images in my posts is i post my message with the image uploaded then view my image, copy the url of the image and edit the post to include [ img] URL here [ /img] where i want the image.

Take care
AtomR
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 21st Mar 2009 13:54
Quote: "Ive recently tried using "GG Blended Bump Mapping Shader v4" but to be fair, Im not entirely sure its best one to be using, but its a start...
Anyway, when I apply it to a set of objects that make up a terrain, it appears fuzzy. Is that down to one of the constants, a texture, or am I missing something?"


Is that one of the versions with roads? It probably needs a bit of updating - but I don't think that is your problem. (And the name of the shader is misleading - if you're using the one I think you are it does NOT do bump mapping. Another shader demo for me to tidy up and post on this thread:

shaders for advanced terrain

Have you tried changing the number of times the images are tiled across the terrain?

If that doesn't work, could you post or email a zip file containing all your code and media so I can check it myself?
Monk
15
Years of Service
User Offline
Joined: 25th Sep 2008
Location: Standing in the snow =D
Posted: 21st Mar 2009 18:22 Edited at: 21st Mar 2009 20:00
Thanks AtomR, I'll try that next time... =)

@ GG
How would you change the number of time the images are tiled across the terrain?, would that involve changing the .Fx file?

Oh and I'm not using AT so Im not sure that those shaders would work... Would they?

Sorry GG, the code might load slowly, it creating a fair few objects from matrices to memblocks, but it should run fine after loading...

@AtomR, is the multiple shader thing only a problem with certain shaders? Because I tried it out of curiosity in my Community Competition entry, a fog shader, and GG "Possibly misleading name" Blended Bumpmapping shader and it seems to work ok...
Edit: er... Nope =( . The shader applied last seems to overrule the first one Shame...

Attachments

Login to view attachments
Kuper
16
Years of Service
User Offline
Joined: 25th Feb 2008
Playing: Planescape:Torment
Posted: 21st Mar 2009 18:41
Hi!so i have question about "set screen effect" command.I took PostBloom shader by evolved and try to change operations with plain in front of camera by "set screen effect" commands.But screen become dark.Images which are rendering are correct but result image dont appear on the screen(((((

Attachments

Login to view attachments
Monk
15
Years of Service
User Offline
Joined: 25th Sep 2008
Location: Standing in the snow =D
Posted: 21st Mar 2009 20:19 Edited at: 21st Mar 2009 20:30
If the code you posted corresponds to the exe, you might be missing a sync command at the end of the loop.

Also, what version are you using, because the texture screen command wont work for me...

Kuper
16
Years of Service
User Offline
Joined: 25th Feb 2008
Playing: Planescape:Torment
Posted: 21st Mar 2009 20:41
Im using u7.1 I change texture plain to texture screen ONLY and I dont change the original evolveds code.
Monk
15
Years of Service
User Offline
Joined: 25th Sep 2008
Location: Standing in the snow =D
Posted: 21st Mar 2009 21:33
Fair enough, but the loop is still missing a sync command....
I downgraded back to 6.9 so that may explain why the command won't work.

Kuper
16
Years of Service
User Offline
Joined: 25th Feb 2008
Playing: Planescape:Torment
Posted: 21st Mar 2009 22:00
The sync mask^ of main camera is in the function render to target().first render to target() is used for shader cameras and last Render to target is for camera 0
Monk
15
Years of Service
User Offline
Joined: 25th Sep 2008
Location: Standing in the snow =D
Posted: 21st Mar 2009 22:44
Hmm, well when I ran your exe, the screen wasnt refreshing, but seemed to be drawing on what was already there, ie the text was just writing on top of text already there, not clearing and then writing...
Maybe as its drawing to the screen, it needs a cls command?...

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 22nd Mar 2009 00:37
Quote: "@ GG
How would you change the number of time the images are tiled across the terrain?, would that involve changing the .Fx file?

Oh and I'm not using AT so Im not sure that those shaders would work... Would they?

Sorry GG, the code might load slowly, it creating a fair few objects from matrices to memblocks, but it should run fine after loading..."


You can change the tiling by changing the values assumed by the shader in the following line:



The first pair of values are used to tile the five detail maps over the terrain and the second pair are used for the road map (which wouldn't usually be tiled). You can change the values used by the shader using something like the following DBPro commands:



Unfortunately I can't test your demo because the exe just crashes on my machine (could be a memory issue) - and the dba code won't compile (you use plug-ins that I don't have).

The shader should work with any object which has vertex XYZ position data, normal data and stage 0 UV coords. You also need to give "vertScale" a sensible value for your terrain.
Monk
15
Years of Service
User Offline
Joined: 25th Sep 2008
Location: Standing in the snow =D
Posted: 22nd Mar 2009 12:43 Edited at: 22nd Mar 2009 13:09
Ah, so you would have to make the values smaller, not bigger =) I tried values upto 1004, but I didnt go below 4....

Thanks for trying GG, I could post another exe later today with premade objects instead of making them from scratch, that might save your memory.
And as for plug ins, I only have Ian Ms, Sparkys, The free version of Uber terrain, NDB, and something called shader data.dll which I didnt know I had... But as far as I know, all the commands are your standard DBPro ones.
Ill keep playing with it, and Thanks for the help =)

P.s Do you have the link to all your original shaders? Ill try a few of them and see how they work

Edit:
I changed the values to 0.1,0.1,1,1 and that stopped all the fuzzinees =) Looks like it was just looks of images tiled really close together that caused this. Is there anyway though to change the UVTiling for different images, so that you can have larger images for the grass, but lots of smaller images for the cliff perse...

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 22nd Mar 2009 13:12 Edited at: 22nd Mar 2009 13:13
Quote: "And as for plug ins, I only have Ian Ms, Sparkys, The free version of Uber terrain, NDB, and something called shader data.dll which I didnt know I had... But as far as I know, all the commands are your standard DBPro ones. "


The only ones I have from that list are IanM's (who can do without them ) and ShaderData.dll which comes with DarkShader. I've just had another look and the problem was the command:



which doesn't seem to be necessary. I've just tried commenting it out and it compiles now. But still crashes and I get a Windows warning that virtual memory is too low etc . I seem to be getting that message a lot recently.

There is one thing you could check. What are the UV coords of your terrain? I usually assume the four corners of the terrain have (0,0), (0,1), (1,0) and (1,1) and tile accordingly, but if your coords are very different from that then you'd need to choose very different tiling values from the ones I've used.

Quote: "P.s Do you have the link to all your original shaders?"


Sadly no. It's one of the BIG tasks on my "to do" list.

Edit I see we posted at the same time and you've sorted it.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 22nd Mar 2009 13:33
Quote: "Is there anyway though to change the UVTiling for different images, so that you can have larger images for the grass, but lots of smaller images for the cliff perse..."


Yes, should be easy to modify the shader to allow for that. Might find time to do that later today.
Monk
15
Years of Service
User Offline
Joined: 25th Sep 2008
Location: Standing in the snow =D
Posted: 22nd Mar 2009 13:40 Edited at: 22nd Mar 2009 14:53
Thanks GG,
It would just make it easier because after I changed the Tiling, the grass looked better, but the cliff looked noticeably tiled...

If your virtual memory is too low, change the size of the terrain. At the moment, the code makes a 10000,10000,400,400 matrix, splits it into 10x10 pieces and then makes 100 memblocks. Try making the terrain smaller to start with, and Ill post a edited version of the code in a minute...

About shdaerdata.dll, I dont have darkshader, so I dont know where that came from...

I'll also have a browse to see if I can find your original shaders on the forum, and post a link if I find anything =)

Edit:
GG, could you try this code? Its similar to before but the original matrix is 100 times smaller ( only 1000,1000,40,40) and it only makes one memblock not 100, so that should work for you now .



Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 22nd Mar 2009 20:58
Quote: "About shdaerdata.dll, I dont have darkshader, so I dont know where that came from..."


It's possible that it comes with the latest upgrades automatically now - it just contains the extra shader commands that were introduced with Dark Shader.

Quote: "so that should work for you now"


Doesn't I'm afraid.

I'll test it on my laptop which sometimes runs programs that won't run on my ancient soon to be replaced PC.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 23rd Mar 2009 02:29
Quote: "Yes, should be easy to modify the shader to allow for that. Might find time to do that later today."


I couldn't resist the temptation to add the roads and the flexible UV tiling to my latest terrain blend and bump mapping shader and, of course, encountered an unexpected bug.

My revised shader requires 11 textures - but something tells me there is an upper limit of 8. Perhaps that's the problem. There is a messy way round that of course. But not tonight.

I should have concentrated on the matter in hand - the UV tiling (which really ought to be easy).

As the saying goes: "The road to Hell is paved with good intentions...".
Monk
15
Years of Service
User Offline
Joined: 25th Sep 2008
Location: Standing in the snow =D
Posted: 23rd Mar 2009 21:58
Thank you for keeping working on this.

Ill keep working on trying to get a demo working for you.

Glad you're having fun and hope you dont get too far down the road =)

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 24th Mar 2009 01:06 Edited at: 24th Mar 2009 01:08
Monk

I've now got a working revised version of my terrain blending with roads and some normal mapping. It also allows you to choose different UV tiling constants for each texture. I've also cleaned up the seams that were visible in the earlier demo.

I had to compromise with the normal mapping since I seem to be limited to 8 textures in the shader (must look that up sometime). I've opted for keeping normal mapping for the rocky parts of the terrain and for the road so the final textures used are:

4 detail terrain textures for blending
1 normal map for one of the detail textures
1 road map
1 road texture
1 normal map for the road

You can set the six separate UV tilings - but for a given texture the U and V tilings are assumed to be the same. The six adjustable tilings are for:

the four detail maps
the road map
the road texture

the two normal maps are assumed to be tiled the same as the corresponding texture.

Is that more or less what you are after?

I attach a screenshot. Will post a full demo on the WIP board "Shaders for Advanced Terrain" thread when I've finished "improving" it.

Attachments

Login to view attachments
Monk
15
Years of Service
User Offline
Joined: 25th Sep 2008
Location: Standing in the snow =D
Posted: 24th Mar 2009 18:40 Edited at: 24th Mar 2009 19:43
That sounds perfect for what Im trying to do, though Im probably not gonna use the road textures or road map in my project.

Ill be sure to check that demo out.
Cheers for the help !

And Ive changed this demo of mine so that its not making anything at all.

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 24th Mar 2009 20:15
Quote: "That sounds perfect for what Im trying to do, though Im probably not gonna use the road textures or road map in my project."


If you don't need roads then I can easily re-instate the normal mapping for all four terrain textures and keep the separate UV tiling that I've just added. I'll do that over the next few days and add both versions to the WIP thread.

Quote: "And Ive changed this demo of mine so that its not making anything at all."


I'll check it later when I get home.
Monk
15
Years of Service
User Offline
Joined: 25th Sep 2008
Location: Standing in the snow =D
Posted: 24th Mar 2009 20:19
Thanks for the time you've put in =)

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 25th Mar 2009 12:24
Quote: "And Ive changed this demo of mine so that its not making anything at all."


The download button won't work for some reason. I vaguely recall similar problems uploading images if the files had extenders in upper case, e.g. .JPG instead of .jpg.
Monk
15
Years of Service
User Offline
Joined: 25th Sep 2008
Location: Standing in the snow =D
Posted: 25th Mar 2009 16:20 Edited at: 25th Mar 2009 19:00
Right, well when I get home Ill sort that out...

I really should check stuff works before I upload it, this isnt the first time this week...

Attachments

Login to view attachments
AtomR
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Portugal
Posted: 25th Mar 2009 23:04 Edited at: 25th Mar 2009 23:06
Ok, i've finally finished reading all 28 pages of this thread. I didn't want to miss any piece of knowledge, advice and wisdom stored in here so i read it all Only took me 8 days

I now can understand a lot of the shaders floating around here and realise my problem is not with the shader language itself but with 3d maths. I can't grasp very well the diferent types of spaces available (view, world, projection etc). Anyone have a good link for the math part of writing shaders?

I've downloaded the two pdf that Sid Sinister (if i'm not mistaken) posted and will reading them ASAP.

But i just have this one curiosity about cube maps. From GG's GGReflect01.fx posted in page one of this thread

Quote: "texture EnvironmentTexture
< string Type = "cube"; string ResourceName = "SnowGG.dds"; >;

sampler EnvironmentSample = sampler_state
{ Texture = (EnvironmentTexture);
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
MIPFILTER = LINEAR;
ADDRESSU = WRAP;
ADDRESSV = WRAP;
ADDRESSW = WRAP;
};"


So a cubemap is a 3d texture that has 3 uvcoords. I assume the w component also receives values in the interval [0,1].
{u,v,0}{u,0,w}{0,v,w}{u,v,1}{u,1,w}{1,v,w} these adresses would probably allow me to sample from the textube like each face is a 2dtexture, correct? But for uvw {.5,.5,.5} which face will the be sampled?

Take care
AtomR
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 26th Mar 2009 00:30
AtomR

Good question.

Have a look at the attached simple demo and study the values of the texture coordinate vector used in the shader - it is the float3 UVW. Ask yourself what values it is given in the shader.

Cube maps are sampled rather differently from standard textures and volume textures. The coordinates are interpreted as a direction vector pointing out from the centre of a cube which you can imagine has been textured with your cube map. The shader texture sampler then samples the cube map where the direction vector (or raycast if you prefer) intersects this imaginary cube.

That is precisely what is happening in the demo because the shader uses the object's own position coordinates as the direction vector. This has the effect that as the object rotates it's texture rotates with it.

For cube map samplers the UVW coords can be anything - it is the direction that matters. For example, you could have UVW = (-2, -1, 0) or UVW = (-7.2, -3.6, 0). These would both look up the same point in the cube map because they represent the same direction - the magnitude of the vector is unimportant.

This brings us to your final question:

Quote: "But for uvw {.5,.5,.5} which face will the be sampled?"


You can see from my description above that your question should really have been:

Quote: "But for uvw {0.0,0.0,0.0} which face will the be sampled?"


The short answer is that I don't know.

Try it and see what happens.

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: 26th Mar 2009 01:14
Monk

Thanks for the new demo. Still doesn't run on my PC - but does on my laptop which has a bit more memory.

I've tracked down your problem. I suspect it's your object's UV coords - they seem to repeat the range 0 to 1 as you move across the terrain. This means that when you change the tiling you change it for each "quad" in your terrain separately - not smoothly over the whole terrain.

The shader assumes that the whole terrain has UV coords which change continuously across the terrain. So if the right hand edge of one quad has U coord 0.17 say, then the left hand edge of the next quad to the right will also have U coord 0.17. I doubt this is the case for your terrain.

There are two solutions (at least):

1. modify the shader so it uses the XZ coords of the terrain (with a suitable tiling);
2. modify your terrain's UV coords.

Hope this helps.
AtomR
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Portugal
Posted: 26th Mar 2009 14:50 Edited at: 26th Mar 2009 15:02
I'm going nowhere with this cubemap business.
Quote: "Have a look at the attached simple demo and study the values of the texture coordinate vector used in the shader - it is the float3 UVW. Ask yourself what values it is given in the shader.
"


In this shader it assumes the values of each vertex's world position. In this example (-25,-25,-25) to (25,25,25). Since it just needs a direction vector i supose it'll be the same as (-1,-1,-1) to (1,1,1).

Hmm just noticed and slightly off topic: In the simple cubemap shader your vertex shader receives a normal. Does it serve a purpose or did u just think you were gonna need it and forgot to take out later? Same goes for Out.pos=mul(In.pos,wvp); This is not being used in the PShader. Does it still need to be there in the SVhader?


Quote: "That is precisely what is happening in the demo because the shader uses the object's own position coordinates as the direction vector. This has the effect that as the object rotates it's texture rotates with it.
"

The texture rotates with it because the objects xyz positions are given in "local space" right? If, in the VShader, instead of Out.UVW = In.pos; we had Out.UVW = mul(In.pos, World); (where world is float4x4 World:World; duh!) the texture would stay put while the object rotated.

As I was thinking in my previous post, by fixing one coordinate you can select which face gets textured to your objects. Here is what I have now

This receives the object's UV data and constructs the UVW component depending on the face you want to use in this line float3 UVW ={(In.UV.x*2)-1,-1,(In.UV.y*2)-1};
(In.UV.x*2)-1 is to transform the UV from the [0,1] range to the [-1,1] range. Only one thing doesn't work. The face will be textured only once on the object regardless of how many time the UVs are tiled in the original object.



This picture ilustrates my feeble understanding of UV coords. The shader will always have values from 0-1 when it comes to UV data correct? So why doesn't it tile the image read on the cubemap?

[edit]
Regarding UVW {0,0,0} i was thinking maybe it uses it as the direction oposed to {1,1,1} so i tested it and it does indeed return the same color as {-1,-1,-1}. Maybe it was a coincidence in the one cube map i tried it on ^^

[edit2]
My mistake. It does indeed tile the texture. I was just loading the wrong object that had only repeated it once in its uvs.

Take care
AtomR

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: 26th Mar 2009 16:32
AtomR

A few comments:

Quote: "I'm going nowhere with this cubemap business."


Actually, you seem to be making progress.

Quote: "In this shader it assumes the values of each vertex's world position. In this example (-25,-25,-25) to (25,25,25)."


No. It uses the object's own internal coordinates whcich are fixed as the object rotates.

Quote: "In this example (-25,-25,-25) to (25,25,25). Since it just needs a direction vector i supose it'll be the same as (-1,-1,-1) to (1,1,1)."


Yes.

Quote: "In the simple cubemap shader your vertex shader receives a normal. Does it serve a purpose or did u just think you were gonna need it and forgot to take out later?"


You're right. It was left in by mistake - you can safely delete it.

Quote: "Same goes for Out.pos=mul(In.pos,wvp); This is not being used in the PShader. Does it still need to be there in the SVhader?"


That line is essential. It tells the GPU where to put the cube. It isn't used by the pixel shader. Every vertex shader must output values for the position vector.

Quote: "The texture rotates with it because the objects xyz positions are given in "local space" right? If, in the VShader, instead of Out.UVW = In.pos; we had Out.UVW = mul(In.pos, World); (where world is float4x4 World:World; duh!) the texture would stay put while the object rotated."


Yes and yes. Did you try? That's how I learnt how a lot of things work.

Quote: "As I was thinking in my previous post, by fixing one coordinate you can select which face gets textured to your objects."


I think so.
AtomR
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Portugal
Posted: 26th Mar 2009 18:01
Quote: "
Actually, you seem to be making progress.
"


LOL indeed. That's what happens when while you're writing your questions you end up having other ideas and trying them out. By the end of that post I had the shader doing everything i wanted it to do.

Quote: "Quote: "The texture rotates with it because the objects xyz positions are given in "local space" right? If, in the VShader, instead of Out.UVW = In.pos; we had Out.UVW = mul(In.pos, World); (where world is float4x4 World:World; duh!) the texture would stay put while the object rotated."

Yes and yes. Did you try? That's how I learnt how a lot of things work.
"


I did indeed try it out and confirmed but left the question to confirm if my reasoning was correct

Quote: "Quote: "As I was thinking in my previous post, by fixing one coordinate you can select which face gets textured to your objects."

I think so. "

That was a statement. You can

I've managed to alter the terrain shader to multi texture the terrain using the 2 mask bitmaps and sample the diferent texture from the cube map.
My next step will be to try adding a second cubemap with the normal maps+specular in alpha channel of each of these textures so we can have a terrain with 6 textures each of them normal mapped and with specular light.
Never done a shader that applies bump mapping so i'll probably fail miserably the first few attempts ^^

A few questions: Do cube maps make a big dent on performance or is it the same as having 6 textures loaded? Using cubemaps we can have 8*6=48 textures in an object Probably overkill but its good to have the option, no?

How do we create an object with more then one uv stage using 3ds max? I know how to do it DBP but for testing purposes i needed one that i could actually use in FXComposer.

How many instructions can we have on a vs/ps 2.0?

Take care
AtomR
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 26th Mar 2009 18:54 Edited at: 26th Mar 2009 18:55
Sounds like you are making excellent progress with this stuff.

Quote: "Do cube maps make a big dent on performance or is it the same as having 6 textures loaded?"


I haven't noticed any drop in performance - I think it's more like having one texture. Performance may depend on the resolution of the 6 component images.

Quote: "Using cubemaps we can have 8*6=48 textures in an object"


Yes - but you have that option anyway by putting several images side by side into one big texture and relying on the UV data to sort them out. The big problem with that approach is that you will get visible seams if you try to tile something like a terrain with one of the component sub-images. Whether or not it matters depends on what precisely you are doing of course. Food for thought (and experimentation) though.

Quote: "Probably overkill but its good to have the option, no?"


Yes.

Quote: "How do we create an object with more then one uv stage using 3ds max?"


I have no idea. I'm sure someone else will know.

Quote: "How many instructions can we have on a vs/ps 2.0?"


From the horse's mouth (the MS DX9 SDK):



The pixel shader is usually the bottle-neck. I've often had to fiddle with a pixel shader to get the instruction count down, but never the vertex shader.

Attachments

Login to view attachments
AtomR
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Portugal
Posted: 26th Mar 2009 19:15
Quote: "
Yes - but you have that option anyway by putting several images side by side into one big texture and relying on the UV data to sort them out. The big problem with that approach is that you will get visible seams if you try to tile something like a terrain with one of the component sub-images. Whether or not it matters depends on what precisely you are doing of course. Food for thought (and experimentation) though.
"


You can? Ouch
I had tried that method but no matter what i tried i couldn't get the shader to not show the whole bitmap. I had a 512*512 image with 4 256*256 textures in it. If i wanted to texture an object with the first half of the bitmap then i was doing uv=(u/2,v/2) which would have meant that for uv (0,0) => (0,0) and for uv (1,1)=>(.5,.5). I think this reasing is correct but somehow implemented it wrong. Will have to try that one again.

Unfortunatelly i need to focus on a school project for now and will be fiddling with shaders a lot less for the next few days

Take care
AtomR
Monk
15
Years of Service
User Offline
Joined: 25th Sep 2008
Location: Standing in the snow =D
Posted: 26th Mar 2009 20:47
Thanks GG, that certainly is enlightening, if not slightly confusing....

Quote: "There are two solutions (at least):

1. modify the shader so it uses the XZ coords of the terrain (with a suitable tiling);
2. modify your terrain's UV coords."


I dont know how to do either...

Could these "Quads" (not entirely sure what they are) be repeating because the terrain is made up of 10x10 objects in a grid?

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 26th Mar 2009 22:23
Monk

When I tried adjusting the tiling of your terrain object I noticed that the texturing changed as if neighbouring "quads", i.e. pairs of polys (polys = triangles in plain English ) re-started the UV coords from 0 each time whereas the shader assumes the UV coords increase gradually as you move along the terrain's Z or X axes.

I have no idea how you constructed your object. How did you specify the UV coords?

Anyway, to get to the point, you can change the UV coords using the set vertexdata UV command. I suggest you scale the UV coords so that, viewed from above, the top left corner of your terrain has UV = (0, 0) and the bottom right has UV = (1, 1) and scale intermediate vertices accordingly. You can probably just use the XZ coords, suitably scaled, for this.

An alternative is for some kind person to edit the shader yet again so that it converts the terrain's XZ coords into values in the range 0 to 1 in the same way. If you do that then the terrain doesn't need UV data at all. This assumes that the XZ coords of the terrain form a simple rectangular grid (but even that might not be essential).

AtomR

Quote: "I think this reasing is correct but somehow implemented it wrong. Will have to try that one again."


It should work. When you get time try it again and post back if you're still stuck.
Monk
15
Years of Service
User Offline
Joined: 25th Sep 2008
Location: Standing in the snow =D
Posted: 26th Mar 2009 22:44
This is the function I used to generate the memblock and the object that became my terrain objects.


Its the code from the memmatrix code snippet by ...(Ive forgotten who, Sorry) The name Kevin Verbeek springs to mind....

Im not entirely sure, but from my small recollection of memblock layouts, I dont think that code has the necessary UV bit on the end, but Ive never bother to correct that...

One of the areas Im planning to find out about sometime soon is UV, so now I have a reason I should get cracking

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 26th Mar 2009 22:55
From a quick glance you are setting the UV values to 0 or 1 which is exactly what I was suggesting - i.e. the last two entries in each of the 6 vertices are one of the four pairs (0,0), (0, 1), (1, 0), (1, 1). That block of code looks like a quad to me.
Plystire
21
Years of Service
User Offline
Joined: 18th Feb 2003
Location: Staring into the digital ether
Posted: 27th Mar 2009 12:24
Well, I just got my hands on DarkShader not too long ago and have been playing around with it.

I had been wanting Bloom in my game for a while but all previous attempts had failed. With the assistance of DarkShader it was rather painless, though.

The bloom that came with DarkShader was nice and all, but I wanted one that was tone-based (or as close to tone based as I could get) much like the way Evolved's Bloom had done it. So, I opened the two up side by side and surprisingly after a couple of hours of work, I got what I wanted!


I wanted to share the new customized Bloom with everyone and didn't know where to put it so I decided to put it here!

Attached is the .fx, DarkShader compatible and ready for launch!
Please let me know what you think and if there are any ways I could improve upon it.


The one and only,


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: 28th Mar 2009 15:24
Monk

I've sent you an email with a revised version of your earlier demo. Hopefully the revisions fix the problem you were having. The crucial changes were:



and



The first change merely re-instates more sensible tiling values and the second recalculates the UV values so that the lefthand edge of the terrain has U = 0, the righthand edge U = 1, and similarly for the V values of the edges in the Z axis. All other values are interpolated between these according to the terrain's XZ coords.

The revised UV code should also remove a lot of repeated multiplication which may speed things up a bit.

This new demo doesn't fix the other issue of having separate tiling for each texture which I'll post separately on my shader AT thread when i get a few minutes.
Monk
15
Years of Service
User Offline
Joined: 25th Sep 2008
Location: Standing in the snow =D
Posted: 28th Mar 2009 17:44
Thank you GG, I got the email and have replied before I read this... So the email is kinda no point now because you just explained it =)
Thanks, Ill keep messing with shaders!

AtomR
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Portugal
Posted: 29th Mar 2009 22:44 Edited at: 29th Mar 2009 22:52
I finally found some time to continue messing around with the terrain shader i'm working on and was very excited when i saw all 6 textures with bump mapping on FX Composer's preview screen. Imediatelly I set out to put it to work on DBPro but i can't :/ I based my normal map on GG's "GGbump01.fx" that I found on this thread and only used semantics that he used so I'm preaty sure i didn't use anything that is not DBP compliant. So why doesn't the shader work in DBP and it does on FXComposer?

In the pixel shader i'm only using UV0 for texture coords for the sake of simplicity. That shouldn't be the problem because that only means that all base textures + masks only repeat themselves once on the object instead of tiling.

My head is mush from searching the problem so i leave the project and FX file here if anyone has some spare time to try and see what's wrong. I've gotta go back to my school project and only in about a week (less if i'm fast) will i be free to devote myself to this again.
L8rz and happy coding

[EDIT]
forgot the attachment

Take care
AtomR

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: 29th Mar 2009 23:53
AtomR

If you want to apply shaders to Advanced Terrain objects you really need to look at the examples on this thread:

Shaders for Advanced Terrain

I've had a quick look at your demo (I'm in the middle of preparing supper at the moment ) and the obvious thing missing is that Advanced Terrain objects don't have normals so you need to change the object's vertex structure to include them. There's just a little bit more to it than that but if you look at the terrain blending demo with bumpmapping you'll see how it's done.

Have a look and post back if you get stuck. Can't remember off-hand whether there's another DBPro-inspired obstacle to overcome.

But you definitely need normals or bumpmapping won't work.
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 30th Mar 2009 13:50
Hi!
I have simple question about screen effect -
Is it avaliable to render one screen effect to another?
For example, i need several renders for post-bloom.
thanks

A door is a door is a door. Even a swinging one. I always wanted to take part in my life. Here, have a piece of gum. I've played Gulman 3D part 2!
AtomR
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Portugal
Posted: 30th Mar 2009 14:36
Before posting i thought about that o i converted the AT object's fvf to include normals and then did set object smoothing and since there was no change i assumed that wasn't the problem. But then again does set object smoothing work at all when all normals have 0,0,0 value? I'll checak that all out when i get home later.

Take care
AtomR
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 30th Mar 2009 15:04
Quote: "Before posting i thought about that o i converted the AT object's fvf to include normals and then did set object smoothing "


That should work - although you'll get visible seams between the AT limbs if you calculate normals that way. Some fvf formats need some extra fiddling about to get bump mapping shaders to work. Which fvf format did you use?

If you let me know I'll try to find time do some more testing with your demo myself.

But the bottom line is that your fvf format must have normals if you want to use texture space based normal mapping as in your shader - but not if you are using world or object space based normal maps (which you aren't).
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 30th Mar 2009 15:11
mrHandy

Your questions are rarely simple.

Quote: "Hi!
I have simple question about screen effect -
Is it avaliable to render one screen effect to another?
For example, i need several renders for post-bloom.
thanks"


I think the answer is "Yes" - but it really depends on what you are trying to do.

I guess you could use set camera to image then apply that to a screen quad then process the screen quad again.

Or, you could use several passes in the same technique using intermediate render targets as in the Dark Shader blur and bloom shaders (if I recall correctly).
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 30th Mar 2009 15:38 Edited at: 30th Mar 2009 15:52
AtomR

Just had a closer look at your code. Here's one problem:



You've only set vector 1 so I guess vectors 2 and 3 are initialised to zero which in turn means that both "ambiColor" and "lightColor" are set to zero, i.e. no light.

But you still need normals ...

Here's your code fixed (but you'll have to study the rendered scene to check it's what you want - see screen shot):



Attachments

Login to view attachments
AtomR
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Portugal
Posted: 30th Mar 2009 17:17 Edited at: 30th Mar 2009 19:56
Ah damn it, why do i always need to copy paste everything instead of writing it from scratch X_x Suits me right for being so lazy. So when i added normals i still had everything both ambient and light colors as 0,0,0 i.e. Black, which is exactly what i was seeing on the terrain. Thank you, Gandalf.
Gonna look on the web for some math on how to calculate normals and then do some proper smoothing on the terrain.

[EDIT]

Is there such thing as an INT command in HLSL? For instance I want to turn 17.05 into 0.05. In basic i'd 17.05-INT(17.05). How can i do that in shader speak

[EDIT2]
This is it i think.

Quote: "modf (DirectX HLSL)

Splits the value x into fractional and integer parts, each of which has the same sign as x.
ret modf(x, out ip)
Parameters

x

[in] The x input value.
ip

[out] The integer portion of x.

Return Value

The signed-fractional portion of x.
Type Description
Name In/Out Template Type Component Type Size
x in scalar, vector, or matrix float, int any
ip out same as input x float, int same dimension(s) as input x
ret out same as input x float, int same dimension(s) as input x"


Take care
AtomR
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 30th Mar 2009 20:27
round(x) - returns x rounded to the nearest integer.

A door is a door is a door. Even a swinging one. I always wanted to take part in my life. Here, have a piece of gum. I've played Gulman 3D part 2!
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 30th Mar 2009 20:31
Also!

In 3ds max, when you applying UVmap or unwrap, you should use UV channel number as it is UV stage itself.

A door is a door is a door. Even a swinging one. I always wanted to take part in my life. Here, have a piece of gum. I've played Gulman 3D part 2!

Login to post a reply

Server time is: 2024-05-02 19:51:27
Your offset time is: 2024-05-02 19:51:27