Matrix4.js 21.8 KB
Newer Older
M
Mr.doob 已提交
1
/**
M
Mr.doob 已提交
2
 * @author mrdoob / 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
 * @author timknip / http://www.floorplanner.com/
B
Ben Houston 已提交
10
 * @author bhouston / http://exocortex.com
11
 * @author WestLangley / http://github.com/WestLangley
M
Mr.doob 已提交
12 13
 */

14
THREE.Matrix4 = function () {
15

16
	this.elements = new Float32Array( [
17

18 19 20 21
		1, 0, 0, 0,
		0, 1, 0, 0,
		0, 0, 1, 0,
		0, 0, 0, 1
22

23
	] );
M
Mr.doob 已提交
24

25
	if ( arguments.length > 0 ) {
26

27
		THREE.error( 'THREE.Matrix4: the constructor no longer reads arguments. use .set() instead.' );
28 29

	}
30

31
};
32

33 34 35
THREE.Matrix4.prototype = {

	constructor: THREE.Matrix4,
36

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

		var te = this.elements;

41 42 43 44
		te[ 0 ] = n11; te[ 4 ] = n12; te[ 8 ] = n13; te[ 12 ] = n14;
		te[ 1 ] = n21; te[ 5 ] = n22; te[ 9 ] = n23; te[ 13 ] = n24;
		te[ 2 ] = n31; te[ 6 ] = n32; te[ 10 ] = n33; te[ 14 ] = n34;
		te[ 3 ] = n41; te[ 7 ] = n42; te[ 11 ] = n43; te[ 15 ] = n44;
M
Mr.doob 已提交
45

46 47 48 49
		return this;

	},

50
	identity: function () {
51

M
Mr.doob 已提交
52 53 54 55 56 57 58 59
		this.set(

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

		);
60 61 62

		return this;

63
	},
M
Mr.doob 已提交
64

65
	copy: function ( m ) {
66

67
		this.elements.set( m.elements );
M
Mr.doob 已提交
68

69 70
		return this;

M
Mr.doob 已提交
71 72
	},

73 74
	extractPosition: function ( m ) {

75
		THREE.warn( 'THREE.Matrix4: .extractPosition() has been renamed to .copyPosition().' );
76 77 78 79
		return this.copyPosition( m );

	},

80 81 82 83 84
	copyPosition: function ( m ) {

		var te = this.elements;
		var me = m.elements;

85 86 87
		te[ 12 ] = me[ 12 ];
		te[ 13 ] = me[ 13 ];
		te[ 14 ] = me[ 14 ];
88 89 90 91 92

		return this;

	},

93 94
	extractBasis: function ( xAxis, yAxis, zAxis ) {
 
G
gero3 已提交
95
		var te = this.elements;
96
 
97 98 99
		xAxis.set( te[ 0 ], te[ 1 ], te[ 2 ] );
		yAxis.set( te[ 4 ], te[ 5 ], te[ 6 ] );
		zAxis.set( te[ 8 ], te[ 9 ], te[ 10 ] );
100
 
G
gero3 已提交
101
		return this;
102
 		
G
gero3 已提交
103
	},
104 105 106
 
	makeBasis: function ( xAxis, yAxis, zAxis ) {

107 108 109 110 111 112
		this.set(
			xAxis.x, yAxis.x, zAxis.x, 0,
			xAxis.y, yAxis.y, zAxis.y, 0,
			xAxis.z, yAxis.z, zAxis.z, 0,
			0,       0,       0,       1
		);
113

G
gero3 已提交
114
		return this;
115 116 117

	},

118
	extractRotation: function () {
119

120
		var v1;
121 122

		return function ( m ) {
123

C
Colin Ballast 已提交
124
			if ( v1 === undefined ) v1 = new THREE.Vector3();
125 126 127 128

			var te = this.elements;
			var me = m.elements;

129 130 131
			var scaleX = 1 / v1.set( me[ 0 ], me[ 1 ], me[ 2 ] ).length();
			var scaleY = 1 / v1.set( me[ 4 ], me[ 5 ], me[ 6 ] ).length();
			var scaleZ = 1 / v1.set( me[ 8 ], me[ 9 ], me[ 10 ] ).length();
132

133 134 135
			te[ 0 ] = me[ 0 ] * scaleX;
			te[ 1 ] = me[ 1 ] * scaleX;
			te[ 2 ] = me[ 2 ] * scaleX;
136

137 138 139
			te[ 4 ] = me[ 4 ] * scaleY;
			te[ 5 ] = me[ 5 ] * scaleY;
			te[ 6 ] = me[ 6 ] * scaleY;
140

141 142 143
			te[ 8 ] = me[ 8 ] * scaleZ;
			te[ 9 ] = me[ 9 ] * scaleZ;
			te[ 10 ] = me[ 10 ] * scaleZ;
144 145 146 147 148 149 150

			return this;

		};

	}(),

M
Mr.doob 已提交
151 152
	makeRotationFromEuler: function ( euler ) {

153
		if ( euler instanceof THREE.Euler === false ) {
154

155
			THREE.error( 'THREE.Matrix: .makeRotationFromEuler() now expects a Euler rotation rather than a Vector3 and order.' );
M
Mr.doob 已提交
156

157
		}
158 159 160

		var te = this.elements;

M
Mr.doob 已提交
161
		var x = euler.x, y = euler.y, z = euler.z;
162 163 164 165
		var a = Math.cos( x ), b = Math.sin( x );
		var c = Math.cos( y ), d = Math.sin( y );
		var e = Math.cos( z ), f = Math.sin( z );

166
		if ( euler.order === 'XYZ' ) {
167 168 169

			var ae = a * e, af = a * f, be = b * e, bf = b * f;

170 171 172
			te[ 0 ] = c * e;
			te[ 4 ] = - c * f;
			te[ 8 ] = d;
173

174 175 176
			te[ 1 ] = af + be * d;
			te[ 5 ] = ae - bf * d;
			te[ 9 ] = - b * c;
177

178 179 180
			te[ 2 ] = bf - ae * d;
			te[ 6 ] = be + af * d;
			te[ 10 ] = a * c;
181

M
Mr.doob 已提交
182
		} else if ( euler.order === 'YXZ' ) {
183 184 185

			var ce = c * e, cf = c * f, de = d * e, df = d * f;

186 187 188
			te[ 0 ] = ce + df * b;
			te[ 4 ] = de * b - cf;
			te[ 8 ] = a * d;
189

190 191 192
			te[ 1 ] = a * f;
			te[ 5 ] = a * e;
			te[ 9 ] = - b;
193

194 195 196
			te[ 2 ] = cf * b - de;
			te[ 6 ] = df + ce * b;
			te[ 10 ] = a * c;
197

M
Mr.doob 已提交
198
		} else if ( euler.order === 'ZXY' ) {
199 200 201

			var ce = c * e, cf = c * f, de = d * e, df = d * f;

202 203 204
			te[ 0 ] = ce - df * b;
			te[ 4 ] = - a * f;
			te[ 8 ] = de + cf * b;
205

206 207 208
			te[ 1 ] = cf + de * b;
			te[ 5 ] = a * e;
			te[ 9 ] = df - ce * b;
209

210 211 212
			te[ 2 ] = - a * d;
			te[ 6 ] = b;
			te[ 10 ] = a * c;
213

M
Mr.doob 已提交
214
		} else if ( euler.order === 'ZYX' ) {
215 216 217

			var ae = a * e, af = a * f, be = b * e, bf = b * f;

218 219 220
			te[ 0 ] = c * e;
			te[ 4 ] = be * d - af;
			te[ 8 ] = ae * d + bf;
221

222 223 224
			te[ 1 ] = c * f;
			te[ 5 ] = bf * d + ae;
			te[ 9 ] = af * d - be;
225

226 227 228
			te[ 2 ] = - d;
			te[ 6 ] = b * c;
			te[ 10 ] = a * c;
229

M
Mr.doob 已提交
230
		} else if ( euler.order === 'YZX' ) {
231 232 233

			var ac = a * c, ad = a * d, bc = b * c, bd = b * d;

234 235 236
			te[ 0 ] = c * e;
			te[ 4 ] = bd - ac * f;
			te[ 8 ] = bc * f + ad;
237

238 239 240
			te[ 1 ] = f;
			te[ 5 ] = a * e;
			te[ 9 ] = - b * e;
241

242 243 244
			te[ 2 ] = - d * e;
			te[ 6 ] = ad * f + bc;
			te[ 10 ] = ac - bd * f;
245

M
Mr.doob 已提交
246
		} else if ( euler.order === 'XZY' ) {
247 248 249

			var ac = a * c, ad = a * d, bc = b * c, bd = b * d;

250 251 252
			te[ 0 ] = c * e;
			te[ 4 ] = - f;
			te[ 8 ] = d * e;
253

254 255 256
			te[ 1 ] = ac * f + bd;
			te[ 5 ] = a * e;
			te[ 9 ] = ad * f - bc;
257

258 259 260
			te[ 2 ] = bc * f - ad;
			te[ 6 ] = b * e;
			te[ 10 ] = bd * f + ac;
261 262 263

		}

264
		// last column
265 266 267
		te[ 3 ] = 0;
		te[ 7 ] = 0;
		te[ 11 ] = 0;
268 269

		// bottom row
270 271 272 273
		te[ 12 ] = 0;
		te[ 13 ] = 0;
		te[ 14 ] = 0;
		te[ 15 ] = 1;
274

275 276 277 278 279 280
		return this;

	},

	setRotationFromQuaternion: function ( q ) {

281
		THREE.warn( 'THREE.Matrix4: .setRotationFromQuaternion() has been renamed to .makeRotationFromQuaternion().' );
M
Mr.doob 已提交
282

283 284 285 286
		return this.makeRotationFromQuaternion( q );

	},

287
	makeRotationFromQuaternion: function ( q ) {
288 289 290 291 292 293 294 295 296

		var te = this.elements;

		var x = q.x, y = q.y, z = q.z, w = q.w;
		var x2 = x + x, y2 = y + y, z2 = z + z;
		var xx = x * x2, xy = x * y2, xz = x * z2;
		var yy = y * y2, yz = y * z2, zz = z * z2;
		var wx = w * x2, wy = w * y2, wz = w * z2;

297 298 299
		te[ 0 ] = 1 - ( yy + zz );
		te[ 4 ] = xy - wz;
		te[ 8 ] = xz + wy;
300

301 302 303
		te[ 1 ] = xy + wz;
		te[ 5 ] = 1 - ( xx + zz );
		te[ 9 ] = yz - wx;
304

305 306 307
		te[ 2 ] = xz - wy;
		te[ 6 ] = yz + wx;
		te[ 10 ] = 1 - ( xx + yy );
308

309
		// last column
310 311 312
		te[ 3 ] = 0;
		te[ 7 ] = 0;
		te[ 11 ] = 0;
313 314

		// bottom row
315 316 317 318
		te[ 12 ] = 0;
		te[ 13 ] = 0;
		te[ 14 ] = 0;
		te[ 15 ] = 1;
319

320 321 322 323
		return this;

	},

324
	lookAt: function () {
325

326
		var x, y, z;
M
Mr.doob 已提交
327

328
		return function ( eye, target, up ) {
329

C
Colin Ballast 已提交
330 331 332
			if ( x === undefined ) x = new THREE.Vector3();
			if ( y === undefined ) y = new THREE.Vector3();
			if ( z === undefined ) z = new THREE.Vector3();
333

334
			var te = this.elements;
M
Mr.doob 已提交
335

336
			z.subVectors( eye, target ).normalize();
M
Mr.doob 已提交
337

338
			if ( z.length() === 0 ) {
339

340
				z.z = 1;
341

342
			}
343

344
			x.crossVectors( up, z ).normalize();
345

346
			if ( x.length() === 0 ) {
M
Mikael Emtinger 已提交
347

348 349
				z.x += 0.0001;
				x.crossVectors( up, z ).normalize();
350

351
			}
352

353
			y.crossVectors( z, x );
M
Mikael Emtinger 已提交
354

M
Mr.doob 已提交
355

356 357 358
			te[ 0 ] = x.x; te[ 4 ] = y.x; te[ 8 ] = z.x;
			te[ 1 ] = x.y; te[ 5 ] = y.y; te[ 9 ] = z.y;
			te[ 2 ] = x.z; te[ 6 ] = y.z; te[ 10 ] = z.z;
M
Mr.doob 已提交
359

360
			return this;
M
Mr.doob 已提交
361

362
		};
363

364
	}(),
365

366 367 368 369
	multiply: function ( m, n ) {

		if ( n !== undefined ) {

370
			THREE.warn( 'THREE.Matrix4: .multiply() now only accepts one argument. Use .multiplyMatrices( a, b ) instead.' );
371 372 373
			return this.multiplyMatrices( m, n );

		}
374 375 376 377 378 379

		return this.multiplyMatrices( this, m );

	},

	multiplyMatrices: function ( a, b ) {
380 381 382 383

		var ae = a.elements;
		var be = b.elements;
		var te = this.elements;
384

385 386 387 388
		var a11 = ae[ 0 ], a12 = ae[ 4 ], a13 = ae[ 8 ], a14 = ae[ 12 ];
		var a21 = ae[ 1 ], a22 = ae[ 5 ], a23 = ae[ 9 ], a24 = ae[ 13 ];
		var a31 = ae[ 2 ], a32 = ae[ 6 ], a33 = ae[ 10 ], a34 = ae[ 14 ];
		var a41 = ae[ 3 ], a42 = ae[ 7 ], a43 = ae[ 11 ], a44 = ae[ 15 ];
389

390 391 392 393
		var b11 = be[ 0 ], b12 = be[ 4 ], b13 = be[ 8 ], b14 = be[ 12 ];
		var b21 = be[ 1 ], b22 = be[ 5 ], b23 = be[ 9 ], b24 = be[ 13 ];
		var b31 = be[ 2 ], b32 = be[ 6 ], b33 = be[ 10 ], b34 = be[ 14 ];
		var b41 = be[ 3 ], b42 = be[ 7 ], b43 = be[ 11 ], b44 = be[ 15 ];
394

395 396 397 398
		te[ 0 ] = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
		te[ 4 ] = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
		te[ 8 ] = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
		te[ 12 ] = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
399

400 401 402 403
		te[ 1 ] = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
		te[ 5 ] = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
		te[ 9 ] = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
		te[ 13 ] = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
404

405 406 407 408
		te[ 2 ] = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
		te[ 6 ] = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
		te[ 10 ] = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
		te[ 14 ] = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
409

410 411 412 413
		te[ 3 ] = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
		te[ 7 ] = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
		te[ 11 ] = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
		te[ 15 ] = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
414

A
alteredq 已提交
415 416 417 418
		return this;

	},

419
	multiplyToArray: function ( a, b, r ) {
420 421 422

		var te = this.elements;

423
		this.multiplyMatrices( a, b );
424

425 426 427 428
		r[ 0 ] = te[ 0 ]; r[ 1 ] = te[ 1 ]; r[ 2 ] = te[ 2 ]; r[ 3 ] = te[ 3 ];
		r[ 4 ] = te[ 4 ]; r[ 5 ] = te[ 5 ]; r[ 6 ] = te[ 6 ]; r[ 7 ] = te[ 7 ];
		r[ 8 ]  = te[ 8 ]; r[ 9 ]  = te[ 9 ]; r[ 10 ] = te[ 10 ]; r[ 11 ] = te[ 11 ];
		r[ 12 ] = te[ 12 ]; r[ 13 ] = te[ 13 ]; r[ 14 ] = te[ 14 ]; r[ 15 ] = te[ 15 ];
M
Mr.doob 已提交
429

430
		return this;
M
Mr.doob 已提交
431

432
	},
M
Mr.doob 已提交
433

434
	multiplyScalar: function ( s ) {
435 436 437

		var te = this.elements;

438 439 440 441
		te[ 0 ] *= s; te[ 4 ] *= s; te[ 8 ] *= s; te[ 12 ] *= s;
		te[ 1 ] *= s; te[ 5 ] *= s; te[ 9 ] *= s; te[ 13 ] *= s;
		te[ 2 ] *= s; te[ 6 ] *= s; te[ 10 ] *= s; te[ 14 ] *= s;
		te[ 3 ] *= s; te[ 7 ] *= s; te[ 11 ] *= s; te[ 15 ] *= s;
442

443 444
		return this;

445 446
	},

447 448
	multiplyVector3: function ( vector ) {

449
		THREE.warn( 'THREE.Matrix4: .multiplyVector3() has been removed. Use vector.applyMatrix4( matrix ) or vector.applyProjection( matrix ) instead.' );
450
		return vector.applyProjection( this );
451 452 453 454 455

	},

	multiplyVector4: function ( vector ) {

456
		THREE.warn( 'THREE.Matrix4: .multiplyVector4() has been removed. Use vector.applyMatrix4( matrix ) instead.' );
457 458 459 460
		return vector.applyMatrix4( this );

	},

461 462
	multiplyVector3Array: function ( a ) {

463
		THREE.warn( 'THREE.Matrix4: .multiplyVector3Array() has been renamed. Use matrix.applyToVector3Array( array ) instead.' );
464 465 466 467
		return this.applyToVector3Array( a );

	},

468
	applyToVector3Array: function () {
469

470
		var v1;
M
Mr.doob 已提交
471

472
		return function ( array, offset, length ) {
473

C
Colin Ballast 已提交
474
			if ( v1 === undefined ) v1 = new THREE.Vector3();
475 476
			if ( offset === undefined ) offset = 0;
			if ( length === undefined ) length = array.length;
477

B
brason 已提交
478
			for ( var i = 0, j = offset; i < length; i += 3, j += 3 ) {
479 480 481 482

				v1.x = array[ j ];
				v1.y = array[ j + 1 ];
				v1.z = array[ j + 2 ];
483

484
				v1.applyMatrix4( this );
485

486 487 488
				array[ j ]     = v1.x;
				array[ j + 1 ] = v1.y;
				array[ j + 2 ] = v1.z;
489

490
			}
491

492
			return array;
493

494 495 496
		};

	}(),
497

M
Mr.doob 已提交
498
	rotateAxis: function ( v ) {
499

500
		THREE.warn( 'THREE.Matrix4: .rotateAxis() has been removed. Use Vector3.transformDirection( matrix ) instead.' );
M
Mr.doob 已提交
501

502
		v.transformDirection( this );
M
Mr.doob 已提交
503 504 505

	},

W
WestLangley 已提交
506
	crossVector: function ( vector ) {
507

508
		THREE.warn( 'THREE.Matrix4: .crossVector() has been removed. Use vector.applyMatrix4( matrix ) instead.' );
W
WestLangley 已提交
509
		return vector.applyMatrix4( this );
M
Mr.doob 已提交
510 511 512

	},

513
	determinant: function () {
514

515 516
		var te = this.elements;

517 518 519 520
		var n11 = te[ 0 ], n12 = te[ 4 ], n13 = te[ 8 ], n14 = te[ 12 ];
		var n21 = te[ 1 ], n22 = te[ 5 ], n23 = te[ 9 ], n24 = te[ 13 ];
		var n31 = te[ 2 ], n32 = te[ 6 ], n33 = te[ 10 ], n34 = te[ 14 ];
		var n41 = te[ 3 ], n42 = te[ 7 ], n43 = te[ 11 ], n44 = te[ 15 ];
521

522 523
		//TODO: make this more efficient
		//( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm )
524

525
		return (
526
			n41 * (
527 528 529 530 531 532
				+ n14 * n23 * n32
				 - n13 * n24 * n32
				 - n14 * n22 * n33
				 + n12 * n24 * n33
				 + n13 * n22 * n34
				 - n12 * n23 * n34
M
Mr.doob 已提交
533
			) +
534
			n42 * (
535 536 537 538 539 540
				+ n11 * n23 * n34
				 - n11 * n24 * n33
				 + n14 * n21 * n33
				 - n13 * n21 * n34
				 + n13 * n24 * n31
				 - n14 * n23 * n31
M
Mr.doob 已提交
541
			) +
542
			n43 * (
543 544 545 546 547 548
				+ n11 * n24 * n32
				 - n11 * n22 * n34
				 - n14 * n21 * n32
				 + n12 * n21 * n34
				 + n14 * n22 * n31
				 - n12 * n24 * n31
M
Mr.doob 已提交
549 550
			) +
			n44 * (
551 552 553 554 555 556
				- n13 * n22 * n31
				 - n11 * n23 * n32
				 + n11 * n22 * n33
				 + n13 * n21 * n32
				 - n12 * n21 * n33
				 + n12 * n23 * n31
M
Mr.doob 已提交
557
			)
558

M
Mr.doob 已提交
559
		);
560 561

	},
M
Mr.doob 已提交
562

563
	transpose: function () {
564 565

		var te = this.elements;
566
		var tmp;
567

568 569 570
		tmp = te[ 1 ]; te[ 1 ] = te[ 4 ]; te[ 4 ] = tmp;
		tmp = te[ 2 ]; te[ 2 ] = te[ 8 ]; te[ 8 ] = tmp;
		tmp = te[ 6 ]; te[ 6 ] = te[ 9 ]; te[ 9 ] = tmp;
571

572 573 574
		tmp = te[ 3 ]; te[ 3 ] = te[ 12 ]; te[ 12 ] = tmp;
		tmp = te[ 7 ]; te[ 7 ] = te[ 13 ]; te[ 13 ] = tmp;
		tmp = te[ 11 ]; te[ 11 ] = te[ 14 ]; te[ 14 ] = tmp;
M
Mr.doob 已提交
575 576 577 578 579

		return this;

	},

580
	flattenToArrayOffset: function ( array, offset ) {
581

582
		var te = this.elements;
M
Mr.doob 已提交
583

584 585 586 587
		array[ offset     ] = te[ 0 ];
		array[ offset + 1 ] = te[ 1 ];
		array[ offset + 2 ] = te[ 2 ];
		array[ offset + 3 ] = te[ 3 ];
M
Mr.doob 已提交
588

589 590 591 592
		array[ offset + 4 ] = te[ 4 ];
		array[ offset + 5 ] = te[ 5 ];
		array[ offset + 6 ] = te[ 6 ];
		array[ offset + 7 ] = te[ 7 ];
M
Mr.doob 已提交
593

594 595 596 597
		array[ offset + 8 ]  = te[ 8 ];
		array[ offset + 9 ]  = te[ 9 ];
		array[ offset + 10 ] = te[ 10 ];
		array[ offset + 11 ] = te[ 11 ];
M
Mr.doob 已提交
598

599 600 601 602
		array[ offset + 12 ] = te[ 12 ];
		array[ offset + 13 ] = te[ 13 ];
		array[ offset + 14 ] = te[ 14 ];
		array[ offset + 15 ] = te[ 15 ];
M
Mr.doob 已提交
603 604

		return array;
A
alteredq 已提交
605 606 607

	},

608
	getPosition: function () {
609

610
		var v1;
M
Mr.doob 已提交
611

612
		return function () {
613

C
Colin Ballast 已提交
614
			if ( v1 === undefined ) v1 = new THREE.Vector3();
615
			THREE.warn( 'THREE.Matrix4: .getPosition() has been removed. Use Vector3.setFromMatrixPosition( matrix ) instead.' );
616

617
			var te = this.elements;
618
			return v1.set( te[ 12 ], te[ 13 ], te[ 14 ] );
619 620 621 622

		};

	}(),
623

624
	setPosition: function ( v ) {
625 626 627

		var te = this.elements;

628 629 630
		te[ 12 ] = v.x;
		te[ 13 ] = v.y;
		te[ 14 ] = v.z;
631

632
		return this;
633

634
	},
635

636 637
	getInverse: function ( m, throwOnInvertible ) {

M
Mr.doob 已提交
638
		// based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
639 640 641
		var te = this.elements;
		var me = m.elements;

642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
		var n11 = me[ 0 ], n12 = me[ 4 ], n13 = me[ 8 ], n14 = me[ 12 ];
		var n21 = me[ 1 ], n22 = me[ 5 ], n23 = me[ 9 ], n24 = me[ 13 ];
		var n31 = me[ 2 ], n32 = me[ 6 ], n33 = me[ 10 ], n34 = me[ 14 ];
		var n41 = me[ 3 ], n42 = me[ 7 ], n43 = me[ 11 ], n44 = me[ 15 ];

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

664
		var det = n11 * te[ 0 ] + n21 * te[ 4 ] + n31 * te[ 8 ] + n41 * te[ 12 ];
665

M
Mr.doob 已提交
666
		if ( det == 0 ) {
667

668
			var msg = "THREE.Matrix4.getInverse(): can't invert matrix, determinant is 0";
669

M
Mr.doob 已提交
670
			if ( throwOnInvertible || false ) {
671

672
				throw new Error( msg );
673

M
Mr.doob 已提交
674
			} else {
675

676
				THREE.warn( msg );
677 678 679 680 681 682

			}

			this.identity();

			return this;
683

684 685
		}

686
		this.multiplyScalar( 1 / det );
687 688

		return this;
689

690
	},
691

692
	translate: function ( v ) {
693

694
		THREE.error( 'THREE.Matrix4: .translate() has been removed.' );
695 696 697 698

	},

	rotateX: function ( angle ) {
699

700
		THREE.error( 'THREE.Matrix4: .rotateX() has been removed.' );
701

702
	},
703 704

	rotateY: function ( angle ) {
705

706
		THREE.error( 'THREE.Matrix4: .rotateY() has been removed.' );
707 708 709 710

	},

	rotateZ: function ( angle ) {
711

712
		THREE.error( 'THREE.Matrix4: .rotateZ() has been removed.' );
713 714 715

	},

716
	rotateByAxis: function ( axis, angle ) {
717

718
		THREE.error( 'THREE.Matrix4: .rotateByAxis() has been removed.' );
719 720 721 722

	},

	scale: function ( v ) {
723 724

		var te = this.elements;
725 726
		var x = v.x, y = v.y, z = v.z;

727 728 729 730
		te[ 0 ] *= x; te[ 4 ] *= y; te[ 8 ] *= z;
		te[ 1 ] *= x; te[ 5 ] *= y; te[ 9 ] *= z;
		te[ 2 ] *= x; te[ 6 ] *= y; te[ 10 ] *= z;
		te[ 3 ] *= x; te[ 7 ] *= y; te[ 11 ] *= z;
731 732 733 734

		return this;

	},
I
ide user ide_gero3 已提交
735

736 737 738
	getMaxScaleOnAxis: function () {

		var te = this.elements;
I
ide user ide_gero3 已提交
739

740 741 742
		var scaleXSq = te[ 0 ] * te[ 0 ] + te[ 1 ] * te[ 1 ] + te[ 2 ] * te[ 2 ];
		var scaleYSq = te[ 4 ] * te[ 4 ] + te[ 5 ] * te[ 5 ] + te[ 6 ] * te[ 6 ];
		var scaleZSq = te[ 8 ] * te[ 8 ] + te[ 9 ] * te[ 9 ] + te[ 10 ] * te[ 10 ];
743 744 745 746

		return Math.sqrt( Math.max( scaleXSq, Math.max( scaleYSq, scaleZSq ) ) );

	},
747

748
	makeTranslation: function ( x, y, z ) {
749 750 751

		this.set(

752 753 754
			1, 0, 0, x,
			0, 1, 0, y,
			0, 0, 1, z,
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
			0, 0, 0, 1

		);

		return this;

	},

	makeRotationX: function ( theta ) {

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

		this.set(

			1, 0,  0, 0,
770
			0, c, - s, 0,
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
			0, s,  c, 0,
			0, 0,  0, 1

		);

		return this;

	},

	makeRotationY: function ( theta ) {

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

		this.set(

			 c, 0, s, 0,
			 0, 1, 0, 0,
788
			- s, 0, c, 0,
789 790 791 792 793 794 795 796 797 798 799 800 801 802
			 0, 0, 0, 1

		);

		return this;

	},

	makeRotationZ: function ( theta ) {

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

		this.set(

803
			c, - s, 0, 0,
804 805 806 807 808 809 810 811 812 813 814 815 816 817
			s,  c, 0, 0,
			0,  0, 1, 0,
			0,  0, 0, 1

		);

		return this;

	},

	makeRotationAxis: function ( axis, angle ) {

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

818 819 820 821 822
		var c = Math.cos( angle );
		var s = Math.sin( angle );
		var t = 1 - c;
		var x = axis.x, y = axis.y, z = axis.z;
		var tx = t * x, ty = t * y;
823 824 825

		this.set(

826
			tx * x + c, tx * y - s * z, tx * z + s * y, 0,
827 828 829 830 831 832 833 834 835 836
			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

		);

		 return this;

	},

837
	makeScale: function ( x, y, z ) {
838 839 840

		this.set(

841 842 843
			x, 0, 0, 0,
			0, y, 0, 0,
			0, 0, z, 0,
844 845 846 847 848 849 850 851
			0, 0, 0, 1

		);

		return this;

	},

M
Mr.doob 已提交
852 853 854 855 856 857 858 859 860 861
	compose: function ( position, quaternion, scale ) {

		this.makeRotationFromQuaternion( quaternion );
		this.scale( scale );
		this.setPosition( position );

		return this;

	},

862 863
	decompose: function () {

864
		var vector, matrix;
865 866 867

		return function ( position, quaternion, scale ) {

C
Colin Ballast 已提交
868 869
			if ( vector === undefined ) vector = new THREE.Vector3();
			if ( matrix === undefined ) matrix = new THREE.Matrix4();
870

871 872
			var te = this.elements;

873 874 875
			var sx = vector.set( te[ 0 ], te[ 1 ], te[ 2 ] ).length();
			var sy = vector.set( te[ 4 ], te[ 5 ], te[ 6 ] ).length();
			var sz = vector.set( te[ 8 ], te[ 9 ], te[ 10 ] ).length();
876

877 878
			// if determine is negative, we need to invert one scale
			var det = this.determinant();
879
			if ( det < 0 ) {
880

881
				sx = - sx;
882

883 884
			}

885 886 887
			position.x = te[ 12 ];
			position.y = te[ 13 ];
			position.z = te[ 14 ];
888 889 890 891 892

			// scale the rotation part

			matrix.elements.set( this.elements ); // at this point matrix is incomplete so we can't use .copy()

893 894 895
			var invSX = 1 / sx;
			var invSY = 1 / sy;
			var invSZ = 1 / sz;
896

897 898 899
			matrix.elements[ 0 ] *= invSX;
			matrix.elements[ 1 ] *= invSX;
			matrix.elements[ 2 ] *= invSX;
900

901 902 903
			matrix.elements[ 4 ] *= invSY;
			matrix.elements[ 5 ] *= invSY;
			matrix.elements[ 6 ] *= invSY;
904

905 906 907
			matrix.elements[ 8 ] *= invSZ;
			matrix.elements[ 9 ] *= invSZ;
			matrix.elements[ 10 ] *= invSZ;
908 909 910 911 912 913 914 915 916 917 918 919 920

			quaternion.setFromRotationMatrix( matrix );

			scale.x = sx;
			scale.y = sy;
			scale.z = sz;

			return this;

		};

	}(),

921
	makeFrustum: function ( left, right, bottom, top, near, far ) {
922 923

		var te = this.elements;
924 925 926 927 928 929 930 931
		var x = 2 * near / ( right - left );
		var y = 2 * near / ( top - bottom );

		var a = ( right + left ) / ( right - left );
		var b = ( top + bottom ) / ( top - bottom );
		var c = - ( far + near ) / ( far - near );
		var d = - 2 * far * near / ( far - near );

932 933 934 935
		te[ 0 ] = x;	te[ 4 ] = 0;	te[ 8 ] = a;	te[ 12 ] = 0;
		te[ 1 ] = 0;	te[ 5 ] = y;	te[ 9 ] = b;	te[ 13 ] = 0;
		te[ 2 ] = 0;	te[ 6 ] = 0;	te[ 10 ] = c;	te[ 14 ] = d;
		te[ 3 ] = 0;	te[ 7 ] = 0;	te[ 11 ] = - 1;	te[ 15 ] = 0;
936 937 938 939 940 941 942

		return this;

	},

	makePerspective: function ( fov, aspect, near, far ) {

943
		var ymax = near * Math.tan( THREE.Math.degToRad( fov * 0.5 ) );
944 945 946 947 948 949 950 951 952
		var ymin = - ymax;
		var xmin = ymin * aspect;
		var xmax = ymax * aspect;

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

	},

	makeOrthographic: function ( left, right, top, bottom, near, far ) {
953 954

		var te = this.elements;
955 956 957 958 959 960 961 962
		var w = right - left;
		var h = top - bottom;
		var p = far - near;

		var x = ( right + left ) / w;
		var y = ( top + bottom ) / h;
		var z = ( far + near ) / p;

963 964 965 966
		te[ 0 ] = 2 / w;	te[ 4 ] = 0;	te[ 8 ] = 0;	te[ 12 ] = - x;
		te[ 1 ] = 0;	te[ 5 ] = 2 / h;	te[ 9 ] = 0;	te[ 13 ] = - y;
		te[ 2 ] = 0;	te[ 6 ] = 0;	te[ 10 ] = - 2 / p;	te[ 14 ] = - z;
		te[ 3 ] = 0;	te[ 7 ] = 0;	te[ 11 ] = 0;	te[ 15 ] = 1;
967 968 969

		return this;

M
Mr.doob 已提交
970 971
	},

972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
	fromArray: function ( array ) {

		this.elements.set( array );

		return this;

	},

	toArray: function () {

		var te = this.elements;

		return [
			te[ 0 ], te[ 1 ], te[ 2 ], te[ 3 ],
			te[ 4 ], te[ 5 ], te[ 6 ], te[ 7 ],
			te[ 8 ], te[ 9 ], te[ 10 ], te[ 11 ],
			te[ 12 ], te[ 13 ], te[ 14 ], te[ 15 ]
		];

	},

M
Mr.doob 已提交
993
	clone: function () {
994

995
		return new THREE.Matrix4().fromArray( this.elements );
M
Mr.doob 已提交
996

997
	}
P
philogb 已提交
998

999
};