所谓skybox就是六个面即六张图能够无缝的拼成一个正方体的盒子。

 package
{
import away3d.cameras.Camera3D;
import away3d.cameras.lenses.PerspectiveLens;
import away3d.containers.Scene3D;
import away3d.containers.View3D;
import away3d.controllers.HoverController;
import away3d.primitives.SkyBox;
import away3d.textures.BitmapCubeTexture;
import away3d.utils.Cast; import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard; /**
* @author Frost.Yen
* @E-mail [email protected]
* @create 2015-9-28 上午10:26:21
*
*/
public class Away3dSkybox extends Sprite
{
//skybox
[Embed(source="assets/snow_positive_x.jpg")]
private var EnvPosX:Class;
[Embed(source="assets/snow_positive_y.jpg")]
private var EnvPosY:Class;
[Embed(source="assets/snow_positive_z.jpg")]
private var EnvPosZ:Class;
[Embed(source="assets/snow_negative_x.jpg")]
private var EnvNegX:Class;
[Embed(source="assets/snow_negative_y.jpg")]
private var EnvNegY:Class;
[Embed(source="assets/snow_negative_z.jpg")]
private var EnvNegZ:Class; private var view:View3D;
private var scene:Scene3D;
private var camera:Camera3D;
private var cameraController:HoverController;
private var cubeTexture:BitmapCubeTexture;
private var move:Boolean = false;
private var lastPanAngle:Number;
private var lastTiltAngle:Number;
private var lastMouseX:Number;
private var lastMouseY:Number;
private var tiltSpeed:Number = 2;
private var panSpeed:Number = 2;
private var distanceSpeed:Number = 1000;
private var tiltIncrement:Number = 0;
private var panIncrement:Number = 0;
private var distanceIncrement:Number = 0;
public function Away3dSkybox()
{
initEngine();
initMaterials();
initObjects();
initListeners();
}
/**
* Initialise the engine
*/
private function initEngine():void
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT; view = new View3D();
scene = view.scene;
camera = view.camera;
//camera.lens.far = 1000000;
view.camera.lens = new PerspectiveLens(90);
//setup controller to be used on the camera
cameraController = new HoverController(camera, null, 0, 10, 25000, 0, 70); view.addSourceURL("srcview/index.html");
addChild(view);
}
private function initMaterials():void
{
//create skybox texture
cubeTexture = new BitmapCubeTexture(Cast.bitmapData(EnvPosX), Cast.bitmapData(EnvNegX), Cast.bitmapData(EnvPosY), Cast.bitmapData(EnvNegY), Cast.bitmapData(EnvPosZ), Cast.bitmapData(EnvNegZ));
}
/**
* Initialise the scene objects
*/
private function initObjects():void
{
//create skybox.
scene.addChild(new SkyBox(cubeTexture));
}
/**
* Initialise the listeners
*/
private function initListeners():void
{ addEventListener(Event.ENTER_FRAME, onEnterFrame);
view.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
view.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
stage.addEventListener(Event.RESIZE, onResize);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
onResize();
}
/**
* Navigation and render loop
*/
private function onEnterFrame(event:Event):void
{
if (move) {
cameraController.panAngle = 0.3*(stage.mouseX - lastMouseX) + lastPanAngle;
cameraController.tiltAngle = 0.3*(stage.mouseY - lastMouseY) + lastTiltAngle;
} cameraController.panAngle += panIncrement;
cameraController.tiltAngle += tiltIncrement;
cameraController.distance += distanceIncrement; view.render();
} /**
* Key down listener for camera control
*/
private function onKeyDown(event:KeyboardEvent):void
{
switch (event.keyCode) {
case Keyboard.UP:
case Keyboard.W:
tiltIncrement = tiltSpeed;
break;
case Keyboard.DOWN:
case Keyboard.S:
tiltIncrement = -tiltSpeed;
break;
case Keyboard.LEFT:
case Keyboard.A:
panIncrement = panSpeed;
break;
case Keyboard.RIGHT:
case Keyboard.D:
panIncrement = -panSpeed;
break;
case Keyboard.Z:
distanceIncrement = distanceSpeed;
break;
case Keyboard.X:
distanceIncrement = -distanceSpeed;
break;
}
} /**
* Key up listener for camera control
*/
private function onKeyUp(event:KeyboardEvent):void
{
switch (event.keyCode) {
case Keyboard.UP:
case Keyboard.W:
case Keyboard.DOWN:
case Keyboard.S:
tiltIncrement = 0;
break;
case Keyboard.LEFT:
case Keyboard.A:
case Keyboard.RIGHT:
case Keyboard.D:
panIncrement = 0;
break;
case Keyboard.Z:
case Keyboard.X:
distanceIncrement = 0;
break;
}
} /**
* Mouse down listener for navigation
*/
private function onMouseDown(event:MouseEvent):void
{
move = true;
lastPanAngle = cameraController.panAngle;
lastTiltAngle = cameraController.tiltAngle;
lastMouseX = stage.mouseX;
lastMouseY = stage.mouseY;
stage.addEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
} /**
* Mouse up listener for navigation
*/
private function onMouseUp(event:MouseEvent):void
{
move = false;
stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
} /**
* Mouse stage leave listener for navigation
*/
private function onStageMouseLeave(event:Event):void
{
move = false;
stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
} /**
* stage listener for resize events
*/
private function onResize(event:Event = null):void
{
view.width = stage.stageWidth;
view.height = stage.stageHeight;
}
}
}
05-03 22:22