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: 31st Mar 2008 18:26 Edited at: 31st Mar 2008 18:28
In other words, try something like the following in the pixel shader:



This assumes that things like "col", "mySampler", etc, have been correctly defined elsewhere in the shader.

Edit: just seen previous post. Answer is "YES".
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 31st Mar 2008 18:33
Thanks! I think its better that 'lerp'! (faster)

A door is a door is a door. Even a swinging one. =0
bergice
16
Years of Service
User Offline
Joined: 5th Jun 2007
Location: Oslo,Norway
Posted: 31st Mar 2008 21:58
Chris K @

On page 2 you said you where doing a caustic shader.
Is it finished?
I soo need it!
Credit
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 3rd Apr 2008 11:39
Hi!

I need to mke one trick in shader like

Pixel shader
if dot(Base tex)='black color'
...1
else if 'white color'
...2
endif
return float...

ps and white is {255,255,255}?

Very need to make this example in correct syntax!

Thanks guys!

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: 3rd Apr 2008 13:33
You generally shouldn't do many conditional if statements in a shader as it can get run through over a million times per frame(or more), but as you asked, the syntax is the same as C++:





So in this case you want:



Though you may be able to get away with an implied condition(but I haven't tested it), i.e.



mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 3rd Apr 2008 16:43
Thank you!

But you said that func slows down FPS?

A door is a door is a door. Even a swinging one. =0
Math89
20
Years of Service
User Offline
Joined: 23rd Jan 2004
Location: UK
Posted: 3rd Apr 2008 18:48
There is another interesting syntax for conditions, don't know if it changes the performances (actually, I don't think so, but I'm not sure ).

float value = (mySampleTexture > 0.0 ? 1.0 : 0.0);

value will be set to 1.0 if the texture isn't completely black, 0.0 otherwise.


Conditions and loops are always slow because the computer needs to jump to a different part of your code. You could avoid this in assembler language using conditional instructions, but asm is not recommended to learn shaders.
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 3rd Apr 2008 19:03 Edited at: 3rd Apr 2008 19:10
Quote: "You could avoid this in assembler language using conditional instructions"

Conditional constructs always require jumps, apart from in the case of (non-jumping) conditional instructions, of which I'm not sure if any actually exist in shader programming.

mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 7th Apr 2008 12:16
Hello again!
I've got tough situation:
Cube
Textured using UWV Unwrap
One map, one side of cube per part of map.
Task: paint known pixel on map with color
Ways: shader or memblock

Please help, i really need this thing (gameplay key moment)

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: 7th Apr 2008 12:39
What do you mean? If you have a cube with 6 texture maps, aka 6 sets of UV data, on 6 limbs, you can just normally texture each limb with a different texture, why does this require a memblock or a shader?

mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 7th Apr 2008 18:23 Edited at: 7th Apr 2008 18:28
No, one object!
And cube is example, really object is ~500 trianges and have complex form. The map is 512x512.

But, i've asked wrong question... REALLY I need (using sparky) somehow find location of pixel on 3d model and paint it. And map is second stage with custom UV, originally model is multi-textured with other UV.

Maybe I've wrote wrong again so here is gameplay example:
1st person, player points centered aim on object, and where he points pixel draws with special color.
NOTE: map pixel, not screen pixel

Yeah, I really don't know the best (aka fastest) way to calculate this...Man, I really get stuck on this!

A door is a door is a door. Even a swinging one. =0
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 7th Apr 2008 22:04 Edited at: 7th Apr 2008 22:05
Quote: "NOTE: map pixel, not screen pixel"


I assume you mean the point on the object?

Why not just get the point's XYZ coords using standard DBPro commands,
then pass these to the shader in a vector,
then pass the interpolated object world position coords from the vertex shader to the pixel shader,
then do a test in the pixel shader to check the length of the difference between the object's interpolated world position and the input point's XYZ coords,
and if the difference is small enough (NOT exactly zero for the usual reasons!), paint your special colour, otherwise paint the usual texture or colour?
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 8th Apr 2008 09:21
nonono! =) PS advice's good...
I need to paint map pixel not on screen but direct to map!
It must stay painted until special command, even if I cant see that pixel! (like vertex paint)

A door is a door is a door. Even a swinging one. =0
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 8th Apr 2008 11:47
Do you mean you want to change the object's texture, or the image used to texture the object, or both, or something else?

If you just want to change the image that's used for texturing the object then you don't need a shader - what you need is a way of converting the object's world XYZ coords into the image's UV coords (or something like that) and then make the relevant change to the image using standard DBP commands.
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 8th Apr 2008 12:04
1. he image used to texture the object
2. Yep, XYZ to UV... know how?

PS Is there thread or snipped how to draw in memblock or lock buffer...aka fast drawing
PPS How to mix two UV channels from two identical X files?

A door is a door is a door. Even a swinging one. =0
Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 8th Apr 2008 14:31
Is the XYZ position definition exactly on the objects surface, or do you need to find the UV position closest to it?

-= Out here in the fields, I fight for my meals =-
Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 8th Apr 2008 14:33
@ bergice

I did finish it yes, and it looked very nice but unfortunately I don't have it any more. I can tell you roughly how it worked if you want. I remember it didn't work so well with very low poly models... (like cubes)

-= Out here in the fields, I fight for my meals =-
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 8th Apr 2008 17:12 Edited at: 8th Apr 2008 17:17
@ Chris K

Yes, yes, closest! Can you tell me how, please?

Althoug my model is not cube but complex object!
And btw, do you know how to mix two UV channels from X objects in one and then save dbo?

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: 9th Apr 2008 16:26
Sorry to interrupt you guys, but I still have a small problem left with the shader I made some pages ago and I hope that this is solved pretty fast.



It basically overrides all basic DB Lighting. What I want now is to let the bricks with the shader look exactly the same as the ones without shader (so the faces at the side are darker etc).

How would I go about this?

mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 9th Apr 2008 17:06
I'd prefer to write similar shaders for every type of brick.

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: 9th Apr 2008 17:43
Hi,

I only use the shader for texturing the bricks which are optimized (like this). Bricks like Finish and Start are always unique, no need to optimize them. So it's easier and, I think, less GPU intensive to use UV Mapped models for them instead of using a shader where it isn't necessary.

Hope you solve your XYZ to UV problem, though, I kind of feel bad for interrupting you but can't wait til you're finished. ^^

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 10th Apr 2008 01:05
Quote: "It basically overrides all basic DB Lighting. What I want now is to let the bricks with the shader look exactly the same as the ones without shader (so the faces at the side are darker etc).

How would I go about this?"


You need to add lighting calculations to the shader - and pass things like light colour and direction to the shader from DBPro using the set effect constant vector command. (You might also be able to set various "effect states" to pick up lighting from the settings in DBP - but I'm not too sure about this.)

You'd need something along the following lines for simple diffuse lighting from a directional light.

In the shader declarations:



The vertex shader would need to input the object's normal and output it after transforming it to World coordinates, e.g. :



Finally, the pixel shader would calculate the amount of light falling on the pixel:



and multiply the output RGBA values by diffuse.
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 10th Apr 2008 12:34
@ Alsan
Wow, man! superb optimize!
But in that case i VERY recommend you pixel lighting shader!
Why? Because default lighting is VERTEX lighting - less vertexes you have - worse lighting you see! With your good optimize
default lighting craps your nice graphics. Honestly.

PS PixLight is very fast and if you use at least your one shader -
players with v-cards without shaders wouldnt see effects (or whole).
So use shaders freely!

PPS you can merge good pixlight to your shader from Evolveds shaders. (see Ultimate Shader Pack thread)

PPPS =) You did that optimizer?

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: 10th Apr 2008 15:58
@ Green Gandalf:

Thanks first of all.

I did what you say and reincluded the vertex shader again.



Doesn't work right now. ^^ My problem is: How do I get the Normal from the vertex shader to the pixel shader? I quickly browsed through the DX SDK and read, that the vertex shader output is automatically the pixel shader input or something along these lines. Unfortunately the SDK is quite confusing - if there is further informtion on this in it, then I missed it.

(I basically copied the Vertex shader from your terrain blending shader and modified it. So I don't know whether the pos or UV is necessary in this shader. The SDK says, that only pos is necessary for a vertex shader, so I guess it has to stay while the UV is redundant)

Or are there other errors in the code? It's probably a enormously dumb and embarrassing mistake. Sorry to ask so much questions, I'm still quite a n00b in shader stuff and don't know any other efficient way to learn than to ask questions on mass. ^^

@ mrHandy: Thanks for the suggestion, but I don't think I need high end pixel shader lighting for those bricks, as they're always flat. I'm just searching for a fast (efficient) way to get this basic shading effect which DBPro objects have by default.

And yes, I programmed the optimizer for my game project. It's a quite long code actually, but very fast. I basically make a array with all bricks in it and then browse through it checking if there are faces that can be merged. The mesh is then created using a memblock.

mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 10th Apr 2008 16:54
@Alsan
Man, thats tough!

Listen, could you post snippet how to write data to memblock, because help file has table of mesh memblock structure, but it turns bit tricky on practice to make your own mesh

Althogh, if i guess right, you are making game like this:
http://www.gamespot.com/pc/puzzle/ballance/index.html
You should check it! Its *OMFG* how good!

@Green Gandalf = or anybody

Could you please give me some advices:
=i have two x files with different uv mapping, and i want to make one dbo with both mapping channels...how to?
=how to paint in memblock one known pixel?
=how to get UV coords from known vertex? (better without using memblock)
10x anyway!

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: 10th Apr 2008 17:24 Edited at: 10th Apr 2008 17:34
@Alsan

Basically the names of the variables you assign semantics to don't matter, it's the semantics themselves that do. So for instance, if I have a vertex input of 'hello : COLOR0' and wish to directly send this to my pixel shader, I will most likely just have 'whatever :COLOR0' as my vertex shader output and in my vertex shader I would use something like, 'OUT.whatever = IN.hello;', this will just directly pass the colour to the pixel shader, now what the SDK is saying is that your pixel shader's input should be the same as the vertex shader's output, because to get my colour I passed to my pixel shader, I need to have something like 'myColour : COLOR0' as input for the PS. In your shader you have a pixel shader input of 'float4 UV1 : texcoord1;', which means it uses the semantic of TEXCOORD1, but your vertex shader doesn't output this so it can't read this value.

In my current shaders I just use 3 structs to pass data, a vertex input struct which is the information the application sends to the shader, vertex output, which is also used at my pixel shader input and the pixel shader output, which is always just COLOR.

So as you can see your vertex shader outputs 'Normal' using the semantic NORMAL, but none of your pixel shader inputs use this semantic so you're not really sending the data anywhere.

Also, remember that just because a semantic is called NORMAL/TEXCOORD1 or whatever, it doesn't mean that's the data that needs to be stored in it, you can store anything you like, such as a colour value in the UV, as the UV can actually be UVWX(4 components) so you can do quite a bit with them as you have 8 to work with.

Quote: "i have two x files with different uv mapping, and i want to make one dbo with both mapping channels...how to?"


If they are the same mesh but only differ in UV values, then you can just change the FVF for the first object, then using the vertexdata commands you could read the UV from stage 0 from the 2nd object and copy it to the first object's stage 1, but I'm pretty sure you can only lock the data from one thing at a time so it might be slow.

Quote: "how to paint in memblock one known pixel?"


Assuming it's 32 bit, 'write memblock DWord ID, 12 + (x+y*width) * 4, colourHere'

Quote: "how to get UV coords from known vertex? (better without using memblock)"


Either with memblocks or the vertexdata commands, search the forums as I'm sure there are examples of both.

Alsan
18
Years of Service
User Offline
Joined: 20th May 2005
Location: Germany - Nähe Kassel
Posted: 10th Apr 2008 18:27
@ dark coder: Thanks for that great explanation!

Though I feel like my head is exploding right now that made things a lot clearer.

In spite of that I still don't see what's wrong with the shader now. I used it before without the vertex shader and it worked great. Do I have to rewrite the whole shader now because of the VS? E.g. inputting all the data in the vertex shader input instead of the pixel shader input? If yes, why did it work before? Isn't it circuitous to do that via VS if you can do it directly in the PS Input?

I think I have to sleep a night about that as I'm completely confused right now. ^^ Shaders are great, but really a step beyond DarkBasic difficulty.

@ mrHandy: Memblock Meshes are quite simple if you get the hang of it. I learned it from some tutorials floating around here in the forum, you could try a forum search to find them.

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 10th Apr 2008 20:54
@Alsan

Quote: "Or are there other errors in the code? It's probably a enormously dumb and embarrassing mistake. Sorry to ask so much questions, I'm still quite a n00b in shader stuff and don't know any other efficient way to learn than to ask questions on mass."


No problem - we all have to learn sometime. And you are making the effort to do the work yourself.

Dark Coders suggestions are definitely on the right track. The other problem with your shader is that when you use both a vertex shader and a pixel shader the latter will need to get it's vertex dependent data from the vertex shader output structure. This means that everything that is needed per vertex (such as normals and UV coords) needs to be placed in the vertex shader's output structure even if no additional processing is required.

For example, the quantities UV0 and UV1 expected by your pixel shader must be part of the vertex shader output - which in turn must be part of the pixel shader input. Also, as Dark Coder points out, you need to make sure that the semantic names match up, i.e. "texcoord0", etc. Also, most things that are output from the vertex shader are passed by one of texcoord0, ..., texcoord7.

Quote: "Shaders are great, but really a step beyond DarkBasic difficulty."


True, I'm afraid - but at least most shaders are short.

Here's a simple diffuse lighting shader - you just need to add/amend the relevant bits to your shader.



Note that in this example I've used the same structure for the vertex shader output and pixel shader input. This simplifies the task of making sure the semantics match up - but you need to remember that the "position" semantic cannot be accessed by the pixel shader directly.
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 11th Apr 2008 15:24 Edited at: 11th Apr 2008 15:27
Hi!
I have question
about stencil shadow fx-
if use gpu shading (only this usefull)
there is no errors but self-shadowing...
how to avoid that?

ps i mean self-sading, see pic.

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

Attachments

Login to view attachments
Alsan
18
Years of Service
User Offline
Joined: 20th May 2005
Location: Germany - Nähe Kassel
Posted: 11th Apr 2008 18:04
Thank you very much, Green Gandalf! Again your explanation made things much clearer. I'll try to merge those two shaders.

Please don't expect any answers for some days, I'm quite busy atm. (but normally always find time to code a bit ^^)

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 13th Apr 2008 22:55 Edited at: 18th Apr 2008 22:30
It seems ages since I posted a shader demo so here's a simple one which shows how to use intermediate render targets in shaders using the new features that come with Dark Shader.

[So you'll need to have Dark Shader installed. ]

Intermediate render targets are not needed in this shader, of course, but are used to show how you can set them up.

The demo renders a simple scene in two steps:

The first step uses a camera effect to render the scene using camera 1 and then processes that image in two passes to remove first the red component, and then the blue. The resulting green-scale image, number 3, is applied to the cube in the centre of the screen.

The second step renders the whole scene in the usual way using camera 0.

Notice that the result of each sync appears as a slight change to the image on the cube in the centre - an infinite sequence of smaller images in fact (of course, you can only see differences from the first few syncs). The scene appears black initially.

The "fps" is meaningless of course.

Edit Corrected some confusing/misleading comments in the fx file.

Attachments

Login to view attachments
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 14th Apr 2008 11:36
Umm... Anyone seen my last posts, answer please.

A door is a door is a door. Even a swinging one. =0
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 14th Apr 2008 21:15
Quote: "Anyone seen my last posts, answer please."


Yes, I've seen them - unless they're on a different board.
Sasuke
18
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 15th Apr 2008 00:24 Edited at: 15th Apr 2008 00:25
Don't know if i'm interrupting here but i've finally dived into shaders head first. I've played around with shaders for awhile but i've never fully created my own. Here a shader i'm making:



My shader has normal mapping/specular mapping + specular mask/light mapping + mask and a sort of masked grain cube mapping. Everything work perfectly but, using evolved shader and i've add the mask as an example:



The problem is I don't want the base to be affect when changing opacity of the cubemap affected by the mask. If you change the LerpFactor to 1 the base just turns black. I can't figure out how to stop this, can anyone help please.

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: 15th Apr 2008 00:54
Quote: "The problem is I don't want the base to be affect when changing opacity of the cubemap affected by the mask. If you change the LerpFactor to 1 the base just turns black. I can't figure out how to stop this, can anyone help please. "


From your screenshot your cube map seems to be black. Why do you expect another colour?
Sasuke
18
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 15th Apr 2008 01:13 Edited at: 15th Apr 2008 01:15
Sorry, I removed cubemapping from that shader, but If I can solve the problem in Evolved's shader I can solve it in mine, plus I need a cube mapping shader with a working mask.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 15th Apr 2008 02:02
I don't understand your problem.

If you put the lerpfactor to 0 you should get the base texture, i.e. tex2D(Base,IN.Tex) in your shader, whereas if you set it to 1 you will get the masked cubemap, i.e. texCUBE(cubeMap,IN.Ref)*Mask in your code. You won't get the base texture if you set the lerpfactor to 1 - you will if you set it to any other value between 0 and 1, e.g. 0.5 will give you the average of the base and the masked cubemap.

Why are you masking it anyway?
Sasuke
18
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 15th Apr 2008 02:35 Edited at: 15th Apr 2008 02:37
Basically if I set lerpfactor to 0.5 I would get darker base texture and the masked cubemap would be semi transparent but I want base texture to retain its brightness and only the opacity of the masked cubemap to change. I guess lerp is not the way to go but I don't know enogh about shaders to solve this. Also i'm masking it cause I only what parts of the texture to have a cubemap.
coolgames
18
Years of Service
User Offline
Joined: 26th Sep 2005
Location: Oregon, USA
Posted: 15th Apr 2008 03:18
I'm having problems getting my shader to alphablend correctly. The object will just fade to the color of the camera backdrop. I'm texturing the object using an image captured from the camera using "set camera to image".

Here's my shader:


I'm trying to make a shadow system that renders a real time shadow by capturing an image of the object using a seperate camera. I want to set the alpha of the backdrop to 0, while coloring the rest of the captured image black.

How can I get the alpha blending to work correctly?

Thanks,
Coolgames
Sasuke
18
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 15th Apr 2008 03:50
Green Gandalf, I think i've solved it using this:


Sorry for the confusion earlier, but I hope you can see what I was trying to do.
Sasuke
18
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 15th Apr 2008 04:51 Edited at: 15th Apr 2008 04:52
Woohoo, finally got my shader working, I upload a vid(2.45 MB) thats worth seeing my shader in action, Normal Mapping(2 Lights) + Specular Mapping with mask + Light Mapping with Mask and finally Cube Mapping with Mask. Also added some bloom to the scene.

Attachments

Login to view attachments
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 15th Apr 2008 11:02
@Green Gandlaf

I mean posts in this thread, another treads is copies because i
think not so many people reads this thread.

So, have you got any ideas about stencil shadows question?

A door is a door is a door. Even a swinging one. =0
bjadams
AGK Backer
16
Years of Service
User Offline
Joined: 29th Mar 2008
Location:
Posted: 15th Apr 2008 11:24
Sasuke, your shader looks great.

can you maybe post it in here + a small dbpro example of how to use it, for all of us who are just starting out with shaders?

thanks a lot.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 15th Apr 2008 14:47 Edited at: 15th Apr 2008 14:49
Sasuke

Glad you got it working. I haven't seen your video yet, but from your description and code snippet it sounds like you wanted a reflective effect. Your references to "opacity" made me think you wanted the object to appear semi-transparent - hence my earlier comments. Did you mean "reflectiveness of the object" rather than "opacity of the cubemap"?

coolgames

Quote: "I'm trying to make a shadow system that renders a real time shadow by capturing an image of the object using a seperate camera"


Guess what I've been working on for the last few days.

Have you looked at the example that comes with the MS DX9 SDK?

Your code seems to set the object's alpha to zero for ALL pixels here:



This means the alphablending settings here:



just give you whatever's already been rendered whatever that happens to be. If you set "col.a" to 1, for example, you will just be rendering the shadowmap.

What information is contained in your shadowTexture? Is it just black/white for shadow/noshadow or is the shadowing contained in the alpha component? I can only guess. If it's just a black/white shadow map then you probably want to set "col.a" equal to something like the sum of the rgb components of shadowTexture like this:



This assumes that the main texture is already rendered. How are you rendering the main texture?

The snippet above just gives you a blend between the shadowTexture and the previosly rendered scene depending on the brightness of the shadowTexture pixel. Is that what you want?

Quote: "How can I get the alpha blending to work correctly?"


The first, vital, step is to explain clearly what textures/images you are trying to blend and say what contains the blending information (and in what form).

Quote: "I want to set the alpha of the backdrop to 0, while coloring the rest of the captured image black."


What do you mean? The backdrop is the whole image, there is no "rest".


mrHandy

Quote: "So, have you got any ideas about stencil shadows question?"


Sorry, no I haven't.
mr Handy
16
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 15th Apr 2008 16:35
oh, well...

okay, that wasn't my last qetion =)

now i'm want to displace proj texture
using displace coords of base texture.
but nothing works if i use same formula.

PS my shader must be shadow proj on parallax

A door is a door is a door. Even a swinging one. =0
coolgames
18
Years of Service
User Offline
Joined: 26th Sep 2005
Location: Oregon, USA
Posted: 15th Apr 2008 18:05
I'm capturing a picture of a darkbasic object using a seperate camera. I set that camera to render to an image. I texture a plane object using that image. I want to use a shader to render one color of the image as transparent, the rest I want to color black.

Here's an image without the shader applied:


Here's an image with the shader applied:


When I set the alpha of the texture in the shader, the plane just fades to the backdrop color of the camera, instead of blending with the ground texture.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 15th Apr 2008 19:49
coolgames

When you say you are rendering an object with a separate camera do you mean just that object and nothing else? If so, why don't you just colour that camera's backdrop black? If your object is a sphere then your image will be the sphere with whatever texture, etc, you used for it set against a black background.

Quote: "I want to use a shader to render one color of the image as transparent, the rest I want to color black."


I still don't know what you mean. Could you talk us through a simple example step by step. Are you talking about arbitrary pixels in the image, or pixels on the object within the image or something else? Or are you talking about a specific colour such as purple or what?

I also don't understand what your images are supposed to show. Where's this plane you keep referring to? Is it the greenish area, the black area or both or something else? And where's the player and what should it look like? You are leaving me to make blind guesses.
coolgames
18
Years of Service
User Offline
Joined: 26th Sep 2005
Location: Oregon, USA
Posted: 15th Apr 2008 20:13
Here's what I'm doing:

1.I "make camera 1" and set camera 1 to render to a texture.
2.I make another plane object labeled "shadow" that I apply the texture to.
3.I position camera 1 to render only the sphere object(player) from the point of view of the light.
4.The plane labeled shadow is now textured with an image of the sphere(player). This corresponds to the first picture I posted.
5. The texture has the green background that is the default backdrop color for camera 1. In the pixel shader, I want to make it so that all the green pixels are completely transparent, while making the rest of the image colored black. This creates a "shadow" of the sphere.

The shader I posted above is not the final shader. I was just testing the alpha mapping part. When I tell the pixel shader to render all pixels at alpha=0, I expect the object to be invisible, but instead it has turned to the backdrop color of the main camera that is rendering the scene.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 15th Apr 2008 23:51 Edited at: 15th Apr 2008 23:58
coolgames

I wrote that last message from my work PC which for some obscure reason only showed the top part of your two images - the rest showed up as black (and likewise with Sasuke's) hence some of my questions. I'm now at home and your images make a lot more sense so I'll have another look (probably tomorrow). I can see that I'm also going to be having words with some IT people at work tomorrow ...

I think there might be a simple solution to your problem - and you seem to have made more progress with this than I have.
I'd like to see the final result if that's OK.

Sorry about any misunderstanding I may have caused.



Edit: OK, just had a quick look at your latest reply. That makes a lot of sense and is perfectly consistent with your image (now that I can see all of it). I'll post a more coherent answer as soon as posible. The detailed explanation and images help enormously, thank you.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 16th Apr 2008 00:14
coolgames

Part of your problem may be due the rendering order of your objects. If the ground is below the shadow plain object and is rendered first then your transparent plain should show the ground. If the transparent plain is rendered first and is also above the ground you'll see the backdrop - because the depth test will stop the ground being rendered. If the shadow plain is below the ground I guess you won't see it either way.

I think you can correct this by applying the set object transparency command to the shadow plain object in DBP (I think the flag needs to be set to 4 - you'll need to check). The aim is to make sure that solid objects such as the ground and sky are rendered first - this should get rid of the backdrop.

Let me know how you get on. Transparency effects can be confusing and some things can't be done easily in DBP (if at all).

Login to post a reply

Server time is: 2024-05-18 16:28:16
Your offset time is: 2024-05-18 16:28:16