→ Click here or the image above for the reflection demo → View the source code
I alluded to a desire to implement reflections in Away3D in my prior post on reflections in AS3. Well here it is. It wasn’t quite as easy of a translation as I thought it would be, but with lots of digging through the Away3D dev list, as well as spotting a good example at geepers.co.uk, I managed to get a basic demo up and running.
While there’s a good bit of code that goes into this demo, the secret sauce is right here:
1 2 3 4 5 6 |
|
What this code does is give us the DisplayObject that contains our 3D object within the given view. We are also able to determine the bounds of the DisplayObject and its 2 dimensional position within the view. With this information we are able to effectively create a snapshot of the visible 3d object. From there we need to create BitmapData that is the inverse of the DisplayObject. To do that we apply the appropriate matrix (invert the y scale) to the DisplayObject when it is drawn to BitmapData. After that we assign the BitmapData to the display Bitmap and then position it according to the bounds of the DisplayObject.
1 2 3 4 5 6 7 |
|
Finally we create an alpha gradient mask that will be applied to the Bitmap in order to give it that cool, fading reflection look.
1 2 3 4 5 6 7 8 |
|
And there you go, reflections in Away3D. There are a number of limitations to this method, though:
- It can only draw reflections of objects currently visible in the view. In fact, there should be a check on the (_view.session.getContainer(_view) as DisplayObjectContainer).getChildAt(0) call to make sure that the view contains any children.
- The reflection is drawn of the entire view, not just the object in question. That’s why this works best with only one object in the view. You can use multiple views to get reflections for multiple objects.
- This example only does reflections of the view as its manipulated along the X & Y planes. Moving the object along the Z plane will not effect the reflection properly. I’m sure a method utilizing planes could do better.
If those limitations don’t bother you, then go to town. If they do, be patient. I’m working on another idea for reflection in Away3D that will be a lot more flexible, though more complex and processor intensive.
Remember how my previous blog post on shadows (of course you do) showed how you can easily enhance the 3D feel of your site? Of course you do. Now you can add reflections to that repetoire. It’s all about subtle changes to give your work a more polished look and feel. Have fun and let me know if you create reflections this way, or if you have an implementation of your own.