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

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

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

    [SWF(backgroundColor=0x000000)]

    public class ImgAlpha extends Sprite
    {
        // _______________________________________________________________________
        //                                                                  vars3D
        private var container : Sprite;
        private var scene     : Scene3D;
        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  : BitmapMaterial;
        private var bmpData   : BitmapData;

        private var planeObj2 : DisplayObject3D;
        private var material2  : ColorMaterial;

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

        [Embed(source='csc_logo.gif')]
        private var Image0:Class;

        // _______________________________________________________________________
        //                                                             Constructor
        public function ImgAlpha():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*2.0;
            camera.focus = 500;
            camera.zoom = 1;

            //rootNode生成
            rootNode = scene.addChild( new DisplayObject3D( "rootNode" ) );

            //BitmapDataを作成して、画像の張り込み。
            var image0:Bitmap=new Image0();
            bmpData = new BitmapData(image0.width,image0.height,true,0x00000000);

            var colorTrans:ColorTransform = new ColorTransform();
            colorTrans.alphaMultiplier = 0.5;
            bmpData.draw(image0,null,colorTrans);

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

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

            //マテリアル2
            material2 = new ColorMaterial( 0x0099FF, 1);
            material2.oneSide = false;
            material2.updateBitmap();

            //Planeオブジェクト2
            var p2:Plane = new Plane( material2, planeSize, planeSize, segment, segment);
            planeObj2 = scene.addChild(p2);
            planeObj2.material.lineColor = 0x00FF00;
            planeObj2.material.lineAlpha = 1;

        }

        // _______________________________________________________________________
        //                                                                    Loop
        private function loop3D( event:Event ):void
        {
            //オブジェクトを回転
            valx += 1;
            valy += 1;
            planeObj.rotationX = valy;
            planeObj.rotationY = valx;

            planeObj2.rotationX = valy;
            planeObj2.rotationY = valx;
            planeObj2.rotationZ = 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;
        }

    }
}