Preserving transparency when using shaders?

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
rougan
Citizen
Posts: 58
Joined: Wed Aug 12, 2015 10:30 am

Preserving transparency when using shaders?

Post by rougan »

Heya,
I'm not sure why what I'm doing isn't working! :shock:

This is my code:

Code: Select all

shaders = {}
    --Grayscale
    shaders.grayscale = love.graphics.newShader[[
        extern number factor;
        vec4 effect( vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords ){
          vec4 pixel = Texel(texture, texture_coords );//This is the current pixel color
          number average = (pixel.r+pixel.b+pixel.g)/3.0;
          pixel.r = pixel.r + (average-pixel.r) * factor;
          pixel.g = pixel.g + (average-pixel.g) * factor;
          pixel.b = pixel.b + (average-pixel.b) * factor;
          pixel.a = pixel.a;
          return pixel;
        }
    ]]
	
function love.load()
	background = love.graphics.newImage("backing.png")
	icon = love.graphics.newImage("icon.png")
	love.window.setMode(background:getDimensions())
	shader = false
end 

function love.draw()
	--Set the shader
	if shader then 
		love.graphics.setShader(shaders.grayscale)
		shaders.grayscale:send("factor",0)
	end
	love.graphics.draw(background)
	love.graphics.setColor(1,1,1,0.2)
	love.graphics.draw(icon,60,60)
	love.graphics.setColor(1,1,1,1)
	
	--Reset the shader
	love.graphics.setShader()
end 
Disabling the shaders the screen looks like this:
good.PNG
good.PNG (48.35 KiB) Viewed 4613 times
But once I enabled them, the icon transparency disappears:
bad.PNG
bad.PNG (49.22 KiB) Viewed 4613 times
I'm at a loss because I don't really understand why the

Code: Select all

pixel.a = pixel.a
line doesn't cut it. I also tried using color.a but to no avail.

Thanks for any help!! :awesome:
drunken_munki
Party member
Posts: 134
Joined: Tue Mar 29, 2011 11:05 pm

Re: Preserving transparency when using shaders?

Post by drunken_munki »

OK a simple one, two things.

1. You don't need to specify in your shader:

Code: Select all

pixel.a = pixel.a;
Unless you want to modify the alpha further. Otherwise remove this line.

2. To use the current love color multiply your shader result with the vec4 color parameter; so replace;

Code: Select all

return pixel;
with

Code: Select all

return pixel * color;
User avatar
rougan
Citizen
Posts: 58
Joined: Wed Aug 12, 2015 10:30 am

Re: Preserving transparency when using shaders?

Post by rougan »

drunken_munki wrote: Wed Jul 25, 2018 2:03 pm OK a simple one, two things.

1. You don't need to specify in your shader:

Code: Select all

pixel.a = pixel.a;
Unless you want to modify the alpha further. Otherwise remove this line.

2. To use the current love color multiply your shader result with the vec4 color parameter; so replace;

Code: Select all

return pixel;
with

Code: Select all

return pixel * color;
Thanks so much for the help! I didn't think about multiplying anything by the color var.
User avatar
yetneverdone
Party member
Posts: 448
Joined: Sat Sep 24, 2016 11:20 am
Contact:

Re: Preserving transparency when using shaders?

Post by yetneverdone »

Also, instead of doing this per draw,

Code: Select all

shaders.grayscale:send("factor",0)
just do this once (after the shader is created)
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests