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

FastMatrix

Get Adobe Flash player
by lizhi 28 Apr 2015
    Embed
/**
 * Copyright lizhi ( http://wonderfl.net/user/lizhi )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/je3k
 */

package 
{
	import flash.display.Sprite;
	import flash.geom.Matrix3D;
	import flash.geom.Utils3D;
	import flash.geom.Vector3D;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.getTimer;
	/**
	 * ...
	 * @author lizhi
	 */
	public class Matrix3DTest extends Sprite
	{
		private var label:TextField;
		public function Matrix3DTest() 
		{
			label = new TextField;
			addChild(label);
			label.autoSize = "left";
			label.defaultTextFormat = new TextFormat("宋体");
			
			var matr:Matrix3D = new Matrix3D;
			var matr2:Matrix3D = new Matrix3D;
			var rawData:Vector.<Number> = matr.rawData;
			matr.copyRawDataTo(rawData, 0, true);
			var fmatr:FastMatrix = FastMatrix.newMatrixFromRawData(rawData);
			var fmatr2:FastMatrix = FastMatrix.newMatrixFromRawData(rawData);
			
			var pos:Vector3D = new Vector3D(1, 2, 3,1);
			var c:int = 1000000;
			var time:int = getTimer();
			for (var i:int = 0; i < c; i++) 
			{
				var pos2:Vector3D = Utils3D.projectVector(matr, pos);
			}
			trace2("flash,project",getTimer() - time);
			
			
			time = getTimer();
			for (var j:int = 0; j < c; j++) 
			{
				pos2 = fmatr.projectVector(pos, pos2);
			}
			trace2("fast.project",getTimer() - time);
			
			time = getTimer();
			for (var k:int = 0; k < c; k++) 
			{
				matr.append(matr2);
			}
			trace2("flash.appent", getTimer() - time);
			
			time = getTimer();
			for (var l:int = 0; l < c; l++) 
			{
				fmatr.append(fmatr2);
			}
			trace2("fast.appent", getTimer() - time);
		}
		
		private function trace2(...args):void {
			label.appendText(args + "\n");
		}
	}

}
import flash.geom.Vector3D;

class FastMatrix {
	private static var V0:Number;
	private static var V1:Number;
	private static var V2:Number;
	private static var V3:Number;
	private static var V4:Number;
	private static var V5:Number;
	private static var V6:Number;
	private static var V7:Number;
	private static var V8:Number;
	private static var V9:Number;
	private static var V10:Number;
	private static var V11:Number;
	private static var V12:Number;
	private static var V13:Number;
	private static var V14:Number;
	private static var V15:Number;
	
	public var v0:Number;
	public var v1:Number;
	public var v2:Number;
	public var v3:Number;
	public var v4:Number;
	public var v5:Number;
	public var v6:Number;
	public var v7:Number;
	public var v8:Number;
	public var v9:Number;
	public var v10:Number;
	public var v11:Number;
	public var v12:Number;
	public var v13:Number;
	public var v14:Number;
	public var v15:Number;
	
	public static function newMatrixFromRawData(r:Vector.<Number>,transpose:Boolean=false):FastMatrix {
		var m:FastMatrix = new FastMatrix;
		m.fromRawData(r,transpose);
		return m;
	}
	
	public function append(m:FastMatrix):void {
		V0 = v0;
		V1 = v1;
		V2 = v2;
		V3 = v3;
		V4 = v4;
		V5 = v5;
		V6 = v6;
		V7 = v7;
		V8 = v8;
		V9 = v9;
		V10 = v10;
		V11 = v11;
		V12 = v12;
		V13 = v13;
		V14 = v14;
		V15 = v15;
		
		v0 = V0 * m.v0+ V4* m.v1+ V8* m.v2+ V12* m.v3;
		v1 = V1* m.v0+ V5* m.v1+ V9* m.v2+ V13* m.v3;
		v2 = V2* m.v0+ V6* m.v1+ V10* m.v2+ V14* m.v3;
		v3 = V3* m.v0+ V7* m.v1+ V11* m.v2+ V15* m.v3;
		
		v4 = V0 * m.v4+ V4* m.v5 + V8* m.v6 + V12* m.v7;
		v5 = V1* m.v4+ V5* m.v5 + V9* m.v6 + V13* m.v7;
		v6 = V2* m.v4+ V6* m.v5 + V10* m.v6 + V13* m.v7;
		v7 = V3* m.v4+ V7* m.v5 + V11* m.v6 + V15* m.v7;
		
		v8 = V0 * m.v8 + V4* m.v9 + V8* m.v10 + V12* m.v11;
		v9 = V1* m.v8 + V5* m.v9 + V9* m.v10 + V13* m.v11;
		v10 = V2* m.v8 + V6* m.v9 + V10* m.v10 + V14* m.v11;
		v11 = V3* m.v8 + V7* m.v9 + V11* m.v10 + V15* m.v11;
		
		v12 = V0 * m.v12 + V4* m.v13 + V8* m.v14 + V12* m.v15;
		v13 = V1* m.v12 + V5* m.v13 + V9* m.v14 + V13* m.v15;
		v14 = V2* m.v12 + V6* m.v13 + V10* m.v14 + V14* m.v15;
		v15 = V3* m.v12 + V7* m.v13 + V11* m.v14 + V15* m.v15;
	}
	
	public function projectVector(pos:Vector3D, target:Vector3D):Vector3D {
		var x:Number = pos.x;
		var y:Number = pos.y;
		var z:Number = pos.z;
		var w:Number = pos.w;
		var pw:Number = v12 * x + v13 * y + v14 * z + v15 * w;
		target.x = (v0 * x + v1 * y + v2 * z + v3 * w) / pw;
		target.y = (v4 * x + v5 * y + v6 * z + v7 * w) / pw;
		target.z = (v8 * x + v9 * y + v10 * z + v11 * w) / pw;
		target.w = pw;
		return target;
	}
	
	public function fromRawData(r:Vector.<Number>,transpose:Boolean=false):void {
		v0 = r[0];
		v1 = r[1];
		v2 = r[2];
		v3 = r[3];
		v4 = r[4];
		v5 = r[5];
		v6 = r[6];
		v7 = r[7];
		v8 = r[8];
		v9 = r[9];
		v10 = r[10];
		v11 = r[11];
		v12 = r[12];
		v13 = r[13];
		v14 = r[14];
		v15 = r[15];
		if (transpose) {
			
		}
	}
}