package { import alternativa.engine3d.loaders.Loader3DS; import alternativa.types.Point3D; import alternativa.utils.FPS; import flash.display.*; import flash.events.*; import flash.system.*; import jp.clockmaker.alternativa3d.BasicTemplate; [SWF(width = 720, height = 480, frameRate = 60)] /** * @author Yasu (clockmaker.jp) */ public class Miku extends BasicTemplate { [Embed(source = 'imgs/alternativa3d_big.png')] private const LOGO_IMAGE:Class; private var loader:Loader3DS; /** * 初期化されたときに実行されるイベントです。 * 初期化時に実行したい処理をオーバーライドして記述します。 */ override protected function atInit():void { graphics.beginFill(0x0); graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight); var context:LoaderContext = new LoaderContext(true); loader = new Loader3DS(); loader.addEventListener(Event.COMPLETE, loadCompleteHandler); loader.load("http://clockmaker.jp/labs/090807_alternativa3d/test.3ds", context); // FPS display launch FPS.init(stage); // mouse interactive stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove); stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp); addChild(new LOGO_IMAGE); } private function loadCompleteHandler(e:Event = null):void { scene.root.addChild(loader.content); loader.content.scaleX = loader.content.scaleY = loader.content.scaleZ = 20; } /** * Event.ENTER_FRAME 時に実行されるレンダリングのイベントです。 * レンダリング前に実行したい処理をオーバーライドして記述します。 */ override protected function atPreRender():void { // Mouse Interactive camera.x = 500 * Math.sin(cameraYaw * Math.PI / 180); camera.y = 500 * Math.cos(cameraYaw * Math.PI / 180); camera.z = cameraPitch; cameraContoller.lookAt(new Point3D(0, 0, 0)); } // ---------------------------------------------- // Mouse Interactive // ---------------------------------------------- private var isOribiting:Boolean; private var cameraPitch:Number = 200; private var cameraYaw:Number = 180; private var previousMouseX:Number; private var previousMouseY:Number; private function onMouseDown(event:MouseEvent):void { isOribiting = true; previousMouseX = event.stageX; previousMouseY = event.stageY; stage.quality = StageQuality.LOW; } private function onMouseUp(event:MouseEvent):void { isOribiting = false; stage.quality = StageQuality.HIGH; } private function onMouseMove(event:MouseEvent):void { var differenceX:Number = event.stageX - previousMouseX; var differenceY:Number = event.stageY - previousMouseY; if(isOribiting) { cameraPitch += differenceY; cameraYaw += differenceX * 0.25; previousMouseX = event.stageX; previousMouseY = event.stageY; } } } }