AR Code Samples

Libraries

Papervision 3D

FLARToolkit

Main File

package { import flash.events.Event; import flash.events.MouseEvent; import org.papervision3d.lights.PointLight3D; import org.papervision3d.objects.DisplayObject3D; import org.papervision3d.objects.parsers.DAE; import org.papervision3d.materials.shadematerials.GouraudMaterial; import org.papervision3d.lights.PointLight3D;

[SWF(width=640,height=480,frameRate=30,backgroundColor=0x0)] public class Alpha1 extends PV3DARApp { private var universe:DisplayObject3D; private var daeFile:DAE; private var pointLight:PointLight3D;

public function Alpha1 { this.init('Data/camera_para.dat', 'Data/marker16.pat'); }		protected override function onInit:void { super.onInit; daeFile = new DAE; daeFile.load("Mackenzie_logo.dae"); daeFile.z = 40; daeFile.scaleY = 1.0; daeFile.scaleZ = 1.0; daeFile.rotationX = 90; daeFile.rotationY = 0; daeFile.rotationZ = 0; /*this.pointLight = new PointLight3D(true, false); this.pointLight.z = -1500; this.pointLight.y = 1000; this.pointLight.x = 1000;*/ this.universe = new DisplayObject3D; this.universe.addChild(daeFile); this.universe.z = -20; this._baseNode.addChild(this.universe); //this.universe.addChild(pointLight); //daeFile.setChildMaterial(this.universe, gouraudMaterial); }	} }

ARAppBase

package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.PixelSnapping; import flash.display.Sprite; import flash.events.Event; import flash.events.IOErrorEvent; import flash.events.SecurityErrorEvent; import flash.media.Camera; import flash.media.Video; import flash.net.URLLoader; import flash.net.URLLoaderDataFormat; import flash.net.URLRequest; import org.libspark.flartoolkit.core.FLARCode; import org.libspark.flartoolkit.core.param.FLARParam; import org.libspark.flartoolkit.core.raster.rgb.FLARRgbRaster_BitmapData; import org.libspark.flartoolkit.detector.FLARSingleMarkerDetector; [Event(name="init",type="flash.events.Event")] [Event(name="init",type="flash.events.Event")] [Event(name="ioError",type="flash.events.IOErrorEvent")] [Event(name="securityError",type="flash.events.SecurityErrorEvent")]

public class ARAppBase extends Sprite { private var _loader:URLLoader; private var _cameraFile:String; private var _codeFile:String; private var _width:int; private var _height:int; private var _codeWidth:int; protected var _param:FLARParam; protected var _code:FLARCode; protected var _raster:FLARRgbRaster_BitmapData; protected var _detector:FLARSingleMarkerDetector; protected var _webcam:Camera; protected var _video:Video; protected var _capture:Bitmap; public function ARAppBase { }		protected function init(cameraFile:String, codeFile:String, canvasWidth:int = 320, canvasHeight:int = 240, codeWidth:int = 80):void { this._cameraFile = cameraFile; this._width = canvasWidth; this._height = canvasHeight; this._codeFile = codeFile; this._codeWidth = codeWidth; this._loader = new URLLoader; this._loader.dataFormat = URLLoaderDataFormat.BINARY; this._loader.addEventListener(Event.COMPLETE, this._onLoadParam); this._loader.addEventListener(IOErrorEvent.IO_ERROR, this.dispatchEvent); this._loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.dispatchEvent); this._loader.load(new URLRequest(this._cameraFile)); }		private function _onLoadParam(e:Event):void { this._loader.removeEventListener(Event.COMPLETE, this._onLoadParam); this._param = new FLARParam; this._param.loadARParam(this._loader.data); this._param.changeScreenSize(this._width, this._height); this._loader.dataFormat = URLLoaderDataFormat.TEXT; this._loader.addEventListener(Event.COMPLETE, this._onLoadCode); this._loader.load(new URLRequest(this._codeFile)); }		private function _onLoadCode(e:Event):void { this._code = new FLARCode(16, 16); this._code.loadARPatt(this._loader.data); this._loader.removeEventListener(Event.COMPLETE, this._onLoadCode); this._loader.removeEventListener(IOErrorEvent.IO_ERROR, this.dispatchEvent); this._loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, this.dispatchEvent); this._loader = null;

// setup webcam this._webcam = Camera.getCamera; if (!this._webcam) { throw new Error('No webcam!!!!'); }			this._webcam.setMode(this._width, this._height, 30); this._video = new Video(this._width, this._height); this._video.attachCamera(this._webcam); this._capture = new Bitmap(new BitmapData(this._width, this._height, false, 0), PixelSnapping.AUTO, true); // setup ARToolkit this._raster = new FLARRgbRaster_BitmapData(this._capture.bitmapData); this._detector = new FLARSingleMarkerDetector(this._param, this._code, this._codeWidth); this.onInit; }		protected function onInit:void { }	} }

PV3DARApp

package { import flash.display.Sprite; import flash.events.Event; import org.libspark.flartoolkit.core.transmat.FLARTransMatResult; import org.libspark.flartoolkit.pv3d.FLARBaseNode; import org.libspark.flartoolkit.pv3d.FLARCamera3D; import org.papervision3d.render.LazyRenderEngine; import org.papervision3d.scenes.Scene3D; import org.papervision3d.view.Viewport3D; import org.papervision3d.view.stats.StatsView; public class PV3DARApp extends ARAppBase { protected var _base:Sprite; protected var _viewport:Viewport3D; protected var _camera3d:FLARCamera3D; protected var _scene:Scene3D; protected var _renderer:LazyRenderEngine; protected var _baseNode:FLARBaseNode; protected var _resultMat:FLARTransMatResult = new FLARTransMatResult; public function PV3DARApp { }		protected override function onInit:void { super.onInit; this._base = this.addChild(new Sprite) as Sprite; this._capture.width = 640; this._capture.height = 480; this._base.addChild(this._capture); this._viewport = this._base.addChild(new Viewport3D(320, 240)) as Viewport3D; this._viewport.scaleX = 640 / 320; this._viewport.scaleY = 480 / 240; this._viewport.x = -4; // 4pix ??? this._camera3d = new FLARCamera3D(this._param); this._scene = new Scene3D; this._baseNode = this._scene.addChild(new FLARBaseNode) as FLARBaseNode; this._renderer = new LazyRenderEngine(this._scene, this._camera3d, this._viewport); //this.stage.addChild(new StatsView(this._renderer)); this.addEventListener(Event.ENTER_FRAME, this._onEnterFrame); }		private function _onEnterFrame(e:Event = null):void { this._capture.bitmapData.draw(this._video); if (this._detector.detectMarkerLite(this._raster, 80) && this._detector.getConfidence > 0.5) { this._detector.getTransformMatrix(this._resultMat); this._baseNode.setTransformMatrix(this._resultMat); this._baseNode.visible = true; } else { this._baseNode.visible = false; }			this._renderer.render; }		public function set mirror(value:Boolean):void { if (value) { this._base.scaleX = -1; this._base.x = 640; } else { this._base.scaleX = 1; this._base.x = 0; }		}		public function get mirror:Boolean { return this._base.scaleX < 0; }	} }