In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Hero

AWD format for Away3d

   Demonstrates:

   How to use new awd2 format for away3d
   How to create character interaction and animation
   How to set custom material on a model.

   Code, model and map by LoTh
   blog : http://3dflashlo.wordpress.com/
   3dsmax file & model https://github.com/lo-th/loth_labs

   Code reference https://github.com/away3d/away3d-examples-fp11/
   Intermediate_CharacterAnimation.as by Rob Bateman
Get Adobe Flash player
by Loth2012 19 Oct 2012

    Talk

    Loth2012 at 04 Oct 2012 03:13
    sorry for those who do not see anything, my current provider is molusque
    yonatan at 04 Oct 2012 18:29
    I'm not sure if that's the problem, but both of your crossdomain.xml files are missing the </cross-domain-policy> closing tag...
    makc3d at 04 Oct 2012 20:28
    also you could load the model itself to related files, appending it to 1px gif file for example
    makc3d at 04 Oct 2012 20:30
    or, you could try to make gh-pages site on github and upload crossdomain.xml + model file there
    Loth2012 at 08 Oct 2012 21:17
    Map updated
    hyzhaka at 18 Oct 2012 15:19
    Model disappears on a punch
    Loth2012 at 19 Oct 2012 04:00
    yes, little error on code updated
    Loth2012 at 19 Oct 2012 04:05
    i have to update my model with new bone at 0,0,0 and link to my pelvis bone that correct the position Y problem

    Tags

    Embed
/**
 * Copyright Loth2012 ( http://wonderfl.net/user/Loth2012 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/iyVB
 */

/*
   AWD format for Away3d

   Demonstrates:

   How to use new awd2 format for away3d
   How to create character interaction and animation
   How to set custom material on a model.

   Code, model and map by LoTh
   blog : http://3dflashlo.wordpress.com/
   3dsmax file & model https://github.com/lo-th/loth_labs

   Code reference https://github.com/away3d/away3d-examples-fp11/
   Intermediate_CharacterAnimation.as by Rob Bateman

 */
package {
    import away3d.animators.transitions.CrossfadeStateTransition;
    import away3d.animators.SkeletonAnimationState;
    import away3d.animators.SkeletonAnimationSet;
    import away3d.animators.SkeletonAnimator;
    import away3d.animators.data.Skeleton;
    import away3d.lights.shadowmaps.NearDirectionalShadowMapper;
    import away3d.materials.methods.FilteredShadowMapMethod;
    import away3d.materials.methods.NearShadowMapMethod;
    import away3d.utils.Cast;
    
    import away3d.core.managers.Stage3DManager;
    import away3d.core.managers.Stage3DProxy;
    import away3d.events.Stage3DEvent;
    import away3d.entities.Mesh;
    import away3d.debug.AwayStats;
    import away3d.primitives.SkyBox;
    import away3d.lights.LightBase;
    import away3d.loaders.Loader3D;
    import away3d.lights.LightProbe;
    import away3d.lights.PointLight;
    import away3d.events.LoaderEvent;
    import away3d.containers.View3D;
    import away3d.events.AssetEvent;
    import away3d.core.base.Geometry;
    import away3d.events.MouseEvent3D;
    import away3d.library.AssetLibrary;
    import away3d.materials.LightSources;
    import away3d.textures.BitmapTexture;
    import away3d.materials.ColorMaterial;
    import away3d.lights.DirectionalLight;
    import away3d.library.assets.AssetType;
    import away3d.loaders.parsers.AWD2Parser;
    import away3d.materials.methods.FogMethod;
    import away3d.materials.methods.OutlineMethod;
    import away3d.materials.methods.CelDiffuseMethod;
    import away3d.materials.methods.CelSpecularMethod;
    
    import away3d.materials.methods.EnvMapMethod;
    import away3d.materials.methods.LightMapMethod;
    import away3d.materials.methods.FresnelSpecularMethod;
    import away3d.materials.methods.TripleFilteredShadowMapMethod;
    import away3d.materials.lightpickers.StaticLightPicker;
    import away3d.cameras.lenses.PerspectiveLens;
    import away3d.materials.DefaultMaterialBase;
    import away3d.controllers.HoverController;
    import away3d.materials.TextureMaterial;
    import away3d.primitives.SphereGeometry;
    import away3d.primitives.PlaneGeometry;
    
    import flash.filters.DropShadowFilter;
    import flash.display.StageScaleMode;
    import flash.display.StageQuality;
    import flash.events.KeyboardEvent;
    import flash.display.BitmapData;
    import flash.display.StageAlign;
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.text.TextFormat;
    import flash.text.TextField;
    import flash.display.LoaderInfo;
    import flash.display.Bitmap;
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.display.Shape;
    import flash.geom.Vector3D;
    import flash.events.Event;
    import flash.geom.Matrix;
    import flash.ui.Keyboard;
    import flash.net.URLRequest;
    import flash.display.DisplayObject;
    import flash.events.ProgressEvent;
    import flash.net.URLLoader;
    import flash.net.URLLoaderDataFormat;
    import flash.system.LoaderContext;
    
    public class Hero extends Sprite {
        private static var COLOR:Object = {
            sun : 0xFFFFEF, moon : 0x333380,
            sky : 0x445665 , ground : 0x333338,
            background : 0x000000 , fog : 0xAEAEA8
            }
        private var DemoAmbiant:Array = [0.8, 0.2];
        private var DemoDiffuse:Array = [0.0, 0.0];
        private var DemoSpecular:Array = [0.5, 0.2];
        private var _capture : Bitmap;
        
        // mapping online
        private static const MESH_PATH:String = 'http://3dflashlo.free.fr/wonderfl/assets/';
        //private static const MESH_PATH:String = 'https://github.com/lo-th/loth_labs/blob/master/bin/';
        private static const PATH:String = "http://assets.wonderfl.net/images/related_images/"
        private static const FILES:Array =[
        '/b/bf/bf46/bf46db2d305a3f9c2c546bac66e8e9871f8a5967', '/1/19/1932/1932eff5ef95c4f1ba599fd21461dd84134b08d7',
        '/1/1d/1d31/1d310ffbc77007d8837e97b5961c53ea8a882b69', '/e/e7/e707/e707e6ec16e6f271cc6c1fdd3e7afb24897e6683',
        '/d/de/de30/de30f176d4af7a8cda13ab197f7e7cfbec06a02c', '/3/38/3884/3884f4a8b9e9328ad0e3ed5503c94462db5eccfe'
        ];
        // mapping Local
        private static const PATH_LOCAL:String = 'assets/';
        private static const FILES_LOCAL:Array = [ 'onkba_N.png', 'onkba_NORM.jpg', 'onkba_OCL.jpg', 'gun_N.jpg', 'gun_NORM.jpg', 'gun_OCL.jpg'];
        private static var _local:Boolean = false;
        // engine
        protected static var _lightPicker:StaticLightPicker;
        protected static var _hoverCtrl:HoverController;
        protected static var _stage3DProxy:Stage3DProxy;
        protected static var _manager:Stage3DManager;
        protected static var _stats:AwayStats;
        protected static var _skyBox:SkyBox;
        protected static var _view:View3D;
        // referency
        private var _materials:Vector.<DefaultMaterialBase>;
        private var _mapList:Vector.<BitmapData>;
        private var _light:Array;
        private var _n:int;
        // animation
        private var squelette:Skeleton;
        private var animator:SkeletonAnimator;
        private var animationSet:SkeletonAnimationSet;
        private var stateTransition:CrossfadeStateTransition = new CrossfadeStateTransition(0.5);
        // character movement
        private var currentRotationInc:Number = 0;
        private var movementDirection:Number;
        private var isRunning:Boolean;
        private var isMoving:Boolean;
        private var isJumping:Boolean;
        private var currentAnim:String;
        // animation constants
        private static const SEQUENCE:Array = ['Breathe', 'Walk', 'Run', 'Fight', 'Boxe', 'Jump'];
        private static const SEQSPEED:Array = [1, 1, 2, 1.5, 1.6, 1];
        private static const ROTATION_SPEED:Number = 10;
        private static const XFADE_TIME:Number = 0.5;
        // scene objects
        private var hero:Mesh;
        private var gun:Mesh;
        private var ground:Mesh;
        // advanced eye 
        private var _heroPieces:Mesh;
        private var _eyes:Mesh;
        private var _eyeL:Mesh;
        private var _eyeR:Mesh;
        private var _eyesTarget:Mesh;
        private var _eyeCount:int = 0;
        private var _eyeMapClose:ColorMaterial;
        private var _eyeMapOpen:TextureMaterial;
        private var _eyeLook:Mesh;
        
        // navigation
        private var _prevMouseX:Number;
        private var _prevMouseY:Number;
        private var _mouseMove:Boolean;
        private var _cameraHeight:Number = 0;
        
        private var _eyePosition:Vector3D;
        private var cloneActif:Boolean = false;
        private var _mapper:Mapper;
        private var _text:TextField;
        
        private var _fog:FogMethod;
        private var _fog2:FogMethod;
        private var _reflect:EnvMapMethod;
        private var _specularMethod:FresnelSpecularMethod;
        private var _shadowMethod:NearShadowMapMethod;
        private var photoLoad:PhotoLoader;
        
        /** Constructor */
        public function Hero() {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
        }
        
        private function init(e:Event = null):void{
            removeEventListener(Event.ADDED_TO_STAGE, init);
            stage.align = 'TL';
            stage.scaleMode = 'noScale';
            stage.quality = 'low';
            stage.frameRate = 30;
            stage.color = COLOR.background;
            // ★wonderfl capture
            Wonderfl.disable_capture();
            //_capture = addChild(new Bitmap(new BitmapData(465, 465, false, 0x000000))) as Bitmap ;
            _manager = Stage3DManager.getInstance(stage);
            _stage3DProxy = _manager.getFreeStage3DProxy();
            _stage3DProxy.addEventListener(Stage3DEvent.CONTEXT3D_CREATED, onContextCreated);
        }
        
        private function onContextCreated(e:Stage3DEvent):void{
            _stage3DProxy.color = COLOR.background;
            _stage3DProxy.antiAlias = 8;
            _stage3DProxy.width = stage.stageWidth;
            _stage3DProxy.height = stage.stageHeight;
            initEngine();
            initText();
            initLights();
            initListeners();
            
            // start by loading all bitmap
            _mapList = new Vector.<BitmapData>();
            loadBitmap();
            }
            
        //-------------------------------- AWAY3D
        
        /** Initialise the engine */
        private function initEngine():void {
            _view = new View3D();
            _view.stage3DProxy = _stage3DProxy;
            _view.shareContext = true;
            _view.backgroundColor = COLOR.background;
            
            _view.camera.lens = new PerspectiveLens(65);
            _view.camera.lens.far = 30000;
            _view.camera.lens.near = 1;
            
            _hoverCtrl = new HoverController(_view.camera, null, 180, 0, 1000, 6, 90);
            _hoverCtrl.tiltAngle = 5;
            _hoverCtrl.panAngle = 160;
            _hoverCtrl.minTiltAngle = -60;
            _hoverCtrl.maxTiltAngle = 60;
            _hoverCtrl.autoUpdate = false;
            
            addChild(_view);
            
            //away3d stat
            _stats = new AwayStats(_view, false, true);
            _stats.alpha = 0.5;
            addChild(_stats);
            
            //material reference
            _materials = new Vector.<DefaultMaterialBase>();
            //light referency
            _light = [];
            // hero content
            _player = new Mesh(new Geometry());
            _view.scene.addChild(_player);
            
            //auto map generator
            _mapper = new Mapper();
        }
        
        //-------------------------------- LOOP
        
        /** Render loop */
        private function onEnterFrame(event:Event):void {
            //update character animation
            if (hero) {
                _player.rotationY += currentRotationInc;
                updateEye();
                _hoverCtrl.lookAtPosition = new Vector3D(hero.x, _cameraHeight, hero.z);
            }
            //update camera controler
            _hoverCtrl.update();
            //update light
            //_light[1].position = _view.camera.position;
            //update view
            _view.render();
            // ★ wonderfl capture
            if (_capture) _stage3DProxy.context3D.drawToBitmapData(_capture.bitmapData);
        }
        
        //-------------------------------- LISTENER
        
        /** Initialise the listeners */
        private function initListeners():void {
            //add render loop
            _stage3DProxy.addEventListener(Event.ENTER_FRAME, onEnterFrame, false, 0, true);
            //add key listeners
            stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp, false, 0, true);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown, false, 0, true);
            //navigation
            stage.addEventListener(MouseEvent.MOUSE_DOWN, onStageMouseDown, false, 0, true);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove, false, 0, true);
            stage.addEventListener(MouseEvent.MOUSE_UP, onStageMouseLeave, false, 0, true);
            stage.addEventListener(MouseEvent.MOUSE_WHEEL, onStageMouseWheel, false, 0, true)
            stage.addEventListener(Event.MOUSE_LEAVE, onStageMouseLeave, false, 0, true);
            //add resize event
            stage.addEventListener(Event.RESIZE, onResize, false, 0, true);
            onResize();
        }
        
        //-------------------------------- LIGHT
        
        /** Initialise the lights */
        private function initLights():void {
            //create a light for shadows that mimics the sun's position in the skybox
            var sun:DirectionalLight = new DirectionalLight(-0.5, -1, 0.3);
            sun.castsShadows = true;
            sun.shadowMapper = new NearDirectionalShadowMapper(.1);
            _view.scene.addChild(sun);
            _light.push(sun);
            //create a light for ambient effect that mimics the sky
            var moon:PointLight = new PointLight();
            moon.y = 500;
            moon.radius = 1000;
            moon.fallOff = 2500;
            _view.scene.addChild(moon);
            _light.push(moon);
            makeSky();
        }
        
        //-------------------------------- SKY
        
        /** sky change */
        private function randomSky():void {
            COLOR.ground = 0xFFFFFF * Math.random();
            COLOR.fog = 0xFFFFFF * Math.random();
            COLOR.sky = 0xFFFFFF * Math.random();
            COLOR.sun = 0xFFFFFF * Math.random();
            COLOR.moon = 0xFFFFFF * Math.random();
            DemoAmbiant = [0.4, 0.3];
            DemoAmbiant = [Math.random(), Math.random()];
            DemoDiffuse = [Math.random(), Math.random()];
            DemoSpecular = [Math.random() * 4, Math.random() * 2];
            makeSky();
        }
        
        private function makeSky():void {
            if (_light[2]) {
                _view.scene.removeChild(_light[2]);
                _light.pop();
            }
            if (_skyBox) {
                _view.scene.removeChild(_skyBox);
                _skyBox.dispose();
            }
            //generate vector degrade sky
            _mapper.vectorSky([COLOR.ground, COLOR.fog, COLOR.sky], 8);
            
            _skyBox = new SkyBox(_mapper.sky);
            _view.scene.addChild(_skyBox);
            //add new probe light
            var probe:LightProbe = new LightProbe(_mapper.sky);
            _view.scene.addChild(probe);
            _light.push(probe);
            //sun
            _light[0].color = COLOR.sun;
            _light[0].ambientColor = COLOR.fog;
            _light[0].ambient = DemoAmbiant[0];
            _light[0].diffuse = DemoDiffuse[0];
            _light[0].specular = DemoSpecular[0];
            // moon
            _light[1].color = COLOR.moon;
            _light[1].ambientColor = COLOR.ground;
            _light[1].ambient = DemoAmbiant[1];
            _light[1].diffuse = DemoDiffuse[1];
            _light[1].specular = DemoSpecular[1];
            _lightPicker = new StaticLightPicker(_light);
            
            for each (var o:DefaultMaterialBase in _materials) {
                if (o.name == 'ground') {
                    o.removeMethod(_fog);
                    o.removeMethod(_reflect);
                    _fog = new FogMethod(1000, 10000, COLOR.fog);
                    //_reflect = new EnvMapMethod(_mapper.sky, 0.5)
                    o.addMethod(_fog);
                        //o.addMethod(_reflect);
                }
                o.lightPicker = _lightPicker;
            }
        }
        
        //-------------------------------- MATERIAL
        
        /** Initialise the material */
        protected function initMaterial():void {
            // global methode
            _fog = new FogMethod(0, _view.camera.lens.far, COLOR.fog);
            _reflect = new EnvMapMethod(_mapper.sky, 0.5)
            _specularMethod = new FresnelSpecularMethod();
            _specularMethod.normalReflectance = 1.5;
            // global shadow
            _shadowMethod = new NearShadowMapMethod(new FilteredShadowMapMethod(_light[0]));
            _shadowMethod.epsilon = .0007;
            
            var material:DefaultMaterialBase;
            
            // 0 - ground
            var grid:Shape = new Shape();
            grid.graphics.beginFill(0xAAAAAA, 0.3);
            grid.graphics.drawRect(0, 0, 4, 128);
            grid.graphics.drawRect(0, 0, 128, 4);
            var b:BitmapData = new BitmapData(128, 128, false, 0x606060);
            b.draw(grid);
            material = materialFromBitmap('ground', [b, b, b], true, false);
            material.ambient = 1;
            material.gloss = 30;
            material.specular = 1;
            material.repeat = true;
            material.addMethod(_fog);
            // 1 - Hero
            material = materialFromBitmap('hero', [_mapList[0], _mapList[1], _mapList[2]])
            material.gloss = 16;
            material.specular = 0.6;
            material.ambient = 1;
            //transparency
            material.alphaPremultiplied = true;
            material.alphaThreshold = 0.9;
            // 2 - gun
            material = materialFromBitmap('gun', [_mapList[3], _mapList[4], _mapList[5]])
            material.gloss = 16;
            material.specular = 0.6;
            material.ambient = 1;
            
            initObjects();
        }
        
        /** Make Material from bitmap */
        protected function materialFromBitmap(name:String, Bitmaps:Array, Spec:Boolean=false, cartoon:Boolean = true):DefaultMaterialBase {
            // auto map if one bitmap
            if (Bitmaps.length == 1) {
                Bitmaps.push(_mapper.bitdata[1]);
                Bitmaps.push(_mapper.bitdata[0]);
            }
            var material:TextureMaterial = new TextureMaterial(new BitmapTexture(Bitmaps[0]));
            material.normalMap = new BitmapTexture(Bitmaps[1]);
            //material.normalMap = Cast.bitmapTexture(Bitmaps[1]);
            if (Spec) material.specularMap = new BitmapTexture(Bitmaps[2]);
            else material.addMethod(new LightMapMethod(new BitmapTexture(Bitmaps[2])));
            material.addMethod(new LightMapMethod(new BitmapTexture(Bitmaps[2])));
            material.name = name;
            material.lightPicker = _lightPicker;
            material.diffuseLightSources = LightSources.PROBES;
            material.specularLightSources = LightSources.LIGHTS;
           // material.specularMethod = _specularMethod;
            material.shadowMethod = _shadowMethod;
            if (cartoon) {
                    material.addMethod(new OutlineMethod(0x000000, 0.2, true));
                    material.diffuseMethod = new CelDiffuseMethod(3);
                   // material.specularMethod = new CelSpecularMethod(.2);
                   // CelSpecularMethod(material.specularMethod).smoothness = .3;
                    //CelDiffuseMethod(material.diffuseMethod).smoothness = .3;
            }
            //push to reference
            _materials.push(material);
            return material;
        }
        
        //-------------------------------- OBJECI
        
        /** Initialise the scene object */
        private function initObjects():void {
            ground = new Mesh(new PlaneGeometry(100000, 100000), _materials[0]);
            ground.geometry.scaleUV(2000, 2000);
            ground.y = -480;
            ground.castsShadows = false;
            _view.scene.addChild(ground);
            // load model awd
            if(_local) load(PATH_LOCAL+"onkba.awd")
            else load(MESH_PATH+"onkba.awd");
        }
        
        //-------------------------------- LOADER
        
        /** Global Load Binary file */
        private function load(url:String):void {
            var loader:URLLoader = new URLLoader();
            var extention:String = url.substring(url.length - 3);
            loader.dataFormat = URLLoaderDataFormat.BINARY;
            if(extention == 'awd') loader.addEventListener(Event.COMPLETE, parseAWD, false, 0, true);
            loader.addEventListener(ProgressEvent.PROGRESS, loadProgress, false, 0, true);
            loader.load(new URLRequest(url));
        }
        
        /** Display current load */
        private function loadProgress(e:ProgressEvent):void {
            var P:int = int(e.bytesLoaded / e.bytesTotal * 100);
            if (P != 100) log('Load : ' + P + ' % | ' + int((e.bytesLoaded / 1024) << 0) + ' ko\n');
            else message();
        }
        
        //-------------------------------- BITMAP DISPLAY
        
        /** load bitmap from WonderFl or Local */
        public function loadBitmap():void {
            _n = 0;
            photoLoad = new PhotoLoader();
            completeBitmap(); // start first
        }
        
        protected function completeBitmap(e:Event=null):void {
            if (e) {
                var f:Bitmap = new Bitmap(photoLoad.content);
                _mapList.push(f.bitmapData);
                _n++;
            }
            if (FILES[_n]){ 
                log("image " + _n);
                photoLoad = new PhotoLoader();
                photoLoad.addEventListener(PhotoLoader.COMPLETE, completeBitmap, false, 0, true);
                if (_local) photoLoad.load(PATH_LOCAL + FILES_LOCAL[_n]);
                else photoLoad.load(PATH+FILES[_n]);
           } else {
               log("final"+ _mapList.length);
               initMaterial();
           }
         }
        
        //-------------------------------- AWD DISPLAY
        
        /** Load AWD */
        private function parseAWD(e:Event):void {
            var loader:URLLoader = e.target as URLLoader
            var loader3d:Loader3D = new Loader3D(false);
            loader3d.addEventListener(AssetEvent.ASSET_COMPLETE, onAssetComplete, false, 0, true);
            loader3d.addEventListener(LoaderEvent.RESOURCE_COMPLETE, finalAWD, false, 0, true);
            loader3d.loadData(loader.data, null, null, new AWD2Parser());
            loader.removeEventListener(ProgressEvent.PROGRESS, loadProgress);
            loader.removeEventListener(Event.COMPLETE, parseAWD);
            loader = null;
        }
        
        /** Listener function for asset complete event on awd */
        private function onAssetComplete(event:AssetEvent):void {
            var mesh:Mesh;
            var material:TextureMaterial;
            var specularMethod:FresnelSpecularMethod;
            if (event.asset.assetType == AssetType.SKELETON) {
                // create a new skeleton animation set
                animationSet = new SkeletonAnimationSet(3);
                // wrap our skeleton animation set in an animator object and add our sequence objects
                animator = new SkeletonAnimator(animationSet, event.asset as Skeleton);
                animator.updateRootPosition = false;
            } else if (event.asset.assetType == AssetType.ANIMATION_STATE) {
                // create state objects for each animation state encountered 
                var animationState:SkeletonAnimationState = event.asset as SkeletonAnimationState;
                animationSet.addState(animationState.name, animationState);
            } else if (event.asset.assetType == AssetType.MESH) {
                // find the 3d mesh by name
                mesh = Mesh(event.asset);
                if (mesh) {
                    if (mesh.name == "Onkba") {
                        hero = mesh;
                        hero.material = _materials[1];
                        hero.castsShadows = true;
                        hero.scale(10);
                    }
                    if (mesh.name == "Gun") {
                        gun = mesh;
                        gun.material = _materials[2];
                        gun.castsShadows = true;
                        gun.scale(10);
                        gun.z = 90;
                        gun.x= 20;
                        gun.y = 50;
                        gun.rotationY = 0;
                        gun.rotationZ = 120;
                    }
                }
            }
        }
        private static var _player:Mesh;
        /** Check if all resourse loaded */
        private function finalAWD(e:LoaderEvent):void {
            //apply our animator to our mesh
            hero.animator = animator;
            //add dynamique eyes
            addHeroEye(10);
            makeGunClone();
            //default to breathe sequence
            rest();
            
            var loader3d:Loader3D = e.target as Loader3D;
            loader3d.removeEventListener(AssetEvent.ASSET_COMPLETE, onAssetComplete);
            loader3d.removeEventListener(LoaderEvent.RESOURCE_COMPLETE, finalAWD);
            
            _player.addChild(hero);
            
            _player.addChild(gun);
        }
        
        //-------------------------------- CLONE
        
        /** Clone Onkba hero */
        private function makeClone(n:int=6):void {
            if (!cloneActif) {
                cloneActif = true;
                var g:Mesh;
                var decal:int = -(n * 400) / 2;
                for (var j:int = 1; j < n; j++) {
                    for (var i:int = 1; i < n; i++) {
                        g = Mesh(hero.clone());
                        g.x = decal + (400 * i);
                        g.z = (decal + (400 * j));
                        if (g.x != 0 || g.z != 0)
                            _view.scene.addChild(g);
                        //create diferente animator
                        var animatorX:SkeletonAnimator = new SkeletonAnimator(animationSet, animator.skeleton);
                        animatorX.updateRootPosition = false;
                        var anim:String = SEQUENCE[(int(Math.random()*5))]
                        animatorX.play(anim, stateTransition);
                        g.animator = animatorX;
                    }
                }
            }
        }
        
        /** Clone gun */
        private function makeGunClone(n:int=10):void {
            var g:Mesh;
            for (var i:int = 1; i < n; i++) {
                g = Mesh(gun.clone());
                g.z = gun.z+ (100 * i);
                g.y=-250;
                g.x=-500
                _view.scene.addChild(g);
            }
        }
        
        //-------------------------------- ANIMATION
        
        /** Character breath animation */
        private function rest():void {
            isMoving = false;
            if (currentAnim == SEQUENCE[0]) return;
            currentAnim = SEQUENCE[0];
            animator.playbackSpeed = SEQSPEED[0];
            animator.play(currentAnim, stateTransition);
        }
        
        /** Character fight animation */
        private function fight():void {
            if (currentAnim == SEQUENCE[3]) return;
            currentAnim = SEQUENCE[3];
            animator.playbackSpeed = SEQSPEED[3];
            animator.play(currentAnim, stateTransition);
        }
        
        /** Character fight animation */
        private function fight2():void {
            if (currentAnim == SEQUENCE[4]) return;
            currentAnim = SEQUENCE[4];
            animator.playbackSpeed = SEQSPEED[4];
            animator.play(currentAnim, stateTransition);
        }
        
        /** Character jump animation */
        private function jump():void {
            isJumping = false;
        }
        
        /** Character Mouvement */
        private function updateMovement(dir:Number):void {
            isMoving = true;
            var anim:String = isRunning ? SEQUENCE[2] : SEQUENCE[1];
            animator.playbackSpeed = dir * (isRunning ? SEQSPEED[2] : SEQSPEED[1]);
            if (currentAnim == anim) return;
            currentAnim = anim;
            animator.play(currentAnim, stateTransition);
        }
        
        //-------------------------------- KEYBORD
        
        /** Key down listener for animation */
        private function onKeyDown(event:KeyboardEvent):void {
            switch (event.keyCode) {
                case Keyboard.SHIFT: 
                    isRunning = true;
                    if (isMoving)
                        updateMovement(movementDirection);
                    break;
                case Keyboard.UP: case Keyboard.W: case Keyboard.Z: //fr
                    updateMovement(movementDirection = 1);
                    break;
                case Keyboard.DOWN: case Keyboard.S: 
                    updateMovement(movementDirection = -1);
                    break;
                case Keyboard.LEFT: case Keyboard.A: case Keyboard.Q: //fr
                    currentRotationInc = -ROTATION_SPEED;
                    break;
                case Keyboard.RIGHT: case Keyboard.D: 
                    currentRotationInc = ROTATION_SPEED;
                    break;
                case Keyboard.E: 
                    fight2();
                    break;
                case Keyboard.SPACE: case Keyboard.R: 
                    fight();
                    break;
                case Keyboard.N: 
                    randomSky();
                    break;
                case Keyboard.B: 
                    makeClone();
                    break;
            }
        }
        
        /** Key up listener */
        private function onKeyUp(event:KeyboardEvent):void {
            switch (event.keyCode) {
                case Keyboard.SHIFT: 
                isRunning = false;
                if (isMoving) updateMovement(movementDirection);
                break;
                case Keyboard.UP: case Keyboard.W: case Keyboard.Z: //fr
                case Keyboard.DOWN: case Keyboard.S: case Keyboard.SPACE: 
                case Keyboard.E: case Keyboard.R:
                rest();
                break;
                case Keyboard.LEFT: case Keyboard.A: case Keyboard.Q: //fr
                case Keyboard.RIGHT: case Keyboard.D: 
                    currentRotationInc = 0;
                    break;
            }
        }
        
        //-------------------------------- NAVIGATION
        
        /** stage listener and mouse control */
        private function onResize(event:Event=null):void {
            _view.width = stage.stageWidth;
            _view.height = stage.stageHeight;
            _stats.x = stage.stageWidth - _stats.width;
        }
        
        private function onStageMouseDown(ev:MouseEvent):void {
            _prevMouseX = ev.stageX;
            _prevMouseY = ev.stageY;
            _mouseMove = true;
        }
        
        private function onStageMouseLeave(event:Event):void {
            _mouseMove = false;
        }
        
        private function onStageMouseMove(ev:MouseEvent):void {
            if (_mouseMove) {
                _hoverCtrl.panAngle += (ev.stageX - _prevMouseX);
                _hoverCtrl.tiltAngle += (ev.stageY - _prevMouseY);
            }
            _prevMouseX = ev.stageX;
            _prevMouseY = ev.stageY;
        }
        
        /** mouseWheel listener */
        private function onStageMouseWheel(ev:MouseEvent):void {
            _hoverCtrl.distance -= ev.delta * 5;
            if (_hoverCtrl.distance < 600) {
                if (ev.delta > 0)
                    _cameraHeight += 13;
                else
                    _cameraHeight -= 13;
            }
            if (_hoverCtrl.distance < 100)
                _hoverCtrl.distance = 100;
            else if (_hoverCtrl.distance > 2000)
                _hoverCtrl.distance = 2000;
        }
        
        //-------------------------------- EYE
        
        /** Dynamique eye */
        public function addHeroEye(Scale:Number):void {
            if (_eyes)
                return;
            // texture
            _eyeMapClose = new ColorMaterial(0xA13D1E);
            _eyeMapClose.lightPicker = _lightPicker;
            _eyeMapClose.specularLightSources = LightSources.LIGHTS;
            //_eyeMapClose.shadowMethod = new TripleFilteredShadowMapMethod(DirectionalLight(_light[0]));
            _eyeMapClose.gloss = 12;
            _eyeMapClose.specular = 0.6;
            _eyeMapClose.ambient = 1;
            _materials.push(_eyeMapClose);
            //get eye texture from reference bitmap
            var m:Matrix = new Matrix();
            m.translate(-283/2, -197/2);
            var b:BitmapData = new BitmapData(256/2, 256/2, false, 0xffffff)
            b.draw(_mapList[0], m)
            
            _eyeMapOpen = new TextureMaterial(new BitmapTexture(b));
            _eyeMapOpen.lightPicker = _lightPicker;
            _eyeMapOpen.specularLightSources = LightSources.LIGHTS;
            //_eyeMapOpen.addMethod(new EnvMapMethod(_mapper.sky, 0.1));
            //_eyeMapOpen.shadowMethod = new TripleFilteredShadowMapMethod(DirectionalLight(_light[0]));
            _eyeMapOpen.gloss = 300;
            _eyeMapOpen.specular = 5;
            _eyeMapOpen.ambient = 1;
            _eyeMapOpen.repeat = true;
            _materials.push(_eyeMapOpen);
            // object
            _eyes = new Mesh(new Geometry());
            _eyesTarget = new Mesh(new Geometry());
            _eyeR = new Mesh(new SphereGeometry(1, 32, 24), _eyeMapClose);
            _eyeL = new Mesh(new SphereGeometry(1, 32, 24), _eyeMapClose);
            _eyeR.castsShadows = _eyeL.castsShadows = false;
            
            _eyes.addChild(_eyeR);
            _eyes.addChild(_eyeL);
            
            _eyeR.geometry.scaleUV(2, 1);
            _eyeL.geometry.scaleUV(2, 1);
            
            _eyeR.z = _eyeL.z = 3.68;
            _eyeR.x = _eyeL.x = 6;
            _eyeR.y = 1.90;
            _eyeL.y = -1.46;
            
            _heroPieces = new Mesh(new Geometry());
            _heroPieces.scale(Scale);
            _heroPieces.addChild(_eyesTarget);
            _heroPieces.addChild(_eyes);
            _eyeLook = new Mesh(new PlaneGeometry(0.3, 0.3, 1, 1), new ColorMaterial(0xFFFFFF, 1));
            _eyeLook.rotationX = 90;
            _eyeLook.visible = false;
            var h:ColorMaterial = new ColorMaterial(0xFFFFFF, 1)
            var zone:Mesh = new Mesh(new PlaneGeometry(12, 6, 1, 1), h);
            zone.castsShadows = false;
            zone.material.blendMode = "multiply";
            zone.addEventListener(MouseEvent3D.MOUSE_MOVE, onMeshMouseMove);
            zone.addEventListener(MouseEvent3D.MOUSE_OVER, onMeshMouseOver);
            zone.addEventListener(MouseEvent3D.MOUSE_OUT, onMeshMouseOut);
            zone.mouseEnabled = true;
            zone.rotationX = 90;
            zone.rotationZ = 90;
            zone.z = 10;
            zone.x = 6;
            zone.y = 0.22;
            _eyeLook.z = 10.2;
            _eyeLook.x = 6;
            _eyeLook.y = 0.22;
            _eyePosition = _eyeLook.position;
            
            _eyes.addChild(zone);
            _eyes.addChild(_eyeLook);
           // _view.scene.addChild(_heroPieces);
            _player.addChild(_heroPieces);
        }
        
        /** mesh listener for mouse over interaction */
        private function onMeshMouseOver(event:MouseEvent3D):void {
            event.target.showBounds = true;
            _eyeLook.visible = true;
            onMeshMouseMove(event);
        }
        
        /** mesh listener for mouse out interaction */
        private function onMeshMouseOut(event:MouseEvent3D):void {
            event.target.showBounds = false;
            _eyeLook.visible = false;
            _eyeLook.position = _eyePosition;
        }
        
        /** mesh listener for mouse move interaction */
        private function onMeshMouseMove(event:MouseEvent3D):void {
            _eyeLook.position = new Vector3D(event.localPosition.z + 6, event.localPosition.x, event.localPosition.y + 10);
        }
        
        private function updateEye():void {
            if (_heroPieces && _player) {
               // _heroPieces.transform = hero.transform;//_player.transform;
                //get the head bone
                if (animator)
                    if (animator.globalPose.numJointPoses >= 40) {
                        _eyes.transform = animator.globalPose.jointPoses[39].toMatrix3D();
                        _eyes.position.add(new Vector3D(-10.22, 0, 0));
                    }
                // look 
                _eyeR.lookAt(_eyeLook.position.add(new Vector3D(0, 1.4, 0)), new Vector3D(0, 1, 1));
                _eyeL.lookAt(_eyeLook.position.add(new Vector3D(0, -1.4, 0)), new Vector3D(0, 1, 1));
                // open close eye    
                _eyeCount++
                if (_eyeCount > 300)
                    closeEye();
                if (_eyeCount > 309)
                    openEye();
            }
        }
        
        private function closeEye():void {
            _eyeR.material = _eyeMapClose;
            _eyeL.material = _eyeMapClose;
        }
        
        private function openEye():void {
            _eyeR.material = _eyeMapOpen;
            _eyeL.material = _eyeMapOpen;
            _eyeCount = 0;
        }
        
        //-------------------------------- FLASH SIDE
        
        /** Create an instructions overlay */
        private function initText():void {
            _text = new TextField();
            _text.defaultTextFormat = new TextFormat("Verdana", 9, 0xAAAAAA);
            _text.x=2;
            _text.width = 300;
            _text.height = 250;
            _text.selectable = false;
            _text.mouseEnabled = true;
            _text.wordWrap = true;
            _text.filters = [new DropShadowFilter(1, 45, 0x0, 1, 0, 0)];
            addChild(_text);
        }
        
        /** final message */
        protected function message():void {
            _text.htmlText = "HERO 1.0\n"
            _text.appendText("Cursor keys / WSAD / ZSQD - move\n");
            _text.appendText("SHIFT - hold down to run\n");
            _text.appendText("E - punch  R - guard\n");
            _text.appendText("N - random sky\n");
            _text.appendText("B - clone\n");
        }
        
        /** log for display info */
        private function log(t:String):void {
            _text.htmlText = t;
        }
    }
}



//______________________________________MAPPER

import flash.filters.DisplacementMapFilter;
import away3d.textures.BitmapCubeTexture;
import flash.display.DisplayObject;
import flash.filters.ShaderFilter;
import flash.geom.ColorTransform;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.Shader;
import flash.display.Sprite;
import flash.geom.Matrix;
import flash.geom.Point;
    
internal class Mapper extends Sprite {
    private var _shaders:Vector.<Shader>;
    private var _bitmap:Vector.<Bitmap>;
    private var _bitdata:Vector.<BitmapData>;
    private var _skydata:Vector.<BitmapData>;
    private var _sky:BitmapCubeTexture;
    
    //-------------------------------- REFERENCY

    public function get bitdata():Vector.<BitmapData> { return _bitdata; }  
    public function get sky():BitmapCubeTexture { return _sky; }
    
    //-------------------------------- VECTOR SKY
    
    /** create vector sky */
    public function vectorSky(C:Array, quality:uint=8):void {
        var xl:uint = 128 * quality;
        var pinch:uint = xl / 3.6;
        // sky color from bottom to top;
        var color:Array = [brighten(C[0], 50), darken(C[0], 25), darken(C[0], 5), darken(C[1], 0), C[1], C[2], darken(C[2], 25), darken(C[2], 50)];
        var side:BitmapData = new BitmapData(xl, xl, false, color[1]);
        var top:BitmapData = new BitmapData(xl, xl, false, color[6]);
        var floor:BitmapData = new BitmapData(xl, xl, false, color[1]);
        // side
        var matrix:Matrix = new Matrix();
        matrix.createGradientBox(xl, xl, -Math.PI / 2)
        var g:Sprite = new Sprite();
        g.graphics.beginGradientFill('linear', [color[1], color[2], color[3], color[4], color[5], color[6]], [1, 1, 1, 1, 1, 1], [90, 110, 120, 126, 180, 230], matrix);
        g.graphics.drawRect(0, 0, xl, xl);
        g.graphics.endFill();
        var displacement_map:DisplacementMapFilter = new DisplacementMapFilter(pinchMap(xl, xl), new Point(0, 0), 4, 2, 0, pinch, "clamp")
        g.filters = [displacement_map];
        side.draw(g);
        // top
        matrix = new Matrix();
        g.graphics.clear();
        matrix.createGradientBox(xl, xl, 0, 0, 0);
        g.graphics.beginGradientFill('radial', [color[7], color[6]], [1, 1], [0, 255], matrix);
        g.graphics.drawEllipse(0, 0, xl, xl);
        g.graphics.endFill();
        top.draw(g);
        // bottom
        matrix = new Matrix();
        matrix.createGradientBox(xl, xl, 0, 0, 0);
        g.graphics.clear();
        g.graphics.beginGradientFill('radial', [color[0], color[1]], [1, 1], [0, 255], matrix);
        g.graphics.drawEllipse(0, 0, xl, xl);
        g.graphics.endFill();
        floor.draw(g);
        
        _skydata = new Vector.<BitmapData>();
        _skydata.push(side, top, floor);
        if (_sky) _sky.dispose();
        _sky = new BitmapCubeTexture(side, side, top, floor, side, side);
    }
    
    /** add some sphericale distortion */
    private function pinchMap(w:int, h:int):BitmapData {
        var i:int, BCol:Number, GCol:Number;
        var b:BitmapData = new BitmapData(w, h, false, 0x000000)
        var vx:int = w >> 1, vy:int = h >> 1;
        for (var j:int = 0; j < h; j++) {
            for ( i = 0; i < w; i++) {
                BCol = 127 + (i - vx) / (vx) * 127 * (1 - Math.pow((j - vy) / (vy), 2));
                GCol = 127 + (j - vy) / (vy) * 127 * (1 - Math.pow((i - vx) / (vx), 2));
                b.setPixel(i, j, (GCol << 8) | BCol);
            }
        }
        return b;
    }
        
    //-------------------------------- COLORS UTILS
        
    /** brighten color */
    public function brighten(hexColor:Number, percent:Number):Number {
        if (isNaN(percent)) percent = 0;
        if (percent > 100) percent = 100;
        if (percent < 0) percent = 0;
        var factor:Number = percent / 100, rgb:Object = hexToRgb(hexColor);
        rgb.r += (255 - rgb.r) * factor; rgb.b += (255 - rgb.b) * factor; rgb.g += (255 - rgb.g) * factor;
        return rgbToHex(Math.round(rgb.r), Math.round(rgb.g), Math.round(rgb.b));
    }
        
    /** darken color */
    public function darken(hexColor:Number, percent:Number):Number {
        if (isNaN(percent)) percent = 0;
        if (percent > 100) percent = 100;
        if (percent < 0) percent = 0;
        var factor:Number = 1 - (percent / 100), rgb:Object = hexToRgb(hexColor);
        rgb.r *= factor; rgb.b *= factor; rgb.g *= factor;
        return rgbToHex(Math.round(rgb.r), Math.round(rgb.g), Math.round(rgb.b));
    }
        
    /** conversion */
    public function rgbToHex(r:Number, g:Number, b:Number):Number { return (r << 16 | g << 8 | b); }
    public function hexToRgb(hex:Number):Object { return {r: (hex & 0xff0000) >> 16, g: (hex & 0x00ff00) >> 8, b: hex & 0x0000ff}; }
        
    /** apply color to object */
    public function color(o:DisplayObject, c:int=0, a:Number=1):void {
        if (!o) return;
        var nc:ColorTransform = o.transform.colorTransform;
        nc.color = c;
        nc.alphaMultiplier = a;
        if (c == 0) o.transform.colorTransform = new ColorTransform();
        else o.transform.colorTransform = nc;
    }
        
    //-------------------------------- MATH TOOL
        
    /** random generator */
    private function Ran(max:Number=1, min:Number=0):Number { return Math.floor(Math.random() * (max - min + 1)) + min; }
}


//______________________________________ BITMAP LOADER

import flash.display.Sprite;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.system.LoaderContext;

internal class PhotoLoader extends Sprite {
    private var loader:Loader;
    public var content:BitmapData;
    public static const COMPLETE:String = Event.COMPLETE;

    public function PhotoLoader() {
        loader = new Loader();
    }
    public function load(file:String):void {
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, complete, false, 0, true);
        loader.load(new URLRequest(file), new LoaderContext(true));
    }
    private function complete(evt:Event):void {
        loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, complete);
        content = Bitmap(loader.contentLoaderInfo.content).bitmapData;
        dispatchEvent(new Event(PhotoLoader.COMPLETE));
        loader.unload();
        loader = null;
    }

}