<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="800" minHeight="600" width="800" height="600" xmlns:local="*"
               creationComplete="init();" frameRate="30" backgroundColor="0x555555" viewSourceURL="srcview/index.html">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            import away3d.containers.ObjectContainer3D;
            import away3d.core.base.Face;
            import away3d.core.math.Number3D;
            import away3d.core.utils.Cast;
            import away3d.extrusions.TextExtrusion;
            import away3d.lights.PointLight3D;
            import away3d.loaders.Loader3D;
            import away3d.loaders.Max3DS;
            import away3d.materials.BitmapMaterial;
            import away3d.materials.ColorMaterial;
            import away3d.materials.MovieMaterial;
            import away3d.materials.ShadingColorMaterial;
            import away3d.primitives.Plane;
            import away3d.primitives.TextField3D;
            import spark.events.IndexChangeEvent;
            import wumedia.vector.VectorText;
            
            private const TEXT_WIDTH:Number = 600;
            private const TEXT_HEIGHT:Number = 100;
            
            [Embed("arial.3DS", mimeType="application/octet-stream")] private var arial3DS:Class;
            [Embed("arial_new.3DS", mimeType="application/octet-stream")] private var arialHigh3DS:Class;
            [Embed("arial.swf", mimeType="application/octet-stream")] private var FontBytes:Class;
            [Embed("arial.png")] private var arialPng:Class;
            
            private var text_bitmap:Plane, text_sprite:Plane, text_movie:Plane;
            private var text_3d:TextField3D;
            private var text_extruded:ObjectContainer3D;
            private var text_model:ObjectContainer3D, text_model_high:ObjectContainer3D;
            
            public function init():void {
                if (stage) {
                    initAway3D();
                } else {
                    this.addEventListener(Event.ADDED_TO_STAGE, function(e:Event):void { initAway3D(); });
                }
            }    
            
            private function initAway3D():void {
                stage.quality = flash.display.StageQuality.HIGH;
                away3dMain.view.camera.position = new Number3D(0,0,-1000);
                away3dMain.view.camera.lookAt(new Number3D(0,0,0));
                
                // extract font
                VectorText.extractFont(new FontBytes(), null, false);
                
                // create light for shading
                var pointLight:PointLight3D = new PointLight3D();
                pointLight.color = 0xFFFFFF;
                pointLight.ambient = 0.1;
                pointLight.diffuse = 0.5;
                pointLight.specular = 0.1;
                pointLight.brightness = 20;
                pointLight.position = new Number3D(0,300,-300);
                away3dMain.view.scene.addLight(pointLight);
                
                // bitmap
                text_bitmap = new Plane({width:TEXT_WIDTH, height:TEXT_HEIGHT});
                text_bitmap.material = new BitmapMaterial(Cast.bitmap(arialPng), {smooth:true});
                text_bitmap.bothsides = true;
                text_bitmap.rotationX = 90;
                away3dMain.view.scene.addChild(text_bitmap);
                
                // movieclip
                var mc:MovieClip = new MovieClip();
                var textfield:TextField = new TextField();
                textfield.text = "SavageLook.com";
                textfield.setTextFormat(new TextFormat("Arial", 100, 0xff0000));
                textfield.autoSize = "left";
                mc.addChild(textfield);
                
                text_movie = new Plane({width:TEXT_WIDTH, height:TEXT_HEIGHT});
                text_movie.material = new MovieMaterial(mc);
                text_movie.bothsides = true;
                text_movie.rotationX = 90;
                away3dMain.view.scene.addChild(text_movie);
                
                // sprite bitmapmaterial
                text_sprite = new Plane({width:TEXT_WIDTH, height:TEXT_HEIGHT});                
                var bmd:BitmapData = new BitmapData(textfield.width, textfield.height, true, 0x00000000);
                bmd.draw(textfield);
                text_sprite.material = new BitmapMaterial(bmd);
                text_sprite.bothsides = true;
                text_sprite.rotationX = 90;
                away3dMain.view.scene.addChild(text_sprite);
                
                // textfield3d
                text_3d = createTextField3D();
                text_3d.scale(1.5);
                away3dMain.view.scene.addChild(text_3d);
                
                // extrusion
                text_extruded = new ObjectContainer3D();
                var t1:TextField3D = createTextField3D(true);
                var t2:TextField3D = createTextField3D(true);
                t2.z = 20;
                var ext:TextExtrusion = new TextExtrusion(t1, {depth:20});
                /*
                for each (var face:Face in ext.faces) {
                    face.material = null;
                }
                ext.material = new ColorMaterial(0xffffff);
                */
                
                text_extruded = new ObjectContainer3D();
                text_extruded.addChild(t1);
                text_extruded.addChild(ext);
                text_extruded.addChild(t2);
                text_extruded.scale(1.75);
                away3dMain.view.scene.addChild(text_extruded);
                
                // load high poly model
                text_model_high = Max3DS.parse(arialHigh3DS);
                text_model_high.y = -50;
                text_model_high.rotationX = 90;
                text_model_high.rotationY = 180;
                text_model_high.scale(3.5);
                text_model_high.materialLibrary.getMaterial("default").material = new ShadingColorMaterial(0xff0000);
                away3dMain.view.scene.addChild(text_model_high);
                
                // load model
                text_model = Max3DS.parse(arial3DS);
                text_model.y = -100;
                text_model.rotationX = 90;
                text_model.rotationY = 180;
                text_model.scale(3.3);
                text_model.materialLibrary.getMaterial("default").material = new ShadingColorMaterial(0xff0000);
                away3dMain.view.scene.addChild(text_model);
                
                // start initial scene
                switcher.selectedIndex = 0;
                showText("PNG + BitmapMaterial + Plane");
                
                this.addEventListener(Event.ENTER_FRAME, render);
            }
            
            private function createTextField3D(useShadingMaterial:Boolean=false):TextField3D {
                var text:TextField3D = new TextField3D("Arial");
                text.text = "SavageLook.com";
                if (useShadingMaterial) {
                    text.material = new ShadingColorMaterial(0xff0000);
                } else {
                    text.material = new ColorMaterial(0xff0000);
                }
                text.size = 58;
                text.align = "C";
                text.bothsides = true;
                return text;
            }
            
            private function showText(type:String):void {
                for (var i:uint = 0; i < away3dMain.view.scene.children.length; i++) {
                    away3dMain.view.scene.children[i].visible = false;
                }
                
                switch (type) {
                    case "PNG + BitmapMaterial + Plane":
                        text_bitmap.visible = true;
                        break;
                    case "Textfield + MovieClip + MovieMaterial + Plane":
                        text_movie.visible = true;
                        break;
                    case "TextField + Sprite + BitmapMaterial + Plane":
                        text_sprite.visible = true;
                        break;
                    case "TextField3D":
                        text_3d.visible = true;
                        break;
                    case "TextField3D + TextExtrusion":
                        text_extruded.visible = true;
                        break;
                    case "3DS Model (low poly)":
                        text_model.visible = true;
                        break;
                    case "3DS Model (high poly)":
                        text_model_high.visible = true;
                        break;
                }
            }
            
            private function render(evt:Event):void {
                text_bitmap.rotationY += 1;
                text_movie.rotationY += 1;
                text_sprite.rotationY += 1;
                text_3d.rotationY += 1;
                text_extruded.rotationY += 1;
                text_model.rotationY += 1;
                text_model_high.rotationY += 1;
                
                away3dMain.view.render();
            }

            protected function combobox1_changeHandler(event:IndexChangeEvent):void
            {
                showText((event.target as ComboBox).selectedItem);
            }

        ]]>
    </fx:Script>
    <local:AwayUIC width="100%" height="100%" id="away3dMain"/>
    <s:ComboBox x="111" y="10" width="325" change="combobox1_changeHandler(event)" id="switcher">
        <mx:ArrayList>
            <fx:String>PNG + BitmapMaterial + Plane</fx:String>
            <fx:String>Textfield + MovieClip + MovieMaterial + Plane</fx:String>
            <fx:String>TextField + Sprite + BitmapMaterial + Plane</fx:String>
            <fx:String>TextField3D</fx:String>
            <fx:String>TextField3D + TextExtrusion</fx:String>
            <fx:String>3DS Model (low poly)</fx:String>
            <fx:String>3DS Model (high poly)</fx:String>
        </mx:ArrayList>
    </s:ComboBox>
    <s:Label x="10" y="15" text="Text Type:" height="18" width="93" color="#FFFFFF" fontSize="16" fontWeight="bold"/>
</s:Application>