Help with colored lighting and blendmode

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
Gravy
Citizen
Posts: 80
Joined: Sun Jan 22, 2012 10:15 pm
Location: CA, USA

Help with colored lighting and blendmode

Post 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.
Attachments
moving_light.love
(538 Bytes) Downloaded 204 times
User avatar
headchant
Party member
Posts: 105
Joined: Fri Sep 03, 2010 12:39 pm
Contact:

Re: Help with colored lighting and blendmode

Post 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.
Attachments
light.love
(177.85 KiB) Downloaded 232 times
Gravy
Citizen
Posts: 80
Joined: Sun Jan 22, 2012 10:15 pm
Location: CA, USA

Re: Help with colored lighting and blendmode

Post 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?
Attachments
moving_light2.love
(632 Bytes) Downloaded 393 times
User avatar
dreadkillz
Party member
Posts: 223
Joined: Sun Mar 04, 2012 2:04 pm
Location: USA

Re: Help with colored lighting and blendmode

Post 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
Gravy
Citizen
Posts: 80
Joined: Sun Jan 22, 2012 10:15 pm
Location: CA, USA

Re: Help with colored lighting and blendmode

Post by Gravy »

Awesome. Thanks!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], Semrush [Bot] and 4 guests