Opacity shader with or without textures

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.
Post Reply
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Opacity shader with or without textures

Post by airstruck »

I want to create a shader that will modify the global opacity of a scene, regardless of whether textures are used.

Im not sure how to determine whether textures are present. I found a similar question here but there are no satisfying answers. For now I've settled for checking for the presence of texture coords greater than 0, but I expect this might create some artifacts in the upper left corners of things (although I haven't noticed any yet).

Here's the shader I'm using. Can this be improved? Is there some value passed in from Love that can help determine whether a texture is present? If not, would it be possible to add something like this?

Code: Select all

extern number opacity;
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords ) {
    number alpha = opacity;
    vec3 rgb = color.rgb;
    if (texture_coords.x > 0 || texture_coords.y > 0) {
        vec4 pixel = Texel(texture, texture_coords );
        vec4 result = pixel * color;
        rgb = result.rgb;
        alpha = opacity * pixel.a;
    }
    return vec4(rgb, alpha);
}
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Opacity shader with or without textures

Post by Nixola »

I don't think you need that "if", if I recall correctly the texture should be white with full opacity (1,1,1,1) if no texture is set, so you should not encounter any issue
(Also, can't you use [wiki]love.graphics.setColor[/wiki]? Do you need a shader, maybe to change opacity a great number of times without affecting performance too much?)
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Opacity shader with or without textures

Post by airstruck »

Nixola wrote:I don't think you need that "if"
In 0.8.0, removing the "if" (treating everything as textured) cause untextured graphic primitives not to display at all. I wonder if this has changed in a later version. I'm using 0.8.0 on my Debian laptop because the libc in the repos is behind where it needs to be for Love trunk, but I may update soon. For now I'm trying to keep my game compatible with both 0.8.0 and 0.9.x, since I'll be deploying on Android.
Nixola wrote:can't you use love.graphics.setColor?
I can't really see a clean way to do it. I'm rendering one scene within another scene (showing part of the level on the pre-level screen) as part of a scene transtion, and it kind of zooms and fades in when you start the level. So I think it really needs to be "global" opacity for that scene.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Opacity shader with or without textures

Post by Nixola »

I think you can safely update to 0.9: LÖVE provides up to date .deb, .rpm and source for Linux distros and binaries for Mac and Win; also, LÖVE on Android/iOS (which is still experimental) is up to date as well, using either 0.9.1 or 0.9.2 (can't remember now, but I think it's the latter). I checked on my Windows 7 x64 machine and I can't get the primitives to draw either on 0.8.0, but it should work on the latest LÖVE version.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Opacity shader with or without textures

Post by airstruck »

Nixola wrote:I think you can safely update to 0.9
The problem is liblove wants libc6 >= 2.15, but under this version of Debian the repos have libc6 2.13. So far I'm too lazy to build libc, but I may try to build Love against 2.13. I'll test your suggestion on Android though.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Opacity shader with or without textures

Post by Nixola »

You could try downloading version 2.19 from here? It's a package for a newer debian version, but it might work anyway
If you're lazy: x86_64, x86 packages
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
slime
Solid Snayke
Posts: 3129
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Opacity shader with or without textures

Post by slime »

time thief wrote:
Nixola wrote:I don't think you need that "if"
In 0.8.0, removing the "if" (treating everything as textured) cause untextured graphic primitives not to display at all. I wonder if this has changed in a later version.
It has – in 0.9 and newer, everything is textured (primitive shapes use a 1x1 white texture across their whole shape), so it will work as you want in that case.

Here's pixel shader code that will work (in 0.9+) for textured and untextured cases, and when SpriteBatches with per-sprite colors are used (which wouldn't work if you just used setColor with no shader.)

Code: Select all

extern number opacity;

vec4 effect(vec4 vcolor, Image tex, vec2 texcoord, vec2 pixcoord)
{
    vec4 texcolor = Texel(tex, texcoord);
    vcolor.a *= opacity;   
    return texcolor * vcolor;
}
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Opacity shader with or without textures

Post by bartbes »

time thief wrote: The problem is liblove wants libc6 >= 2.15, but under this version of Debian the repos have libc6 2.13.
The deb was built on ubuntu, so that's probably why it has that dependency, as far as I know there's nothing that would prevent you from compiling love.
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Opacity shader with or without textures

Post by airstruck »

Thanks for the quick replies.
slime wrote:in 0.9 and newer, everything is textured
Ahh, that's good, thanks for clearing that up.
bartbes wrote:as far as I know there's nothing that would prevent you from compiling love.
I might do that at some point (probably only if I want to make a pull request or something though). For now I'm just upgrading this laptop from Debian 7 to 8 and keeping my fingers crossed.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Opacity shader with or without textures

Post by Nixola »

The .deb I got you should be from Debian 8, so you should be able to upgrade and everything should be fine; even if LÖVE doesn't update, its .deb should now work
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests