Applying multiple shaders to a effect?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
Tjakka5
Party member
Posts: 243
Joined: Thu Dec 26, 2013 12:17 pm

Applying multiple shaders to a effect?

Post 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?
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Applying multiple shaders to a effect?

Post 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().

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
Fenrir
Party member
Posts: 222
Joined: Wed Nov 27, 2013 9:44 am
Contact:

Re: Applying multiple shaders to a effect?

Post 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.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Applying multiple shaders to a effect?

Post 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?

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
Tjakka5
Party member
Posts: 243
Joined: Thu Dec 26, 2013 12:17 pm

Re: Applying multiple shaders to a effect?

Post 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.
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: Applying multiple shaders to a effect?

Post by bobbyjones »

Use this as an opportunity to learn all about canvases they will be needed eventually anyway.
User avatar
I~=Spam
Party member
Posts: 206
Joined: Fri Dec 14, 2012 11:59 pm

Re: Applying multiple shaders to a effect?

Post 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 ;)
My Tox ID: 0F1FB9170B94694A90FBCF6C4DDBDB9F58A9E4CDD0B4267E50BF9CDD62A0F947E376C5482610
User avatar
Tjakka5
Party member
Posts: 243
Joined: Thu Dec 26, 2013 12:17 pm

Re: Applying multiple shaders to a effect?

Post 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
User avatar
I~=Spam
Party member
Posts: 206
Joined: Fri Dec 14, 2012 11:59 pm

Re: Applying multiple shaders to a effect?

Post 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.
My Tox ID: 0F1FB9170B94694A90FBCF6C4DDBDB9F58A9E4CDD0B4267E50BF9CDD62A0F947E376C5482610
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: Applying multiple shaders to a effect?

Post 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.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests