Page 1 of 1

Shader affecting love.graphics.setColor?

Posted: Sun Jul 03, 2016 1:59 am
by Greninjask
Hello all,

I'm using shaders in my game (very fun by the way!) but I noticed something unexpected... whenever I use love.graphics.setShader(shader_1), whatever is drawn replaces the love.graphics.setColor to basic white. Here is the code for my shader:

Code: Select all

shader_1 = love.graphics.newShader[[
    extern number mouse_x = 0;
    extern number mouse_y = 0;
    vec4 effect( vec4 color, Image texture, vec2 tex_coordinates, vec2 scrn_coordinates ){
      if(pow((scrn_coordinates.x - mouse_x), 2) + pow((scrn_coordinates.y - mouse_y), 2) < pow(200, 2)){
        return vec4(0.0, 0.0, 0.0, 0.0);
      }
      else{
        return Texel(texture, tex_coordinates);
      }
    }
  ]]
What this shader does is it looks for the pixels around the mouse and does NOT draw them (revealing the background color of the game).

And here is the code that I use the shader with:

Code: Select all

local function draw_world()
	love.graphics.setShader(shader_1)
		love.graphics.setColor(0, 0, 255)
		love.graphics.draw(graphics.background, 0, 0) -- Expecting a BLUE colored background
		love.graphics.setColor(0, 255, 0)
		love.graphics.draw(graphics.enemy, 0, 0) -- Expecting a GREEN colored enemy
	love.graphics.setShader()
	love.graphics.setColor(255, 0, 0)
	love.graphics.draw(graphics.player, 100, 0) -- Got a RED colored player
end
The shader works as expected EXCEPT it ignores my love.graphics.setColor functions, and always set the color to (255, 255, 255). Is there a way around this? It's important for the color to be changing even while the shader is active.

Re: Shader affecting love.graphics.setColor?

Posted: Sun Jul 03, 2016 2:09 am
by zorg
The color parameter in the fragment(pixel) shader is equivalent to what you set with love.graphics.setColor, so if you don't use that in your shader code, it won't affect the outcome; see the examples on the wiki page: [wiki]love.graphics.newShader[/wiki]
Just modify your code a bit and it should work: :3

Code: Select all

if(pow((scrn_coordinates.x - mouse_x), 2) + pow((scrn_coordinates.y - mouse_y), 2) < pow(200, 2)){
    return vec4(0.0, 0.0, 0.0, 0.0);
}
else{
    return Texel(texture, tex_coordinates) * color;
}

Re: Shader affecting love.graphics.setColor?

Posted: Sun Jul 03, 2016 1:56 pm
by Greninjask
Awesome! That was a really easy fix. Thanks so much for the advice!

Re: Shader affecting love.graphics.setColor?

Posted: Sun Jul 03, 2016 2:28 pm
by zorg
No problem, have fun coding. :3