In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

FLARToolKit use DisplayObject#transform test

from flartoolkit SimpleCube example
print this marker: http://saqoosha.net/lab/FLARToolKit/flarlogo-marker.pdf
DisplayObject#transform使ってFLARのテスト
/**
 * Copyright nutsu ( http://wonderfl.net/user/nutsu )
 * GNU General Public License, v3 ( http://www.gnu.org/licenses/quick-guide-gplv3.html )
 * Downloaded from: http://wonderfl.net/c/ztdH
 */

// forked from nutsu's FLARToolKit use flash.geom package test
// forked from mash's FLARToolKit SimpleCube sample
// from flartoolkit SimpleCube example
// print this marker: http://saqoosha.net/lab/FLARToolKit/flarlogo-marker.pdf
// DisplayObject#transform使ってFLARのテスト
package  
{
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.*;
	import org.libspark.flartoolkit.core.FLARMat;
	import org.libspark.flartoolkit.core.param.FLARParam;
	import org.libspark.flartoolkit.core.transmat.FLARTransMatResult;
	import org.libspark.flartoolkit.core.types.FLARIntSize;
	import org.libspark.flartoolkit.example.ARAppBase;
	
	[SWF( width=640, height=480, frameRate=30, backgroundColor=0 )]
	public class ARTest extends ARAppBase
	{
		private var _base:Sprite;
		private var _container:Sprite;
		private var viewScale:Number = 2;
		
		//
		private var _rmat:FLARTransMatResult = new FLARTransMatResult();
		private var _de_mat:Matrix3D;
		private var _sp_mat:Matrix3D;
		private var _focial_length:Number;
		private var _p_scaleY:Number;
		
		public function ARTest() 
		{
			Wonderfl.capture_delay( 5 );
			addEventListener(Event.INIT, _onInit);
            init('http://assets.wonderfl.net/static/flar/camera_para.dat', 'http://assets.wonderfl.net/static/flar/flarlogo.pat');
		}
		
		private function _onInit(e:Event):void 
		{
			addChild( _base = new Sprite() );
			
			//
			_capture.width = 320*viewScale;
			_capture.height = 240*viewScale;
			_base.addChild(_capture);
			//
			//display container for ar matrix.
			_base.addChild( _container = new Sprite()  );
			
			//
			_sp_mat = new Matrix3D();
			_container.transform.matrix3D = _sp_mat;
			var p:PerspectiveProjection   = new PerspectiveProjection();
			p.projectionCenter = new Point(_capture.width/2, _capture.height/2);
			_container.transform.perspectiveProjection = p;
			
			//
			_de_mat = new Matrix3D();
			initCamera( this._param );
			
			//
			initARObjects();
			
			//
			stage.addEventListener(Event.ENTER_FRAME, _onEnterFrame);
        }
		
		private function initARObjects():void
		{
            var plane:Shape = new Shape();
			plane.graphics.lineStyle( 2, 0xffffff );
			plane.graphics.beginFill( 0xff0000, 0.5 );
			plane.graphics.drawRect( -40, -40, 80, 80 );
			plane.graphics.endFill();
			//x
			plane.graphics.lineStyle( 2, 0x0000ff );
			plane.graphics.moveTo( 0, 0 );
			plane.graphics.lineTo( 40, 0 );
			//y
			plane.graphics.lineStyle( 2, 0xff0000 );
			plane.graphics.moveTo( 0, 0 );
			plane.graphics.lineTo( 0, 40 );
			
			var zax:Shape = new Shape();
			zax.graphics.lineStyle( 2, 0x00ff00 );
			zax.graphics.moveTo( 0, 0 );
			zax.graphics.lineTo( 0, 40 );
			zax.rotationX = 90;
			
			_container.addChild( plane );
			_container.addChild( zax );
		}
			
		private function _onEnterFrame(e:Event):void 
		{
			_capture.bitmapData.draw(_video);
			var detected:Boolean = false;
			try {
				detected = _detector.detectMarkerLite(_raster, 80) && _detector.getConfidence() > 0.5;
			} catch (e:Error) {}
			
			if (detected) {
				_detector.getTransformMatrix(_rmat);
				_de_mat.rawData = Vector.<Number>(
					[  _rmat.m00,   _rmat.m10,   _rmat.m20, 0,
					   _rmat.m01,   _rmat.m11,   _rmat.m21, 0,
					   _rmat.m02,   _rmat.m12,   _rmat.m22, 0,
					   _rmat.m03,   _rmat.m13,   _rmat.m23, 1 ]
				);
				_sp_mat.rawData = Vector.<Number>([0,1,0,0, 1,0,0,0, 0,0,1,0, 0,0,0,1]);
				_sp_mat.append( _de_mat );
				_sp_mat.appendScale( 1, _p_scaleY , 1 );
				_sp_mat.appendTranslation(
					_container.transform.perspectiveProjection.projectionCenter.x,
					_container.transform.perspectiveProjection.projectionCenter.y,
					-_container.transform.perspectiveProjection.focalLength );
				
				_container.visible = true;
			} else {
				_container.visible = false;
			}
		}
		
		//無駄あったから整理した
		private function initCamera( param:FLARParam ):void
		{
			var  size:FLARIntSize = param.getScreenSize();
			var  width:int  = size.w;
			var  height:int = size.h;
			var  tMat:FLARMat = new FLARMat (3, 4);
			var  iMat:FLARMat = new FLARMat (3, 4);
			param.getPerspectiveProjectionMatrix().decompMat(iMat, tMat);
			var icpara:Array = iMat.getArray();
			for (var i:int = 0; i < 4; i++) {
				icpara[1][i] = (height - 1) * (icpara[2][i]) - icpara[1][i];
			}
			var w:Number = icpara[0][0] / icpara[2][2];
			var h:Number = -icpara[1][1] / icpara[2][2];
			_p_scaleY = h / w;
			_container.transform.perspectiveProjection.focalLength = viewScale * w;
		}
	}
}