开始学习AWAY3D,只会些简单的,先一步步的了解,学起。这里是怎么将面拼成一个立方体的例子。大家也可以学习拼更多的几何体。

package
{
    import away3d.cameras.Camera3D;
    import away3d.containers.Scene3D;
    import away3d.containers.View3D;
    import away3d.controllers.HoverController;
    import away3d.debug.Trident;
    import away3d.entities.Mesh;
    import away3d.lights.DirectionalLight;
    import away3d.materials.TextureMaterial;
    import away3d.materials.lightpickers.StaticLightPicker;
    import away3d.primitives.PlaneGeometry;
    import away3d.utils.Cast;
    
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.display.LoaderInfo;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.net.URLRequest;
    import flash.text.TextFormat;

    public class AwayCoordinate extends Sprite
    {
        private var imageList:Array =["../embeds/image1.jpg","../embeds/image2.jpg","../embeds/image3.jpg","../embeds/image4.jpg","../embeds/image5.jpg","../embeds/image6.jpg","../embeds/image7.jpg","../embeds/image8.jpg","../embeds/image9.jpg"]        
        private var _view:View3D;
        private var _scene:Scene3D;
        private var _camera:Camera3D;
        private var _controller:HoverController;
        
        private var _planeMaterial:TextureMaterial;
        
        private var _light1:DirectionalLight;
        private var _light2:DirectionalLight;
        private var _lightPicker:StaticLightPicker;
        
        private var _move:Boolean;
        private var _lastPanAngle:Number;
        private var _lastTiltAngle:Number;
        private var _lastMouseX:Number;
        private var _lastMouseY:Number;
        
        private var _tri:Trident;
        private var _textureVector:Vector.<TextureMaterial> = new Vector.<TextureMaterial>();
        private var _loadTotalNum:int =0;

        public function AwayCoordinate()
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            load(imageList[_loadTotalNum]);
        }

        private function Init():void
        {
            InitEngine();
            InitMaterial();
            InitMesh();
            InitListener();
            InitTrident();
        }
    
        private function InitEngine():void
        {
            _scene=new Scene3D();
            _camera=new Camera3D();

            _view=new View3D();
            _view.antiAlias=4;
            _view.scene=_scene;
            _view.camera=_camera;
            addChild(_view);
            
            _controller = new HoverController(_camera);
        }
        
        private function InitTrident():void
        {
            _tri=new Trident(550, true);
            _scene.addChild(_tri);
        }
        
        private function InitLight():void
        {
            _light1 = new DirectionalLight();
            _light1.ambient =0.1;
            _light1.diffuse = 0.3;
            
            _light2 = new DirectionalLight();
            _light2.ambient = 0.2 ;
            _light2.diffuse = 0.5;
            
            _lightPicker = StaticLightPicker([_light1,_light2]);
        }
        
        private function InitMaterial():void
        {
        
//            var videoTexture:VideoTexture = new VideoTexture("../embeds/Eric.flv",256,256,true,true);
//            _planeMaterial = new TextureMaterial(videoTexture);
            
            for(var i:int =0 ;i<9;i++)
            {    
                //var textureMaterial:TextureMaterial = new TextureMaterial(Cast.bitmapTexture(_planeM1));
                //_textureVector.push(textureMaterial);
            }
        }
        
        private function load(_url:String):void
        {
            var loader:Loader = new Loader();
            var request:URLRequest = new URLRequest();
            request.url = _url;
            loader.load(request);
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
        }
        
        private function completeHandler(e:Event):void
        {
            if(_loadTotalNum >=8)
            {
                Init();
                return;
            }
            
            var textureMaterial:TextureMaterial = new TextureMaterial(Cast.bitmapTexture(processBitmap(e.target.content as Bitmap)));
            _textureVector.push(textureMaterial);
            _loadTotalNum ++;
            load(imageList[_loadTotalNum]);
            (e.target as LoaderInfo).loader.unloadAndStop();
        }
        
        private function processBitmap(bitmap:Bitmap):BitmapData
        {
            var w:int =-1;
            var h:int =-1;
            var bd:BitmapData;
            var ready:int = -1; 
            
            for(var i:int =0 ; i<12 ;i++)
            {
                if(Math.pow(2,i) > bitmap.width && w == -1)
                {
                    w = Math.pow(2,i);
                    if(ready == -1)
                    {
                        ready = 0;
                    }
                    else
                    {
                        break;
                    }
                }
                
                if(Math.pow(2,i) > bitmap.height && h ==-1)
                {
                    h= Math.pow(2,i);
                    
                    if(ready == -1)
                    {
                        ready = 0;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            
            bd = new BitmapData(w,h);
            bd.copyPixels(bitmap.bitmapData,new Rectangle(0,0,bitmap.width,bitmap.height),new Point((w *0.5 - bitmap.width * 0.5),(h * 0.5 - bitmap.height * 0.5)));
            return bd;
        }

        
        private function InitMesh():void
        {
            var radious:int =270;
            var angle:int =90;
            var txtFormat:TextFormat = new TextFormat();
            txtFormat.size = 52;
            
            for(var i:int =0 ; i<6 ; i++)
            {
                var _planeGeometry:PlaneGeometry = new PlaneGeometry(700,700);
                _planeGeometry.doubleSided = true;
                _planeGeometry.yUp = false;
                
                var _plane:Mesh = new Mesh(_planeGeometry);
                _plane.material = _textureVector[i];
                _scene.addChild(_plane);

                
//                if(i ==0)
//                {
//                    _plane.x = accurateNumber(radious *)
//                    _plane.y = 0
//                
//                }
                
                if(i ==0)
                {
                    _plane.x = accurateNumber(radious * 0);
                    _plane.z = accurateNumber(radious);
                    _plane.rotationY =180;
                }
                else if(i ==1)
                {
                    _plane.x = accurateNumber(radious);
                    _plane.z = accurateNumber(0);
                    _plane.rotationY = -90;
                }
                else if(i ==2)
                {
                    _plane.x = accurateNumber(0);
                    _plane.z = accurateNumber(-radious);
                    _plane.rotationY = 0;
                }
                else if(i ==3)
                {
                    _plane.x = accurateNumber(-radious);
                    _plane.z = accurateNumber(0);
                    _plane.rotationY = 90;
                }
                else if(i ==4)
                {
                    _plane.y = accurateNumber(radious);
                    _plane.z = accurateNumber(0);
                    _plane.rotationX = 90;
                    _plane.rotationY = 90;
                }
                else if(i ==5)
                {
                    _plane.y = accurateNumber(-radious);
                    _plane.z = accurateNumber(0);
                    _plane.rotationX = -90;
                    _plane.rotationY = 90;
                }
                
//                if (i < 4)
//                {
//                    if(i!=0)
//                    {
//                    _plane.x = accurateNumber(radious * Math.sin((angle * i) / 180 * Math.PI), 1);
//                    _plane.z = accurateNumber(radious * Math.cos((angle * i) / 180 * Math.PI), 1);
//                    if(i%2==0)
//                        _plane.rotationY = 180-angle * i;
//                    else
//                        _plane.rotationY = -angle * i;
//                    }
//                    else
//                    {
//                        _plane.x = accurateNumber(radious);
//                        _plane.z = accurateNumber(radious);
//                        _plane.rotationY =180;
//                    }
//                }
            }
        }
        
        private function accurateNumber(num:Number,deg:int =1):Number
        {
            var degNum:Number = Math.pow(0.1,deg);
            return Math.round(num/degNum) * degNum;
        }
        private function InitListener():void
        {
            addEventListener(Event.ENTER_FRAME, enterHandler);
            stage.addEventListener(Event.RESIZE,resizeHandler);
            stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
            stage.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
            resizeHandler();
        }

        private function resizeHandler(e:Event =null):void
        {
            _view.width = stage.stageWidth;
            _view.height = stage.stageHeight;
        }
        
        private function mouseDownHandler(e:MouseEvent):void
        {
            _move = true;
            _lastPanAngle = _controller.panAngle;
            _lastTiltAngle = _controller.tiltAngle;
            _lastMouseX = stage.mouseX;
            _lastMouseY = stage.mouseY;
        }
        
        private function mouseUpHandler(e:MouseEvent):void
        {
            _move = false;
        }
        
        private function enterHandler(e:Event):void
        {
            if(_move)
            {
                _controller.panAngle = 0.3 *(stage.mouseX - _lastMouseX) + _lastPanAngle;
                _controller.tiltAngle = 0.3 * (stage.mouseY - _lastMouseY) + _lastTiltAngle;
            }
            _view.render();
        }
    }
}