I was recently playing around with Papervision and wanted to create a cube that faded in and out. A pretty simple thing to do, I was even using a Tween engine so it was just a few lines of code. However, the cube would just not fade! After a quick search on Google I found a useful post that quickly explain how to fix this. Basically you need to set the DO3D so it uses it’s own container like this:
Simple! But why is this? Being an inquisitive person I wanted to know exactly what was the reason for this and after a quick search into the code I found out why.
Basically, as I’m sure most people are aware, everything you render in Papervision is drawn to a Viewport3D, which extends Sprite and can be added to the display list. In order for you to have an alpha on an individual DisplayObject3D (or anything that extends it like a Sphere or a Cube) you need to give that object it’s own ViewportLayer and then the alpha can be applied to that. This is also useful for putting filters on objects in PaperVision.
Whilst doing this bit of snooping around I thought that a much nicer way of doing this would be to make the alpha variable a private member of the DisplayObject3D class, then create a getter/setter for it. Within the setter we can check to see if useOwnContainer is set to true, if it isn’t, we can do so thus removing the need for the code snippet above. I’ve amended the DisplayObject3D class slightly to incorporate this and you can download it here. Just replace the current DisplayObject3D.as in org/papervision3d/objects and away you go.
I’ve also added getters/setters for filters and blendMode which work in the same way.
I hope the above helps people and I really recommend doing your own snooping about in the PaperVision code to get a better understanding of how it all works as this will really help you to work with it.
UPDATE July 8, 2009, 18:33:
Since writing this post I have spoke to Chris O’Byrne about it and he raised some interested points that maybe I overlooked. His main point was that in a way my “fix” obfuscates how Papervision works and that it’s perhaps better to make developers set useOwnContainer themselves so they gather a better understanding of what they are actually doing. I think Chris may have a good point to be honest, although maybe an alert/warning to make beginners with Papervision aware would be a good compromise.