package
{
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;

    import org.papervision3d.scenes.*;
    import org.papervision3d.objects.*;
    import org.papervision3d.cameras.*;
    import org.papervision3d.materials.*;

    public class Pv3DBmpmat extends Sprite
    {
        // _______________________________________________________________________
        //                                                                  vars3D
        private var container : Sprite;
        private var scene     : Scene3D;
        private var camera    : Camera3D;

        private var planeObj  : DisplayObject3D;
        private var planeSize : int = 300;      //Planeオブジェクト1辺の長さ
        private var segment   : int = 1;        //面の分割数

        private var count : int = 0;
        private var textField : TextField;
        private var rrect:Shape;
        private var format:TextFormat;

        static public const
            TEXTURE_W:int = 240,    ///< テクスチャの幅
            TEXTURE_H:int = 240;    ///< テクスチャの高さ

        // _______________________________________________________________________
        //                                                             Constructor
        public function Pv3DBmpmat():void
        {
            stage.frameRate = 60;
            stage.quality   = "MEDIUM";
            stage.scaleMode = "noScale";
            stage.align = StageAlign.TOP_LEFT;
            this.addEventListener(Event.ENTER_FRAME, loop3D);
            this.stage.addEventListener(Event.RESIZE, onStageResize);

            init3D();
        }

        // _______________________________________________________________________
        //                                                                  Init3D
        private function init3D():void
        {
            //コンテナ生成
            this.container = new Sprite();
            addChild(this.container);
            this.container.x = this.stage.stageWidth  / 2;
            this.container.y = this.stage.stageHeight / 2;

            //シーン生成
            scene = new Scene3D( container );

            //カメラ設定
            camera = new Camera3D();
            camera.z = -planeSize;
            camera.focus = 500;
            camera.zoom = 1;

            //テキストオブジェクトの作成            
            textField=new TextField();
            textField.text = ""+count;
            textField.width = TEXTURE_W;
            textField.height = TEXTURE_H;
            textField.wordWrap = true;

            format = new TextFormat();
            format.font ="_等幅";
            format.size =80;
            format.color=0x000000;
            format.bold =true;
            textField.setTextFormat(format);
            textField.visible = false;          ///< 画面に表示されないように
            addChild(textField);

            //BitmapDataの作成。Bitmapに角丸矩形を描く
            var bmpData:BitmapData = new BitmapData(TEXTURE_W,TEXTURE_H,true,0x00000000);
            drawTextureBG(bmpData);

            //マテリアル設定
            var material:BitmapMaterial = new BitmapMaterial(bmpData);
            material.oneSide = false;

            //Planeオブジェクト生成
            planeObj = scene.addChild( new Plane( material, planeSize, planeSize, segment, segment) );
        }

        private function drawTextureBG(bmp:BitmapData):void
        {
            rrect = new Shape();
            rrect.graphics.lineStyle(0,0x47FF8A);
            rrect.graphics.beginFill(0x47FF8A);
            rrect.graphics.drawRoundRect(0,0,TEXTURE_W,TEXTURE_H,50);
            rrect.graphics.endFill();
            bmp.draw(rrect);
        }

        // _______________________________________________________________________
        //                                                                    Loop
        private function loop3D( event:Event ):void
        {
            planeObj.rotationY += 1;
            planeObj.rotationX += 1;

            count++;

            textField.text = ""+count;
            textField.setTextFormat(format);

            drawTextureBG(planeObj.material.bitmap);

            var pos:Matrix = new Matrix();
            pos.translate( (TEXTURE_W - textField.textWidth)/2, (TEXTURE_H - textField.textHeight)/2);
            planeObj.material.bitmap.draw(textField, pos);
            planeObj.material.updateBitmap();           ///< 一応なくても動くようだが

            //レンダリング
            this.scene.renderCamera( camera );
        }

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

    }
}