Shader affecting love.graphics.setColor?

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
Greninjask
Prole
Posts: 6
Joined: Sun Mar 20, 2016 5:11 pm

Shader affecting love.graphics.setColor?

Post 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.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Shader affecting love.graphics.setColor?

Post 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;
}
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Greninjask
Prole
Posts: 6
Joined: Sun Mar 20, 2016 5:11 pm

Re: Shader affecting love.graphics.setColor?

Post by Greninjask »

Awesome! That was a really easy fix. Thanks so much for the advice!
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Shader affecting love.graphics.setColor?

Post by zorg »

No problem, have fun coding. :3
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests