Tile based lights with different colors overlapping

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
User avatar
veethree
Inner party member
Posts: 875
Joined: Sat Dec 10, 2011 7:18 pm

Tile based lights with different colors overlapping

Post by veethree »

So i'm working on a little tile based lighting system, And to my surprise, It's coming along nicely. As of now it supports multiple light sources with various radiuses/radii(or whatever the god damn plural is). So the next thing i want to implement is colored lights. And that's where the problem lies, I have no idea how i would handle multiple lights with varying colors overlapping.

I will attach the .love, But i thought it may help to explain briefly how the whole system works.

So i have a grid of cells, Each cell has a light_level parameter (It's really just the alpha value for the cell, but light_level looks cooler), And a boolean which tells the system if the cell is a light source or not (meaning it emits light)

Then to update the lights i loop through all the cells, On each cell i loop through all the light sources, And check if the cell is within the radius of any of them, Then based on the distance between the cell and the sources i calculate the light_level for the cell, If the cell is within the range of multiple lights, I calculate the light_level for the lights individually, Then add them together. I'm not updating the lights each frame so it's not as inefficient as it sounds (Could probably be more efficient though)

So yeah, Any help would be greatly appreciated, Thanks in advance.

.love controls:
Left click to add a light at mouse position, Right click to remove it. And the mouse wheel to adjust the radius of the lights. (The light you're about to place that is not the ones that are already there)
Attachments
Tile lights.love
(2.14 KiB) Downloaded 195 times
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Tile based lights with different colors overlapping

Post by raidho36 »

I really suggest you to simply use stencil (or whatever) based lights system. Easier to implement (or reuse), nicer result.
User avatar
veethree
Inner party member
Posts: 875
Joined: Sat Dec 10, 2011 7:18 pm

Re: Tile based lights with different colors overlapping

Post by veethree »

raidho36 wrote:I really suggest you to simply use stencil (or whatever) based lights system. Easier to implement (or reuse), nicer result.
Could you elaborate? I'm not sure what you mean by "stencil based"
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Tile based lights with different colors overlapping

Post by raidho36 »

Well not really stencil based, but I've seen people doing it using stencils and shaders rather than offscreen surfaces.

Anyway, here's the thing: you create two surfaces, one for accumulation of total color and other for current processing, for every light souce fill the current surface with light and shadows, apply to accumulation surface with addition mode, repeat. Finally, apply resulted surface with "multiplication" color mode to the plain lit white image.
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Tile based lights with different colors overlapping

Post by Ranguna259 »

Or you could make a layer for each light source ?

(Here's a side view exemple of each layer)
('c' being the center of the circle and '_' being the ever darker light)
Inicial(no light source): (0 layers)

one light source:

Code: Select all

:        ______c______
(1 layer)

two light sources:

Code: Select all

: ______c______
:        ______c______
(2 layers)

three light sources

Code: Select all

:   _________________c_________________
: ______c______
:        ______c______
(3 layers)

You see, each source has it's own layer so it is

Code: Select all

cell[x][y][l]
l being the number of the layer, each layer has only one source.
You could also do

Code: Select all

cell[l].x
cell[l].y
Since each source has it's own layer and each cell has it's alpha value then love will take care or overlaping all the layers so each cell will be on top of the other creating a different colors when there are two cells from two layers with different colors each, this happens due to the alpha's value.

I don't think I explained well enough, tell me if you didn't understand, and once you are done please post the final .love, I want see the pretty colors too ;)
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Tile based lights with different colors overlapping

Post by micha »

veethree wrote:Then to update the lights i loop through all the cells, On each cell i loop through all the light sources, And check if the cell is within the radius of any of them, Then based on the distance between the cell and the sources i calculate the light_level for the cell, If the cell is within the range of multiple lights, I calculate the light_level for the lights individually, Then add them together. I'm not updating the lights each frame so it's not as inefficient as it sounds (Could probably be more efficient though)
How about this: Separate the light into three channels for red, green and blue. Process each channel as you describe in the quote. In other words: Just "add" the value for red, green and blue separately.
User avatar
veethree
Inner party member
Posts: 875
Joined: Sat Dec 10, 2011 7:18 pm

Re: Tile based lights with different colors overlapping

Post by veethree »

micha wrote:
veethree wrote:Then to update the lights i loop through all the cells, On each cell i loop through all the light sources, And check if the cell is within the radius of any of them, Then based on the distance between the cell and the sources i calculate the light_level for the cell, If the cell is within the range of multiple lights, I calculate the light_level for the lights individually, Then add them together. I'm not updating the lights each frame so it's not as inefficient as it sounds (Could probably be more efficient though)
How about this: Separate the light into three channels for red, green and blue. Process each channel as you describe in the quote. In other words: Just "add" the value for red, green and blue separately.
I can't figure out if that would work or not..Good idea though, Gonna try that at some point. Thanks.
User avatar
TimeLoop
Prole
Posts: 28
Joined: Wed Apr 17, 2013 8:20 am

Re: Tile based lights with different colors overlapping

Post by TimeLoop »

When increasing the lights' size I was expecting to increase the radius of the light source itself but it only increases the radius of the area where the light is banished, so for example I would expect to increase the size of the lamp bulb if they where lamps, and that means that the area affected by the light gets increased too.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 207 guests