<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
    backgroundColor="#000000" backgroundAlpha="1"
    pageTitle=" ...let me touch yer vertices... "
    xmlns:mx="http://www.adobe.com/2006/mxml" 
    xmlns:xx="com.fontvirus.*"
    layout="absolute"
    viewSourceURL="srcview/index.html"
    applicationComplete="init3D()">
    <mx:Script>
        <![CDATA[/*00XXXX                        XXXX00        
                   00XXxx                        xxXX00        
                   ee0000eeeeRR            RReeee0000ee        
                         xxXX00            00XXxx              
                         XXXX00            00XXXX              
                   eexxXXXXXXXXXXXXXXXXXXXXXXXXXXXXxxee        
                   00XXXXeeee00XXXXXXXXXXXX00eeeeXXXX00        
               xxXXXXXXXX    00XXXXXXXXXXXX00    XXXXXXXXxx    
               XXXXXXXXXX    00XXXXXXXXXXXX00    XXXXXXXXXX    
           eeeeXXXXXXXXXXeeeeRRXXXXXXXXXXXXRReeeeXXXXXXXXXXeeee
           XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
           XXXX""""RRXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXRR""""XXXX
           XXXX    00XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX00    XXXX
           XXXX    00XXXX""""""""""""""""""""""""XXXX00    XXXX
           XXRR    00XXXX                        XXXX00    RRXX
           eeXX    00xxxxRR000000ee    ee000000RRxxxx00    XXee
                         xxXXXXXXxx    xxXXXXXXxx                            
                         XXXXXXXXXX    XXXXXXXXXX                       
             _____            __        .__
           _/ ____\___   ____/  |____  _|__|______  __ __ ______
           \   __/  _ \ /   \   __\  \/ /  \_  __ \|  |  |  ___/
            |  |(  <_> )  |  \  |  \   /|  ||  | \/|  |  |___ \
            |__| \____/|__|  /__|   \_/ |__||__|  /\____/____  >
                           \/                     \/         \/
                    .  .  f a c e . 3 D . d e m o .  .               */
        
        //________________________________________________________________
        //                                                         importz
            import flash.display.*;
            import mx.controls.*;
            //filmend in papervision3D
            import org.papervision3d.view.*;
            import org.papervision3d.render.*;
            import org.papervision3d.events.*;
            import org.papervision3d.scenes.*;
            import org.papervision3d.cameras.*;
            import org.papervision3d.materials.*;
            import org.papervision3d.core.utils.*;
            import org.papervision3d.materials.special.*;
            import org.papervision3d.objects.primitives.*;
            import org.papervision3d.core.geom.renderables.*;
            //custom shiznit
            import com.fontvirus.*;
        //________________________________________________________________
        //                                                            varz
            private var fps            :FlexFPS;
            private var cam            :Camera3D;
            private var scene          :Scene3D;
            private var view           :Viewport3D;
            private var renderer       :BasicRenderEngine;
            private var skin           :CompositeMaterial;
            private var terrian        :Plane; 
            private var intro          :Boolean = true;
            private var thisface       :Triangle3D = null;
            private var lastface       :Triangle3D = null;
        //________________________________________________________________
        //                                                     constructor    
            public function init3D():void
            {
                fps = new FlexFPS(0x006600, 0x000000, 0xffffff, 0);
                addChild(fps);
        
                cam = new Camera3D();
                scene = new Scene3D();
                view = new Viewport3D(paperCanvas.width, paperCanvas.height, true, true, false, false);
                paperCanvas.addChild(view);
                renderer = new BasicRenderEngine();
                
                skin = new CompositeMaterial();
                skin.interactive = true;
                skin.doubleSided = true;
                skin.addMaterial(new WireframeMaterial(0xcccccc, 1, 2));
                skin.addMaterial(new ColorMaterial(0x006600));
                
                terrian = new Plane(skin, 500, 700, 10, 10);
                terrian.y += 50;
                terrian.name = "plane";
                scene.addChild(terrian);
                
                terrian.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK, extrude);
                terrian.addEventListener(InteractiveScene3DEvent.OBJECT_MOVE, highlight);
                terrian.addEventListener(InteractiveScene3DEvent.OBJECT_OUT, highlight);
                
                addEventListener(Event.ENTER_FRAME, loop3D);
            }
        //________________________________________________________________
        //                                                     render loop
            private function loop3D(e:Event):void
            {
                fps.update("rendered: " + view.lastRenderList.length + " polys");
        
                if(intro==true){        
                    if(cam.z<-300){
                        cam.z+=4;
                    }
                    if(terrian.rotationX<185){
                        terrian.rotationX+=1.1;
                        lblx.text = "rotation x: " + terrian.rotationX.toPrecision(3);
                    } else {
                        lblx.filters = [new GlowFilter(0xFFFFFF, 1, 5, 5, 5, 1, true, false)];
                        thex.enabled = true;
                    }
                    if(terrian.rotationY<125){
                        terrian.rotationY+=.73;
                        lbly.text = "rotation y: " + terrian.rotationY.toPrecision(3);
                    } else {
                        lbly.filters = [new GlowFilter(0xFFFFFF, 1, 5, 5, 5, 1, true, false)];
                        they.enabled = true;
                        intro = false;
                    }
                    if(terrian.rotationZ<85){
                        terrian.rotationZ+=.5;
                        lblz.text = "rotation z: " + terrian.rotationZ.toPrecision(3);
                    } else {
                        lblz.filters = [new GlowFilter(0xFFFFFF, 1, 5, 5, 5, 1, true, false)];
                        thez.enabled = true;
                    }
                }
                //render the scene    
                renderer.renderScene(scene, cam, view);
            }
        //________________________________________________________________
        //                                                         testing
            private function extrude(e:InteractiveScene3DEvent):void
            {
                if(direction.selectedIndex==0){
                    e.face3d.v0.z-=ammount.value;
                } else {
                    e.face3d.v0.z+=ammount.value;
                }
                
                var mat:CompositeMaterial = new CompositeMaterial();
                mat.interactive = true;
                mat.doubleSided = true;
                mat.addMaterial(new WireframeMaterial(0xffffff, 1, 2));
        
                if(e.face3d._materialName=="dark"){        
                    mat.addMaterial(new ColorMaterial(0x006600, 1 , true));
                    e.face3d.material = mat;
                    e.face3d._materialName = "light";
                } else {
                    mat.addMaterial(new ColorMaterial(0x003300, 1 , true));
                    e.face3d.material = mat;
                    e.face3d._materialName = "dark";
                }
            }
            private function highlight(e:InteractiveScene3DEvent):void
            {
                lastface = thisface;
                thisface = e.face3d;

                var mat:CompositeMaterial = new CompositeMaterial();
                mat.interactive = true;
                mat.doubleSided = true;
                mat.addMaterial(new WireframeMaterial(0xffffff, 1, 2));
                
                if(thisface!=null){
                    mat.addMaterial(new ColorMaterial(0x00FF00, 1, true));
                    thisface.material = mat;
                    if(e.face3d._materialName=="dark"){
                        thisface._materialName = "dark";
                    } else {
                        thisface._materialName = "light";
                    }
                    lbldebug.text = "face3D: " + thisface.id;
                } else {
                    lbldebug.text = "face3D: null";
                }
                
                if(lastface!=null){
                    if(lastface!=thisface){
                        if(lastface._materialName=="light"){
                            mat.addMaterial(new ColorMaterial(0x006600, 1, true));
                            lastface.material = mat;
                            lastface._materialName = "light";
                        } else {
                            mat.addMaterial(new ColorMaterial(0x003300, 1, true));
                            lastface.material = mat;
                            lastface._materialName = "dark";
                        }
                    }
                }
            }
            private function moveit():void
            {
                terrian.rotationX = thex.value;
                lblx.text = "rotation x: " + thex.value.toPrecision(3);
                terrian.rotationY = they.value;
                lbly.text = "rotation y: " + they.value.toPrecision(3);
                terrian.rotationZ = thez.value;
                lblz.text = "rotation z: " + thez.value.toPrecision(3);
            }
            private function reset():void
            {
                intro = true;
                cam.z = -1000;
                scene.removeChildByName("plane");
                terrian = new Plane(skin, 500, 700, 10, 10);
                terrian.y += 50;
                terrian.name = "plane";
                scene.addChild(terrian);
                terrian.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK, extrude);
                terrian.addEventListener(InteractiveScene3DEvent.OBJECT_MOVE, highlight);
                terrian.addEventListener(InteractiveScene3DEvent.OBJECT_OUT, highlight);
                thex.value = 185;
                thex.enabled = false;
                they.value = 125;
                they.enabled = false;
                thez.value = 85;
                thez.enabled = false;
                lblx.filters = [new GlowFilter(0x666666, 1, 5, 5, 5, 1, true, false)];
                lbly.filters = [new GlowFilter(0x666666, 1, 5, 5, 5, 1, true, false)];
                lblz.filters = [new GlowFilter(0x666666, 1, 5, 5, 5, 1, true, false)];
            }
        ]]>
    </mx:Script>
    <mx:Style>
        Panel {
           borderStyle: solid;
           borderColor: #ffffff;
           borderAlpha: 1;
           borderThickness: 1;
           roundedBottomCorners: false;
           cornerRadius: 0;
           headerHeight: 23;
           headerColors: #999999, #000000;
           backgroundColor: #000000;
           titleStyleName: "header";
        }
        .header {
           borderStyle: solid;
           borderColor: #ffffff;
           borderAlpha: 1;
           borderThickness: 1;
           letterSpacing: 2;
           color: #ffffff;
           textAlign: center;
           fontWeight: bold;
        }    
        ComboBox {
           cornerRadius: 2;
           color: #ffffff;
           borderColor: #ffffff;
           fillColors: #333333, #000000, #000000, #333333;
           selectionColor: #000000;
           textSelectedColor: #ffffff;
           rollOverColor: #00ff00;
           textRollOverColor: #000000;
           themeColor: #00ff00;
           alternatingItemColors: #000000, #000000;
           textAlign: center;
        }
        Button {
           cornerRadius: 0;
           fillColors: #333333, #000000, #000000, #333333;
           color: #ffffff;
           textRollOverColor: #ffffff;
           textSelectedColor: #cccccc;
           themeColor: #00ff00;
        }        
    </mx:Style>
    <xx:Canvas3D id="paperCanvas" top="0" bottom="0" left="0" right="0" backgroundAlpha="0"/>
    <mx:Panel width="321" height="123" layout="absolute" right="0" bottom="0" title="face3D terrain gen">
        <mx:Label width="85" color="#FFFFFF" height="27" id="lbldebug" x="7" y="26"/>
        <mx:ComboBox x="100" y="47" width="93" dataProvider="['convex','concave']" id="direction"></mx:ComboBox>
        <mx:Label x="100" y="77" text="ammount" color="#FFFFFF"/>
        <mx:HSlider value="10" x="100" y="89" width="94" id="ammount" minimum="1" maximum="50"/>
        <mx:VRule x="90" y="28" strokeColor="#5F5656" height="78"/>
        <mx:VRule x="201" y="28" strokeColor="#5F5656" height="78"/>
        <mx:Label id="lblx" text="rotation x:" color="#666666" width="106" x="211" y="26"/>
        <mx:HSlider value="185" width="110" id="thex" minimum="0" maximum="360" liveDragging="true" change="moveit()" x="207" y="35" enabled="false"/>
        <mx:Label id="lbly" text="rotation y:" color="#666666" width="106" x="211" y="52"/>
        <mx:HSlider value="125" width="112" id="they" minimum="0" maximum="360" liveDragging="true" change="moveit()" x="205" y="61" enabled="false"/>
        <mx:Label id="lblz" text="rotation z:" color="#666666" width="105" x="212" y="78"/>
        <mx:HSlider value="85" width="112" id="thez" minimum="0" maximum="360" liveDragging="true" change="moveit()" x="205" y="88" enabled="false"/>
        <mx:Button x="7" y="79" label="reset" width="62" click="reset()"/>
        <mx:Label x="100" y="26" text="on click:" color="#FFFFFF"/>
    </mx:Panel>
</mx:Application>