TIGSource Forums

Developer => Technical => Topic started by: Morgan Ender on April 08, 2011, 07:59:52 AM



Title: Dynamic 2D Lighting
Post by: Morgan Ender on April 08, 2011, 07:59:52 AM
Is it possible without using depth maps and shadow maps and all those kinds of things? What's the general idea behind it? How would one efficiently implement dynamic lighting in a 2D game?

I'm not asking for a catch-all "make my lighting system for me" answer, I'd just like to know how you'd generally start with a lighting system that doesn't depend on texture-based calculation "maps".  :shrug2:


Title: Re: Dynamic 2D Lighting
Post by: kevglass on April 08, 2011, 08:05:54 AM
I implemented this a while ago using a tilemap with vertex colours set based on light sources, it was reasonably efficient:

(http://dl.dropbox.com/u/1668516/concept/warp3.png)

Just setting the vertex colours on the tiles and the entities moving between them based on the distance to point light gave some nice effects. Eventually I put a second layer of smaller tiles across the top to improve the resolution of the light maps.

Kev


Title: Re: Dynamic 2D Lighting
Post by: slembcke on April 08, 2011, 10:08:30 AM
2D lights are pretty easy without shadows, and even with shadows they aren't too bad.

Without shadows you just need to generate a simple lightmap, an offscreen image that is the size of the screen that holds how bright certain onscreen areas are. Each frame, you clear it to your ambient lighting value, then use additive blending ((or you can also use a "screen" blending mode if possible for better results) and draw radial gradients for each of your lights. After drawing your normal scene, you use a multiply blending mode to draw the lightmap over the top.

(http://files.slembcke.net/upshot/upshot_4DoDGECy.png)
The top third is the lightmap. Just circle gradients added together. Colored lighting is trivial too, just tint it a different color. You can even use a different shaped mask for the light if you want.
The middle third is the first image I found handy. Just some cat fur.
The last is the lightmap multiplied against the image. Tada! Easy.

IIRC there was a lengthy post on the forums about implementing this sort of lighting along with shadows in Flash. Though shadows use a *lot* of fillrate and draw calls and really require hardware acceleration if you want more than one or two lights on screen at a time.


Title: Re: Dynamic 2D Lighting
Post by: slembcke on April 08, 2011, 10:58:41 AM
http://forums.tigsource.com/index.php?topic=8803.0


Title: Re: Dynamic 2D Lighting
Post by: Blademasterbobo on April 08, 2011, 08:02:03 PM
http://www.facebook.com/note.php?note_id=395139091995


Title: Re: Dynamic 2D Lighting
Post by: slembcke on April 09, 2011, 06:38:13 AM
Kinda weird that he uses a triangle fan to draw the visible parts. I can't really think of a simple way to compute that unless I'm missing something obvious? Masking out the occluded parts instead is trivial and I would guess that the wasted fillrate is way less expensive than finding the visibility perimeter on the CPU.


Title: Re: Dynamic 2D Lighting
Post by: mcc on April 09, 2011, 03:13:06 PM
Normal mapping works quite well in 2D and produces really striking object shadowing... I saw a good Flash demonstration of this once that I now cannot find.

http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=2d+normal+map


Title: Re: Dynamic 2D Lighting
Post by: slembcke on April 10, 2011, 02:33:20 PM
Yeah, although you really have to do deferred lighting if you want to use it. Multipass lighting isn't really an option unless you are using hard alpha and depth testing.

I was like inches away from implementing it myself before realizing that I can barely make enough art to make a game as is, adding more scope creep and another layer of art that I can already barely handle isn't doing myself any favors. Heh.


Title: Re: Dynamic 2D Lighting
Post by: Rob Lach on April 11, 2011, 12:04:20 AM
I worked on a simple 2d lighting system.

Here's a video of an early iteration.
http://www.vimeo.com/10106815


I have a game in the works which will use it extensively.


Title: Re: Dynamic 2D Lighting
Post by: slembcke on April 11, 2011, 05:21:41 AM
Are the shadows projected from the sprites themselves or did you calculate boundary data from them? Is it just using a lot of samples in a pixel shader or is there something fancier going on? How is the performance on longer shadows?


Title: Re: Dynamic 2D Lighting
Post by: Rob Lach on April 11, 2011, 06:06:34 AM
In that versions it's pretty simple.

It's all shaders. Basically i'm casting off lines and seeing if they're obstructed. The bottom left viewport is where the light reaches and the color dictates from which angle the light hits it (you don't need to save that off but I did for debug reasons).

I made that version in XNA so I got it running on an xbox 360 so I could get a real world idea of performance and it ran a cool 60fps up to about 10 light sources or so.

My latest version is a bit more advanced and faster but the general premise is the same.