SHARE
TWEET

Untitled

a guest Dec 8th, 2018 97 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import com.transmote.flar.FLARManager;
  2. import com.transmote.flar.marker.FLARMarkerEvent;
  3. import com.transmote.flar.marker.FLARMarker;
  4.  
  5. import cityu.scm.FLARBasicViewPV3D; // easy-to-use wrapper for FLAR + PV3D
  6.  
  7. import org.papervision3d.objects.DisplayObject3D;
  8.  
  9. // ----------------------------------------------------------
  10.  
  11. var flarManager:FLARManager = new FLARManager("FlarDemo-SingleMarker.xml", true);
  12.  
  13. addChildAt(flarManager.flarCameraSource, 0);
  14.  
  15. // ----------------------------------------------------------
  16. var basicView:FLARBasicViewPV3D;
  17. var pivotDo3d:DisplayObject3D;
  18.        
  19. flarManager.addEventListener(Event.INIT, flarManagerInited);
  20. function flarManagerInited(e:Event): void {
  21.     flarManager.removeEventListener(Event.INIT, flarManagerInited);
  22.  
  23.     // ----------------------------------------------------------
  24.     // init basic view
  25.     basicView = new FLARBasicViewPV3D(flarManager, stage.stageWidth, stage.stageHeight);
  26.     addChild(basicView);
  27.    
  28.     //basicView.showMarkerOutlines();
  29.     basicView.showXYZAxes();
  30.     //basicView.enableSilhouetteRendering();
  31.    
  32.     initPivotDo3d();
  33.    
  34.     init3DScene();
  35.    
  36.     // start to rendering 3D scene
  37.     basicView.startRendering();
  38.     // ----------------------------------------------------------
  39.    
  40.     stage.addEventListener(Event.ENTER_FRAME, forEveryFrame);  
  41. }
  42.  
  43. function initPivotDo3d(): void {
  44.     pivotDo3d = new DisplayObject3D(); 
  45.     basicView.scene.addChild(pivotDo3d);
  46.     pivotDo3d.visible = false; // no marker detected at the beginning
  47. }
  48.  
  49. function forEveryFrame(e:Event): void {
  50.     if (flarManager.activeMarker != null) {
  51.         pivotDo3d.visible = true;          
  52.  
  53.         // apply the FLARToolkit transformation matrix to the pivotDo3d
  54.         basicView.updateModelWrtMarker(pivotDo3d, flarManager.activeMarker);
  55.     } else {
  56.         pivotDo3d.visible = false;
  57.     }
  58.    
  59. }
  60.  
  61. // ----------------------------------------------------------
  62. import org.papervision3d.objects.parsers.*;             // load external models
  63. import org.papervision3d.events.*;
  64.  
  65. import org.papervision3d.materials.*;
  66. import org.papervision3d.materials.utils.MaterialsList;
  67.  
  68. function init3DScene(): void {
  69.     // load some external model
  70.     var model:DAE = new DAE();
  71.    
  72.     // ------------------------------------------------------
  73.     // exported from Maya using OpenCollada plug-in
  74. //  model.load("./resources/assets/ColladaMaya/Test-OpenCollada.dae");
  75.    
  76.     // ------------------------------------------------------
  77.     // exported from Maya's built-in DAE-FBX exporter
  78.     // there is some problem with texture loading
  79.     //model.load("./resources/assets/ColladaMaya/Test-DaeFbx.dae");
  80.    
  81.     // so setup the materials manually 
  82.     var materialList:MaterialsList = new MaterialsList();
  83.    
  84.     // test 1: apply a bitmap material to all objects
  85.     //materialList.addMaterial(
  86.     //  new BitmapFileMaterial("./resources/assets/image_2.jpg"), "all");
  87.    
  88.     // test 2: apply a flat shade material to all objects
  89.     //materialList.addMaterial(
  90.     //  basicView.getMaterialByPatternId(0), "all");
  91.    
  92.     // test 3: associate different materials with predefined material names
  93.      materialList.addMaterial(
  94.         new BitmapFileMaterial("./resources/assets/image_2.jpg"), "lambert1");
  95.      materialList.addMaterial(
  96.         basicView.getMaterialByPatternId(0), "lambert2");
  97.  
  98.     model.load("./resources/assets/ColladaMaya/Test-DaeFbx.dae", materialList);
  99.  
  100.     // ------------------------------------------------------
  101.     // need to import org.papervision3d.events.*;
  102.     model.addEventListener(FileLoadEvent.LOAD_COMPLETE, modelLoaded);
  103. }
  104.  
  105. import cityu.scm.PV3DBoundingBox; // to make the loaded have proper size wrt marker
  106.  
  107. function modelLoaded(e:FileLoadEvent):void {
  108.     trace("model loaded successfully");
  109.  
  110.     // get the loaded model
  111.     var model:DAE = e.target as DAE;
  112.     // add to scene via pivot do3d
  113.     pivotDo3d.addChild(model);
  114.    
  115.     // adjust model size
  116.     var bb:PV3DBoundingBox = new PV3DBoundingBox(model);
  117.     // desired scale is model-dependent
  118.     bb.scaleDo3d(model, 120);
  119.    
  120.     // adjust model orientation
  121.     // desired orientation is model-dependent
  122.     model.rotationX = 90;
  123.    
  124.     // ---------------------------------------------------
  125.     // check pre-defined material names
  126.     trace("materials:", model.materials);      
  127.    
  128.     // enable interactivity
  129.     model.materials.getMaterialByName("lambert1").interactive = true;
  130.     model.materials.getMaterialByName("lambert2").interactive = true;
  131.  
  132.     // Exercise
  133.     // event listeners for pTorus1
  134.     // ...
  135.     // pTorus rotate in Z direction when it is clicked
  136.        
  137.     // event listeners for pPyramid1
  138.     // ...
  139.     // pPyramid scale smaller when it is clicked
  140.    
  141.     // event listeners for pPipe1
  142.     // ...
  143.     // pPipe1 scale larger in scaleX when it is clicked.
  144.  
  145.    
  146.     /*
  147.     basicView.attachEventListeners(model, InteractiveScene3DEvent.OBJECT_CLICK,
  148.                                modelClicked);
  149.     */
  150. }
  151.  
  152. function modelClicked(e:InteractiveScene3DEvent): void {
  153.     e.displayObject3D.localRotationZ += 5;
  154. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top