package { import aether.textures.natural.MarbleTexture; import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Sprite; import flash.display.TriangleCulling; import flash.events.Event; import flash.geom.Matrix; import flash.geom.Matrix3D; import flash.geom.Rectangle; import flash.geom.Vector3D; [SWF(width=600, height=400, backgroundColor=0x000000)] /** * Demonstrates the use of drawTriangles(), Matrix3D and Vector3D to render a 3D model. * This draws a textured pyramid to the screen and rotates it each frame. The texture is * completed generated at runtime using the aether library. */ public class graphic_flex_image_effects_07_Flex_MarblePyramid extends Sprite { private var _marble:BitmapData; private var _model:graphic_flex_image_effects_07_Flex_Mesh3D; private var _transform:Matrix3D; /** * Constructor. Centers sprite, creates model and texture then renders model. * An event listener is set up to handle the animation. */ public function graphic_flex_image_effects_07_Flex_MarblePyramid() { x = stage.stageWidth/2; y = stage.stageHeight/2; _model = new graphic_flex_image_effects_07_Flex_PyramidMesh(this); // creates initial 3D transform _transform = new Matrix3D(); createMarble(); render(); addEventListener(Event.ENTER_FRAME, onSpriteEnterFrame); } /** * Generates marble texture to be used with the pyramid model. */ private function createMarble():void { // creates a marble texture using aether _marble = new MarbleTexture(512, 512, 0x227766).draw(); // creates four black rectangles of different opacity // in large bitmap data to be used for baked in lighting var lighting:BitmapData = new BitmapData(900, 900, true, 0x00000000); lighting.fillRect(new Rectangle(450, 0, 450, 450), 0x44000000); lighting.fillRect(new Rectangle(0, 450, 450, 450), 0x44000000); lighting.fillRect(new Rectangle(450, 450, 450, 450), 0x88000000); // image is translated and rotated for better use with the texture and model var matrix:Matrix = new Matrix(); matrix.translate(-450, -450); matrix.rotate(Math.PI*.25); matrix.translate(255, 255); // draws lighting onto marble texture _marble.draw(lighting, matrix); lighting.dispose(); } /** * Draws the 3D model to the screen using drawTriangles(). */ private function render():void { // applies current transform to the model _model.applyTransform(_transform); graphics.clear(); graphics.beginBitmapFill(_marble); graphics.drawTriangles( _model.vertices, _model.sides, _model.uvtData, TriangleCulling.NEGATIVE ); graphics.endFill(); } /** * Handler for the ENTER_FRAME event. This rotates the transform, * then rerenders the model. * * @param event Event dispatched by this sprite. */ private function onSpriteEnterFrame(event:Event):void { _transform.appendRotation(2, Vector3D.Y_AXIS); render(); } } }