package {
    import flash.display.*;
    import flash.events.*;
 
    import org.papervision3d.scenes.*;
    import org.papervision3d.objects.*;
    import org.papervision3d.cameras.*;
    import org.papervision3d.materials.*;
 
    public class MovieCube extends Sprite
    {
        private var container : Sprite;
        private var scene     : MovieScene3D;
        private var camera    : Camera3D;
        private var rootNode  : DisplayObject3D;
        private var cubeObj   : Cube;
        private var material  : ColorMaterial;
 
        //---------------------------------------------Constructor
        public function MovieCube():void
        {
            stage.frameRate = 60;
            stage.quality   = "MEDIUM";
            stage.scaleMode = "noScale";
            stage.align = StageAlign.TOP_LEFT;
 
            //コンテナ生成
            container = new Sprite();
            container.x = stage.stageWidth / 2;
            container.y = stage.stageHeight / 2;
            stage.addChild( container );
 
            //シーン生成
            scene = new MovieScene3D( container );
 
            //rootNode生成
            rootNode = new DisplayObject3D();
            scene.addChild( rootNode );
 
            //マテリアル設定
            material = new ColorMaterial( 0x000088, 0.2 ); //第2引数はオブジェクトの色の薄さ?
            material.oneSide = false;
            material.smooth = true;
 
            //Cubeオブジェクト生成
            cubeObj = new Cube(material, 800, 800, 400, 2, 2, 2); 
            //var cube:Cube = new Cube(material, 幅, 高さ, 奥行き, 2, 2, 2);
            rootNode.addChild(cubeObj);
 
            //camera設定
            camera = new Camera3D();
            camera.zoom = 2; //数値が増えるほどオブジェクトに近づいた視点
 
            stage.addEventListener(Event.ENTER_FRAME, _enterframe);
            stage.addEventListener(Event.RESIZE, onStageResize);
        }

        //---------------------------------------------_enterframe
        private function _enterframe(e:Event):void{
            cubeObj.rotationY=container.mouseX;
            cubeObj.rotationX=container.mouseY;
            material.updateBitmap();
            scene.renderCamera( camera );
        }

        //---------------------------------------------onStageResize
        private function onStageResize(e:Event):void{
             container.x = this.stage.stageWidth  / 2;
             container.y = this.stage.stageHeight / 2;
        }
    }
}