package examples { import away3d.containers.ObjectContainer3D; import away3d.containers.Scene3D; import away3d.containers.View3D; import away3d.core.base.Object3D; import away3d.lights.DirectionalLight3D; import away3d.materials.ShadingColorMaterial; import away3d.primitives.Cube; import com.transmote.flar.FLARManager; import com.transmote.flar.camera.FLARCamera_Away3D; import com.transmote.flar.marker.FLARMarker; import com.transmote.flar.marker.FLARMarkerEvent; import com.transmote.flar.tracker.FLARToolkitManager; import com.transmote.flar.utils.geom.AwayGeomUtils; import flash.display.Sprite; import flash.events.Event; import flash.geom.Rectangle; import flash.geom.Vector3D; /** * FLARManager_Tutorial3D demonstrates how to set up a basic augmented reality application, * using FLARManager, FLARToolkit, and Away3D. * see the accompanying tutorial writeup here: * http://words.transmote.com/wp/flarmanager/inside-flarmanager/basic-augmented-reality/ * * @author Eric Socolofsky * @url http://transmote.com/flar */ public class FLARManagerTutorial_3D extends Sprite { private var flarManager:FLARManager; private var view:View3D; private var camera3D:FLARCamera_Away3D; private var scene3D:Scene3D; private var light:DirectionalLight3D; private var activeMarker:FLARMarker; private var cubeContainer:ObjectContainer3D; public function FLARManagerTutorial_3D () { this.addEventListener(Event.ADDED_TO_STAGE, this.onAdded); } private function onAdded (evt:Event) :void { this.removeEventListener(Event.ADDED_TO_STAGE, this.onAdded); // pass the path to the FLARManager xml config file into the FLARManager constructor. // FLARManager creates and uses a FLARCameraSource by default. // the image from the first detected camera will be used for marker detection. // also pass an IFLARTrackerManager instance to communicate with a tracking library, // and a reference to the Stage (required by some trackers). this.flarManager = new FLARManager("../resources/flar/flarConfig.xml", new FLARToolkitManager(), this.stage); // to switch tracking engines, pass a different IFLARTrackerManager into FLARManager. // refer to this page for information on using different tracking engines: // http://words.transmote.com/wp/inside-flarmanager-tracking-engines/ // this.flarManager = new FLARManager("../resources/flar/flarConfig.xml", new FlareManager(), this.stage); // this.flarManager = new FLARManager("../resources/flar/flarConfig.xml", new FlareNFTManager(), this.stage); // add FLARManager.flarSource to the display list to display the video capture. this.addChild(Sprite(this.flarManager.flarSource)); // begin listening for FLARMarkerEvents. this.flarManager.addEventListener(FLARMarkerEvent.MARKER_ADDED, this.onMarkerAdded); this.flarManager.addEventListener(FLARMarkerEvent.MARKER_UPDATED, this.onMarkerUpdated); this.flarManager.addEventListener(FLARMarkerEvent.MARKER_REMOVED, this.onMarkerRemoved); // wait for FLARManager to initialize before setting up Away3D environment. this.flarManager.addEventListener(Event.INIT, this.onFlarManagerInited); } private function onFlarManagerInited (evt:Event) :void { this.flarManager.removeEventListener(Event.INIT, this.onFlarManagerInited); this.scene3D = new Scene3D(); this.camera3D = new FLARCamera_Away3D(this.flarManager, new Rectangle(0, 0, this.stage.stageWidth, this.stage.stageHeight)); this.view = new View3D({x:0.5*this.stage.stageWidth, y:0.5*this.stage.stageHeight, scene:this.scene3D, camera:this.camera3D}); this.addChild(this.view); this.light = new DirectionalLight3D(); this.light.direction = new Vector3D(500, -300, 200); this.scene3D.addLight(light); // create the cube to display on the detected marker. var mat:ShadingColorMaterial = new ShadingColorMaterial(null, {ambient:0xFF1919, diffuse:0x730000, specular: 0xFFFFFF}); var cube:Cube = new Cube({material:mat, width:40, height:40, depth:40}); cube.z = -20; // create a container for the cube, that will accept matrix transformations. this.cubeContainer = new ObjectContainer3D(); this.cubeContainer.addChild(cube); this.scene3D.addChild(this.cubeContainer); this.addEventListener(Event.ENTER_FRAME, this.onEnterFrame); } private function onMarkerAdded (evt:FLARMarkerEvent) :void { trace("["+evt.marker.patternId+"] added"); this.cubeContainer.visible = true; this.activeMarker = evt.marker; } private function onMarkerUpdated (evt:FLARMarkerEvent) :void { trace("["+evt.marker.patternId+"] updated"); this.cubeContainer.visible = true; this.activeMarker = evt.marker; } private function onMarkerRemoved (evt:FLARMarkerEvent) :void { trace("["+evt.marker.patternId+"] removed"); this.cubeContainer.visible = false; this.activeMarker = null; } private function onEnterFrame (evt:Event) :void { // apply the FLARToolkit transformation matrix to the Cube. if (this.activeMarker) { this.cubeContainer.transform = AwayGeomUtils.convertMatrixToAwayMatrix(this.activeMarker.transformMatrix); } // update the Away3D view. this.view.render(); } } }