package {
    import flash.display.*;
    import flash.events.*;
 
    import org.papervision3d.scenes.*;
    import org.papervision3d.objects.*;
    import org.papervision3d.cameras.*;
    import org.papervision3d.materials.*;
 
    [SWF(backgroundColor=0x000000)]
 
    public class MoviePlane extends Sprite
    {
        private var container : Sprite;
        private var scene     : MovieScene3D;
        private var camera    : Camera3D;
        private var rootNode  : DisplayObject3D;
 
        private var planeObj  : DisplayObject3D;
        private var planeSize : int = 300;//Planeオブジェクト1辺の長さ
        private var segment   : int = 1;  //面の分割数
        private var material  : ColorMaterial;
 
        //---------------------------------------------Constructor
        public function MoviePlane():void
        {
            stage.frameRate = 60;
            stage.quality   = "MEDIUM";
            stage.scaleMode = "noScale";
            stage.align = StageAlign.TOP_LEFT;
 
            //コンテナ生成
            container = new Sprite();
            container.x = 200;
            container.y = 200;
            stage.addChild( container );
 
            //シーン生成(Scene3D→MovieScene3Dでマウスで動かせるようになる?)
            scene = new MovieScene3D( container );
 
            //rootNode生成
            rootNode = new DisplayObject3D();
            scene.addChild( rootNode );
 
            //マテリアル設定
            material = new ColorMaterial( 0x0000ff, 1 );
            material.oneSide = false;
 
            //Planeオブジェクト生成
            planeObj = new Plane( material, planeSize, planeSize, segment, segment );
            rootNode.addChild( planeObj );
 
            //camera設定
            camera = new Camera3D();
            camera.z = -planeSize;
            camera.focus = 50;
            camera.zoom = 1;
 
            stage.addEventListener(Event.ENTER_FRAME, _enterframe);
            stage.addEventListener(Event.RESIZE, onStageResize);
        }

        //---------------------------------------------_enterframe
        private function _enterframe(e:Event):void{
            planeObj.rotationY=container.mouseX;
            planeObj.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;
        }
    }
}