Advertisement
Guest User

Untitled

a guest
Dec 8th, 2018
154
0
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. var flarManager:FLARManager = new FLARManager("FlarDemo-TwoMarkers.xml");
  11.  
  12. addChildAt(Sprite(flarManager.flarSource), 0);
  13.  
  14. // ----------------------------------------------------------
  15. var basicView:FLARBasicViewPV3D;
  16. var pivotDo3ds: Array = new Array();
  17. var modelDo3ds: Array = new Array();
  18.        
  19. flarManager.addEventListener(Event.INIT, flarManagerInited);
  20. function flarManagerInited(e:Event): void {
  21.     flarManager.removeEventListener(Event.INIT, flarManagerInited);
  22.  
  23.     // init basic view
  24.     basicView = new FLARBasicViewPV3D(flarManager, stage.stageWidth, stage.stageHeight);
  25.     addChild(basicView);
  26.    
  27.     // ----------------------------------------------
  28.     // init 3D scene    
  29.     for (var i = 0; i < 2; i++) {
  30.         pivotDo3ds.push(new DisplayObject3D());
  31.         basicView.scene.addChild(pivotDo3ds[i]);
  32.         pivotDo3ds[i].visible = false;     
  33.     }
  34.  
  35.     addSphere(); // for do3dContainers[0]
  36.     addCube();   // for do3dContainers[1]
  37.     // ----------------------------------------------
  38.    
  39.     // start to rendering 3D scene
  40.     basicView.startRendering();
  41.    
  42.     stage.addEventListener(Event.ENTER_FRAME, forEveryFrame);  
  43.     stage.addEventListener(MouseEvent.CLICK, onClick);
  44. }
  45.  
  46. // ----------------------------------------------------------
  47. import org.papervision3d.objects.primitives.Sphere;
  48.  
  49. function addSphere(): void {       
  50.     // create a sphere with radius 20
  51.     // Sphere(material:MaterialObject3D = null, radius:Number = 100, segmentsW:int = 8, segmentsH:int = 6)
  52.     var sphere:Sphere = new Sphere(basicView.getMaterialByPatternId(0), 20, 16, 12);
  53.     // shift sphere upwards so it sits on top of paper instead of being cut half-way
  54.     sphere.z = 20; 
  55.    
  56.     modelDo3ds.push(sphere); // for later use
  57.     pivotDo3ds[0].addChild(sphere);
  58. }
  59. // ----------------------------------------------------------
  60.  
  61. // ----------------------------------------------------------
  62. import org.papervision3d.objects.primitives.Cube;       // cube
  63. import org.papervision3d.materials.shadematerials.FlatShadeMaterial; // shading & material
  64. import org.papervision3d.materials.utils.MaterialsList;
  65.  
  66. function addCube(): void {
  67.     // apply same material to all sides of the cube
  68.     var material:FlatShadeMaterial = basicView.getMaterialByPatternId(1);
  69.     var materialsList:MaterialsList = new MaterialsList({all: material});  
  70.    
  71.     // The width of a marker on-screen at which the scale of
  72.     // its transformation matrix is 1.0: 80 pixels.
  73.     var cube:Cube = new Cube(materialsList, 40, 40, 40);
  74.     // shift cube upwards so it sits on top of paper instead of being cut half-way
  75.     cube.z = 20;   
  76.    
  77.     modelDo3ds.push(cube); // for later use
  78.     pivotDo3ds[1].addChild(cube);
  79. }
  80. // ----------------------------------------------------------
  81.  
  82. var moving:Boolean = false; // to control when to move sphere
  83. function onClick(e:MouseEvent): void {
  84.     moving = !moving;
  85. }
  86.  
  87. function forEveryFrame(e:Event): void {
  88.     // update model transformations wrt corresponding markers
  89.     for (var i:uint = 0; i < flarManager.activeMarkers.length; i++) {
  90.         var marker:FLARMarker = flarManager.activeMarkers[i];
  91.         var pid:uint = marker.patternId;
  92.         if (pid < 2) { // we have two models only
  93.             pivotDo3ds[pid].visible = true;
  94.             basicView.updateModelWrtMarker(pivotDo3ds[pid], marker);
  95.         }
  96.     }
  97.  
  98.     // add some animation effects
  99.     modelDo3ds[0].localRotationZ += 2; // angle in degree
  100.     modelDo3ds[1].localRotationZ += 2; // angle in degree
  101.  
  102.     if (moving) {
  103.         modelDo3ds[0].x += 4; // moving sphere along x axis
  104.        
  105.         // get screen coordinate of sphere center
  106.         var screenCoords:Point = basicView.screenCoords(modelDo3ds[0]);
  107.         // screenXTxt.text = "Screen X: " + String(screenCoords.x);
  108.         // screenYTxt.text = "Screen Y: " + String(screenCoords.y);
  109.         screenTxt.text = "Screen X: " + Math.floor(screenCoords.x) + ", Screen Y: " + Math.floor(screenCoords.y);
  110.         if (screenCoords.x < 0 || screenCoords.x > stage.stageWidth ||
  111.             screenCoords.y < 0 || screenCoords.y > stage.stageHeight) {
  112.             modelDo3ds[0].x = 0; // reset
  113.         }
  114.        
  115.         // hit test
  116.         if (basicView.hitTest(modelDo3ds[0], modelDo3ds[1], 1.5)) {
  117.             modelDo3ds[0].x = 0; // reset
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement