Matrix4.js 17.1 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
7 8
 * @author alteredq / http://alteredqualia.com/
 * @author mikael emtinger / http://gomo.se/
M
Mr.doob 已提交
9 10
 */

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

M
Mr.doob 已提交
13 14 15 16 17 18 19 20
	this.set(

		n11 || 1, n12 || 0, n13 || 0, n14 || 0,
		n21 || 0, n22 || 1, n23 || 0, n24 || 0,
		n31 || 0, n32 || 0, n33 || 1, n34 || 0,
		n41 || 0, n42 || 0, n43 || 0, n44 || 1

	);
21

22 23
	this.flat = new Array( 16 );
	this.m33 = new THREE.Matrix3();
24

25
};
26

27
THREE.Matrix4.prototype = {
28

29
	set : function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
M
Mr.doob 已提交
30

M
Mr.doob 已提交
31 32 33 34
		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;
M
Mr.doob 已提交
35

36 37 38 39
		return this;

	},

40
	identity : function () {
41

M
Mr.doob 已提交
42 43 44 45 46 47 48 49
		this.set(

			1, 0, 0, 0,
			0, 1, 0, 0,
			0, 0, 1, 0,
			0, 0, 0, 1

		);
50 51 52

		return this;

53
	},
M
Mr.doob 已提交
54

55
	copy : function ( m ) {
M
Mr.doob 已提交
56

M
Mr.doob 已提交
57 58 59 60 61 62 63 64
		this.set(

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

		);
M
Mr.doob 已提交
65

66 67
		return this;

M
Mr.doob 已提交
68 69
	},

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

72
		var x = THREE.Matrix4.__v1, y = THREE.Matrix4.__v2, z = THREE.Matrix4.__v3;
M
Mr.doob 已提交
73

M
Mr.doob 已提交
74
		z.sub( eye, center ).normalize();
75

M
Mikael Emtinger 已提交
76
		if ( z.length() === 0 ) {
77

M
Mikael Emtinger 已提交
78
			z.z = 1;
79

M
Mikael Emtinger 已提交
80
		}
81

M
Mr.doob 已提交
82
		x.cross( up, z ).normalize();
M
Mikael Emtinger 已提交
83 84

		if ( x.length() === 0 ) {
85

M
Mikael Emtinger 已提交
86 87
			z.x += 0.0001;
			x.cross( up, z ).normalize();
88

M
Mikael Emtinger 已提交
89 90
		}

M
Mr.doob 已提交
91
		y.cross( z, x ).normalize();
M
Mr.doob 已提交
92

M
Mr.doob 已提交
93

94 95 96
		this.n11 = x.x; this.n12 = y.x; this.n13 = z.x;
		this.n21 = x.y; this.n22 = y.y; this.n23 = z.y;
		this.n31 = x.z; this.n32 = y.z; this.n33 = z.z;
M
Mr.doob 已提交
97

98 99
		return this;

100
	},
101

102
	multiplyVector3 : function ( v ) {
103

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

107 108 109
		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;
110

111 112
		return v;

M
Mr.doob 已提交
113
	},
114

115
	multiplyVector4 : function ( v ) {
116

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

M
Mr.doob 已提交
119 120 121 122
		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 已提交
123

124 125
		return v;

126
	},
127

128 129 130 131 132 133 134 135 136 137 138 139 140 141
	rotateAxis : function ( v ) {

		var vx = v.x, vy = v.y, vz = v.z;

		v.x = vx * this.n11 + vy * this.n12 + vz * this.n13;
		v.y = vx * this.n21 + vy * this.n22 + vz * this.n23;
		v.z = vx * this.n31 + vy * this.n32 + vz * this.n33;

		v.normalize();

		return v;

	},

142
	crossVector : function ( a ) {
M
Mr.doob 已提交
143

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

146 147 148
		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 已提交
149

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

152
		return v;
M
Mr.doob 已提交
153

154
	},
155

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

M
Mr.doob 已提交
158 159 160 161 162 163 164 165 166 167
		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;

M
Mr.doob 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
		this.n11 = a11 * b11 + a12 * b21 + a13 * b31;
		this.n12 = a11 * b12 + a12 * b22 + a13 * b32;
		this.n13 = a11 * b13 + a12 * b23 + a13 * b33;
		this.n14 = a11 * b14 + a12 * b24 + a13 * b34 + a14;

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

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

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

188
		/*
A
alteredq 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
		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;
208
		*/
209

A
alteredq 已提交
210 211 212 213
		return this;

	},

214
	multiplyToArray : function ( a, b, r ) {
215

216
		this.multiply( a, b );
217

218 219 220 221
		r[ 0 ] = this.n11; r[ 1 ] = this.n21; r[ 2 ] = this.n31; r[ 3 ] = this.n41;
		r[ 4 ] = this.n12; r[ 5 ] = this.n22; r[ 6 ] = this.n32; r[ 7 ] = this.n42;
		r[ 8 ]  = this.n13; r[ 9 ]  = this.n23; r[ 10 ] = this.n33; r[ 11 ] = this.n43;
		r[ 12 ] = this.n14; r[ 13 ] = this.n24; r[ 14 ] = this.n34; r[ 15 ] = this.n44;
M
Mr.doob 已提交
222

223
		return this;
M
Mr.doob 已提交
224

225
	},
M
Mr.doob 已提交
226

227
	multiplySelf : function ( m ) {
M
Mr.doob 已提交
228

229
		this.multiply( this, m );
230

231 232
		return this;

233 234
	},

235
	multiplyScalar : function ( s ) {
236 237 238 239 240 241

		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;

242 243
		return this;

244 245
	},

246
	determinant : function () {
247

248 249 250 251 252
		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;

253 254 255
		//TODO: make this more efficient
		//( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm )
		return (
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
			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+
M
Mr.doob 已提交
284 285
			n11 * n22 * n33 * n44
		);
286 287

	},
M
Mr.doob 已提交
288

289
	transpose : function () {
M
Mr.doob 已提交
290

291
		var tmp;
292

293 294 295
		tmp = this.n21; this.n21 = this.n12; this.n12 = tmp;
		tmp = this.n31; this.n31 = this.n13; this.n13 = tmp;
		tmp = this.n32; this.n32 = this.n23; this.n23 = tmp;
296

297 298 299
		tmp = this.n41; this.n41 = this.n14; this.n14 = tmp;
		tmp = this.n42; this.n42 = this.n24; this.n24 = tmp;
		tmp = this.n43; this.n43 = this.n34; this.n43 = tmp;
M
Mr.doob 已提交
300 301 302 303 304

		return this;

	},

305
	clone : function () {
306 307

		var m = new THREE.Matrix4();
308

309 310 311 312
		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;
313

314 315 316
		return m;

	},
M
Mr.doob 已提交
317

318 319 320 321 322 323
	flatten : function () {

		this.flat[ 0 ] = this.n11; this.flat[ 1 ] = this.n21; this.flat[ 2 ] = this.n31; this.flat[ 3 ] = this.n41;
		this.flat[ 4 ] = this.n12; this.flat[ 5 ] = this.n22; this.flat[ 6 ] = this.n32; this.flat[ 7 ] = this.n42;
		this.flat[ 8 ]  = this.n13; this.flat[ 9 ]  = this.n23; this.flat[ 10 ] = this.n33; this.flat[ 11 ] = this.n43;
		this.flat[ 12 ] = this.n14; this.flat[ 13 ] = this.n24; this.flat[ 14 ] = this.n34; this.flat[ 15 ] = this.n44;
324

M
Mr.doob 已提交
325
		return this.flat;
326 327 328

	},

329
	flattenToArray : function ( flat ) {
A
alteredq 已提交
330

M
Mr.doob 已提交
331 332 333 334
		flat[ 0 ] = this.n11; flat[ 1 ] = this.n21; flat[ 2 ] = this.n31; flat[ 3 ] = this.n41;
		flat[ 4 ] = this.n12; flat[ 5 ] = this.n22; flat[ 6 ] = this.n32; flat[ 7 ] = this.n42;
		flat[ 8 ]  = this.n13; flat[ 9 ]  = this.n23; flat[ 10 ] = this.n33; flat[ 11 ] = this.n43;
		flat[ 12 ] = this.n14; flat[ 13 ] = this.n24; flat[ 14 ] = this.n34; flat[ 15 ] = this.n44;
335

336
		return flat;
M
Mr.doob 已提交
337

338
	},
339

340
	flattenToArrayOffset : function( flat, offset ) {
341

M
Mr.doob 已提交
342
		flat[ offset ] = this.n11;
A
alteredq 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
		flat[ offset + 1 ] = this.n21;
		flat[ offset + 2 ] = this.n31;
		flat[ offset + 3 ] = this.n41;

		flat[ offset + 4 ] = this.n12;
		flat[ offset + 5 ] = this.n22;
		flat[ offset + 6 ] = this.n32;
		flat[ offset + 7 ] = this.n42;

		flat[ offset + 8 ]  = this.n13;
		flat[ offset + 9 ]  = this.n23;
		flat[ offset + 10 ] = this.n33;
		flat[ offset + 11 ] = this.n43;

		flat[ offset + 12 ] = this.n14;
		flat[ offset + 13 ] = this.n24;
		flat[ offset + 14 ] = this.n34;
		flat[ offset + 15 ] = this.n44;

		return flat;

	},

366
	setTranslation : function( x, y, z ) {
367

M
Mr.doob 已提交
368 369 370 371 372 373 374 375
		this.set(

			1, 0, 0, x,
			0, 1, 0, y,
			0, 0, 1, z,
			0, 0, 0, 1

		);
376

377
		return this;
378

379
	},
380

381
	setScale : function ( x, y, z ) {
M
Mr.doob 已提交
382 383 384 385 386 387 388

		this.set(

			x, 0, 0, 0,
			0, y, 0, 0,
			0, 0, z, 0,
			0, 0, 0, 1
389

M
Mr.doob 已提交
390
		);
391

392
		return this;
393

394
	},
395

396
	setRotationX : function ( theta ) {
397 398

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

M
Mr.doob 已提交
400 401 402 403 404 405 406 407
		this.set(

			1, 0,  0, 0,
			0, c, -s, 0,
			0, s,  c, 0,
			0, 0,  0, 1

		);
408

409
		return this;
410

411
	},
412

413
	setRotationY : function( theta ) {
414 415 416

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

M
Mr.doob 已提交
417 418 419 420 421 422 423 424
		this.set(

			 c, 0, s, 0,
			 0, 1, 0, 0,
			-s, 0, c, 0,
			 0, 0, 0, 1

		);
425

426
		return this;
427

428
	},
429

430
	setRotationZ : function( theta ) {
431 432

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

M
Mr.doob 已提交
434 435 436 437 438 439 440 441
		this.set(

			c, -s, 0, 0,
			s,  c, 0, 0,
			0,  0, 1, 0,
			0,  0, 0, 1

		);
442

443
		return this;
444

445
	},
446

447
	setRotationAxis : function( axis, angle ) {
448

M
Mr.doob 已提交
449
		// Based on http://www.gamedev.net/reference/articles/article1199.asp
450

451
		var c = Math.cos( angle ),
M
Mr.doob 已提交
452 453 454 455 456 457 458 459 460 461 462
		s = Math.sin( angle ),
		t = 1 - c,
		x = axis.x, y = axis.y, z = axis.z,
		tx = t * x, ty = t * y;

		this.set(

		 	tx * x + c, tx * y - s * z, tx * z + s * y, 0,
			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
463

M
Mr.doob 已提交
464
		);
465

466 467
		 return this;

468
	},
469

470
	setPosition : function( v ) {
471

M
Mr.doob 已提交
472 473 474
		this.n14 = v.x;
		this.n24 = v.y;
		this.n34 = v.z;
475

476
		return this;
477

478
	},
479

480
	setRotationFromEuler : function( v ) {
481

M
Mr.doob 已提交
482
		var x = v.x, y = v.y, z = v.z,
483 484 485 486
		a = Math.cos( x ), b = Math.sin( x ),
		c = Math.cos( y ), d = Math.sin( y ),
		e = Math.cos( z ), f = Math.sin( z ),
		ad = a * d, bd = b * d;
487

488 489 490
		this.n11 = c * e;
		this.n12 = - c * f;
		this.n13 = d;
491

492 493 494
		this.n21 = bd * e + a * f;
		this.n22 = - bd * f + a * e;
		this.n23 = - b * c;
495

496 497 498
		this.n31 = - ad * e + b * f;
		this.n32 = ad * f + b * e;
		this.n33 = a * c;
499

500 501
		return this;

502 503
	},

504
	setRotationFromQuaternion : function( q ) {
505

M
Mr.doob 已提交
506
		var x = q.x, y = q.y, z = q.z, w = q.w,
M
Mr.doob 已提交
507 508 509 510
		x2 = x + x, y2 = y + y, z2 = z + z,
		xx = x * x2, xy = x * y2, xz = x * z2,
		yy = y * y2, yz = y * z2, zz = z * z2,
		wx = w * x2, wy = w * y2, wz = w * z2;
511

512 513 514
		this.n11 = 1 - ( yy + zz );
		this.n12 = xy - wz;
		this.n13 = xz + wy;
515

516 517 518
		this.n21 = xy + wz;
		this.n22 = 1 - ( xx + zz );
		this.n23 = yz - wx;
519

520 521 522
		this.n31 = xz - wy;
		this.n32 = yz + wx;
		this.n33 = 1 - ( xx + yy );
523

524 525
		return this;

526
	},
527

M
Mr.doob 已提交
528
	scale : function ( v ) {
529

M
Mr.doob 已提交
530
		var x = v.x, y = v.y, z = v.z;
531

M
Mr.doob 已提交
532 533 534 535
		this.n11 *= x; this.n12 *= y; this.n13 *= z;
		this.n21 *= x; this.n22 *= y; this.n23 *= z;
		this.n31 *= x; this.n32 *= y; this.n33 *= z;
		this.n41 *= x; this.n42 *= y; this.n43 *= z;
536 537

		return this;
A
alteredq 已提交
538

539
	},
M
Mr.doob 已提交
540

541
	extractPosition : function ( m ) {
M
Mr.doob 已提交
542

543 544 545
		this.n14 = m.n14;
		this.n24 = m.n24;
		this.n34 = m.n34;
M
Mr.doob 已提交
546

547
	},
M
Mr.doob 已提交
548

549
	extractRotation : function ( m, s ) {
550

551
		var invScaleX = 1 / s.x, invScaleY = 1 / s.y, invScaleZ = 1 / s.z;
M
Mr.doob 已提交
552

553 554 555
		this.n11 = m.n11 * invScaleX;
		this.n21 = m.n21 * invScaleX;
		this.n31 = m.n31 * invScaleX;
P
philogb 已提交
556

557 558 559
		this.n12 = m.n12 * invScaleY;
		this.n22 = m.n22 * invScaleY;
		this.n32 = m.n32 * invScaleY;
560

561 562 563
		this.n13 = m.n13 * invScaleZ;
		this.n23 = m.n23 * invScaleZ;
		this.n33 = m.n33 * invScaleZ;
M
Mr.doob 已提交
564

565
	}
P
philogb 已提交
566 567 568

};

A
alteredq 已提交
569 570 571 572 573
THREE.Matrix4.makeInvert = function ( m1, m2 ) {

	// based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm

	var n11 = m1.n11, n12 = m1.n12, n13 = m1.n13, n14 = m1.n14,
M
Mr.doob 已提交
574 575 576
	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;
A
alteredq 已提交
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601

	if( m2 === undefined ) m2 = new THREE.Matrix4();

	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;
	m2.multiplyScalar( 1 / m1.determinant() );

	return m2;

};

602
THREE.Matrix4.makeInvert3x3 = function ( m1 ) {
603

604
	// input:  THREE.Matrix4, output: THREE.Matrix3
605 606
	// ( based on http://code.google.com/p/webgl-mjs/ )

607
	var m33 = m1.m33, m33m = m33.m,
608 609 610 611 612 613 614 615 616
	a11 =   m1.n33 * m1.n22 - m1.n32 * m1.n23,
	a21 = - m1.n33 * m1.n21 + m1.n31 * m1.n23,
	a31 =   m1.n32 * m1.n21 - m1.n31 * m1.n22,
	a12 = - m1.n33 * m1.n12 + m1.n32 * m1.n13,
	a22 =   m1.n33 * m1.n11 - m1.n31 * m1.n13,
	a32 = - m1.n32 * m1.n11 + m1.n31 * m1.n12,
	a13 =   m1.n23 * m1.n12 - m1.n22 * m1.n13,
	a23 = - m1.n23 * m1.n11 + m1.n21 * m1.n13,
	a33 =   m1.n22 * m1.n11 - m1.n21 * m1.n12,
617

618
	det = m1.n11 * a11 + m1.n21 * a12 + m1.n31 * a13,
619

620 621 622
	idet;

	// no inverse
M
Mikael Emtinger 已提交
623 624 625 626
	if (det == 0) {
		throw "matrix not invertible";
	}
	
627 628
	idet = 1.0 / det;

629 630 631
	m33m[ 0 ] = idet * a11; m33m[ 1 ] = idet * a21; m33m[ 2 ] = idet * a31;
	m33m[ 3 ] = idet * a12; m33m[ 4 ] = idet * a22; m33m[ 5 ] = idet * a32;
	m33m[ 6 ] = idet * a13; m33m[ 7 ] = idet * a23; m33m[ 8 ] = idet * a33;
632

633
	return m33;
634

635 636
}

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

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

641 642 643 644 645 646 647
	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 已提交
648

649 650 651 652
	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 已提交
653

654
	return m;
655

M
Mr.doob 已提交
656
};
M
Mr.doob 已提交
657

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

660 661 662 663 664 665 666 667 668 669 670
	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 );

};

671
THREE.Matrix4.makeOrtho = function ( left, right, top, bottom, near, far ) {
672 673 674 675 676

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

	m = new THREE.Matrix4();
	w = right - left;
677
	h = top - bottom;
678 679
	p = far - near;
	x = ( right + left ) / w;
680
	y = ( top + bottom ) / h;
681
	z = ( far + near ) / p;
682

683 684 685 686
	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;
687

688
	return m;
689

M
Mr.doob 已提交
690
};
A
alteredq 已提交
691

692 693 694
THREE.Matrix4.__v1 = new THREE.Vector3();
THREE.Matrix4.__v2 = new THREE.Vector3();
THREE.Matrix4.__v3 = new THREE.Vector3();