Away3D基础教程(五):制作全景功能

效果如下:

 
制作思路:
1:全景功能其实就是在一个球体内部贴上一张鱼眼全景图片,然后把摄像机放在球体内部中心点,旋转摄像机就可以实现全景效果了。因为全景图要贴到球的内部,所以要把球体的双面贴图功能打开,_SphereGeometry.bothSides = true;  还有一种skyBox贴上6张不同面的全景图制作方法不在此做介绍,全景功能还是用球体方便。
 
2:使用HoverController这个摄像机控制类,  摄像机围绕球体原点旋转。这个我们经常用来坐360度环绕某点查看某3D物体。或者制作360度全景图。
 
2:把camera放到球体内部,_view.camera.position = _mesh.position; 设置camera的位置为球体的中心位置,在这里把球体放在了_mesh对象里,所以_mesh的中心位置就是球体的中心位置。
 
3:鼠标按下并移动开始旋转球体:
_cameraHoverController.panAngle = (mouseX - _mouseX) * .3 + _startX;



_cameraHoverController.tiltAngle = (mouseY - _mouseY) * .3 + _startY;

 

完整代码如下:

package  

{

    import away3d.containers.View3D;

    import away3d.controllers.HoverController;

    import away3d.entities.Mesh;

    import away3d.materials.TextureMaterial;

    import away3d.primitives.SphereGeometry;

    import flash.display.Loader;

    import flash.display.MovieClip;

    import flash.display.Sprite;

    import flash.events.Event;

    import flash.events.MouseEvent;

    import flash.display.StageAlign;

    import flash.display.StageScaleMode;

    import away3d.utils.Cast;

    import away3d.tools.helpers.MeshHelper;

    import flash.text.TextField;

    import flash.text.TextFieldAutoSize;

    

    /**

     * ...

     * @author yl

     */

    [SWF(width='800',height='600',frameRate="60", backgroundColor="0x000000")]

    public class MyAway3d_5 extends Sprite 

    {

        [Embed(source = "../embeds/ball.jpg")]

        private static var BallMaterial:Class;private var _startX:Number;

        private var _startY:Number;

        private var _mouseX:Number;

        private var _mouseY:Number;

        

        private var _view:View3D;

        private var _cameraHoverController:HoverController;

        

        private var _sphere:SphereGeometry;

        private var _sphereMaterial:TextureMaterial;

        private var _mesh:Mesh;public function MyAway3d_5() 

        {

            if (stage) {

                init();

            }else {

                this.addEventListener(Event.ADDED_TO_STAGE, init);

            }

        }private function init(e:Event = null):void {

            stage.scaleMode = StageScaleMode.NO_SCALE;

            stage.align = StageAlign.TOP_LEFT;

            this.removeEventListener(Event.ADDED_TO_STAGE, init);

            

            _view = new View3D();

            addChild(_view);

            _view.antiAlias = 6;

            //摄像机渲染范围2000

            _view.camera.lens.far = 2000;

            

            _cameraHoverController = new HoverController(_view.camera, null, 90, 0, 300);

            _cameraHoverController.yFactor = 1;

            _sphere = new SphereGeometry(1000, 64);

            //_sphere.yUp = false;

            _sphereMaterial = new TextureMaterial(Cast.bitmapTexture(BallMaterial));

            _sphereMaterial.bothSides = true;

            _sphereMaterial.smooth = true;

            _mesh = new Mesh(_sphere, _sphereMaterial);

            //MeshHelper.invertFaces(_mesh);

            

            _view.camera.position = _mesh.position;



            _view.scene.addChild(_mesh);this.addEventListener(Event.ENTER_FRAME, onEnterFrame);

            stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownFunction);

            stage.addEventListener(MouseEvent.MOUSE_WHEEL, onWheel);

        }private function onWheel(e:MouseEvent):void 

        {

            if (e.delta > 0) {

                if (_cameraHoverController.distance > 6) {

                    _cameraHoverController.distance -= 5;

                }

            }else if (e.delta < 0) {

                if (_cameraHoverController.distance < 600) {

                    _cameraHoverController.distance += 5;

                }

            }

        }

        

        private function mouseDownFunction(e:MouseEvent):void 

        {

            _startX = _cameraHoverController.panAngle;

            _startY = _cameraHoverController.tiltAngle;

            _mouseX = mouseX;

            _mouseY = mouseY;

            stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveFunction);

            stage.addEventListener(MouseEvent.MOUSE_UP,mouseUpFunction);

        }

        

        private function mouseUpFunction(e:MouseEvent):void 

        {

            stage.removeEventListener(MouseEvent.MOUSE_MOVE,mouseMoveFunction);

            stage.removeEventListener(MouseEvent.MOUSE_UP,mouseUpFunction);

            //stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownFunction);

        }

        

        private function mouseMoveFunction(e:MouseEvent):void 

        {

            _cameraHoverController.panAngle = (mouseX - _mouseX) * .3 + _startX;

            _cameraHoverController.tiltAngle = (mouseY - _mouseY) * .3 + _startY;

        }

        

        private function onEnterFrame(e:Event):void 

        {

            _view.render();

        }

    }



}

 

你可能感兴趣的:(基础)