package examples { import com.transmote.flar.FLARManager; import com.transmote.flar.camera.FLARCamera_Flash3D; import com.transmote.flar.marker.FLARMarkerEvent; import com.transmote.flar.tracker.FlareManager; import com.transmote.utils.time.FramerateDisplay; import examples.support.MarkerPlane; import flash.display.Sprite; import flash.events.ErrorEvent; import flash.events.Event; import flash.geom.Rectangle; /** * FLARManagerExample_Flash3D maps a Loader object (a loaded image or animation) * to a detected marker, using Flash Player 10's 3D capabilities. * * @author Eric Socolofsky * @url http://transmote.com/flar */ public class FLARManagerExample_Flash3D extends Sprite { private var flarManager:FLARManager; private var markerPlane:MarkerPlane; private var scene:Sprite; private var camera:FLARCamera_Flash3D; public function FLARManagerExample_Flash3D () { 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 FlareManager(), this.stage); // handle any errors generated during FLARManager initialization. this.flarManager.addEventListener(ErrorEvent.ERROR, this.onFlarManagerError); // 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); // framerate display helps to keep an eye on performance. var framerateDisplay:FramerateDisplay = new FramerateDisplay(); this.addChild(framerateDisplay); this.flarManager.addEventListener(Event.INIT, this.onFlarManagerInited); } private function onFlarManagerError (evt:ErrorEvent) :void { this.flarManager.removeEventListener(ErrorEvent.ERROR, this.onFlarManagerError); this.flarManager.removeEventListener(Event.INIT, this.onFlarManagerInited); trace(evt.text); // NOTE: developers can include better feedback to the end user here if desired. } private function onFlarManagerInited (evt:Event) :void { this.scene = new Sprite(); this.addChild(this.scene); // load an image to display on top of the marker. // note, this could be any content loaded with a Loader, still or animated. this.markerPlane = new MarkerPlane("../resources/assets/saqoosha.jpg"); this.markerPlane.visible = false; this.scene.addChild(this.markerPlane); this.camera = new FLARCamera_Flash3D(this.flarManager, new Rectangle(0, 0, this.stage.stageWidth, this.stage.stageHeight)); this.camera.scene = this.scene; } private function onMarkerAdded (evt:FLARMarkerEvent) :void { //trace("["+evt.marker.patternId+"] added"); this.onMarkerUpdated(evt); } private function onMarkerUpdated (evt:FLARMarkerEvent) :void { //trace("["+evt.marker.patternId+"] updated"); if (this.markerPlane) { this.markerPlane.visible = true; this.camera.applyTransform(evt.marker.transformMatrix); } } private function onMarkerRemoved (evt:FLARMarkerEvent) :void { //trace("["+evt.marker.patternId+"] removed"); if (this.markerPlane) { this.markerPlane.visible = false; } } } }