2008年11月28日

PV3D 2.0: How to do simple realtime shadows

PV3D 2.0: How to do simple realtime shadows

Dec 6th, 2007

Some people have mailed asking about how the shadows were done and where was the function in the API to create them.

Well, first of all PV3D 2.0 doesn't do the shadows automatically, but the new features let you do it (an many more other things). As soon as I knew that with the new version you were able to render the same scene in different sprites I though "shadows!". So, went quickly to give it a try and it worked perfectly. The idea is basically to do 2 renders per frame (this may be slow if you apply it at the end of a project). First render from the top of the scene, apply that render to the white plane at the bottom, and then second render from the user's camera.

This is the view from the "light camera" that gets rendered into a 512x256 bitmapViewport:



Next thing I wanted was to make the shadow a less dark, so, having already the bitmap, I just need to apply a colorTransform to it and make it a bit more pale.



And finally, apply a BlurFilter to the bitmap so the shadows is a bit diffuse



And that's all, this is the result:



Just as a last note... for the ball example I had to change before rendering the shadow the texture of the ball to black, and then change it back to the backed AO material.

Next thing on this matter will be to try to implement stencil shadows (because at the moment the shadow only works if the light is on the top, or one side...)

Oh! I almost forgot, here is the source file (requires flex2SDK, PV3D 2.0 and Tweener).