Page 1 of 1

Lighting

Posted: Sun Oct 21, 2012 2:06 pm
by Anickyan
I have been thinking about something: an easy way to apply simple lighting to games. I was thinking about creating an emtpy ImageData, and filling it with a dark color, with a low alpha value. And loop through all of the tiles, and measure the distance from every light source, and then apply the correct shading.

Measuring distances is pretty easy:

d = sqrt( (x2 - x1) ^ 2 + (y2 - y1) ^ 2 )

And then I would use ImageData:setPixel() to apply appropriate shading to the tile/pixel.

I was wondering if this would be too slow, and if there were any alternatives. I do not really want to get into PixelEffects, as I only need very simple lighting.

Re: Lighting

Posted: Sun Oct 21, 2012 3:37 pm
by kikito
I've seen several ways of adding light to a game. Have you tried searching through the forums?

Re: Lighting

Posted: Sun Oct 21, 2012 3:51 pm
by Ref
What's wrong to just use a simple shader?
Just an option depending on what you want to accomplish.

Re: Lighting

Posted: Sun Oct 21, 2012 3:56 pm
by Anickyan
That is not the effect I wanted, but thanks anyways. I don't want it to be a perfect circle, but rather be "blocky", and only have one shade per tile.

Re: Lighting

Posted: Sun Oct 21, 2012 4:43 pm
by Inny
Sounds like you want a flood fill algorithm that traverses out a number of tiles from the light source, remembering distance traveled, and setting the lighting value to the larger of the various lighting sources that traveled the block. This would give you a kind of diamond lighting shape, but making it a circle would be easy enough if you took some considerations like if you travel to the same block from multiple directions, add a logarithmic value.

Re: Lighting

Posted: Tue Oct 23, 2012 12:20 pm
by graham77
I've seen several ways of adding light to a game. Have you tried searching through the forums?
I second his opinion, off course there are many ways of doing it. Search through forums /blogs else I'll tell you ways.

Re: Lighting

Posted: Wed Oct 24, 2012 7:40 pm
by qaisjp
I'd like to create SuperMeatBoy like lighting but there is no point using shaders (that my current pc cannot run) when SuperMeatBoy doesn't use much of them (which my current pc can run)

Re: Lighting

Posted: Thu Oct 25, 2012 12:03 pm
by T-Bone
It doesn't have to be slow, if you save your rendering to a Canvas, you only need to calculate the lighting once. But if you want stuff to react to movements of the light source for example, it might get very slow. It is also very resolution dependent. If you calculate the lighting in a low resolution and then scale it up, it can be much faster (and look more pixely which is awesome).

Re: Lighting

Posted: Thu Oct 25, 2012 12:24 pm
by substitute541
There are lots of ways. Probably the simplest isto find the distance from one tile, to a light source, divide it by something to create a ratio, multiply the ratio by its brightness (which is a percentage) and just use the new value to somehow darken the tile.