Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.transmote.flar.FLARManager;
- import com.transmote.flar.marker.FLARMarkerEvent;
- import com.transmote.flar.marker.FLARMarker;
- import cityu.scm.FLARBasicViewPV3D; // easy-to-use wrapper for FLAR + PV3D
- import org.papervision3d.objects.DisplayObject3D;
- // ----------------------------------------------------------
- var flarManager:FLARManager = new FLARManager("FlarDemo-TwoMarkers.xml");
- addChildAt(Sprite(flarManager.flarSource), 0);
- // ----------------------------------------------------------
- var basicView:FLARBasicViewPV3D;
- var pivotDo3ds: Array = new Array();
- var modelDo3ds: Array = new Array();
- flarManager.addEventListener(Event.INIT, flarManagerInited);
- function flarManagerInited(e:Event): void {
- flarManager.removeEventListener(Event.INIT, flarManagerInited);
- // init basic view
- basicView = new FLARBasicViewPV3D(flarManager, stage.stageWidth, stage.stageHeight);
- addChild(basicView);
- // ----------------------------------------------
- // init 3D scene
- for (var i = 0; i < 2; i++) {
- pivotDo3ds.push(new DisplayObject3D());
- basicView.scene.addChild(pivotDo3ds[i]);
- pivotDo3ds[i].visible = false;
- }
- addSphere(); // for do3dContainers[0]
- addCube(); // for do3dContainers[1]
- // ----------------------------------------------
- // start to rendering 3D scene
- basicView.startRendering();
- stage.addEventListener(Event.ENTER_FRAME, forEveryFrame);
- stage.addEventListener(MouseEvent.CLICK, onClick);
- }
- // ----------------------------------------------------------
- import org.papervision3d.objects.primitives.Sphere;
- function addSphere(): void {
- // create a sphere with radius 20
- // Sphere(material:MaterialObject3D = null, radius:Number = 100, segmentsW:int = 8, segmentsH:int = 6)
- var sphere:Sphere = new Sphere(basicView.getMaterialByPatternId(0), 20, 16, 12);
- // shift sphere upwards so it sits on top of paper instead of being cut half-way
- sphere.z = 20;
- modelDo3ds.push(sphere); // for later use
- pivotDo3ds[0].addChild(sphere);
- }
- // ----------------------------------------------------------
- // ----------------------------------------------------------
- import org.papervision3d.objects.primitives.Cube; // cube
- import org.papervision3d.materials.shadematerials.FlatShadeMaterial; // shading & material
- import org.papervision3d.materials.utils.MaterialsList;
- function addCube(): void {
- // apply same material to all sides of the cube
- var material:FlatShadeMaterial = basicView.getMaterialByPatternId(1);
- var materialsList:MaterialsList = new MaterialsList({all: material});
- // The width of a marker on-screen at which the scale of
- // its transformation matrix is 1.0: 80 pixels.
- var cube:Cube = new Cube(materialsList, 40, 40, 40);
- // shift cube upwards so it sits on top of paper instead of being cut half-way
- cube.z = 20;
- modelDo3ds.push(cube); // for later use
- pivotDo3ds[1].addChild(cube);
- }
- // ----------------------------------------------------------
- var moving:Boolean = false; // to control when to move sphere
- function onClick(e:MouseEvent): void {
- moving = !moving;
- }
- function forEveryFrame(e:Event): void {
- // update model transformations wrt corresponding markers
- for (var i:uint = 0; i < flarManager.activeMarkers.length; i++) {
- var marker:FLARMarker = flarManager.activeMarkers[i];
- var pid:uint = marker.patternId;
- if (pid < 2) { // we have two models only
- pivotDo3ds[pid].visible = true;
- basicView.updateModelWrtMarker(pivotDo3ds[pid], marker);
- }
- }
- // add some animation effects
- modelDo3ds[0].localRotationZ += 2; // angle in degree
- modelDo3ds[1].localRotationZ += 2; // angle in degree
- if (moving) {
- modelDo3ds[0].x += 4; // moving sphere along x axis
- // get screen coordinate of sphere center
- var screenCoords:Point = basicView.screenCoords(modelDo3ds[0]);
- // screenXTxt.text = "Screen X: " + String(screenCoords.x);
- // screenYTxt.text = "Screen Y: " + String(screenCoords.y);
- screenTxt.text = "Screen X: " + Math.floor(screenCoords.x) + ", Screen Y: " + Math.floor(screenCoords.y);
- if (screenCoords.x < 0 || screenCoords.x > stage.stageWidth ||
- screenCoords.y < 0 || screenCoords.y > stage.stageHeight) {
- modelDo3ds[0].x = 0; // reset
- }
- // hit test
- if (basicView.hitTest(modelDo3ds[0], modelDo3ds[1], 1.5)) {
- modelDo3ds[0].x = 0; // reset
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement