Page 1 of 1

Preserving transparency when using shaders?

Posted: Wed Jul 25, 2018 1:41 pm
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 950 times
But once I enabled them, the icon transparency disappears:
bad.PNG
bad.PNG (49.22 KiB) Viewed 950 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:

Re: Preserving transparency when using shaders?

Posted: Wed Jul 25, 2018 2:03 pm
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;

Re: Preserving transparency when using shaders?

Posted: Wed Jul 25, 2018 2:33 pm
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.

Re: Preserving transparency when using shaders?

Posted: Wed Jul 25, 2018 3:41 pm
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)