// mxmlc -output ./bin/GlowEffect.swf -benchmark=true -default-size 240 240 -default-frame-rate=60 -default-background-color=0xFFFFFF GlowEffect.as

package
{
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.geom.*;
    import flash.utils.*;

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

    [SWF(backgroundColor=0x000000)]

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

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

        private var valx      : Number = 0;
        private var valy      : Number = 0;

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

        // _______________________________________________________________________
        //                                                             Constructor
        public function GlowEffect():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;

            //シーン生成、MovieSchene3Dでないと、DisplayObject3Dのcontainerが作製されないので、これ。
            scene = new MovieScene3D( this.container );

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

            //BitmapDataの作成。Bitmapに角丸矩形を描く
            bmpData = new BitmapData(TEXTURE_W,TEXTURE_H,true,0x00000000);
            var rrect:Shape = new Shape();
            rrect.graphics.lineStyle(0,0x00000000);
            rrect.graphics.beginFill(0x47FF8A);
            rrect.graphics.drawRoundRect(0,0,TEXTURE_W,TEXTURE_H,50);
            rrect.graphics.endFill();
            bmpData.draw(rrect);

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

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

            // sceneに対してaddChildする。sceneに対してaddChildしたものにaddChild(孫関係)だとcontainerが作製されない。
            scene.addChild( planeObj );

            var filter:Array = new Array();
            filter.push(new GlowFilter(0xFFFFFF, 0.5, 20,20, 2, BitmapFilterQuality.HIGH, false, false));
            filter.push(new GlowFilter(0xFFFFFF, 1, 40, 40, 2, BitmapFilterQuality.HIGH, true, false));

            // addChild後でないと、containerが生成されないので、addChildした後で 
            planeObj.container.filters = filter;
        }

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

            //レンダリング
            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;
        }

    }
}