PDA

View Full Version : 2D Shadowcasting Demo



sHTiF
October 19th, 2010, 06:44 PM
Hi there, recently i've been inspired to work on my Genome2D engine again and since reading the topic by justkevin and his Darkness game I focused on dynamic lightning and shadows in 2D game.

Who doesn't know what Genome2D is don't panic its my 2D game engine which i neglected for around a year due to other work so its time to work on it once more. The 15 month old Genome2D demo can be found here http://www.flash-core.com/examples/g2d/270609/ (W/S/A/D/Mouse Click)

Its kind of obsolete since i ported parts of Genome2D to Alchemy and haXe however i had no time to post a new demo yet.

Anyway as mentioned i focused on lights and shadow casting in this new non-interactive demo to show that it can be done quite easily and at decent speed. Latest shadow demo can be found here: http://www.flash-core.com/examples/shadows/

It runs at my Q6600 2.4GHz at 55-60FPS where 60 is the well known new flash player fps cap.

It has 6 dynamic lights with various speeds and 104 shadow casting objects which rerender each frame if needed. I was kind of surprised that justkevin in his post said that the lightning render was a bottleneck with just one shadow casting light.

The shadow casting technique is quite straight forward, each light consists of 2 layers, first layer is the light itself and second layer drawn on top of it is the shadow layer. Each shadow casting object is represented by a polygon (rectangles in this demo but work with any polygons) so light firstly checks if the polygon is in range of the light since there is no need to cast shadows of polygons which are outside of the radiance. Once the polygon is in range it renders it shadow by iterating edges and checking if the edge is a back edge from current light position if yes then draw an edge shadow if not skip the edge since front edges should not cast shadows.

Pretty simple, there is some optimalizations used since the layers are managed using bitmap data copypixels and drawing within a bounding rectangle so the really long shadows outside of light radiance will be cropped within.

There is no scenario specific optimization so as it is it can be used with any kind of objects and those objects may have dynamic nature (moving, scaling, rotating) the shadows will render correctly without any performance loss. And ofcourse the object can have various "height" defined as well as can be seen in the old Genome2D demo crates cast smaller shadow than pillars.

I will probably post more demos just for the fun of it. The two optimalizations that come to mind is quad tree for casting precheck and silhouette drawing using one pass instead of per edge pass. But both of these optimalizations based on my earlier experience will not increase the performance in any significant way, i will give them a try thou ;)

If there are any questions, feel free to ask, if there are any suggestions or ideas for upcoming demos i would be very pleased to read them and maybe implement them.

Thanks.

justkevin
October 19th, 2010, 09:01 PM
The genome example looks great.

With the new one there's so much going on it's hard to tell what's happening, but something doesn't look quite right-- sort of like an individual light is okay, but the intersection of multiple lights looks off.

How does it look with just two lights (maybe freeze one and put the other under mouse control so you can experiment)?

Gathan
October 19th, 2010, 10:38 PM
Anyway as mentioned i focused on lights and shadow casting in this new non-interactive demo to show that it can be done quite easily and at decent speed. Latest shadow demo can be found here: http://www.flash-core.com/examples/shadows/

It runs at my Q6600 2.4GHz at 55-60FPS where 60 is the well known new flash player fps cap.

I'm getting 15-20fps myself now don't get me wrong I like the shadow effect, but something that uses that much processing power isn't very useful for a practical application, that demo game doesn't performance much better either with fps going at a crawl.

sHTiF
October 20th, 2010, 04:10 AM
justkevin: Sure i will post demo with single light or small number of lights so you can see it, this was just a bruteforce test. Couple of other people wrote me that its too chaotic as well ;)

Gathan: 20fps is really low however bear in mind that you hardly have 6 active lights with 104 objects within range this was an over the top demo which i scaled so i was able to see the slowdown <60 fps at my computer. With single dynamic light you will have over 200fps with any number of objects at the scene i have to disagree with you here that its unusable. Static lights in my shadow technique doesn't rerender so you can have as many static lights as you want there is only memory limitation.

sHTiF
October 20th, 2010, 06:57 AM
@Justkevin here is a new demo where one light is static in the middle and the other one can be controlled by mouse drag. http://www.flash-core.com/examples/shadows2/

I am working on further optimalizations if possible. I can also post a debug version where you see the generated light bitmaps without the ADD/MULTIPLY turned on so you can see it better if you want.

Another thing is that this method is not limited to single color channel so you can have lights with different colors as well. Also shadow intensity from distance is easily attainable as well.

When it comes to performance the only thing that matters is the size of the light obviously due to rendering, shadow bitmap size etc. And objects on the scene, all objects are evaluated for possible shadow cast only those within radiance actually cast shadows. So in your game where you have max 3-4 shadow casters within the range at any give time the actual shadow casting and calculation would take almost zero time, then its down to flash render pipeline as speed of the ADD/MULTIPLY itself. I also tried to utilize pixel bender but the overhead of the actual call is too much here, i didn't discard this path for good yet though ;)

The good thing is that size of the object doesn't matter, size of the shadow is cropped to the light bitmap. So you are not limited with a smaller size resources at all.

@Gathan: BTW what kind of computer do you have because i just tested it on old mac with 2.16GHz and it still run over 50FPS and the old Genome2D well over 60. So you either have old computer or more probably run both swfs at the same time, don't forget that swf player within browser uses shared resources so if you run two or more tabs with swfs all of the swfs will be slower. And don't forget to install latest player as well.