[Flash/hx] Dynamic 2D Lighting Engine

641 views
Skip to first unread message

PSvils

unread,
Oct 29, 2012, 2:35:19 PM10/29/12
to haxe...@googlegroups.com
Hey all!

So I haven't been too productive, university is taking up lots of time. But I just wanted to share my Haxe based Dynamic Lighting Engine for Flash!
https://github.com/PDeveloper/hxDynaLight

It takes in polygons or circles as occluding objects, and then you just add a few lights, and it should be rendering dynamic lighting pretty efficiently! A light his a heavier hit than another object, and circles aren't as fast as polygons! I do not recommend anyone porting this over to NME btw, since it uses bitmapData functions, and those run horribly slow on most native platforms!

You can ask questions here or on my blog (there's also a post that gives a very general overview of the workings of the lighting engine)
http://psvilans.wrongbananas.net/dynalight-dynamic-lighting-engine/

On the Github Wiki, there's a code example that should get you started real quick, and afaik, it should all be straight forward to set up, but of course, I'm new at this public library type of thing, so definitely please pass me some feedback! :)
For AS3 users, there's a .swc of the library in the downloads.

Only dependency is polygonal-ds (Polygonal's datastructures library). Not sure if it's okay that my libraries .swc contains some of his classes?

P.

PSvils

unread,
Oct 29, 2012, 2:38:59 PM10/29/12
to haxe...@googlegroups.com
Overview of the engine:
http://psvilans.wrongbananas.net/dynamic-2d-lighting/

Working demo with 100 objects, and 3 moving lights:
http://www.psvilans.wrongbananas.net/projectDir/dynalight/

Tarwin Stroh-Spijer

unread,
Oct 29, 2012, 3:00:18 PM10/29/12
to haxe...@googlegroups.com
Thanks, look really awesome.

If you haven't seen it there's also this great blog on a similar subject: http://www.redblobgames.com/articles/visibility/


Tarwin Stroh-Spijer
_______________________

Touch My Pixel
http://www.touchmypixel.com/
cell: +1 650 842 0920
_______________________


PSvils

unread,
Oct 29, 2012, 3:12:13 PM10/29/12
to haxe...@googlegroups.com, tar...@touchmypixel.com
Mhm, I've seen that and chatted with Amit! :)
He uses a different rendering technique, with the drawTriangles function, which works slow in the Flash Player. I do manual BitmapData drawing (setPixel). Of course, using drawTriangles() looks much better, and also then opens up possibilities of individually coloured lights! It might be better suited for NME though...should check that out...

Philippe Elsass

unread,
Oct 29, 2012, 3:38:42 PM10/29/12
to haxe...@googlegroups.com, tar...@touchmypixel.com
NME would definitely require a solution NOT involving Bitmaps at all.
--
Philippe

Ashiq A.

unread,
Oct 30, 2012, 10:41:03 AM10/30/12
to haxe...@googlegroups.com
Sweet, thanks for sharing! I was looking for something like this. A shame it's Flash-only for now, but it'll do. Oh yes, it'll do quite well...

For anyone else who wants to see the demo (took a bit of digging), it's here: http://www.psvilans.wrongbananas.net/projectDir/dynalight/

+1,

--Ashiq

j...@justinfront.net

unread,
Oct 30, 2012, 11:27:14 AM10/30/12
to haxe...@googlegroups.com
A shame it's Flash-only 

mm yes I was wondering about what approach you might use for javascript since I have been trying pixel manipulation, got some interesting interference pattern effects with adjusting an image alpha based on sine
    
   var w = 250;
           var h = 250;
   var imageData = context.getImageData( 0, 0, w, h );
   var count = 0;
   for( i in 0...w )
   {
       for( j in 0...h )
       {
           count++;
           for( col in 0...4 )
           {
               var k = count*4 + col;
               var p = imageData.data[ k ];
                imageData.data[ k ] = switch( col )
               {
                   case 3: 200 + Math.round( ( 255 - 200 )*Math.sin( ( (i*k)%250 )/Math.PI/5 )  ) ;// alpha
                    case 0: p; // red
                            case 1,2: 0; //  green, blue
                     }
                    }
           }
       }
   }
   context.putImageData( imageData, 0, 0 );

The fancy part is i*k, if you want just a horizontal sine just use k which is what I did initially, if you want more pointillist then i*k*j.  Looking briefly at Jeash I suspect that it's better to work with the raw data (as I am) than to use the bitmapdata wrapped code which probably adds an extra overhead ( I am using drawImage to put the image on the canvas after first using untyped new Image onload callback, creatElement won't work ). Anyway just thought I would share snipit incase someone wants to try creating a js version of the lighting code.

But I guess there must be simpler approaches than/to pixel manipulation in javascript?

David Peek

unread,
Oct 31, 2012, 1:57:14 AM10/31/12
to haxe...@googlegroups.com
Divs?

PSvils

unread,
Oct 31, 2012, 12:41:30 PM10/31/12
to haxe...@googlegroups.com
No, I think pixel manipulation will be the best you can get, though I'm no JS expert, so I'm not the right person to ask.
The bitmapData line/circle/arc drawing code is all in the lib, so it should be pretty easy to port that over to JS! In a newer version of the engine, I'm probably going to do pixel manipulation through a bytearray, which should speed it up a lot, plus be closer to that JS snippet you posted :)

P.
Reply all
Reply to author
Forward
0 new messages