Page 1 of 2

Applying multiple shaders to a effect?

Posted: Thu Jun 18, 2015 2:19 pm
by Tjakka5
Hey all,

Im working on a game in which I will be using a few shaders to make fancy effects and what not; Just some basic ones to change the color of a sprite though.

However, I have noted that you can only load 1 shader at the time, and apply it to a sprite, whereas I would like to be able to apply 2 shaders to a sprite.

For example, normally a sprite would be grayscaled, but when a certain variable is true it should be grayscaled, as well as have a different shader on top of it.


Im thinking I may need to use some kind of loop where I activate shader1, draw it to a canvas, activate shader2, draw it to a canvas, etc, until I get the image I want, then draw it to the screen, but I'm not sure how to do so.

Can anybody help me out here?

Re: Applying multiple shaders to a effect?

Posted: Fri Jun 19, 2015 2:17 am
by s-ol
You can draw the original sprite to canvas with one shader set, then draw the canvas onto the screen with the other shader set.

I would not recommend this though. It is probably better to instead write a shader that handles both, based on an externa value that you can set with shader:send().

Re: Applying multiple shaders to a effect?

Posted: Fri Jun 19, 2015 7:14 am
by Fenrir
Im thinking I may need to use some kind of loop where I activate shader1, draw it to a canvas, activate shader2, draw it to a canvas, etc, until I get the image I want, then draw it to the screen, but I'm not sure how to do so.
It's exactly the concept of multi-pass rendering, to apply multiple shader to a same object you'll need to render it back to a canvas where you'll apply the new shader. So to achieve what you want you can setup a multi-pass system (but it's really needed in cases where you need the result of the previous shader for your new computation) or just write a "bigger" shader handling all the cases you need (or switch between multiple shaders depending on the situation).

A typical example of multi-pass effects is gaussian blur for instance, you can have an example in this post:
viewtopic.php?f=5&t=80216&start=10#p184638
To apply this effect on my character, I have a total of 5 canvas and 4 shaders.

Re: Applying multiple shaders to a effect?

Posted: Fri Jun 19, 2015 3:08 pm
by s-ol
Fenrir wrote:
Im thinking I may need to use some kind of loop where I activate shader1, draw it to a canvas, activate shader2, draw it to a canvas, etc, until I get the image I want, then draw it to the screen, but I'm not sure how to do so.
It's exactly the concept of multi-pass rendering, to apply multiple shader to a same object you'll need to render it back to a canvas where you'll apply the new shader. So to achieve what you want you can setup a multi-pass system (but it's really needed in cases where you need the result of the previous shader for your new computation) or just write a "bigger" shader handling all the cases you need (or switch between multiple shaders depending on the situation).

A typical example of multi-pass effects is gaussian blur for instance, you can have an example in this post:
viewtopic.php?f=5&t=80216&start=10#p184638
To apply this effect on my character, I have a total of 5 canvas and 4 shaders.
wouldn't two canvas' be enough? You can just render back-and-forth. Or do you access the other data inbetween?

Re: Applying multiple shaders to a effect?

Posted: Fri Jun 19, 2015 4:26 pm
by Tjakka5
Thing is; I have never actually used canvases (canvas-i, canvasses?) before, so I think I might just go for the "all-in-one" shader, which will have boolean variables that will control which part of the shader to apply.

Re: Applying multiple shaders to a effect?

Posted: Fri Jun 19, 2015 5:35 pm
by bobbyjones
Use this as an opportunity to learn all about canvases they will be needed eventually anyway.

Re: Applying multiple shaders to a effect?

Posted: Fri Jun 19, 2015 10:41 pm
by I~=Spam
bobbyjones wrote:Use this as an opportunity to learn all about canvases they will be needed eventually anyway.
* probably it depends on what you are doing ;)

Re: Applying multiple shaders to a effect?

Posted: Sat Jun 20, 2015 5:54 am
by Tjakka5
I think I'll probably try to incorporate canvasas as the frame buffer, as I heard its more efficient?
That will also mean I can implement my camera on it a lot more easialy, which would be nice :3

Re: Applying multiple shaders to a effect?

Posted: Mon Jun 22, 2015 2:11 am
by I~=Spam
Tjakka5 wrote:I think I'll probably try to incorporate canvasas as the frame buffer, as I heard its more efficient?
That will also mean I can implement my camera on it a lot more easialy, which would be nice :3
No it will most likely be slower because you are doing two draw calls to draw one thing (draw calls are a lot more expensive than people think). But most likely you have more than enough processing power so don't worry about it. ;) Just do what makes sense to you as long as you are being reasonable.

Re: Applying multiple shaders to a effect?

Posted: Mon Jun 22, 2015 4:42 am
by bobbyjones
How will it be slower? Lol he isn't using canvases yet. Once he adds it in his performance should increase lol. That's the point of canvases.