Complex Spotlight Shadow Caster for Papervision3D

Simulates the projection of shadows coming from a spotlight light source onto irregular surfaces. In the supplied example, an animated MD2 model (from Quake 2) is casting a shadow onto the inside of a sphere. Supported parameters include shadow color, alpha, blur, falloff and texture map size, as well as position, rotation and field of view. This evolves out of the projection work posted previously.
Source:
SpotlightShadowCasterManager.as
Usage:
After instantiating the manager –
_shadowCaster = new SpotlightShadowCasterManager();
– assign one or more DisplayObject3D’s that will block the light from the spotlight:
_shadowCaster.registerShadowCasterObject(myMesh);
Then register one or more meshes that can have shadows cast upon it with registerShadowCasterObject. As the second argument, you must supply a BitmapMaterial utilized by the mesh on which the manager will draw the shadows. Usually, you’ll want to create a CompositeMaterial, with the BitmapMaterial most likely at the top of the stack. It might look something like this:
var compositeMaterial = new CompositeMaterial(); compositeMaterial.addMaterial( new ColorMaterial(0x888888) ); var mat:BitmapMaterial = new BitmapMaterial( new BitmapData(1,1) ); // .. the bitmapdata will get overwritten, // but must contain _something_ var sphere:TriangleMesh3D = new Sphere(compositeMaterial, 1000); _shadowCaster.registerShadowReceiverObject(sphere, mat);
To update the shadow textures, call
_shadowCaster.update();
– probably in your onEnterFrame or onRenderTick function.
That’s enough to get going. From there, it’s just a matter of adjusting the projector’s position and orientation in relation to the objects in your scene with the properties projectorPosition, projectorPitch, and projectorRoll. Check the source for the various configurable properties like blur, alpha, color, etc.
Limitations, bugs:
- As described above, objects must be manually assigned to be either a ‘caster’ or a ‘receiver’. These roles aren’t determined dynamically by the class itself.
- Only one level of shadows is supported. Adding an arbitrary number wouldn’t be difficult, just cumbersome to manage under the current system, and increasingly expensive.
- Unfortunately, a ’shadow receiver’ object can’t have its own texture in addition to the one used to cast shadows onto, at least not in a way that looks decent. The reason is that a 3d object in Papervision3D has only one set of UV texture data and the shadow texture overwrites this with every update.
- There’s undoubtedly a less expensive way to do the raycasting/projection in the main math routine in the private function updateMesh. If you’re adept at this kind of thing, please send your improvements!
- Ugly and obvious artifacts show up on the ‘projectable’ textures when the spotlight is rotated 90 degrees away them. (You may have already noticed this in the previous two projection experiment posts). Again, I’d love any solutions anyone has for this.
Licensed under a Creative Commons Attribution 3.0 License.

June 9th, 2009 at 2:08 am
nice shadows man ! :)
June 9th, 2009 at 7:59 am
yeah!
June 9th, 2009 at 10:19 am
Amazing work! I love it!
Thanks for this nice addition. Keep it up ;)
June 9th, 2009 at 11:13 am
NICE — One step closer….. :)
June 10th, 2009 at 2:26 am
Amazing!
June 10th, 2009 at 10:21 am
Dude… that rocks!
June 10th, 2009 at 9:31 pm
baddly love the awful shadows!
ps:still i’d like to know further about how to control a model :)
October 1st, 2009 at 4:19 am
I see the common limitation of yours ShadowCaster and of Andy Zupko’s is that VectorVision Text3D class in not supported . Is it possible that you one day make it compatible ?
Thanks
December 29th, 2009 at 2:18 pm
Hi All,
I really like the shadow animation. Is it possible to use any other file type like .obj or .dae other than MD2 objects. I really need help with this. I would really appreciate if anyone has an idea about this.
Thanks,