• away3d 4.0.9Gold 学习笔记 摄像机与摄像机控制器(7)


    View3D在初始化时候就已经创建的Camera3D所以有时候我们没有创建Camera3D但我们依然可以看到3D场景。

    Camera3D的默认初始位置在x:0,y:0,z:-1000。

    Camera本身属性较多。我列举一些比较常用的。
    camea.lookAt(new Vector3D).这个是让摄像机指着某点。即便摄像机移动中。也会一直指着此点。
    camera.roll(angle)摄像机绕绿色的轴线旋转
    camera.pitch(angle) 摄像机绕蓝色的轴线旋转
    amera.yaw(angle)摄像机绕红色的轴线旋转

    摄像机按照距离移动。
    camera.moveBackward(distance)
    camera.moveForward(distance);
    camera.moveDown(distance);
    camera.moveLeft(distance);
    camera.moveRight(distance);
    camera.moveUp(distance);

    摄像机渲染距离设置
    camera.lens.far=distance;

    一把鼻涕一把泪--,终于完全搞懂摄像机的控制--,

     

     

     

     

    HoverController 摄像机围绕某点旋转。
    FirstPersonController。第一人称视角控制器。这个可以完美的模拟出以第一视角漫游。

    看一下API:

    HoverController(targetObject:Entity = null, lookAtObject:ObjectContainer3D = null, panAngle:Number = 0, tiltAngle:Number = 90, distance:Number = 1000, minTiltAngle:Number = -90, maxTiltAngle:Number = 90, minPanAngle:Number, maxPanAngle:Number, steps:uint = 8, yFactor:Number = 2, wrapPanAngle:Boolean = false)

     

    FirstPersonController(targetObject:Entity = null, panAngle:Number = 0, tiltAngle:Number = 90, minTiltAngle:Number = -90, maxTiltAngle:Number = 90, steps:uint = 8)

     

     


    2个控制器的基本属性相同。
    targetObject. 这个是我们所要给控制器的摄像机对象。
    lookAtObject:这个是HoverController 拥有的。就是我们所要环绕的对象。
    panAngle:摄像机以Y轴旋转的角度
    tiltAngle:摄像机以X轴旋转的角度
    distance;射线机的距离。
    minPanAngle:以Y轴旋转的最小角度。
    minTileAngle:以X轴旋转的最小角度。
    maxPanAngle:以Y轴旋转的最大角度。
    maxTileAngle:以X轴旋转的最大角度。

    以下2个属性是FirstPersonController 可以模拟出第一视角行走
    摄像机的方向以speed速度前进
    incrementWalk(speed);
    以摄像机为方向的90度以speed速度前进
    incrementStrafe(speed);

     

    练习

     

     

     直接可复制使用

      1 package
      2 {
      3     import away3d.cameras.Camera3D;
      4     import away3d.containers.Scene3D;
      5     import away3d.containers.View3D;
      6     import away3d.controllers.HoverController;
      7     import away3d.debug.AwayStats;
      8     import away3d.entities.Mesh;
      9     import away3d.lights.PointLight;
     10     import away3d.materials.ColorMaterial;
     11     import away3d.materials.lightpickers.StaticLightPicker;
     12     import away3d.primitives.CubeGeometry;
     13     
     14     import flash.display.Sprite;
     15     import flash.display.StageAlign;
     16     import flash.display.StageScaleMode;
     17     import flash.events.Event;
     18     import flash.events.KeyboardEvent;
     19     import flash.events.MouseEvent;
     20     import flash.ui.Keyboard;
     21     
     22     
     23     [SWF(backgroundColor="#000000", frameRate="60", width="500", height="400")]
     24     public class basic_cube_v_4 extends Sprite
     25     {
     26         //engine variables
     27         private var scene:Scene3D;
     28         private var camera:Camera3D;
     29         private var view:View3D;
     30         private var awayStats:AwayStats;
     31         private var cameraController:HoverController;
     32         
     33         //light objects
     34         private var pointLight:PointLight;
     35         private var lightPicker:StaticLightPicker;
     36         
     37         //navigation variables
     38         private var move:Boolean = false;
     39         private var lastPanAngle:Number;
     40         private var lastTiltAngle:Number;
     41         private var lastMouseX:Number;
     42         private var lastMouseY:Number;
     43         
     44         private var tiltIncrement:Number = 0;
     45         private var panIncrement:Number = 0;
     46         private var distanceIncrement:Number = 0;
     47         public const tiltSpeed:Number = 2;
     48         public const panSpeed:Number = 2;
     49         public const distanceSpeed:Number = 2;
     50         
     51         /**
     52          * Constructor
     53          */
     54         public function basic_cube_v_4()
     55         {
     56             initEngine();
     57             initLights();
     58             initObject();
     59             initListeners();
     60         }
     61         
     62         /**
     63          * Initialise the engine
     64          */
     65         private function initEngine():void
     66         {
     67             stage.scaleMode = StageScaleMode.NO_SCALE;
     68             stage.align = StageAlign.TOP_LEFT;
     69             
     70             view = new View3D();
     71             view.antiAlias = 4;
     72             view.forceMouseMove = true;
     73             scene = view.scene;
     74             camera = view.camera;
     75             addChild(view);
     76             
     77             //setup controller to be used on the camera
     78             cameraController = new HoverController(camera, null, 45, 30, 520, -90,90);
     79             
     80             // show statistics
     81             awayStats = new AwayStats(view);
     82             addChild(awayStats);
     83         }
     84         
     85         /**
     86          * Initialise the lights
     87          */
     88         private function initLights():void
     89         {
     90             //create a light for the camera
     91             pointLight = new PointLight();
     92             scene.addChild(pointLight);
     93             // In version 4, you'll need a lightpicker. Materials must then be registered with it (see initObject)
     94             lightPicker = new StaticLightPicker([pointLight]);
     95         }
     96         
     97         /**
     98          * Initialise the scene objects
     99          */
    100         private function initObject():void
    101         {
    102             // cubes
    103             var cubeGeometry:CubeGeometry = new CubeGeometry(400,400,400, 10, 10, 10);
    104             var cubeMaterial:ColorMaterial = new ColorMaterial( 0xFF0000 );
    105             cubeMaterial.lightPicker = lightPicker;
    106             var mesh:Mesh = new Mesh(cubeGeometry, cubeMaterial);
    107             scene.addChild(mesh);
    108         }
    109         
    110         /**
    111          * Initialise the listeners
    112          */
    113         private function initListeners():void
    114         {
    115             addEventListener(Event.ENTER_FRAME, onEnterFrame);
    116             view.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
    117             view.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
    118             stage.addEventListener(Event.RESIZE, onResize);
    119             stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
    120             stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
    121             onResize();
    122         }
    123         
    124         /**
    125          * Navigation and render loop
    126          */
    127         private function onEnterFrame(event:Event):void
    128         {
    129             if (move) {
    130                 cameraController.panAngle = 0.3*(stage.mouseX - lastMouseX) + lastPanAngle;
    131                 cameraController.tiltAngle = 0.3*(stage.mouseY - lastMouseY) + lastTiltAngle;
    132             }
    133             
    134             cameraController.panAngle += panIncrement;
    135             cameraController.tiltAngle += tiltIncrement;
    136             cameraController.distance += distanceIncrement;
    137             
    138             pointLight.position = camera.position;
    139             
    140             view.render();
    141         }
    142         
    143         /**
    144          * Key down listener for camera control
    145          */
    146         private function onKeyDown(event:KeyboardEvent):void
    147         {
    148             switch (event.keyCode) {
    149                 case Keyboard.UP:
    150                 case Keyboard.W:
    151                     tiltIncrement = tiltSpeed;
    152                     break;
    153                 case Keyboard.DOWN:
    154                 case Keyboard.S:
    155                     tiltIncrement = -tiltSpeed;
    156                     break;
    157                 case Keyboard.LEFT:
    158                 case Keyboard.A:
    159                     panIncrement = panSpeed;
    160                     break;
    161                 case Keyboard.RIGHT:
    162                 case Keyboard.D:
    163                     panIncrement = -panSpeed;
    164                     break;
    165                 case Keyboard.Z:
    166                     distanceIncrement = distanceSpeed;
    167                     break;
    168                 case Keyboard.X:
    169                     distanceIncrement = -distanceSpeed;
    170                     break;
    171             }
    172         }
    173         
    174         /**
    175          * Key up listener for camera control
    176          */
    177         private function onKeyUp(event:KeyboardEvent):void
    178         {
    179             switch (event.keyCode) {
    180                 case Keyboard.UP:
    181                 case Keyboard.W:
    182                 case Keyboard.DOWN:
    183                 case Keyboard.S:
    184                     tiltIncrement = 0;
    185                     break;
    186                 case Keyboard.LEFT:
    187                 case Keyboard.A:
    188                 case Keyboard.RIGHT:
    189                 case Keyboard.D:
    190                     panIncrement = 0;
    191                     break;
    192                 case Keyboard.Z:
    193                 case Keyboard.X:
    194                     distanceIncrement = 0;
    195                     break;
    196             }
    197         }
    198         
    199         /**
    200          * Mouse down listener for navigation
    201          */
    202         private function onMouseDown(event:MouseEvent):void
    203         {
    204             move = true;
    205             lastPanAngle = cameraController.panAngle;
    206             lastTiltAngle = cameraController.tiltAngle;
    207             lastMouseX = stage.mouseX;
    208             lastMouseY = stage.mouseY;
    209             stage.addEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
    210         }
    211         
    212         /**
    213          * Mouse up listener for navigation
    214          */
    215         private function onMouseUp(event:MouseEvent):void
    216         {
    217             move = false;
    218             stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
    219         }
    220         
    221         /**dd
    222          * Mouse stage leave listener for navigation
    223          */
    224         private function onStageMouseLeave(event:Event):void
    225         {
    226             move = false;
    227             stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
    228         }
    229         
    230         /**
    231          * stage listener for resize events
    232          */
    233         private function onResize(event:Event = null):void
    234         {
    235             view.width = stage.stageWidth;
    236             view.height = stage.stageHeight;
    237             awayStats.x = stage.stageWidth - awayStats.width;
    238         }
    239     }
    240 }

     

     

     

  • 相关阅读:
    classic problem: select sortjava
    【转】排序算法复习(Java实现)(二): 归并排序,堆排序,桶式排序,基数排序
    【转】排序算法复习(Java实现)(一): 插入,冒泡,选择,Shell,快速排序
    classic problem: 100 horse and 100 dan
    good chocolate
    【转】Java内存模型 http://blog.csdn.net/silentbalanceyh
    http header/ eclipse package path
    design patterns: factory and abstractFactory
    阅读笔记
    提取Dump文件的Callstack,创建windbg的一个扩展应用
  • 原文地址:https://www.cnblogs.com/bulolo/p/2711125.html
Copyright © 2020-2023  润新知