Page 1 of 1

Help with colored lighting and blendmode

Posted: Sun Dec 09, 2012 11:59 pm
by Gravy
Hi all,

So I'm trying to implement a lighting system in my game. For colorless light, it works just fine, like this:

-Draw the game as normal.
-Set canvas to lighting canvas, clear it to black with 255 alpha.
-set blendmode to subtractive, and draw shapes from lighting sources.
-setCanvas() and draw lighting canvas.

To make the light colored, I tried adding the color using additive blendmode before the subtraction, but the problem is that the alpha goes to zero when two lights intersect, so the color falls out. I then tried adding some alpha to the entire canvas after all the lights are drawn, but this doesn't seem to work.

Any suggestions? I'd really like to avoid any per-pixel logic since this will probably make the whole system too slow.

Example of working white light:

Code: Select all

function love.load()
   love.graphics.setBackgroundColor(255, 0, 255)
   canvas = love.graphics.newCanvas(400,300)
   love.graphics.setLineWidth(5)
end

function love.draw()
	canvas:clear(0,0,0,255)
	--lighting canvas
	love.graphics.setCanvas(canvas)
	love.graphics.setBlendMode("subtractive")
    love.graphics.setColor(0, 0, 0,150)
    love.graphics.rectangle("fill", 75, 75, 125, 125)
    love.graphics.setColor(0, 0, 0,200)
    love.graphics.rectangle("fill", 110, 110, 125, 125)
	--outside lighting canvas
	love.graphics.setCanvas()
	love.graphics.setBlendMode("alpha")
	love.graphics.setColor(0, 255, 0,255)
	love.graphics.line(0,0,500,500)
	love.graphics.draw(canvas)
end
Attached is colored light attempt.

Re: Help with colored lighting and blendmode

Posted: Mon Dec 10, 2012 10:53 pm
by headchant
You could use multiplicative blending: draw the light first and then set blendmode to multiplicative, draw the scene canvas. The color of the stuff drawn onto the lightsourcecanvas would then be your 'lightcolor'(or maybe mean something else by lightcolor?). See code in the attached .love file.

Re: Help with colored lighting and blendmode

Posted: Fri Dec 14, 2012 2:09 am
by Gravy
Thanks! This seems to do the trick.

How does multiplicative blending work? Is (x,y,z)*(a,b,c) = (xa,yb,zc)? How does it deal when the result is over 255?

Re: Help with colored lighting and blendmode

Posted: Fri Dec 14, 2012 2:18 am
by dreadkillz
I was curious about the blend modes too. Here's the link on how they work: viewtopic.php?f=4&t=10268&p=62175&hilit=+blend#p62171

Re: Help with colored lighting and blendmode

Posted: Fri Dec 14, 2012 3:00 am
by Gravy
Awesome. Thanks!