Away3d SkyBox Test
Away3d、SkyBoxのテスト
マウスドラッグで回転
/**
* Copyright nappingjellyfish ( http://wonderfl.net/user/nappingjellyfish )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/cCVa
*/
package
{
import flash.display.Sprite;
import away3d.entities.Mesh;
import away3d.primitives.SkyBox;
import away3d.textures.BitmapCubeTexture;
import flash.display.BitmapData;
import flash.events.Event;
import flash.filters.BlurFilter;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;
import away3d.cameras.Camera3D;
import away3d.containers.Scene3D;
import away3d.controllers.FirstPersonController;
import away3d.containers.View3D;
/**
* ...
* @author njf
*/
public class SkyBoxTest extends Sprite
{
private var view:View3D;
private var camera:Camera3D;
private var scene:Scene3D;
private var cameraController:FirstPersonController;
private var cubeTexture:BitmapCubeTexture;
private var mesh:Mesh;
private var stageX:int;
private var stageY:int;
//movement variables
private var drag:Number = 0.5;
private var walkIncrement:Number = 2;
private var strafeIncrement:Number = 2;
private var walkSpeed:Number = 0;
private var strafeSpeed:Number = 0;
private var walkAcceleration:Number = 0;
private var strafeAcceleration:Number = 0;
//rotation variables
private var move:Boolean = false;
private var lastPanAngle:Number;
private var lastTiltAngle:Number;
private var lastMouseX:Number;
private var lastMouseY:Number;
public function SkyBoxTest()
{
addEventListener(Event.ADDED_TO_STAGE, addToStageHandler);
}
private function addToStageHandler(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, addToStageHandler);
init();
}
private function init():void
{
stageX = stage.stageWidth;
stageY = stage.stageHeight;
initEngine();
initObjects();
initListeners();
}
private function initEngine():void
{
view = new View3D();
scene = view.scene;
camera = view.camera;
camera.lens.far = 14000;
camera.lens.near = .05;
camera.y = 0;
camera.z = 600;
//setup controller to be used on the camera
cameraController = new FirstPersonController(camera, 180, 0, -80, 80);
addChild(view);
}
private function initListeners():void
{
addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}
private function initObjects():void
{
var r:Rectangle = new Rectangle();
var bmps:Vector.<BitmapData> = new Vector.<BitmapData>;
var p:Point = new Point();
var size:int = 512;
var num:int = 900;
for (var j:int = 0; j < 6; j++) {
var bmpd:BitmapData = new BitmapData(size, size, true, 0xFF000000);
for (var i:int = 0; i < num; i++)
{
r.x = size * Math.random();
r.y = size * Math.random();
if (i < 0.92*num) {
r.width = r.height = 1;
}else if (i < 0.98*num) {
r.width = r.height = 2;
}else {
r.width = r.height = 3;
}
var red:int = 0xFF * (0.7+0.3*Math.random());
var green:int = 0xFF * (0.7+0.3*Math.random());
var blue:int = 0xFF * (0.7+0.3*Math.random());
var color:uint = (red << 16) | (green << 8) | (blue);
bmpd.fillRect(r, 0xFF000000|color);
}
r.x = r.y = 0;
r.width = size;
r.height = size;
bmpd.applyFilter(bmpd, r, p, new BlurFilter(1,1,5));
bmps.push(bmpd);
}
cubeTexture = new BitmapCubeTexture(bmps[0], bmps[1], bmps[2], bmps[3], bmps[4], bmps[5]);
scene.addChild(new SkyBox(cubeTexture));
}
private function onEnterFrameHandler(e:Event):void
{
if (move) {
cameraController.panAngle = 0.3*(stage.mouseX - lastMouseX) + lastPanAngle;
cameraController.tiltAngle = 0.3*(stage.mouseY - lastMouseY) + lastTiltAngle;
}
view.render();
}
/**
* 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);
}
}
}