Matrix4.js 15.7 KB
Newer Older
M
Mr.doob 已提交
1 2
/**
 * @author mr.doob / http://mrdoob.com/
M
Mr.doob 已提交
3 4
 * @author supereggbert / http://www.paulbrunt.co.uk/
 * @author philogb / http://blog.thejit.org/
5
 * @author jordi_ros / http://plattsoft.com
6
 * @author D1plo1d / http://github.com/D1plo1d
M
Mr.doob 已提交
7 8
 */

9 10 11 12 13 14
THREE.Matrix4 = function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {

	this.n11 = n11 || 1; this.n12 = n12 || 0; this.n13 = n13 || 0; this.n14 = n14 || 0;
	this.n21 = n21 || 0; this.n22 = n22 || 1; this.n23 = n23 || 0; this.n24 = n24 || 0;
	this.n31 = n31 || 0; this.n32 = n32 || 0; this.n33 = n33 || 1; this.n34 = n34 || 0;
	this.n41 = n41 || 0; this.n42 = n42 || 0; this.n43 = n43 || 0; this.n44 = n44 || 1;
15

16
};
17

18
THREE.Matrix4.prototype = {
19

20
	identity: function () {
M
Mr.doob 已提交
21

22 23 24 25
		this.n11 = 1; this.n12 = 0; this.n13 = 0; this.n14 = 0;
		this.n21 = 0; this.n22 = 1; this.n23 = 0; this.n24 = 0;
		this.n31 = 0; this.n32 = 0; this.n33 = 1; this.n34 = 0;
		this.n41 = 0; this.n42 = 0; this.n43 = 0; this.n44 = 1;
M
Mr.doob 已提交
26

27 28 29 30 31 32 33 34 35 36 37 38 39
		return this;

	},

	set: function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {

		this.n11 = n11; this.n12 = n12; this.n13 = n13; this.n14 = n14;
		this.n21 = n21; this.n22 = n22; this.n23 = n23; this.n24 = n24;
		this.n31 = n31; this.n32 = n32; this.n33 = n33; this.n34 = n34;
		this.n41 = n41; this.n42 = n42; this.n43 = n43; this.n44 = n44;

		return this;

40
	},
M
Mr.doob 已提交
41

M
Mr.doob 已提交
42 43 44 45 46 47 48
	copy: function ( m ) {

		this.n11 = m.n11; this.n12 = m.n12; this.n13 = m.n13; this.n14 = m.n14;
		this.n21 = m.n21; this.n22 = m.n22; this.n23 = m.n23; this.n24 = m.n24;
		this.n31 = m.n31; this.n32 = m.n32; this.n33 = m.n33; this.n34 = m.n34;
		this.n41 = m.n41; this.n42 = m.n42; this.n43 = m.n43; this.n44 = m.n44;

49 50
		return this;

M
Mr.doob 已提交
51 52
	},

53
	lookAt: function ( eye, center, up ) {
M
Mr.doob 已提交
54

A
alteredq 已提交
55
		var x = THREE.Matrix4.__tmpVec1, y = THREE.Matrix4.__tmpVec2, z = THREE.Matrix4.__tmpVec3;
M
Mr.doob 已提交
56

M
Mr.doob 已提交
57 58 59
		z.sub( eye, center ).normalize();
		x.cross( up, z ).normalize();
		y.cross( z, x ).normalize();
M
Mr.doob 已提交
60

61 62 63
		this.n11 = x.x; this.n12 = x.y; this.n13 = x.z; this.n14 = - x.dot( eye );
		this.n21 = y.x; this.n22 = y.y; this.n23 = y.z; this.n24 = - y.dot( eye );
		this.n31 = z.x; this.n32 = z.y; this.n33 = z.z; this.n34 = - z.dot( eye );
M
Mr.doob 已提交
64 65
		this.n41 = 0; this.n42 = 0; this.n43 = 0; this.n44 = 1;

66 67
		return this;

68
	},
69

70
	multiplyVector3: function ( v ) {
71

72 73
		var vx = v.x, vy = v.y, vz = v.z,
		d = 1 / ( this.n41 * vx + this.n42 * vy + this.n43 * vz + this.n44 );
74

75 76 77
		v.x = ( this.n11 * vx + this.n12 * vy + this.n13 * vz + this.n14 ) * d;
		v.y = ( this.n21 * vx + this.n22 * vy + this.n23 * vz + this.n24 ) * d;
		v.z = ( this.n31 * vx + this.n32 * vy + this.n33 * vz + this.n34 ) * d;
78

79 80
		return v;

M
Mr.doob 已提交
81
	},
82

83
	multiplyVector4: function ( v ) {
84

M
Mr.doob 已提交
85
		var vx = v.x, vy = v.y, vz = v.z, vw = v.w;
M
Mr.doob 已提交
86

M
Mr.doob 已提交
87 88 89 90
		v.x = this.n11 * vx + this.n12 * vy + this.n13 * vz + this.n14 * vw;
		v.y = this.n21 * vx + this.n22 * vy + this.n23 * vz + this.n24 * vw;
		v.z = this.n31 * vx + this.n32 * vy + this.n33 * vz + this.n34 * vw;
		v.w = this.n41 * vx + this.n42 * vy + this.n43 * vz + this.n44 * vw;
M
Mr.doob 已提交
91

92 93
		return v;

94
	},
95

96
	crossVector: function ( a ) {
M
Mr.doob 已提交
97

98
		var v = new THREE.Vector4();
M
Mr.doob 已提交
99

100 101 102
		v.x = this.n11 * a.x + this.n12 * a.y + this.n13 * a.z + this.n14 * a.w;
		v.y = this.n21 * a.x + this.n22 * a.y + this.n23 * a.z + this.n24 * a.w;
		v.z = this.n31 * a.x + this.n32 * a.y + this.n33 * a.z + this.n34 * a.w;
M
Mr.doob 已提交
103

104
		v.w = ( a.w ) ? this.n41 * a.x + this.n42 * a.y + this.n43 * a.z + this.n44 * a.w : 1;
105

106
		return v;
M
Mr.doob 已提交
107

108
	},
109

110
	multiply: function ( a, b ) {
M
Mr.doob 已提交
111

M
Mr.doob 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
		var a11 = a.n11, a12 = a.n12, a13 = a.n13, a14 = a.n14,
		a21 = a.n21, a22 = a.n22, a23 = a.n23, a24 = a.n24,
		a31 = a.n31, a32 = a.n32, a33 = a.n33, a34 = a.n34,
		a41 = a.n41, a42 = a.n42, a43 = a.n43, a44 = a.n44,

		b11 = b.n11, b12 = b.n12, b13 = b.n13, b14 = b.n14,
		b21 = b.n21, b22 = b.n22, b23 = b.n23, b24 = b.n24,
		b31 = b.n31, b32 = b.n32, b33 = b.n33, b34 = b.n34,
		b41 = b.n41, b42 = b.n42, b43 = b.n43, b44 = b.n44;

		this.n11 = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
		this.n12 = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
		this.n13 = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
		this.n14 = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;

		this.n21 = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
		this.n22 = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
		this.n23 = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
		this.n24 = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;

		this.n31 = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
		this.n32 = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
		this.n33 = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
		this.n34 = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;

		this.n41 = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
		this.n42 = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
		this.n43 = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
		this.n44 = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
141

142 143
		return this;

144
	},
M
Mr.doob 已提交
145

146
	multiplySelf: function ( m ) {
147

148 149 150
		var n11 = this.n11, n12 = this.n12, n13 = this.n13, n14 = this.n14,
		n21 = this.n21, n22 = this.n22, n23 = this.n23, n24 = this.n24,
		n31 = this.n31, n32 = this.n32, n33 = this.n33, n34 = this.n34,
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
		n41 = this.n41, n42 = this.n42, n43 = this.n43, n44 = this.n44,
		mn11 = m.n11, mn21 = m.n21, mn31 = m.n31, mn41 = m.n41,
		mn12 = m.n12, mn22 = m.n22, mn32 = m.n32, mn42 = m.n42,
		mn13 = m.n13, mn23 = m.n23, mn33 = m.n33, mn43 = m.n43,
		mn14 = m.n14, mn24 = m.n24, mn34 = m.n34, mn44 = m.n44;

		this.n11 = n11 * mn11 + n12 * mn21 + n13 * mn31 + n14 * mn41;
		this.n12 = n11 * mn12 + n12 * mn22 + n13 * mn32 + n14 * mn42;
		this.n13 = n11 * mn13 + n12 * mn23 + n13 * mn33 + n14 * mn43;
		this.n14 = n11 * mn14 + n12 * mn24 + n13 * mn34 + n14 * mn44;

		this.n21 = n21 * mn11 + n22 * mn21 + n23 * mn31 + n24 * mn41;
		this.n22 = n21 * mn12 + n22 * mn22 + n23 * mn32 + n24 * mn42;
		this.n23 = n21 * mn13 + n22 * mn23 + n23 * mn33 + n24 * mn43;
		this.n24 = n21 * mn14 + n22 * mn24 + n23 * mn34 + n24 * mn44;

		this.n31 = n31 * mn11 + n32 * mn21 + n33 * mn31 + n34 * mn41;
		this.n32 = n31 * mn12 + n32 * mn22 + n33 * mn32 + n34 * mn42;
		this.n33 = n31 * mn13 + n32 * mn23 + n33 * mn33 + n34 * mn43;
		this.n34 = n31 * mn14 + n32 * mn24 + n33 * mn34 + n34 * mn44;

		this.n41 = n41 * mn11 + n42 * mn21 + n43 * mn31 + n44 * mn41;
		this.n42 = n41 * mn12 + n42 * mn22 + n43 * mn32 + n44 * mn42;
		this.n43 = n41 * mn13 + n42 * mn23 + n43 * mn33 + n44 * mn43;
		this.n44 = n41 * mn14 + n42 * mn24 + n43 * mn34 + n44 * mn44;
176

177 178
		return this;

179 180
	},

181 182 183 184 185 186 187
	multiplyScalar: function ( s ) {

		this.n11 *= s; this.n12 *= s; this.n13 *= s; this.n14 *= s;
		this.n21 *= s; this.n22 *= s; this.n23 *= s; this.n24 *= s;
		this.n31 *= s; this.n32 *= s; this.n33 *= s; this.n34 *= s;
		this.n41 *= s; this.n42 *= s; this.n43 *= s; this.n44 *= s;

188 189
		return this;

190 191 192 193
	},

	determinant: function () {

194 195 196 197 198
		var n11 = this.n11, n12 = this.n12, n13 = this.n13, n14 = this.n14,
		n21 = this.n21, n22 = this.n22, n23 = this.n23, n24 = this.n24,
		n31 = this.n31, n32 = this.n32, n33 = this.n33, n34 = this.n34,
		n41 = this.n41, n42 = this.n42, n43 = this.n43, n44 = this.n44;

199 200 201
		//TODO: make this more efficient
		//( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm )
		return (
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
			n14 * n23 * n32 * n41-
			n13 * n24 * n32 * n41-
			n14 * n22 * n33 * n41+
			n12 * n24 * n33 * n41+

			n13 * n22 * n34 * n41-
			n12 * n23 * n34 * n41-
			n14 * n23 * n31 * n42+
			n13 * n24 * n31 * n42+

			n14 * n21 * n33 * n42-
			n11 * n24 * n33 * n42-
			n13 * n21 * n34 * n42+
			n11 * n23 * n34 * n42+

			n14 * n22 * n31 * n43-
			n12 * n24 * n31 * n43-
			n14 * n21 * n32 * n43+
			n11 * n24 * n32 * n43+

			n12 * n21 * n34 * n43-
			n11 * n22 * n34 * n43-
			n13 * n22 * n31 * n44+
			n12 * n23 * n31 * n44+

			n13 * n21 * n32 * n44-
			n11 * n23 * n32 * n44-
			n12 * n21 * n33 * n44+
			n11 * n22 * n33 * n44 );
231 232

	},
M
Mr.doob 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254

	transpose: function () {

		function swap( obj, p1, p2 ) {

			var aux = obj[ p1 ];
			obj[ p1 ] = obj[ p2 ];
			obj[ p2 ] = aux;

		}

		swap( this, 'n21', 'n12' );
		swap( this, 'n31', 'n13' );
		swap( this, 'n32', 'n23' );
		swap( this, 'n41', 'n14' );
		swap( this, 'n42', 'n24' );
		swap( this, 'n43', 'n34' );

		return this;

	},

255 256 257
	clone: function () {

		var m = new THREE.Matrix4();
258

259 260 261 262
		m.n11 = this.n11; m.n12 = this.n12; m.n13 = this.n13; m.n14 = this.n14;
		m.n21 = this.n21; m.n22 = this.n22; m.n23 = this.n23; m.n24 = this.n24;
		m.n31 = this.n31; m.n32 = this.n32; m.n33 = this.n33; m.n34 = this.n34;
		m.n41 = this.n41; m.n42 = this.n42; m.n43 = this.n43; m.n44 = this.n44;
263

264 265 266
		return m;

	},
M
Mr.doob 已提交
267

268 269
	flatten: function() {

270 271 272 273 274 275
                var flat = THREE.Matrix4.__flat;

		flat[ 0 ] = this.n11;
		flat[ 1 ] = this.n21;
		flat[ 2 ] = this.n31;
		flat[ 3 ] = this.n41;
A
alteredq 已提交
276

277 278 279 280
		flat[ 4 ] = this.n12;
		flat[ 5 ] = this.n22;
		flat[ 6 ] = this.n32;
		flat[ 7 ] = this.n42;
A
alteredq 已提交
281

282 283 284 285
		flat[ 8 ]  = this.n13;
		flat[ 9 ]  = this.n23;
		flat[ 10 ] = this.n33;
		flat[ 11 ] = this.n43;
A
alteredq 已提交
286

287 288 289 290
		flat[ 12 ] = this.n14;
		flat[ 13 ] = this.n24;
		flat[ 14 ] = this.n34;
		flat[ 15 ] = this.n44;
291

292
		return flat;
M
Mr.doob 已提交
293

294
	},
295

296
	setTranslation: function( x, y, z ) {
297

298 299 300 301 302 303
		this.set( 1, 0, 0, x,
				  0, 1, 0, y,
				  0, 0, 1, z,
				  0, 0, 0, 1 );
		 
		return this;
304

305
	},
306 307

	setScale: function( x, y, z ) {
308

309 310 311 312 313 314
		this.set( x, 0, 0, 0,
				  0, y, 0, 0,
				  0, 0, z, 0,
				  0, 0, 0, 1 );
		
		return this;
315

316
	},
317 318

	setRotX: function( theta ) {
319 320

		var c = Math.cos( theta ), s = Math.sin( theta );
321

322 323 324 325
		this.set( 1, 0,  0, 0,
				  0, c, -s, 0,
				  0, s,  c, 0,
				  0, 0,  0, 1 );
326

327
		return this;
328

329
	},
330 331

	setRotY: function( theta ) {
332 333 334 335 336 337 338

		var c = Math.cos( theta ), s = Math.sin( theta );

		this.set( c, 0, s, 0,
				  0, 1, 0, 0,
				 -s, 0, c, 0,
				  0, 0, 0, 1 );
339

340
		return this;
341

342
	},
343 344

	setRotZ: function( theta ) {
345 346

		var c = Math.cos( theta ), s = Math.sin( theta );
347

348 349 350 351 352 353
		this.set( c, -s, 0, 0,
				  s,  c, 0, 0,
				  0,  0, 1, 0,
				  0,  0, 0, 1 );
		
		return this;
354

355
	},
356 357

	setRotAxis: function( axis, angle ) {
358

359
		//Based on http://www.gamedev.net/reference/articles/article1199.asp
360

361 362 363 364 365
		var c = Math.cos( angle ),
			s = Math.sin( angle ),
			t = 1 - c,
			x = axis.x, y = axis.y, z = axis.z,
			tx = t * x, ty = t * y;
366 367

		 this.set( tx * x + c, tx * y - s * z, tx * z + s * y, 0,
368 369 370
				  tx * y + s * z, ty * y + c, ty * z - s * x, 0,
				  tx * z - s * y, ty * z + s * x, t * z * z + c, 0,
				  0, 0, 0, 1 );
371

372 373
		 return this;

374
	},
375

376 377
	toString: function() {

M
Mr.doob 已提交
378 379 380 381
		return  "| " + this.n11 + " " + this.n12 + " " + this.n13 + " " + this.n14 + " |\n" +
			"| " + this.n21 + " " + this.n22 + " " + this.n23 + " " + this.n24 + " |\n" +
			"| " + this.n31 + " " + this.n32 + " " + this.n33 + " " + this.n34 + " |\n" +
			"| " + this.n41 + " " + this.n42 + " " + this.n43 + " " + this.n44 + " |";
382 383

	}
384

M
Mr.doob 已提交
385
};
M
Mr.doob 已提交
386

387
THREE.Matrix4.translationMatrix = function ( x, y, z ) {
M
Mr.doob 已提交
388

389
	var m = new THREE.Matrix4();
M
Mr.doob 已提交
390

391
	m.setTranslation( x, y, z );
M
Mr.doob 已提交
392

393
	return m;
394

M
Mr.doob 已提交
395
};
M
Mr.doob 已提交
396

397
THREE.Matrix4.scaleMatrix = function ( x, y, z ) {
M
Mr.doob 已提交
398

399
	var m = new THREE.Matrix4();
M
Mr.doob 已提交
400

401
	m.setScale( x, y, z );
M
Mr.doob 已提交
402

403
	return m;
404

M
Mr.doob 已提交
405
};
M
Mr.doob 已提交
406

407
THREE.Matrix4.rotationXMatrix = function ( theta ) {
M
Mr.doob 已提交
408

409
	var m = new THREE.Matrix4();
M
Mr.doob 已提交
410

411
	m.setRotX( theta );
M
Mr.doob 已提交
412

413
	return m;
414

M
Mr.doob 已提交
415
};
M
Mr.doob 已提交
416

417
THREE.Matrix4.rotationYMatrix = function ( theta ) {
M
Mr.doob 已提交
418

419
	var m = new THREE.Matrix4();
M
Mr.doob 已提交
420

421
	m.setRotY( theta );
M
Mr.doob 已提交
422

423
	return m;
424

M
Mr.doob 已提交
425
};
M
Mr.doob 已提交
426

427
THREE.Matrix4.rotationZMatrix = function ( theta ) {
M
Mr.doob 已提交
428

429
	var m = new THREE.Matrix4();
M
Mr.doob 已提交
430

431
	m.setRotZ( theta );
M
Mr.doob 已提交
432

433
	return m;
434

M
Mr.doob 已提交
435
};
M
Mr.doob 已提交
436

P
philogb 已提交
437 438
THREE.Matrix4.rotationAxisAngleMatrix = function ( axis, angle ) {

439
	var m = new THREE.Matrix4();
440

441
	m.setRotAxis( axis, angle );
M
Mr.doob 已提交
442

443
	return m;
P
philogb 已提交
444 445 446

};

447 448
THREE.Matrix4.makeInvert = function ( m1 ) {

449 450 451 452
	var n11 = m1.n11, n12 = m1.n12, n13 = m1.n13, n14 = m1.n14,
		n21 = m1.n21, n22 = m1.n22, n23 = m1.n23, n24 = m1.n24,
		n31 = m1.n31, n32 = m1.n32, n33 = m1.n33, n34 = m1.n34,
		n41 = m1.n41, n42 = m1.n42, n43 = m1.n43, n44 = m1.n44;
453

454 455 456 457
	//TODO: make this more efficient
	//( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm )
	var m2 = new THREE.Matrix4();

458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
	m2.n11 = n23*n34*n42 - n24*n33*n42 + n24*n32*n43 - n22*n34*n43 - n23*n32*n44 + n22*n33*n44;
	m2.n12 = n14*n33*n42 - n13*n34*n42 - n14*n32*n43 + n12*n34*n43 + n13*n32*n44 - n12*n33*n44;
	m2.n13 = n13*n24*n42 - n14*n23*n42 + n14*n22*n43 - n12*n24*n43 - n13*n22*n44 + n12*n23*n44;
	m2.n14 = n14*n23*n32 - n13*n24*n32 - n14*n22*n33 + n12*n24*n33 + n13*n22*n34 - n12*n23*n34;
	m2.n21 = n24*n33*n41 - n23*n34*n41 - n24*n31*n43 + n21*n34*n43 + n23*n31*n44 - n21*n33*n44;
	m2.n22 = n13*n34*n41 - n14*n33*n41 + n14*n31*n43 - n11*n34*n43 - n13*n31*n44 + n11*n33*n44;
	m2.n23 = n14*n23*n41 - n13*n24*n41 - n14*n21*n43 + n11*n24*n43 + n13*n21*n44 - n11*n23*n44;
	m2.n24 = n13*n24*n31 - n14*n23*n31 + n14*n21*n33 - n11*n24*n33 - n13*n21*n34 + n11*n23*n34;
	m2.n31 = n22*n34*n41 - n24*n32*n41 + n24*n31*n42 - n21*n34*n42 - n22*n31*n44 + n21*n32*n44;
	m2.n32 = n14*n32*n41 - n12*n34*n41 - n14*n31*n42 + n11*n34*n42 + n12*n31*n44 - n11*n32*n44;
	m2.n33 = n13*n24*n41 - n14*n22*n41 + n14*n21*n42 - n11*n24*n42 - n12*n21*n44 + n11*n22*n44;
	m2.n34 = n14*n22*n31 - n12*n24*n31 - n14*n21*n32 + n11*n24*n32 + n12*n21*n34 - n11*n22*n34;
	m2.n41 = n23*n32*n41 - n22*n33*n41 - n23*n31*n42 + n21*n33*n42 + n22*n31*n43 - n21*n32*n43;
	m2.n42 = n12*n33*n41 - n13*n32*n41 + n13*n31*n42 - n11*n33*n42 - n12*n31*n43 + n11*n32*n43;
	m2.n43 = n13*n22*n41 - n12*n23*n41 - n13*n21*n42 + n11*n23*n42 + n12*n21*n43 - n11*n22*n43;
	m2.n44 = n12*n23*n31 - n13*n22*n31 + n13*n21*n32 - n11*n23*n32 - n12*n21*n33 + n11*n22*n33;
474
	m2.multiplyScalar( 1 / m1.determinant() );
475 476 477 478 479

	return m2;

};

480
THREE.Matrix4.makeInvert3x3 = function ( m1, m2 ) {
481

482
	// input:  m1: THREE.Matrix4, m2: THREE.Matrix3; output: THREE.Matrix3 (m2)
483 484
	// ( based on http://code.google.com/p/webgl-mjs/ )

485 486
        var m = m1.flatten(),
	m2m = m2.m,
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504

	a11 = m[ 10 ] * m[ 5 ] - m[ 6 ] * m[ 9 ],
	a21 = - m[ 10 ] * m[ 1 ] + m[ 2 ] * m[ 9 ],
	a31 = m[ 6 ] * m[ 1 ] - m[ 2 ] * m[ 5 ],
	a12 = - m[ 10 ] * m[ 4 ] + m[ 6 ] * m[ 8 ],
	a22 = m[ 10 ] * m[ 0 ] - m[ 2 ] * m[ 8 ],
	a32 = - m[ 6 ] * m[ 0 ] + m[ 2 ] * m[ 4 ],
	a13 = m[ 9 ] * m[ 4 ] - m[ 5 ] * m[ 8 ],
	a23 = - m[ 9 ] * m[ 0 ] + m[ 1 ] * m[ 8 ],
	a33 = m[ 5 ] * m[ 0 ] - m[ 1 ] * m[ 4 ],
	det = m[ 0 ] * ( a11 ) + m[ 1 ] * ( a12 ) + m[ 2 ] * ( a13 ),
	idet;

	// no inverse
	if (det == 0) throw "matrix not invertible";

	idet = 1.0 / det;

505 506 507
	m2m[ 0 ] = idet * a11; m2m[ 1 ] = idet * a21; m2m[ 2 ] = idet * a31;
	m2m[ 3 ] = idet * a12; m2m[ 4 ] = idet * a22; m2m[ 5 ] = idet * a32;
	m2m[ 6 ] = idet * a13; m2m[ 7 ] = idet * a23; m2m[ 8 ] = idet * a33;
508 509 510

	return m2;

511 512
}

513
THREE.Matrix4.makeFrustum = function ( left, right, bottom, top, near, far ) {
M
Mr.doob 已提交
514

515
	var m, x, y, a, b, c, d;
M
Mr.doob 已提交
516

517 518 519 520 521 522 523
	m = new THREE.Matrix4();
	x = 2 * near / ( right - left );
	y = 2 * near / ( top - bottom );
	a = ( right + left ) / ( right - left );
	b = ( top + bottom ) / ( top - bottom );
	c = - ( far + near ) / ( far - near );
	d = - 2 * far * near / ( far - near );
M
Mr.doob 已提交
524

525 526 527 528
	m.n11 = x;  m.n12 = 0;  m.n13 = a;   m.n14 = 0;
	m.n21 = 0;  m.n22 = y;  m.n23 = b;   m.n24 = 0;
	m.n31 = 0;  m.n32 = 0;  m.n33 = c;   m.n34 = d;
	m.n41 = 0;  m.n42 = 0;  m.n43 = - 1; m.n44 = 0;
M
Mr.doob 已提交
529

530
	return m;
531

M
Mr.doob 已提交
532
};
M
Mr.doob 已提交
533

534
THREE.Matrix4.makePerspective = function ( fov, aspect, near, far ) {
M
Mr.doob 已提交
535

536 537 538 539 540 541 542 543 544 545 546
	var ymax, ymin, xmin, xmax;

	ymax = near * Math.tan( fov * Math.PI / 360 );
	ymin = - ymax;
	xmin = ymin * aspect;
	xmax = ymax * aspect;

	return THREE.Matrix4.makeFrustum( xmin, xmax, ymin, ymax, near, far );

};

547
THREE.Matrix4.makeOrtho = function ( left, right, top, bottom, near, far ) {
548 549 550 551 552

	var m, x, y, z, w, h, p;

	m = new THREE.Matrix4();
	w = right - left;
553
	h = top - bottom;
554 555
	p = far - near;
	x = ( right + left ) / w;
556
	y = ( top + bottom ) / h;
557
	z = ( far + near ) / p;
558

559 560 561 562
	m.n11 = 2 / w; m.n12 = 0;     m.n13 = 0;      m.n14 = -x;
	m.n21 = 0;     m.n22 = 2 / h; m.n23 = 0;      m.n24 = -y;
	m.n31 = 0;     m.n32 = 0;     m.n33 = -2 / p; m.n34 = -z;
	m.n41 = 0;     m.n42 = 0;     m.n43 = 0;      m.n44 = 1;
563

564
	return m;
565

M
Mr.doob 已提交
566
};
A
alteredq 已提交
567 568 569 570

THREE.Matrix4.__tmpVec1 = new THREE.Vector3();
THREE.Matrix4.__tmpVec2 = new THREE.Vector3();
THREE.Matrix4.__tmpVec3 = new THREE.Vector3();
571
THREE.Matrix4.__flat = new Array( 16 );