Shaders: affect whole screen

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.
Germanunkol
Party member
Posts: 712
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

Shaders: affect whole screen

Post by Germanunkol »

Hi,

I have written a shader which lets objects fade to black (progress < 1) and then back to their original color (progress > 1):

Code: Select all

vec4 col;
extern number progress;
number bright;
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords)
{
	col = texture2D(texture, texture_coords);
	if (progress < 1.0)
	{
		bright=1.0-progress;
	}
	else
	{
		bright=1.0-(2.0-progress);
	}
	return vec4(col.r*bright, col.g*bright, col.b*bright, col.a);
}
This works well, but only for objects which are drawn after the love.graphics.setPixelEffect call, and NOT for the background. But I want the whole screen to fade to black, so I'm somewhat lost.

What I tried:
After love.graphics.setPixelEffect, I draw a rectangle in the background color, filling the whole screen, before I draw anything else. However, using the shader above, this rectangle disappears when the shader is active. If I change the line
return vec4(col.r*bright, col.g*bright, col.b*bright, col.a);
to
return vec4(col.r*bright, col.g*bright, col.b*bright, 1);
then it does not disappear, but then the rectangle is being drawn black during the entire transition.
What am I doing wrong?

Also, I know it can be done using a canvas, but I want to avoid that for compatibility reasons.
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Shaders: affect whole screen

Post by raidho36 »

Why don't you simply draw a fading black overlay on top of all graphics, with no special shader, only using alpha?
User avatar
Ensayia
Party member
Posts: 399
Joined: Sat Jun 12, 2010 7:57 pm

Re: Shaders: affect whole screen

Post by Ensayia »

If you want to do the entire screen, you could try rendering everything to a canvas and then apply the shader to it.

If you want to do individual objects, I would recommend using setColor instead of a shader, as it's a simpler route and easy to call per-object.

Is there a specific reason you need a shader to do this? Or is this simply the preferred method?
User avatar
DaedalusYoung
Party member
Posts: 407
Joined: Sun Jul 14, 2013 8:04 pm

Re: Shaders: affect whole screen

Post by DaedalusYoung »

Germanunkol wrote:Also, I know it can be done using a canvas, but I want to avoid that for compatibility reasons.
Shaders also aren't available on older hardware, so you wouldn't avoid compatibility issues this way.

As said, I'd recommend just drawing a black rectangle with increasing/decreasing alpha.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Shaders: affect whole screen

Post by BlackBulletIV »

Germanunkol wrote:Also, I know it can be done using a canvas, but I want to avoid that for compatibility reasons.
As DaedalusYoung said, if you're using shaders, I wouldn't be worrying about compatibility issues. Shaders and canvases go hand in hand with each other.

So yeah, either draw a black rectangle, or if you're really pedantic about performance, draw a black image (with different levels of alpha, of course).
Germanunkol
Party member
Posts: 712
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

Re: Shaders: affect whole screen

Post by Germanunkol »

raidho36 wrote:Why don't you simply draw a fading black overlay on top of all graphics, with no special shader, only using alpha?
DaedalusYoung wrote:As said, I'd recommend just drawing a black rectangle with increasing/decreasing alpha.
Ensayia wrote:Is there a specific reason you need a shader to do this? Or is this simply the preferred method?
Sorry, I wasn't clear here: The fading to black is just an example which I want to replace with another effect later on, which will be more complex and needs to be drawn on the full screen.
Fading to black was just the simplest first test.
DaedalusYoung wrote:Shaders also aren't available on older hardware, so you wouldn't avoid compatibility issues this way.
Hm. But how large is the group of hardware setups which allow shaders but not canvases? Is it that small? Then I'll just go back to the canvas method, of course, that was simple enough.
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de
User avatar
Jeeper
Party member
Posts: 611
Joined: Tue Mar 12, 2013 7:11 pm
Contact:

Re: Shaders: affect whole screen

Post by Jeeper »

Germanunkol wrote:
raidho36 wrote:Why don't you simply draw a fading black overlay on top of all graphics, with no special shader, only using alpha?
DaedalusYoung wrote:As said, I'd recommend just drawing a black rectangle with increasing/decreasing alpha.
Ensayia wrote:Is there a specific reason you need a shader to do this? Or is this simply the preferred method?
Sorry, I wasn't clear here: The fading to black is just an example which I want to replace with another effect later on, which will be more complex and needs to be drawn on the full screen.
Fading to black was just the simplest first test.
DaedalusYoung wrote:Shaders also aren't available on older hardware, so you wouldn't avoid compatibility issues this way.
Hm. But how large is the group of hardware setups which allow shaders but not canvases? Is it that small? Then I'll just go back to the canvas method, of course, that was simple enough.
I think you misunderstood them. In order for your shader to work on everything, you need to apply it to a canvas where you have already drawn everything too.
Personally I would not worry to much about compatibility issues, if you use a computer that does not support shaders then you should not expect to be able to run everything. Why make your game worse for everyone for a selected few?
Germanunkol
Party member
Posts: 712
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

Re: Shaders: affect whole screen

Post by Germanunkol »

Jeeper wrote: I think you misunderstood them. In order for your shader to work on everything, you need to apply it to a canvas where you have already drawn everything too.
Personally I would not worry to much about compatibility issues, if you use a computer that does not support shaders then you should not expect to be able to run everything. Why make your game worse for everyone for a selected few?
No, I understand what they mean. I know how it works, I already coded it and it works fine. In fact, I had already coded it before posting the original question.

The point is that I thought there must be a way to do it without using canvases. I read so many things about canvases not working for people lately (and the po2-syndrome) that I wanted to avoid them. If there is no such way, then I will use canvases, but I wanted to use as few dependencies as possible.

To my last question: You all state things similar to "If they have shader support, they will most likely have canvas support as well" and I wanted something less vague... that's what I meant. Is there some place where I can see precise numbers about shaders and canvas support?

Also, thanks to you all for the quick and numerous replies!
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de
User avatar
Jeeper
Party member
Posts: 611
Joined: Tue Mar 12, 2013 7:11 pm
Contact:

Re: Shaders: affect whole screen

Post by Jeeper »

Germanunkol wrote:
Jeeper wrote: I think you misunderstood them. In order for your shader to work on everything, you need to apply it to a canvas where you have already drawn everything too.
Personally I would not worry to much about compatibility issues, if you use a computer that does not support shaders then you should not expect to be able to run everything. Why make your game worse for everyone for a selected few?
No, I understand what they mean. I know how it works, I already coded it and it works fine. In fact, I had already coded it before posting the original question.

The point is that I thought there must be a way to do it without using canvases. I read so many things about canvases not working for people lately (and the po2-syndrome) that I wanted to avoid them. If there is no such way, then I will use canvases, but I wanted to use as few dependencies as possible.

To my last question: You all state things similar to "If they have shader support, they will most likely have canvas support as well" and I wanted something less vague... that's what I meant. Is there some place where I can see precise numbers about shaders and canvas support?

Also, thanks to you all for the quick and numerous replies!
Afaik there are no other ways than with canvases. And I am quite sure that there are way more people who cant run Shaders than there are people who have computers that cant handle po2-syndrome. I also think that people with that problem, would have it regardless if you use canvases or not.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Shaders: affect whole screen

Post by BlackBulletIV »

Germanunkol wrote:The point is that I thought there must be a way to do it without using canvases. I read so many things about canvases not working for people lately (and the po2-syndrome) that I wanted to avoid them. If there is no such way, then I will use canvases, but I wanted to use as few dependencies as possible.

To my last question: You all state things similar to "If they have shader support, they will most likely have canvas support as well" and I wanted something less vague... that's what I meant. Is there some place where I can see precise numbers about shaders and canvas support?
There is a way to do it without canvases, involving ImageData's and screenshots, but it's messy, and I imagine it's bloody expensive on the CPU.

Look, anyone who can't support canvases obviously hasn't got a semi-decent GPU. You'll just have to leave them behind if you want to start doing more fancy graphical effects.
Post Reply

Who is online

Users browsing this forum: No registered users and 72 guests