Advertisement
Guest User

FlarmanagerCollada

a guest
Jun 18th, 2013
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package examples {
  2.     import away3d.animators.Animator;
  3.     import away3d.animators.BonesAnimator;
  4.     import away3d.containers.ObjectContainer3D;
  5.     import away3d.containers.Scene3D;
  6.     import away3d.containers.View3D;
  7.     import away3d.core.utils.Cast;
  8.     import away3d.events.Loader3DEvent;
  9.     import away3d.lights.DirectionalLight3D;
  10.     import away3d.loaders.AbstractParser;
  11.     import away3d.loaders.Collada;
  12.     import away3d.loaders.Loader3D;
  13.     import away3d.loaders.utils.AnimationLibrary;
  14.     import away3d.materials.BitmapMaterial;
  15.    
  16.     import com.transmote.flar.FLARManager;
  17.     import com.transmote.flar.camera.FLARCamera_Away3D;
  18.     import com.transmote.flar.camera.FLARCamera_PV3D;
  19.     import com.transmote.flar.marker.FLARMarker;
  20.     import com.transmote.flar.marker.FLARMarkerEvent;
  21.     import com.transmote.flar.tracker.FLARToolkitManager;
  22.     import com.transmote.flar.utils.geom.AwayGeomUtils;
  23.    
  24.     import flash.display.Sprite;
  25.     import flash.events.Event;
  26.     import flash.geom.Rectangle;
  27.     import flash.geom.Vector3D;
  28.     import flash.utils.getTimer;
  29.     import flash.utils.Timer;
  30.     import flash.events.TimerEvent;
  31.    
  32.     import com.greensock.TweenLite;
  33.    
  34.     /**
  35.      * FLARManager_Tutorial3D demonstrates how to display a Collada-formatted model
  36.      * using FLARManager, FLARToolkit, and Away3D.
  37.      * see the accompanying tutorial writeup here:
  38.      * http://words.transmote.com/wp/flarmanager/inside-flarmanager/loading-collada-models/
  39.      *
  40.      * the collada model used for this example, mario_testrun.dae, comes from Away3D's examples.
  41.      *
  42.      * @author  Eric Socolofsky
  43.      * @url     http://transmote.com/flar
  44.      */
  45.     public class FLARManagerCollada extends Sprite {
  46.         private var flarManager:FLARManager;
  47.        
  48.         private var view:View3D;
  49.         private var camera3D:FLARCamera_Away3D;
  50.         private var scene3D:Scene3D;
  51.         private var light:DirectionalLight3D;
  52.        
  53.         private var activeMarker:FLARMarker;
  54.         private var modelLoader:Loader3D;
  55.        
  56.         private var modelContainer:ObjectContainer3D
  57.        
  58.         private var model:ObjectContainer3D
  59.         private var model1:ObjectContainer3D
  60.         private var model2:ObjectContainer3D
  61.         private var model3:ObjectContainer3D
  62.        
  63.         private var modelAnimator:BonesAnimator;
  64.         private var modelAnimator1:BonesAnimator;
  65.         private var modelAnimator2:BonesAnimator;
  66.         private var modelAnimator3:BonesAnimator;
  67.        
  68.         // texture and collada file for mario
  69.         //[Embed(source="/../resources/assets/mario_tex.jpg")]
  70.         private var Charmap:Class;
  71.         [Embed(source="//../resources/assets/KecoaLowPoly2.dae",mimeType="application/octet-stream")]
  72.         private var Kecoa:Class;
  73.        
  74.        
  75.         public function FLARManagerCollada () {
  76.             this.addEventListener(Event.ADDED_TO_STAGE, this.onAdded);
  77.         }
  78.        
  79.         private function onAdded (evt:Event) :void {
  80.             this.removeEventListener(Event.ADDED_TO_STAGE, this.onAdded);
  81.            
  82.             // pass the path to the FLARManager xml config file into the FLARManager constructor.
  83.             // FLARManager creates and uses a FLARCameraSource by default.
  84.             // the image from the first detected camera will be used for marker detection.
  85.             // also pass an IFLARTrackerManager instance to communicate with a tracking library,
  86.             // and a reference to the Stage (required by some trackers).
  87.             this.flarManager = new FLARManager("../resources/flar/flarConfig.xml", new FLARToolkitManager(), this.stage);
  88.            
  89.             // to switch tracking engines, pass a different IFLARTrackerManager into FLARManager.
  90.             // refer to this page for information on using different tracking engines:
  91.             // http://words.transmote.com/wp/inside-flarmanager-tracking-engines/
  92.             //          this.flarManager = new FLARManager("../resources/flar/flarConfig.xml", new FlareManager(), this.stage);
  93.             //          this.flarManager = new FLARManager("../resources/flar/flarConfig.xml", new FlareNFTManager(), this.stage);
  94.            
  95.             // add FLARManager.flarSource to the display list to display the video capture.
  96.             this.addChild(Sprite(this.flarManager.flarSource));
  97.            
  98.             // begin listening for FLARMarkerEvents.
  99.             this.flarManager.addEventListener(FLARMarkerEvent.MARKER_ADDED, this.onMarkerAdded);
  100.             this.flarManager.addEventListener(FLARMarkerEvent.MARKER_UPDATED, this.onMarkerUpdated);
  101.             this.flarManager.addEventListener(FLARMarkerEvent.MARKER_REMOVED, this.onMarkerRemoved);
  102.            
  103.             // wait for FLARManager to initialize before setting up Away3D environment.
  104.             this.flarManager.addEventListener(Event.INIT, this.onFlarManagerInited);
  105.         }
  106.        
  107.         private function onFlarManagerInited (evt:Event) :void {
  108.             this.flarManager.removeEventListener(Event.INIT, this.onFlarManagerInited);
  109.            
  110.             this.scene3D = new Scene3D();
  111.             this.camera3D = new FLARCamera_Away3D(this.flarManager, new Rectangle(0, 0, this.stage.stageWidth, this.stage.stageHeight));
  112.             this.view = new View3D({x:0.5*this.stage.stageWidth, y:0.5*this.stage.stageHeight, scene:this.scene3D, camera:this.camera3D});
  113.             this.addChild(this.view);
  114.            
  115.             this.light = new DirectionalLight3D();
  116.             this.light.direction = new Vector3D(500, -300, 200);
  117.             this.scene3D.addLight(light);
  118.            
  119.             var collada:Collada = new Collada();
  120.             collada.scaling = 1;
  121.             model = collada.parseGeometry(Kecoa) as ObjectContainer3D;
  122.             //model.materialLibrary.getMaterial("FF_FF_FF_mario1").material = new BitmapMaterial(Cast.bitmap(Charmap));
  123.             model.mouseEnabled = false;
  124.             model.rotationY = 180;
  125.             model.rotationX = -90;
  126.             model.x = 0;
  127.             model.y = 0;
  128.             model.z = 0;
  129.             //this.modelAnimator = model.animationLibrary.getAnimation("default").animator as BonesAnimator;
  130.            
  131.             // create a container for the model, that will accept matrix transformations.
  132.             this.modelContainer = new ObjectContainer3D();
  133.             this.modelContainer.addChild(model);
  134.             this.modelContainer.visible = false;
  135.             this.scene3D.addChild(this.modelContainer);
  136.            
  137.            
  138.             var collada1:Collada = new Collada();
  139.             collada1.scaling = 1;
  140.             model1 = collada1.parseGeometry(Kecoa) as ObjectContainer3D;
  141.             //model1.materialLibrary.getMaterial("FF_FF_FF_mario1").material = new BitmapMaterial(Cast.bitmap(Charmap));
  142.             model1.mouseEnabled = false;
  143.             model1.rotationY = 180;
  144.             model1.x = 0;
  145.             model1.y = 100;
  146.             model1.z = 0;
  147.             //this.modelAnimator1 = model1.animationLibrary.getAnimation("default").animator as BonesAnimator;
  148.            
  149.             // create a container for the model, that will accept matrix transformations.
  150.             this.modelContainer.addChild(model1);
  151.             this.modelContainer.visible = false;
  152.             this.scene3D.addChild(this.modelContainer);
  153.            
  154.             var collada2:Collada = new Collada();
  155.             collada2.scaling = 1;
  156.             model2 = collada2.parseGeometry(Kecoa) as ObjectContainer3D;
  157.             //model2.materialLibrary.getMaterial("FF_FF_FF_mario1").material = new BitmapMaterial(Cast.bitmap(Charmap));
  158.             model2.mouseEnabled = false;
  159.             model2.rotationY = 180;
  160.             model2.x = 0;
  161.             model2.y = 200;
  162.             model2.z = 0;
  163.             //this.modelAnimator2 = model2.animationLibrary.getAnimation("default").animator as BonesAnimator;
  164.            
  165.             // create a container for the model, that will accept matrix transformations.
  166.             this.modelContainer.addChild(model2);
  167.             this.modelContainer.visible = false;
  168.             this.scene3D.addChild(this.modelContainer);
  169.            
  170.             var collada3:Collada = new Collada();
  171.             collada3.scaling = 1;
  172.             model3 = collada3.parseGeometry(Kecoa) as ObjectContainer3D;
  173.             //model3.materialLibrary.getMaterial("FF_FF_FF_mario1").material = new BitmapMaterial(Cast.bitmap(Charmap));
  174.             model3.mouseEnabled = false;
  175.             model3.rotationY = 180;
  176.             model3.x = 0;
  177.             model3.y = 0;
  178.             model3.z = 0;          
  179.             //this.modelAnimator3 = model3.animationLibrary.getAnimation("default").animator as BonesAnimator;
  180.            
  181.             // create a container for the model, that will accept matrix transformations.
  182.             this.modelContainer.addChild(model3);
  183.             this.modelContainer.visible = false;
  184.             this.scene3D.addChild(this.modelContainer);
  185.            
  186.             this.addEventListener(Event.ENTER_FRAME, this.onEnterFrame);
  187.            
  188.            
  189.         }
  190.        
  191.         private function onMarkerAdded (evt:FLARMarkerEvent) :void {
  192.             trace("["+evt.marker.patternId+"] added");
  193.             this.modelContainer.visible = true;
  194.             this.activeMarker = evt.marker;
  195.         }
  196.        
  197.         private function onMarkerUpdated (evt:FLARMarkerEvent) :void {
  198.             trace("["+evt.marker.patternId+"] updated");
  199.             this.modelContainer.visible = true;
  200.             this.activeMarker = evt.marker;
  201.         }
  202.        
  203.         private function onMarkerRemoved (evt:FLARMarkerEvent) :void {
  204.             trace("["+evt.marker.patternId+"] removed");
  205.             this.modelContainer.visible = false;
  206.             this.activeMarker = null;
  207.         }
  208.        
  209.         private function onEnterFrame (evt:Event) :void {
  210.             // apply the FLARToolkit transformation matrix to the Cube.
  211.             if (this.activeMarker) {
  212.                 this.modelContainer.transform = AwayGeomUtils.convertMatrixToAwayMatrix(this.activeMarker.transformMatrix);
  213.                 model.y -= 10; 
  214.             }
  215.            
  216.             // update the animation and Away3D view.
  217.             if (this.modelAnimator) {
  218.                 this.modelAnimator.update(getTimer() * .0005);
  219.                 this.modelAnimator1.update(getTimer() * .005);
  220.                 this.modelAnimator2.update(getTimer() * .005);
  221.                 this.modelAnimator3.update(getTimer() * .005);
  222.             }
  223.             this.view.render();
  224.         }
  225.     }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement