标签:
所谓skybox就是六个面即六张图能够无缝的拼成一个正方体的盒子。
1 package 2 { 3 import away3d.cameras.Camera3D; 4 import away3d.cameras.lenses.PerspectiveLens; 5 import away3d.containers.Scene3D; 6 import away3d.containers.View3D; 7 import away3d.controllers.HoverController; 8 import away3d.primitives.SkyBox; 9 import away3d.textures.BitmapCubeTexture; 10 import away3d.utils.Cast; 11 12 import flash.display.Sprite; 13 import flash.display.StageAlign; 14 import flash.display.StageScaleMode; 15 import flash.events.Event; 16 import flash.events.KeyboardEvent; 17 import flash.events.MouseEvent; 18 import flash.ui.Keyboard; 19 20 /** 21 * @author Frost.Yen 22 * @E-mail 871979853@qq.com 23 * @create 2015-9-28 上午10:26:21 24 * 25 */ 26 public class Away3dSkybox extends Sprite 27 { 28 //skybox 29 [Embed(source="assets/snow_positive_x.jpg")] 30 private var EnvPosX:Class; 31 [Embed(source="assets/snow_positive_y.jpg")] 32 private var EnvPosY:Class; 33 [Embed(source="assets/snow_positive_z.jpg")] 34 private var EnvPosZ:Class; 35 [Embed(source="assets/snow_negative_x.jpg")] 36 private var EnvNegX:Class; 37 [Embed(source="assets/snow_negative_y.jpg")] 38 private var EnvNegY:Class; 39 [Embed(source="assets/snow_negative_z.jpg")] 40 private var EnvNegZ:Class; 41 42 private var view:View3D; 43 private var scene:Scene3D; 44 private var camera:Camera3D; 45 private var cameraController:HoverController; 46 private var cubeTexture:BitmapCubeTexture; 47 private var move:Boolean = false; 48 private var lastPanAngle:Number; 49 private var lastTiltAngle:Number; 50 private var lastMouseX:Number; 51 private var lastMouseY:Number; 52 private var tiltSpeed:Number = 2; 53 private var panSpeed:Number = 2; 54 private var distanceSpeed:Number = 1000; 55 private var tiltIncrement:Number = 0; 56 private var panIncrement:Number = 0; 57 private var distanceIncrement:Number = 0; 58 public function Away3dSkybox() 59 { 60 initEngine(); 61 initMaterials(); 62 initObjects(); 63 initListeners(); 64 } 65 /** 66 * Initialise the engine 67 */ 68 private function initEngine():void 69 { 70 stage.scaleMode = StageScaleMode.NO_SCALE; 71 stage.align = StageAlign.TOP_LEFT; 72 73 view = new View3D(); 74 scene = view.scene; 75 camera = view.camera; 76 //camera.lens.far = 1000000; 77 view.camera.lens = new PerspectiveLens(90); 78 //setup controller to be used on the camera 79 cameraController = new HoverController(camera, null, 0, 10, 25000, 0, 70); 80 81 view.addSourceURL("srcview/index.html"); 82 addChild(view); 83 } 84 private function initMaterials():void 85 { 86 //create skybox texture 87 cubeTexture = new BitmapCubeTexture(Cast.bitmapData(EnvPosX), Cast.bitmapData(EnvNegX), Cast.bitmapData(EnvPosY), Cast.bitmapData(EnvNegY), Cast.bitmapData(EnvPosZ), Cast.bitmapData(EnvNegZ)); 88 } 89 /** 90 * Initialise the scene objects 91 */ 92 private function initObjects():void 93 { 94 //create skybox. 95 scene.addChild(new SkyBox(cubeTexture)); 96 } 97 /** 98 * Initialise the listeners 99 */ 100 private function initListeners():void 101 { 102 103 addEventListener(Event.ENTER_FRAME, onEnterFrame); 104 view.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); 105 view.addEventListener(MouseEvent.MOUSE_UP, onMouseUp); 106 stage.addEventListener(Event.RESIZE, onResize); 107 stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown); 108 stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp); 109 onResize(); 110 } 111 /** 112 * Navigation and render loop 113 */ 114 private function onEnterFrame(event:Event):void 115 { 116 if (move) { 117 cameraController.panAngle = 0.3*(stage.mouseX - lastMouseX) + lastPanAngle; 118 cameraController.tiltAngle = 0.3*(stage.mouseY - lastMouseY) + lastTiltAngle; 119 } 120 121 cameraController.panAngle += panIncrement; 122 cameraController.tiltAngle += tiltIncrement; 123 cameraController.distance += distanceIncrement; 124 125 view.render(); 126 } 127 128 /** 129 * Key down listener for camera control 130 */ 131 private function onKeyDown(event:KeyboardEvent):void 132 { 133 switch (event.keyCode) { 134 case Keyboard.UP: 135 case Keyboard.W: 136 tiltIncrement = tiltSpeed; 137 break; 138 case Keyboard.DOWN: 139 case Keyboard.S: 140 tiltIncrement = -tiltSpeed; 141 break; 142 case Keyboard.LEFT: 143 case Keyboard.A: 144 panIncrement = panSpeed; 145 break; 146 case Keyboard.RIGHT: 147 case Keyboard.D: 148 panIncrement = -panSpeed; 149 break; 150 case Keyboard.Z: 151 distanceIncrement = distanceSpeed; 152 break; 153 case Keyboard.X: 154 distanceIncrement = -distanceSpeed; 155 break; 156 } 157 } 158 159 /** 160 * Key up listener for camera control 161 */ 162 private function onKeyUp(event:KeyboardEvent):void 163 { 164 switch (event.keyCode) { 165 case Keyboard.UP: 166 case Keyboard.W: 167 case Keyboard.DOWN: 168 case Keyboard.S: 169 tiltIncrement = 0; 170 break; 171 case Keyboard.LEFT: 172 case Keyboard.A: 173 case Keyboard.RIGHT: 174 case Keyboard.D: 175 panIncrement = 0; 176 break; 177 case Keyboard.Z: 178 case Keyboard.X: 179 distanceIncrement = 0; 180 break; 181 } 182 } 183 184 /** 185 * Mouse down listener for navigation 186 */ 187 private function onMouseDown(event:MouseEvent):void 188 { 189 move = true; 190 lastPanAngle = cameraController.panAngle; 191 lastTiltAngle = cameraController.tiltAngle; 192 lastMouseX = stage.mouseX; 193 lastMouseY = stage.mouseY; 194 stage.addEventListener(Event.MOUSE_LEAVE, onStageMouseLeave); 195 } 196 197 /** 198 * Mouse up listener for navigation 199 */ 200 private function onMouseUp(event:MouseEvent):void 201 { 202 move = false; 203 stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave); 204 } 205 206 /** 207 * Mouse stage leave listener for navigation 208 */ 209 private function onStageMouseLeave(event:Event):void 210 { 211 move = false; 212 stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave); 213 } 214 215 /** 216 * stage listener for resize events 217 */ 218 private function onResize(event:Event = null):void 219 { 220 view.width = stage.stageWidth; 221 view.height = stage.stageHeight; 222 } 223 } 224 }
[ActionScript 3.0] Away3D 天空盒(skybox)例子2
标签:
原文地址:http://www.cnblogs.com/frost-yen/p/4999969.html