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: 17th Mar 2008 15:37
Radek Inkom

Here's a test version of a PS1.3 shader which does scrolling bumpmapping with some distortion. This is the best I can do for the moment.

The "distortion" is fake - I've just scrolled the normal map at a different speed from the main texture. This almost works with water.

Also, I don't think it is possible to get the per-pixel lighting right with PS1.3 - especially for low-poly objects like your sea limb.

In the demo I've set the specular colour to red so you can see it clearly - the high poly plain shows the specular reflection (but not very well) but is almost invisible with a simple plain. This is a consequence of several awkward restrictions in PS1.3 shaders:

1. vertex shader output variables are clamped to the range 0 to 1 (unless used for UV coords - which is why the scrolling does work)

2. the normalize() intrinsic function is not available in the pixel shader - this is needed for correct lighting

3. the pow() intrinsic function is not available in the pixel shader - this is needed for realistic specular highlights

4. dependent texture lookups are not supported - so the "distortion" method I used in the earlier PS2 cannot be used

5. the maximum instruction count is very low - which means it's virtually impossible to get around the other difficulties (and means even this shader has to be split into two passes).

Personally, I don't think the effort is worthwhile with PS1.3 or PS1.4.

I'll attach your demo with the shader to the next post.

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: 17th Mar 2008 17:03 Edited at: 17th Mar 2008 17:25
Radek Inkom

Here's your demo with the trial revised shader. The specular reflection is wasted really and could be removed from the shader.

There is one more thing I can try which will give some sort of specular reflection - not convinced it will look OK till I try.

[Edited spelling. ]

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: 17th Mar 2008 17:31
Quote: "There is one more thing I can try which will give some sort of specular reflection - not convinced it will look OK till I try."


Here are the revised again dba file and shader. This uses a quick fix to get a specular reflection - but as you can see it doesn't change as the observer's view changes (the shader uses a fixed camera view for all pixels). The specular reflections look like a static texture scrolling with the sea.

Probably better without any attempt at specular reflection. Anyone got any other ideas for per-pixel specular reflections with PS1.3?

Attachments

Login to view attachments
Olby
20
Years of Service
User Offline
Joined: 21st Aug 2003
Location:
Posted: 17th Mar 2008 18:33
Hey Gandalf could you give me some tips on how to implement simple vertex alpha value for my shader? I did a quick search but was unable to find a answer for this.

What I actually need is a way to give my reflection shader ability to be alpha transparent - problem is that the DBPro SET ALPHA MAPPING ON does not work on shaded objects. I know it is something to do with the vertex shader pass but I need something more to do this.

The shader I am using is from the DarkShader "Reflection"

Thanks,
Olby


ACER Aspire 5920G: Core2Duo 2.2GHZ, 2GB, GeForce 8600M GT 1280MB, DirectX10, DBPro 6.7
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 17th Mar 2008 20:13
Quote: "how to implement simple vertex alpha value for my shader?"


Do you mean that your object has a diffuse colour at each vertex and you want to use this to set the vertex alpha, or, do you mean that you want to use the alpha component of the object's texture, or do you mean something else such as setting a specific alpha value for the whole object?
wh1sp3r
20
Years of Service
User Offline
Joined: 28th Sep 2003
Location: Czech republic
Posted: 17th Mar 2008 20:46
Hi, I have problem with my code




Screen is always blue .. Object is completly transaprent.

dbpro:



Why ? Where is a problem ? Thank you !


PS: Real programmers aren't afraid of math!.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 17th Mar 2008 21:25
jason p sage
16
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 17th Mar 2008 22:14 Edited at: 17th Mar 2008 22:20
@Green Gandalf - Did you Get My Email? I emailed you with a zip of media and a question. Basically I asked you if my goal for that shader was reasonable.

I want to add Linear Fog (Set color to This Fog) for a water shader I'm using. I'm not fond of shaders (due to my slow vid card) But I need the water shader and having it blend in with DBPRo/DarkGDK (Built In FFP) Fog would Awesome.

On Second thought, having ability to make it transparent would be all the cooler (for underwater looking up LOL)

Thanx in advance....

[edit]DOH - Saw the Email - Thanx![/edit]

Olby
20
Years of Service
User Offline
Joined: 21st Aug 2003
Location:
Posted: 17th Mar 2008 22:17
Quote: "do you mean something else such as setting a specific alpha value for the whole object"


Yes, I mean this. Just like Set Alpha Mapping On, but only done directly in the shader.


ACER Aspire 5920G: Core2Duo 2.2GHZ, 2GB, GeForce 8600M GT 1280MB, DirectX10, DBPro 6.7
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 17th Mar 2008 22:35
Quote: "Yes, I mean this. Just like Set Alpha Mapping On, but only done directly in the shader."


That should be simple.

But must finish cooking and serving our meal.

Hmm ... lots of shader queries all of a sudden. Not surprising I never write any of my own.
Radek Inkom
17
Years of Service
User Offline
Joined: 28th Feb 2007
Location: Poland
Posted: 17th Mar 2008 23:31
Green Gandalf

Thank You
This last shader is cool.

My app will check user PS version and load 1.3 or 2.0 PS shader.

Thank again

Radek
wh1sp3r
20
Years of Service
User Offline
Joined: 28th Sep 2003
Location: Czech republic
Posted: 18th Mar 2008 11:42
I have a question. I'd like to know, How can I change the color of selected pixels in shader.

I know, that pixel shader go thru all pixels, but I would like to draw a small square in the middle of texture.

How can I do it ? Thank you for explantation !


PS: Real programmers aren't afraid of math!.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 18th Mar 2008 20:01
@Olby

Quote: "The shader I am using is from the DarkShader "Reflection""


Is the attached demo what you're after? It's based on one I made earlier ... (but uses a slightly modified Dark Shader shader for your effect).

Attachments

Login to view attachments
Olby
20
Years of Service
User Offline
Joined: 21st Aug 2003
Location:
Posted: 18th Mar 2008 20:45
Excellent, this is just what I wanted! Thanks a bunch Gandalf. You're now credited.


ACER Aspire 5920G: Core2Duo 2.2GHZ, 2GB, GeForce 8600M GT 1280MB, DirectX10, DBPro 6.7
wh1sp3r
20
Years of Service
User Offline
Joined: 28th Sep 2003
Location: Czech republic
Posted: 18th Mar 2008 21:07
Green Gandalf : Hi GG, the big king of shaders Have you answer on my question ? please btw, DarkShader is really cool for learning, I like compiler, which shows errors and warnigns


PS: Real programmers aren't afraid of math!.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 19th Mar 2008 00:13 Edited at: 19th Mar 2008 00:14
Quote: "I know, that pixel shader go thru all pixels, but I would like to draw a small square in the middle of texture."


Should be easy.

If the object has standard UV coords ranging from 0 to 1 then you can mark the middle as the point (0.5, 0.5). If the square has width W then you just need to test whether the UV x and y coords lie within the range 0.5 +-W/2. If they are then output your chosen colour or image - and other way round if they are not. [Edit: but it won't usually be a square of course unless you do a bit more work. Should be a start though.]

Should be a useful exercise in how to use things like if { } else { } statements, lerp() and/or various step functions.

Do a Google search for HLSL intrinsic functions and useful links like the following will pop up.

http://www.paradoxalpress.info/Docs/dx9_out/dx9_graphics_reference_hlsl_intrinsic_functions.htm

Have fun!
wh1sp3r
20
Years of Service
User Offline
Joined: 28th Sep 2003
Location: Czech republic
Posted: 19th Mar 2008 01:41
Green Gandalf : I thought it In vertex shader, It goes thru all vertexes as in pixel shader , yes ? So, I think, that shaders are really easy to program, when I understand, how shaders works at last ! Thank you


PS: Real programmers aren't afraid of math!.
wh1sp3r
20
Years of Service
User Offline
Joined: 28th Sep 2003
Location: Czech republic
Posted: 19th Mar 2008 10:36 Edited at: 19th Mar 2008 10:42
[EDIT] Sorry, I had stupid problem, which I solved


PS: Real programmers aren't afraid of math!.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 19th Mar 2008 12:58
Quote: "Sorry, I had stupid problem, which I solved"


Sounds like good news.

I wish my problems could be solved that easily.
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 19th Mar 2008 17:18
Hi!
I have qestion for any smart guy

Is it possible to make orto camera, that renders screen to tex layer, then shader projects this image (although orto)using cubemapping, and main camera is FOV100?

This is nessesary thing for any shadow shader, that emulates sunlight. Umm...any means shadow mapping from Evolved, perfect shader exept one thing - light angle. If you want to make sunlight, all sunbeams must be parallel, right? And this shader is OMG 100 degrees! Funky-crazy sun =)

I know, that everything is (teoretically) posible but what about DBP?

A door is a door is a door. Even a swinging one. =0
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 19th Mar 2008 17:40
You can render cameras using an orthographic projection, not exactly natively but you can alter the projection matrix of a camera to make it so(Cloggy's D3D DLL also has this ability).

If you render this to texture why would you put it on a cube map? Cube map lookups require an angle vector to be used to calculate where to sample from, orthographic cameras don't render angles(cones).

What you would instead do is point your depth camera(light source) to the main camera and render say, a 1024^2 texture where each texel represents a 1unit^2 area, thus your shadows wouldn't work over a 512unit height and width from the center of the viewport relative to the depth camera's rotation. In your object's vertex shader, in addition to transforming the positions by the main camera's projection matrix you'd also transform by your orthographic camera's one(so you can find out where the vertex is relative to the rendered image), it's then just a case of passing this position to the pixel shader and you can see where it is on your depth map from your light source's camera and see if it's below the current depth and shadow it. Have fun writing this shader .

Also this method has the downside of having a limited area of shadow, but other than using a stupidly high resolution render target image you could use multiple shadow stages, so in addition to your 1unit = 1texel shader map stage you could have a 2nd where 10units = 1texel, thus with the same 1024 map you'd have shadows up to 10240units away, and so on. I've read about this method from some articles somewhere so I'm sure it has a name.

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 19th Mar 2008 20:48
Quote: "If you render this to texture why would you put it on a cube map? Cube map lookups require an angle vector to be used to calculate where to sample from, orthographic cameras don't render angles(cones)."


What sort of orthographic projection are you talking about? An orthographic projection of an object onto a surrounding cube gives you a cube map, e.g. see this link:

http://www2.arts.ubc.ca/TheatreDesign/crslib/drft_1/orthint.htm

Essentially the same idea can be used to construct an orthographic projection of a scene onto an enclosed cube (as might be used for rendering realtime reflections etc in DBPro - except you don't need all 6 views updated at the same time I suppose).

Orthographic projections in cartography seem to be rather different, e.g.:

http://en.wikipedia.org/wiki/Orthographic_projection_(cartography)
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 19th Mar 2008 21:28 Edited at: 19th Mar 2008 21:30
But to make a cube map you need 90degrees FOV for all 6 sides, if you use an orthographic projection i.e. no perspective, then the cube map will be incorrect, it's hard to explain without making diagrams. Essentially with an orthographic camera, if you are looking at an object, it will render the exact same(assuming it is always in-front of you) no matter the camera's distance to it, which is useful for many things, including shadow mapping where precision at any distance from the light source is vital, but for making a cube-map, for use on a skybox, or reflection, as you suggested, it wouldn't be useful; as none of the images would seamlessly tile, you require a 90degree FOV for this, as if you visualize a 2D box with a point in the middle, you then place 4 cameras at this point, each with 90degree FOVs and rotate them 90degrees from each other, you will end up with a circle of camera coverage, but if you use orthographic cameras, which can be illustrated as boxes that extend from the camera to the camera's far clipping range, and their widths are whatever you set them to, you'll only cover what would be a big + sign, anything diagonal to you is invisible.

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 19th Mar 2008 21:51
You're right - I misinterpreted the websites that I gave as references. They both quite clearly give what you've just described. I was obviously having "a senior moment".

Cheers.
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 20th Mar 2008 11:27
Wow...
I've understood only few words...

Umm...could you please write simple schematic step-by-step instruction? Only thing i can do is copypaste one shader parts to another shader.

And what is Cloggy's D3D?

PS And I dont wont make cubemap at all, i'd prefer simple image projection from orto 'sun' camera. Or smth like that...

PPS If you need shadow mapping shader, i can post or you can it find on Odyssey Creators.

A door is a door is a door. Even a swinging one. =0
Alsan
18
Years of Service
User Offline
Joined: 20th May 2005
Location: Germany - Nähe Kassel
Posted: 25th Mar 2008 23:18
Okay, I tried for nearly 4 hours but I give up now. ^^

I have the following problem:



These Blocks are one mesh and were generated using a memblock.

There are three textures, one for the sides, one for the top faces and one for the bottom faces. So I have 3 completely different UV Stages. (I really need these 3 textures, putting them all into 1 texture is absolutely no option. Furthermore, splitting the mesh up into 3 seperate meshes is no option, too.)

The problem is, that only texture 1 is visible because it covers everything under it.

I thought of different methods how I could achieve that on the side only the side texture (stage 0) is displayed and so on. I remembered Green Gandalf's terrain shader (which is great btw ^^) and tried to rewrite it, without success in the end. The picture above shows a mask texture which I added on a new stage with a fourth set of UVs.

I'd be glad if someone could point me into the right direction on how to do this.

Sorry if anything is unclear, I'm near the limit of my English skills.

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 26th Mar 2008 01:10
Quote: "I remembered Green Gandalf's terrain shader"


There's more than one, of course.

The one you want allows you to blend three textures using the RGB colours of a fourth map. You probably only need TWO UV stages - one for mapping the three textures of interest, the other for the RGB "look-up" map.

This is what to do.

In the RGB map:

1. Pure red is texture 1.
2. Pure green is texture 2.
3. Pure blue is texture 3.

In UV stage 0, set the UV coords equal to the appropriate values for the three textures (I'm assuming the vertices are NOT welded along the edges ).

In UV stage 1 set the UV coords equal to appropriate non-overlapping values for the RGB map, e.g.

1. for the top, use the rectangle with corners (0.0, 0.0), (0.2, 0.0), (0.0, 1.0) and (0.2, 1.0);
2. for the sides use the four corners (0.3, 0.0), (0.5, 0.0), (0.3, 1.0) and (0.5, 1.0);
3. for the bottom use the four corners (0.6, 0.0), (0.8, 0.0), (0.6, 1.0) and (0.8, 1.0).

Then colour the RGB map accordingly.

In the pixel shader just use:



Something along those lines should work.
Alsan
18
Years of Service
User Offline
Joined: 20th May 2005
Location: Germany - Nähe Kassel
Posted: 26th Mar 2008 15:49
Hi,

thanks for your help.

Quote: "(I'm assuming the vertices are NOT welded along the edges )."


No, they aren't welded.

I used the terrain shader as a base and changed everything what you said.

This is what I have so far:


(Am I right that the struct thingies are something like UDTs in DBPro?)

And this is what it produces ingame:



So it basically mixes all 3 textures rather than using just one, though the Mask looks completeley as it should (like in the other picture I posted above).

Is there an error in the shader code? Do I need the vertex shader thing at all?

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 26th Mar 2008 22:51
You need to input (and output) both sets of UV coords into the vertex shader - it's anyone's guess what your pixel shader is using for UV1.

Your vertex shader is outputting UV0 (and calling it UV) whereas your pixel shader is expecting UV0 and UV1 - they need to match up.

Otherwise, from a quick look, you seem to be doing the right thing.

[It's possible you don't need the vertex shader. There's one way to find out. ]
Alsan
18
Years of Service
User Offline
Joined: 20th May 2005
Location: Germany - Nähe Kassel
Posted: 27th Mar 2008 00:25
Yessa. It works now, and is extremely fast! Thank you so much!

I deleted the Vertex Shader and rewrote the mask UV Mapping in the DBPro Code.



Shaders are beginning to get fun.

One last question: I looked at every single shader line and tried to understand it. It worked, except for

Quote: " minfilter = linear;
magfilter = linear;
mipfilter = linear;
addressU = wrap;
addressV = wrap;"


What do these lines do? I'm really interested in learning HLSL, so any help/explanation/link would be greatly appreciated.

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 27th Mar 2008 13:40
Quote: "Thank you so much!"


You're welcome. Glad to be able to help.

Quote: "Shaders are beginning to get fun."


Indeed. There's nothing like getting your first one or two working which you have designed yourself - and this one was your design really.

Quote: "Quote: " minfilter = linear;
magfilter = linear;
mipfilter = linear;
addressU = wrap;
addressV = wrap;"

What do these lines do?"


Roughly, these do the following:

minfilter/magfilter

These tell the GFX card how to deal with screen pixels which are larger/smaller than the texture "texels" which are being displayed on the screen. The common choices are (a) "linear" which means an appropriate bilinear average of neighbouring texels is used, or (b) "point" which means that the nearest texel is used. [I think these options correspond to the ones fixed by the DBPro set object filter command. ]

Replace "linear" by "point" and see what happens.

mipfilter

This is similar to min/magfilter but concerns the use of mipmaps. When you get close to an object a high resolution mipmap would be used - and, conversely, far away from the object a low-resolution mipmap of your texture would be used. The filter tells the GFX card what to do at intermediate distances (well all distances really), i.e. should the card use the nearest mipmap ("point") or perform a smoothing average of adjacent mipmap resolutions ("linear")? There are some other options. Without filtering there might be a noticeable "jump" in resolution as you approach an object from a distance.

Again, you could try an experiment.

addressU/addressV

This setting tells the GFX card what to do when the UV coords (or filtering points) are outside the range 0 to 1, e.g. should it wrap around to the other side of the texture ("wrap") or should it use the value of the boundary ("clamp")? You can also choose "mirror". The options "wrap" and "clamp" are the most common ones, e.g. if you are tiling a seamless texture over a large plain you would probably want to use "wrap", if you are texturing a skybox you would probably want to use "clamp" (this might be needed to stop the filtering from averaging the wrong pixels along the edges of the box, e.g. you might have blue sky along one edge of a face and cloud along the other, "wrap" would produce a visible seam in this situation).

Quote: "I'm really interested in learning HLSL, so any help/explanation/link would be greatly appreciated."


Chris K included a few suggestions of mine in his first post on this thread. I used a few books a lot when I first got started with shaders but now I tend to refer to the MS DX9 SDK documents. It's definitely worth downloading it if you can afford the time and space. Although it's rather large and the Help file is cumbersome, it is crammed full of useful information which makes more sense the more you use it.

If I'm really stuck I do a Google search and browse till I find something relevant/comprehensible/etc - or I try a few educated guesses. I often find that a bit of experimentation is the best way of finding out how things work.
Alsan
18
Years of Service
User Offline
Joined: 20th May 2005
Location: Germany - Nähe Kassel
Posted: 27th Mar 2008 14:41
Downloading the SDK right now.

Once again, thanks for this great explanation. Especially the AdressU and AdressV flags seem to be extremely useful, there are so much problems you could easily solve with a small, little shader.

I'll definitely do further experiments with those flags and try to write other simple shaders. And it's great to know that if I'm stuck there are shader gurus like you to help me out.

Thanks again! (Sincere apologies for my fanboy speeches)

Alsan
18
Years of Service
User Offline
Joined: 20th May 2005
Location: Germany - Nähe Kassel
Posted: 28th Mar 2008 00:45
Okay, next problem arrived.

I forgot that the textures need to be dynamic, not hardcoded, and changeable from DarkBasic (I'd really like to use my own resource management).

I've two ideas on how I could achieve this.

The first would be going back to the 4 UV Stage solution, which I'd like to ignore until I know the other one is impossible.

The other one: Can I use and manipulate string variables in shaders? I searched through the SDK and strings weren't mentioned anywhere. Besides there seems to be no "set effect constant string" command in DBPro.
I could also think of using a string array (which contains all texture paths) in the shader and just give an index to the shader.

So, are strings supported and, if yes, how could I manipulate them from DB? Or any other ideas how I could keep the textures dynamic and changeable from DB?

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 28th Mar 2008 02:12
Quote: "I forgot that the textures need to be dynamic, not hardcoded, and changeable from DarkBasic (I'd really like to use my own resource management)."


If they are really dynamic, e.g. from a changing scene, then you can use the camera commands to output a dynamic image.

If they are just selected from a list of preloaded textures then use something like the following in your main program loop:

Alsan
18
Years of Service
User Offline
Joined: 20th May 2005
Location: Germany - Nähe Kassel
Posted: 28th Mar 2008 02:47
Hi,

Guess I wasn't clear enough.

The Shader I'm using now:



At moment Textur 1.dds, Textur 2.dds and Textur 3.dds are hardcoded. I want to define those textures in DB. Besides I need to use the same shader on various objects with different textures. Quite hard to explain. ^^

jason p sage
16
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 28th Mar 2008 02:53
I'm NOT a SHADER expert and I can only change the tweakables to date BUT I know you can pass parameters to shaders, and I know VanB's modified shader thats I THINK is part of the terSculpt terrain editor project - allows dynamically CHANGING a texture that a shader is using.. e.g. you can draw "paths" in a alpha map or whatever and it blends two textures based on said path in aplha map.

what your describing makes sense.. But you MIGHT need to load the SHADER/EFFECT multiple times - where each "version" is using its own set of images... and you SHOULD be able to edit these images in memory.

I recommend scoping out VanB's TerSculpt because I believe that project is open source, works, runs, compiles, and let's you really see it in motion - so you can take what you need from it and meet your goal head on!

dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 28th Mar 2008 03:05
To use the same shader with different textures on different objects, first make the sure 3rd param on the Load Effect command is set to 0, then once you load the object you need to manually texture each stage, i.e. Texture Object objectID, stageID, imageID, though when I write shaders I always leave the texture paths blank so such an issue never arises.

jason p sage
16
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 28th Mar 2008 03:14
DarkCoder - does this allow you to have the effect loaded just once?

dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 28th Mar 2008 03:28
Yes, but remember doing this means all shader constants will be shared so it's not always beneficial to do this.

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 28th Mar 2008 13:31
Quote: "DarkCoder - does this allow you to have the effect loaded just once?"


Not quite.

You can either load ONE copy of the shader and apply it to several objects - which, as Dark Coder says, means that the objects share the same parameters (but NOT necessarily textures if you use load effect "myEffect.fx", myEffectNum, 0), or you can load multiple copies of the same effect and apply them to different objects. For example, you might load several copies of a bumpmapping shader so that you can set specular reflection on some objects and not others.

Like Dark Coder I usually leave the file name blank in the shader - except when I'm testing in FX Composer - but it makes no difference if you use the zero flag.

Alsan Sorry. I assumed you knew how to set textures for effects in DBPro. You just need things like:



You can leave the hard-coded file names in the shader if you wish - sometimes useful for testing.
Alsan
18
Years of Service
User Offline
Joined: 20th May 2005
Location: Germany - Nähe Kassel
Posted: 28th Mar 2008 14:30
@ jason p sage: Thanks! I downloaded TerSculpt, but it also uses one of Green Gandalf's Terrain Shaders which need more than 2 UV Stages.

@ Green Gandalf, dark coder: ^^ The problem I tried to explain was, that I only have 2 UV Stages where I have 4 textures (Quote "You probably only need TWO UV stages - one for mapping the three textures of interest, the other for the RGB "look-up" map."). Leaving out the mask, which uses 1 stage and 1 texture, I would need to texture the same stage with 3 different textures.

The shader doesn't have this problem as the textures loaded in it need no separate UV Stage. But they are hardcoded, which makes it useless for me.

The code you posted would be the 4 Stage solution I mentioned above, which I'll most likely use as I don't think my other idea is possible. ^^

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 28th Mar 2008 14:52
Alsan

The texture stages used in DBPro have nothing to do with the UV stages of your object.

All you need to do in DBPro is make sure the texture stages used in DBPro are in the same order as the textures declared in the shader.

For example, in DBPro you might have:



and in the shader you might have




If you load the shader using



then DBPro will set the shader so it uses "Textur 1.dds", etc.

If you use



then DBPro will use "tex1.png" in place of "Textur 1.dds", "tex2.png" in place of "Textur 2.dds" and so on. This has nothing to do with the UV coords in your model.

You tell the shader which set of UV coords to use in the pixel shader, e.g. in your code:



This tells the computer to use UV0 when looking up Tex1, Tex2 and Tex3, but UV1 when looking up the rgb map. The coords UV0 and UV1 can be any 2D coords you choose - but in this case they happen to be the stage 0 and stage 1 UV coords of your OBJECT, which are passed to the pixel shader via the input structure



in your shader. The semantics texcoord0 and texcoord0 in this case are assumed by the shader to correspond to the two sets of UV coords. If you had used a vertex shader you could over-ride that assumption with whatever you decided to put in texcoord0 and texcoord1 in the vertex shader.

This distiction between texture stages and UV stages is a common source of confusion in DBPro.
Alsan
18
Years of Service
User Offline
Joined: 20th May 2005
Location: Germany - Nähe Kassel
Posted: 28th Mar 2008 15:12
Ahhhhhhhh. I wasn't aware of that difference. ^^

It works now, thanks for your patience.

mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 31st Mar 2008 11:50
Hi! Do you know how to 'lerp' texture with a COLOR? I want to use value to make taxture more white or more black, and now using lerp(Tex,WhiteTex/or/BlackTex,Value) I think that way is working but not right.

2 i wanted to make transparent plain, that have 'gradient' transparensy at edges. Is it really?

3 is it available to project texture not from main camera? Or only cubemap? For example, all dynamic shadows in Unreal Tournament is low-res projected textures from imagenable sun, ortho projected! Is it cubemap?

PS shadow question is very popular on this forum, some people pro's at shaders (guess who ) but nobody made really useful shadow shader. The best attempt was deferred shading 3 by Evolved, but it has limitations - up to 7 lights, ALL shadows or ALL not, omni only, distanse ~5 meters. But, they were tough and worked with transparency! Another his grat work is shadow mapping, its working perfect - exept two things - not orto and as its not orto, so light must be far away to make more parallel shadows, and this bad for shadow detail - it becomes one pixel per square meter! So maybe somehow somebody from forum's pro's in far future will make this shader working properly...
Have a nice day!

A door is a door is a door. Even a swinging one. =0
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 31st Mar 2008 12:31
Quote: "and now using lerp(Tex,WhiteTex/or/BlackTex,Value) I think that way is working but not right."


Should work fine, lerp is a linear transition between two values(can be multiple component values too, like colours), so if you use a 3rd value of 0.0 then the first value will be returned, if you use 1.0 then value 2 will be returned, 0.5 means you'll get exactly 1/2 way between 1 and 2, for all components of your return value.

However if you want to transition to black, it's quicker to just have colour.rgb(or xyz) *= value.

Quote: "2 i wanted to make transparent plain, that have 'gradient' transparensy at edges. Is it really?"


Quite easy, assuming it's UV mapped like a default DBPro plane then you can just calculate this yourself without using a texture lookup(faster 99% of the time), I'd first bias the UV values by -0.5 so I end up with a UV range of -0.5 to 0.5, then I'd force the sign to positive(i.e. abs) so I'd have a range of 0.0 to 0.5, 0.5 would be my edges, and depending on the effect you want I could just subtract 0.25 from this value and multiply the result by 2.0 and multiply the return value's alpha by the inverse of this number(1.0 - value).

Quote: "3 is it available to project texture not from main camera? Or only cubemap? For example, all dynamic shadows in Unreal Tournament is low-res projected textures from imagenable sun, ortho projected! Is it cubemap?"


Didn't I answer this earlier already? At the top of the page on my posts per page setting.

mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 31st Mar 2008 17:17 Edited at: 31st Mar 2008 17:18
TO: dark coder

Could you please post short usage example of
Quote: "colour.rgb(or xyz) *= value"
?

PS And curious qestion - you know so much, so you must have your own shadow shader, yeah? If not -why? Honestly, wery interesting!

A door is a door is a door. Even a swinging one. =0
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 31st Mar 2008 17:48
Quote: "Could you please post short usage example of"


Well that's basically it, if you have a 3 component vector such as colour, you may have something like, IN.colour *= brightness;

Quote: "so you must have your own shadow shader, yeah?"


I haven't made any finished shadow shaders, I once briefly attempted to make an orthographical shadowing shader but forgot that you can't directly get depth from such a projection so I shelved the idea, It'll probably be ages before I get around to finishing it so I wouldn't wait around.

You should try learning yourself instead of waiting for others to make it though :p, all it requires is knowledge of shader structure, good math knowledge, especially matrix/vector calculations pertaining to camera projections, so get reading!

mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 31st Mar 2008 17:58
To dark coder
Sorry, please dont sink i'm stupid, but where i must put this code?
In pixel shader, like

...
float3 color={255,255,255};
...
float3 Texture=Texture*IN.color

?

And i just saw video on youtube, its about FPS creator shadow demo - it says that its not stencil! Comments?

A door is a door is a door. Even a swinging one. =0
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 31st Mar 2008 18:09
Quote: "float3 color={255,255,255};
...
float3 Texture=Texture*IN.color"


Uhh, you can't declare a float then multiply it by something without actually assigning a value to it, or at least doing so would be stupid.

Assuming this 'float3 texture' is filled via sampling from a texture, you could then just have 'texture *= color' assuming your 'float3 color={255,255,255};' is a shader constant, or global variable.

And what Youtube video? I'm not telepathic, I can't guess which video you're talking about :/. By default FPSC X10 uses stencil shadows, but it's not impossible to change this by means of a different shader or doing the same with FPSC.

mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 31st Mar 2008 18:26
//just search 'fpsc shadows'//

so like this

float3 color = {1,1,1}; //1 is white and 0 is black AFAIR

float3 Texture=tex2D(Base,IN.tex);
Texture*=color;
return float4(Texture,1);

Now correct?

A door is a door is a door. Even a swinging one. =0

Login to post a reply

Server time is: 2024-05-19 02:26:06
Your offset time is: 2024-05-19 02:26:06