Sam MacPherson

Flash, Haxe, Game Dev and more…

2D Dynamic Lighting Demo

In my previous post I went over how to implement 2d dynamic lighting on the GPU. After some tweaking I finally came up with a suitable solution for general purpose dynamic lighting. Here is a video demonstration of this in action in a game I am currently developing – codename Zed.

Currently the lighting only works with static objects as light blockers, but it’s not very hard to extend this to moving objects as well.

To get the effect right I had to modify my previous code and split the rendering into 2 separate tasks. First I do an additive light pass which fills in the glowing light you can see in the video. Next I had to do a subtractive shadow pass which worked by just starting with a black texture and subtracting off the alpha component as necessary. After each of these runs I do a 5x Gaussian blur effect to make things look nice and smooth and voila you have general purpose lighting that runs reasonably fast.

Depending on the quality of the light and your graphics card there are some limitations. I still plan on doing some more optimizations and benchmarking, but it seems that you are stuck with ~40 lights max on screen at any given moment. Possible optimizations could be caching of light textures that are static which would allow for a lot more stationary lights like what you saw in the game.


5 responses to “2D Dynamic Lighting Demo

  1. robo September 5, 2012 at 11:49 pm

    is that source available?

  2. Sam September 6, 2012 at 2:06 pm

    Kind of. You can follow a slightly older version of this lighting in my previous post: . Here is the source for the final result: (it has dependencies from elsewhere in my game library, but you can probably work it out for yourself). Let me know if you need anything else. Oh and I wrote this in Haxe btw not sure if you were expecting AS3 or not. 😛

  3. sam May 3, 2013 at 2:04 am

    would you mine open your this great engine to the github ?
    I just find other opensource engine call antill do the same thing
    unfortunately,it’s not use stage3d technology.

  4. sam2 May 6, 2013 at 5:51 am

    thank you
    ,I’m using haxe3 now.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: