1 | /**
|
---|
2 | * @author mrdoob / http://mrdoob.com/
|
---|
3 | * @author supereggbert / http://www.paulbrunt.co.uk/
|
---|
4 | * @author philogb / http://blog.thejit.org/
|
---|
5 | * @author jordi_ros / http://plattsoft.com
|
---|
6 | * @author D1plo1d / http://github.com/D1plo1d
|
---|
7 | * @author alteredq / http://alteredqualia.com/
|
---|
8 | * @author mikael emtinger / http://gomo.se/
|
---|
9 | * @author timknip / http://www.floorplanner.com/
|
---|
10 | * @author bhouston / http://exocortex.com
|
---|
11 | * @author WestLangley / http://github.com/WestLangley
|
---|
12 | */
|
---|
13 |
|
---|
14 |
|
---|
15 | THREE.Matrix4 = function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
|
---|
16 |
|
---|
17 | this.elements = new Float32Array( 16 );
|
---|
18 |
|
---|
19 | // TODO: if n11 is undefined, then just set to identity, otherwise copy all other values into matrix
|
---|
20 | // we should not support semi specification of Matrix4, it is just weird.
|
---|
21 |
|
---|
22 | var te = this.elements;
|
---|
23 |
|
---|
24 | te[0] = ( n11 !== undefined ) ? n11 : 1; te[4] = n12 || 0; te[8] = n13 || 0; te[12] = n14 || 0;
|
---|
25 | te[1] = n21 || 0; te[5] = ( n22 !== undefined ) ? n22 : 1; te[9] = n23 || 0; te[13] = n24 || 0;
|
---|
26 | te[2] = n31 || 0; te[6] = n32 || 0; te[10] = ( n33 !== undefined ) ? n33 : 1; te[14] = n34 || 0;
|
---|
27 | te[3] = n41 || 0; te[7] = n42 || 0; te[11] = n43 || 0; te[15] = ( n44 !== undefined ) ? n44 : 1;
|
---|
28 |
|
---|
29 | };
|
---|
30 |
|
---|
31 | THREE.Matrix4.prototype = {
|
---|
32 |
|
---|
33 | constructor: THREE.Matrix4,
|
---|
34 |
|
---|
35 | set: function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
|
---|
36 |
|
---|
37 | var te = this.elements;
|
---|
38 |
|
---|
39 | te[0] = n11; te[4] = n12; te[8] = n13; te[12] = n14;
|
---|
40 | te[1] = n21; te[5] = n22; te[9] = n23; te[13] = n24;
|
---|
41 | te[2] = n31; te[6] = n32; te[10] = n33; te[14] = n34;
|
---|
42 | te[3] = n41; te[7] = n42; te[11] = n43; te[15] = n44;
|
---|
43 |
|
---|
44 | return this;
|
---|
45 |
|
---|
46 | },
|
---|
47 |
|
---|
48 | identity: function () {
|
---|
49 |
|
---|
50 | this.set(
|
---|
51 |
|
---|
52 | 1, 0, 0, 0,
|
---|
53 | 0, 1, 0, 0,
|
---|
54 | 0, 0, 1, 0,
|
---|
55 | 0, 0, 0, 1
|
---|
56 |
|
---|
57 | );
|
---|
58 |
|
---|
59 | return this;
|
---|
60 |
|
---|
61 | },
|
---|
62 |
|
---|
63 | copy: function ( m ) {
|
---|
64 |
|
---|
65 | this.elements.set( m.elements );
|
---|
66 |
|
---|
67 | return this;
|
---|
68 |
|
---|
69 | },
|
---|
70 |
|
---|
71 | extractPosition: function ( m ) {
|
---|
72 |
|
---|
73 | console.warn( 'DEPRECATED: Matrix4\'s .extractPosition() has been renamed to .copyPosition().' );
|
---|
74 | return this.copyPosition( m );
|
---|
75 |
|
---|
76 | },
|
---|
77 |
|
---|
78 | copyPosition: function ( m ) {
|
---|
79 |
|
---|
80 | var te = this.elements;
|
---|
81 | var me = m.elements;
|
---|
82 |
|
---|
83 | te[12] = me[12];
|
---|
84 | te[13] = me[13];
|
---|
85 | te[14] = me[14];
|
---|
86 |
|
---|
87 | return this;
|
---|
88 |
|
---|
89 | },
|
---|
90 |
|
---|
91 | extractRotation: function () {
|
---|
92 |
|
---|
93 | var v1 = new THREE.Vector3();
|
---|
94 |
|
---|
95 | return function ( m ) {
|
---|
96 |
|
---|
97 | var te = this.elements;
|
---|
98 | var me = m.elements;
|
---|
99 |
|
---|
100 | var scaleX = 1 / v1.set( me[0], me[1], me[2] ).length();
|
---|
101 | var scaleY = 1 / v1.set( me[4], me[5], me[6] ).length();
|
---|
102 | var scaleZ = 1 / v1.set( me[8], me[9], me[10] ).length();
|
---|
103 |
|
---|
104 | te[0] = me[0] * scaleX;
|
---|
105 | te[1] = me[1] * scaleX;
|
---|
106 | te[2] = me[2] * scaleX;
|
---|
107 |
|
---|
108 | te[4] = me[4] * scaleY;
|
---|
109 | te[5] = me[5] * scaleY;
|
---|
110 | te[6] = me[6] * scaleY;
|
---|
111 |
|
---|
112 | te[8] = me[8] * scaleZ;
|
---|
113 | te[9] = me[9] * scaleZ;
|
---|
114 | te[10] = me[10] * scaleZ;
|
---|
115 |
|
---|
116 | return this;
|
---|
117 |
|
---|
118 | };
|
---|
119 |
|
---|
120 | }(),
|
---|
121 |
|
---|
122 | makeRotationFromEuler: function ( euler ) {
|
---|
123 |
|
---|
124 | if ( euler instanceof THREE.Euler === false ) {
|
---|
125 |
|
---|
126 | console.error( 'ERROR: Matrix\'s .makeRotationFromEuler() now expects a Euler rotation rather than a Vector3 and order. Please update your code.' );
|
---|
127 |
|
---|
128 | }
|
---|
129 |
|
---|
130 | var te = this.elements;
|
---|
131 |
|
---|
132 | var x = euler.x, y = euler.y, z = euler.z;
|
---|
133 | var a = Math.cos( x ), b = Math.sin( x );
|
---|
134 | var c = Math.cos( y ), d = Math.sin( y );
|
---|
135 | var e = Math.cos( z ), f = Math.sin( z );
|
---|
136 |
|
---|
137 | if ( euler.order === 'XYZ' ) {
|
---|
138 |
|
---|
139 | var ae = a * e, af = a * f, be = b * e, bf = b * f;
|
---|
140 |
|
---|
141 | te[0] = c * e;
|
---|
142 | te[4] = - c * f;
|
---|
143 | te[8] = d;
|
---|
144 |
|
---|
145 | te[1] = af + be * d;
|
---|
146 | te[5] = ae - bf * d;
|
---|
147 | te[9] = - b * c;
|
---|
148 |
|
---|
149 | te[2] = bf - ae * d;
|
---|
150 | te[6] = be + af * d;
|
---|
151 | te[10] = a * c;
|
---|
152 |
|
---|
153 | } else if ( euler.order === 'YXZ' ) {
|
---|
154 |
|
---|
155 | var ce = c * e, cf = c * f, de = d * e, df = d * f;
|
---|
156 |
|
---|
157 | te[0] = ce + df * b;
|
---|
158 | te[4] = de * b - cf;
|
---|
159 | te[8] = a * d;
|
---|
160 |
|
---|
161 | te[1] = a * f;
|
---|
162 | te[5] = a * e;
|
---|
163 | te[9] = - b;
|
---|
164 |
|
---|
165 | te[2] = cf * b - de;
|
---|
166 | te[6] = df + ce * b;
|
---|
167 | te[10] = a * c;
|
---|
168 |
|
---|
169 | } else if ( euler.order === 'ZXY' ) {
|
---|
170 |
|
---|
171 | var ce = c * e, cf = c * f, de = d * e, df = d * f;
|
---|
172 |
|
---|
173 | te[0] = ce - df * b;
|
---|
174 | te[4] = - a * f;
|
---|
175 | te[8] = de + cf * b;
|
---|
176 |
|
---|
177 | te[1] = cf + de * b;
|
---|
178 | te[5] = a * e;
|
---|
179 | te[9] = df - ce * b;
|
---|
180 |
|
---|
181 | te[2] = - a * d;
|
---|
182 | te[6] = b;
|
---|
183 | te[10] = a * c;
|
---|
184 |
|
---|
185 | } else if ( euler.order === 'ZYX' ) {
|
---|
186 |
|
---|
187 | var ae = a * e, af = a * f, be = b * e, bf = b * f;
|
---|
188 |
|
---|
189 | te[0] = c * e;
|
---|
190 | te[4] = be * d - af;
|
---|
191 | te[8] = ae * d + bf;
|
---|
192 |
|
---|
193 | te[1] = c * f;
|
---|
194 | te[5] = bf * d + ae;
|
---|
195 | te[9] = af * d - be;
|
---|
196 |
|
---|
197 | te[2] = - d;
|
---|
198 | te[6] = b * c;
|
---|
199 | te[10] = a * c;
|
---|
200 |
|
---|
201 | } else if ( euler.order === 'YZX' ) {
|
---|
202 |
|
---|
203 | var ac = a * c, ad = a * d, bc = b * c, bd = b * d;
|
---|
204 |
|
---|
205 | te[0] = c * e;
|
---|
206 | te[4] = bd - ac * f;
|
---|
207 | te[8] = bc * f + ad;
|
---|
208 |
|
---|
209 | te[1] = f;
|
---|
210 | te[5] = a * e;
|
---|
211 | te[9] = - b * e;
|
---|
212 |
|
---|
213 | te[2] = - d * e;
|
---|
214 | te[6] = ad * f + bc;
|
---|
215 | te[10] = ac - bd * f;
|
---|
216 |
|
---|
217 | } else if ( euler.order === 'XZY' ) {
|
---|
218 |
|
---|
219 | var ac = a * c, ad = a * d, bc = b * c, bd = b * d;
|
---|
220 |
|
---|
221 | te[0] = c * e;
|
---|
222 | te[4] = - f;
|
---|
223 | te[8] = d * e;
|
---|
224 |
|
---|
225 | te[1] = ac * f + bd;
|
---|
226 | te[5] = a * e;
|
---|
227 | te[9] = ad * f - bc;
|
---|
228 |
|
---|
229 | te[2] = bc * f - ad;
|
---|
230 | te[6] = b * e;
|
---|
231 | te[10] = bd * f + ac;
|
---|
232 |
|
---|
233 | }
|
---|
234 |
|
---|
235 | // last column
|
---|
236 | te[3] = 0;
|
---|
237 | te[7] = 0;
|
---|
238 | te[11] = 0;
|
---|
239 |
|
---|
240 | // bottom row
|
---|
241 | te[12] = 0;
|
---|
242 | te[13] = 0;
|
---|
243 | te[14] = 0;
|
---|
244 | te[15] = 1;
|
---|
245 |
|
---|
246 | return this;
|
---|
247 |
|
---|
248 | },
|
---|
249 |
|
---|
250 | setRotationFromQuaternion: function ( q ) {
|
---|
251 |
|
---|
252 | console.warn( 'DEPRECATED: Matrix4\'s .setRotationFromQuaternion() has been deprecated in favor of makeRotationFromQuaternion. Please update your code.' );
|
---|
253 |
|
---|
254 | return this.makeRotationFromQuaternion( q );
|
---|
255 |
|
---|
256 | },
|
---|
257 |
|
---|
258 | makeRotationFromQuaternion: function ( q ) {
|
---|
259 |
|
---|
260 | var te = this.elements;
|
---|
261 |
|
---|
262 | var x = q.x, y = q.y, z = q.z, w = q.w;
|
---|
263 | var x2 = x + x, y2 = y + y, z2 = z + z;
|
---|
264 | var xx = x * x2, xy = x * y2, xz = x * z2;
|
---|
265 | var yy = y * y2, yz = y * z2, zz = z * z2;
|
---|
266 | var wx = w * x2, wy = w * y2, wz = w * z2;
|
---|
267 |
|
---|
268 | te[0] = 1 - ( yy + zz );
|
---|
269 | te[4] = xy - wz;
|
---|
270 | te[8] = xz + wy;
|
---|
271 |
|
---|
272 | te[1] = xy + wz;
|
---|
273 | te[5] = 1 - ( xx + zz );
|
---|
274 | te[9] = yz - wx;
|
---|
275 |
|
---|
276 | te[2] = xz - wy;
|
---|
277 | te[6] = yz + wx;
|
---|
278 | te[10] = 1 - ( xx + yy );
|
---|
279 |
|
---|
280 | // last column
|
---|
281 | te[3] = 0;
|
---|
282 | te[7] = 0;
|
---|
283 | te[11] = 0;
|
---|
284 |
|
---|
285 | // bottom row
|
---|
286 | te[12] = 0;
|
---|
287 | te[13] = 0;
|
---|
288 | te[14] = 0;
|
---|
289 | te[15] = 1;
|
---|
290 |
|
---|
291 | return this;
|
---|
292 |
|
---|
293 | },
|
---|
294 |
|
---|
295 | lookAt: function() {
|
---|
296 |
|
---|
297 | var x = new THREE.Vector3();
|
---|
298 | var y = new THREE.Vector3();
|
---|
299 | var z = new THREE.Vector3();
|
---|
300 |
|
---|
301 | return function ( eye, target, up ) {
|
---|
302 |
|
---|
303 | var te = this.elements;
|
---|
304 |
|
---|
305 | z.subVectors( eye, target ).normalize();
|
---|
306 |
|
---|
307 | if ( z.length() === 0 ) {
|
---|
308 |
|
---|
309 | z.z = 1;
|
---|
310 |
|
---|
311 | }
|
---|
312 |
|
---|
313 | x.crossVectors( up, z ).normalize();
|
---|
314 |
|
---|
315 | if ( x.length() === 0 ) {
|
---|
316 |
|
---|
317 | z.x += 0.0001;
|
---|
318 | x.crossVectors( up, z ).normalize();
|
---|
319 |
|
---|
320 | }
|
---|
321 |
|
---|
322 | y.crossVectors( z, x );
|
---|
323 |
|
---|
324 |
|
---|
325 | te[0] = x.x; te[4] = y.x; te[8] = z.x;
|
---|
326 | te[1] = x.y; te[5] = y.y; te[9] = z.y;
|
---|
327 | te[2] = x.z; te[6] = y.z; te[10] = z.z;
|
---|
328 |
|
---|
329 | return this;
|
---|
330 |
|
---|
331 | };
|
---|
332 |
|
---|
333 | }(),
|
---|
334 |
|
---|
335 | multiply: function ( m, n ) {
|
---|
336 |
|
---|
337 | if ( n !== undefined ) {
|
---|
338 |
|
---|
339 | console.warn( 'DEPRECATED: Matrix4\'s .multiply() now only accepts one argument. Use .multiplyMatrices( a, b ) instead.' );
|
---|
340 | return this.multiplyMatrices( m, n );
|
---|
341 |
|
---|
342 | }
|
---|
343 |
|
---|
344 | return this.multiplyMatrices( this, m );
|
---|
345 |
|
---|
346 | },
|
---|
347 |
|
---|
348 | multiplyMatrices: function ( a, b ) {
|
---|
349 |
|
---|
350 | var ae = a.elements;
|
---|
351 | var be = b.elements;
|
---|
352 | var te = this.elements;
|
---|
353 |
|
---|
354 | var a11 = ae[0], a12 = ae[4], a13 = ae[8], a14 = ae[12];
|
---|
355 | var a21 = ae[1], a22 = ae[5], a23 = ae[9], a24 = ae[13];
|
---|
356 | var a31 = ae[2], a32 = ae[6], a33 = ae[10], a34 = ae[14];
|
---|
357 | var a41 = ae[3], a42 = ae[7], a43 = ae[11], a44 = ae[15];
|
---|
358 |
|
---|
359 | var b11 = be[0], b12 = be[4], b13 = be[8], b14 = be[12];
|
---|
360 | var b21 = be[1], b22 = be[5], b23 = be[9], b24 = be[13];
|
---|
361 | var b31 = be[2], b32 = be[6], b33 = be[10], b34 = be[14];
|
---|
362 | var b41 = be[3], b42 = be[7], b43 = be[11], b44 = be[15];
|
---|
363 |
|
---|
364 | te[0] = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
|
---|
365 | te[4] = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
|
---|
366 | te[8] = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
|
---|
367 | te[12] = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
|
---|
368 |
|
---|
369 | te[1] = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
|
---|
370 | te[5] = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
|
---|
371 | te[9] = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
|
---|
372 | te[13] = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
|
---|
373 |
|
---|
374 | te[2] = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
|
---|
375 | te[6] = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
|
---|
376 | te[10] = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
|
---|
377 | te[14] = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
|
---|
378 |
|
---|
379 | te[3] = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
|
---|
380 | te[7] = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
|
---|
381 | te[11] = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
|
---|
382 | te[15] = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
|
---|
383 |
|
---|
384 | return this;
|
---|
385 |
|
---|
386 | },
|
---|
387 |
|
---|
388 | multiplyToArray: function ( a, b, r ) {
|
---|
389 |
|
---|
390 | var te = this.elements;
|
---|
391 |
|
---|
392 | this.multiplyMatrices( a, b );
|
---|
393 |
|
---|
394 | r[ 0 ] = te[0]; r[ 1 ] = te[1]; r[ 2 ] = te[2]; r[ 3 ] = te[3];
|
---|
395 | r[ 4 ] = te[4]; r[ 5 ] = te[5]; r[ 6 ] = te[6]; r[ 7 ] = te[7];
|
---|
396 | r[ 8 ] = te[8]; r[ 9 ] = te[9]; r[ 10 ] = te[10]; r[ 11 ] = te[11];
|
---|
397 | r[ 12 ] = te[12]; r[ 13 ] = te[13]; r[ 14 ] = te[14]; r[ 15 ] = te[15];
|
---|
398 |
|
---|
399 | return this;
|
---|
400 |
|
---|
401 | },
|
---|
402 |
|
---|
403 | multiplyScalar: function ( s ) {
|
---|
404 |
|
---|
405 | var te = this.elements;
|
---|
406 |
|
---|
407 | te[0] *= s; te[4] *= s; te[8] *= s; te[12] *= s;
|
---|
408 | te[1] *= s; te[5] *= s; te[9] *= s; te[13] *= s;
|
---|
409 | te[2] *= s; te[6] *= s; te[10] *= s; te[14] *= s;
|
---|
410 | te[3] *= s; te[7] *= s; te[11] *= s; te[15] *= s;
|
---|
411 |
|
---|
412 | return this;
|
---|
413 |
|
---|
414 | },
|
---|
415 |
|
---|
416 | multiplyVector3: function ( vector ) {
|
---|
417 |
|
---|
418 | console.warn( 'DEPRECATED: Matrix4\'s .multiplyVector3() has been removed. Use vector.applyMatrix4( matrix ) or vector.applyProjection( matrix ) instead.' );
|
---|
419 | return vector.applyProjection( this );
|
---|
420 |
|
---|
421 | },
|
---|
422 |
|
---|
423 | multiplyVector4: function ( vector ) {
|
---|
424 |
|
---|
425 | console.warn( 'DEPRECATED: Matrix4\'s .multiplyVector4() has been removed. Use vector.applyMatrix4( matrix ) instead.' );
|
---|
426 | return vector.applyMatrix4( this );
|
---|
427 |
|
---|
428 | },
|
---|
429 |
|
---|
430 | multiplyVector3Array: function() {
|
---|
431 |
|
---|
432 | var v1 = new THREE.Vector3();
|
---|
433 |
|
---|
434 | return function ( a ) {
|
---|
435 |
|
---|
436 | for ( var i = 0, il = a.length; i < il; i += 3 ) {
|
---|
437 |
|
---|
438 | v1.x = a[ i ];
|
---|
439 | v1.y = a[ i + 1 ];
|
---|
440 | v1.z = a[ i + 2 ];
|
---|
441 |
|
---|
442 | v1.applyProjection( this );
|
---|
443 |
|
---|
444 | a[ i ] = v1.x;
|
---|
445 | a[ i + 1 ] = v1.y;
|
---|
446 | a[ i + 2 ] = v1.z;
|
---|
447 |
|
---|
448 | }
|
---|
449 |
|
---|
450 | return a;
|
---|
451 |
|
---|
452 | };
|
---|
453 |
|
---|
454 | }(),
|
---|
455 |
|
---|
456 | rotateAxis: function ( v ) {
|
---|
457 |
|
---|
458 | console.warn( 'DEPRECATED: Matrix4\'s .rotateAxis() has been removed. Use Vector3.transformDirection( matrix ) instead.' );
|
---|
459 |
|
---|
460 | v.transformDirection( this );
|
---|
461 |
|
---|
462 | },
|
---|
463 |
|
---|
464 | crossVector: function ( vector ) {
|
---|
465 |
|
---|
466 | console.warn( 'DEPRECATED: Matrix4\'s .crossVector() has been removed. Use vector.applyMatrix4( matrix ) instead.' );
|
---|
467 | return vector.applyMatrix4( this );
|
---|
468 |
|
---|
469 | },
|
---|
470 |
|
---|
471 | determinant: function () {
|
---|
472 |
|
---|
473 | var te = this.elements;
|
---|
474 |
|
---|
475 | var n11 = te[0], n12 = te[4], n13 = te[8], n14 = te[12];
|
---|
476 | var n21 = te[1], n22 = te[5], n23 = te[9], n24 = te[13];
|
---|
477 | var n31 = te[2], n32 = te[6], n33 = te[10], n34 = te[14];
|
---|
478 | var n41 = te[3], n42 = te[7], n43 = te[11], n44 = te[15];
|
---|
479 |
|
---|
480 | //TODO: make this more efficient
|
---|
481 | //( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm )
|
---|
482 |
|
---|
483 | return (
|
---|
484 | n41 * (
|
---|
485 | +n14 * n23 * n32
|
---|
486 | -n13 * n24 * n32
|
---|
487 | -n14 * n22 * n33
|
---|
488 | +n12 * n24 * n33
|
---|
489 | +n13 * n22 * n34
|
---|
490 | -n12 * n23 * n34
|
---|
491 | ) +
|
---|
492 | n42 * (
|
---|
493 | +n11 * n23 * n34
|
---|
494 | -n11 * n24 * n33
|
---|
495 | +n14 * n21 * n33
|
---|
496 | -n13 * n21 * n34
|
---|
497 | +n13 * n24 * n31
|
---|
498 | -n14 * n23 * n31
|
---|
499 | ) +
|
---|
500 | n43 * (
|
---|
501 | +n11 * n24 * n32
|
---|
502 | -n11 * n22 * n34
|
---|
503 | -n14 * n21 * n32
|
---|
504 | +n12 * n21 * n34
|
---|
505 | +n14 * n22 * n31
|
---|
506 | -n12 * n24 * n31
|
---|
507 | ) +
|
---|
508 | n44 * (
|
---|
509 | -n13 * n22 * n31
|
---|
510 | -n11 * n23 * n32
|
---|
511 | +n11 * n22 * n33
|
---|
512 | +n13 * n21 * n32
|
---|
513 | -n12 * n21 * n33
|
---|
514 | +n12 * n23 * n31
|
---|
515 | )
|
---|
516 |
|
---|
517 | );
|
---|
518 |
|
---|
519 | },
|
---|
520 |
|
---|
521 | transpose: function () {
|
---|
522 |
|
---|
523 | var te = this.elements;
|
---|
524 | var tmp;
|
---|
525 |
|
---|
526 | tmp = te[1]; te[1] = te[4]; te[4] = tmp;
|
---|
527 | tmp = te[2]; te[2] = te[8]; te[8] = tmp;
|
---|
528 | tmp = te[6]; te[6] = te[9]; te[9] = tmp;
|
---|
529 |
|
---|
530 | tmp = te[3]; te[3] = te[12]; te[12] = tmp;
|
---|
531 | tmp = te[7]; te[7] = te[13]; te[13] = tmp;
|
---|
532 | tmp = te[11]; te[11] = te[14]; te[14] = tmp;
|
---|
533 |
|
---|
534 | return this;
|
---|
535 |
|
---|
536 | },
|
---|
537 |
|
---|
538 | flattenToArray: function ( flat ) {
|
---|
539 |
|
---|
540 | var te = this.elements;
|
---|
541 | flat[ 0 ] = te[0]; flat[ 1 ] = te[1]; flat[ 2 ] = te[2]; flat[ 3 ] = te[3];
|
---|
542 | flat[ 4 ] = te[4]; flat[ 5 ] = te[5]; flat[ 6 ] = te[6]; flat[ 7 ] = te[7];
|
---|
543 | flat[ 8 ] = te[8]; flat[ 9 ] = te[9]; flat[ 10 ] = te[10]; flat[ 11 ] = te[11];
|
---|
544 | flat[ 12 ] = te[12]; flat[ 13 ] = te[13]; flat[ 14 ] = te[14]; flat[ 15 ] = te[15];
|
---|
545 |
|
---|
546 | return flat;
|
---|
547 |
|
---|
548 | },
|
---|
549 |
|
---|
550 | flattenToArrayOffset: function( flat, offset ) {
|
---|
551 |
|
---|
552 | var te = this.elements;
|
---|
553 | flat[ offset ] = te[0];
|
---|
554 | flat[ offset + 1 ] = te[1];
|
---|
555 | flat[ offset + 2 ] = te[2];
|
---|
556 | flat[ offset + 3 ] = te[3];
|
---|
557 |
|
---|
558 | flat[ offset + 4 ] = te[4];
|
---|
559 | flat[ offset + 5 ] = te[5];
|
---|
560 | flat[ offset + 6 ] = te[6];
|
---|
561 | flat[ offset + 7 ] = te[7];
|
---|
562 |
|
---|
563 | flat[ offset + 8 ] = te[8];
|
---|
564 | flat[ offset + 9 ] = te[9];
|
---|
565 | flat[ offset + 10 ] = te[10];
|
---|
566 | flat[ offset + 11 ] = te[11];
|
---|
567 |
|
---|
568 | flat[ offset + 12 ] = te[12];
|
---|
569 | flat[ offset + 13 ] = te[13];
|
---|
570 | flat[ offset + 14 ] = te[14];
|
---|
571 | flat[ offset + 15 ] = te[15];
|
---|
572 |
|
---|
573 | return flat;
|
---|
574 |
|
---|
575 | },
|
---|
576 |
|
---|
577 | getPosition: function() {
|
---|
578 |
|
---|
579 | var v1 = new THREE.Vector3();
|
---|
580 |
|
---|
581 | return function () {
|
---|
582 |
|
---|
583 | console.warn( 'DEPRECATED: Matrix4\'s .getPosition() has been removed. Use Vector3.setFromMatrixPosition( matrix ) instead.' );
|
---|
584 |
|
---|
585 | var te = this.elements;
|
---|
586 | return v1.set( te[12], te[13], te[14] );
|
---|
587 |
|
---|
588 | };
|
---|
589 |
|
---|
590 | }(),
|
---|
591 |
|
---|
592 | setPosition: function ( v ) {
|
---|
593 |
|
---|
594 | var te = this.elements;
|
---|
595 |
|
---|
596 | te[12] = v.x;
|
---|
597 | te[13] = v.y;
|
---|
598 | te[14] = v.z;
|
---|
599 |
|
---|
600 | return this;
|
---|
601 |
|
---|
602 | },
|
---|
603 |
|
---|
604 | getInverse: function ( m, throwOnInvertible ) {
|
---|
605 |
|
---|
606 | // based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
|
---|
607 | var te = this.elements;
|
---|
608 | var me = m.elements;
|
---|
609 |
|
---|
610 | var n11 = me[0], n12 = me[4], n13 = me[8], n14 = me[12];
|
---|
611 | var n21 = me[1], n22 = me[5], n23 = me[9], n24 = me[13];
|
---|
612 | var n31 = me[2], n32 = me[6], n33 = me[10], n34 = me[14];
|
---|
613 | var n41 = me[3], n42 = me[7], n43 = me[11], n44 = me[15];
|
---|
614 |
|
---|
615 | te[0] = n23*n34*n42 - n24*n33*n42 + n24*n32*n43 - n22*n34*n43 - n23*n32*n44 + n22*n33*n44;
|
---|
616 | te[4] = n14*n33*n42 - n13*n34*n42 - n14*n32*n43 + n12*n34*n43 + n13*n32*n44 - n12*n33*n44;
|
---|
617 | te[8] = n13*n24*n42 - n14*n23*n42 + n14*n22*n43 - n12*n24*n43 - n13*n22*n44 + n12*n23*n44;
|
---|
618 | te[12] = n14*n23*n32 - n13*n24*n32 - n14*n22*n33 + n12*n24*n33 + n13*n22*n34 - n12*n23*n34;
|
---|
619 | te[1] = n24*n33*n41 - n23*n34*n41 - n24*n31*n43 + n21*n34*n43 + n23*n31*n44 - n21*n33*n44;
|
---|
620 | te[5] = n13*n34*n41 - n14*n33*n41 + n14*n31*n43 - n11*n34*n43 - n13*n31*n44 + n11*n33*n44;
|
---|
621 | te[9] = n14*n23*n41 - n13*n24*n41 - n14*n21*n43 + n11*n24*n43 + n13*n21*n44 - n11*n23*n44;
|
---|
622 | te[13] = n13*n24*n31 - n14*n23*n31 + n14*n21*n33 - n11*n24*n33 - n13*n21*n34 + n11*n23*n34;
|
---|
623 | te[2] = n22*n34*n41 - n24*n32*n41 + n24*n31*n42 - n21*n34*n42 - n22*n31*n44 + n21*n32*n44;
|
---|
624 | te[6] = n14*n32*n41 - n12*n34*n41 - n14*n31*n42 + n11*n34*n42 + n12*n31*n44 - n11*n32*n44;
|
---|
625 | te[10] = n12*n24*n41 - n14*n22*n41 + n14*n21*n42 - n11*n24*n42 - n12*n21*n44 + n11*n22*n44;
|
---|
626 | te[14] = n14*n22*n31 - n12*n24*n31 - n14*n21*n32 + n11*n24*n32 + n12*n21*n34 - n11*n22*n34;
|
---|
627 | te[3] = n23*n32*n41 - n22*n33*n41 - n23*n31*n42 + n21*n33*n42 + n22*n31*n43 - n21*n32*n43;
|
---|
628 | te[7] = n12*n33*n41 - n13*n32*n41 + n13*n31*n42 - n11*n33*n42 - n12*n31*n43 + n11*n32*n43;
|
---|
629 | te[11] = n13*n22*n41 - n12*n23*n41 - n13*n21*n42 + n11*n23*n42 + n12*n21*n43 - n11*n22*n43;
|
---|
630 | te[15] = n12*n23*n31 - n13*n22*n31 + n13*n21*n32 - n11*n23*n32 - n12*n21*n33 + n11*n22*n33;
|
---|
631 |
|
---|
632 | var det = n11 * te[ 0 ] + n21 * te[ 4 ] + n31 * te[ 8 ] + n41 * te[ 12 ];
|
---|
633 |
|
---|
634 | if ( det == 0 ) {
|
---|
635 |
|
---|
636 | var msg = "Matrix4.getInverse(): can't invert matrix, determinant is 0";
|
---|
637 |
|
---|
638 | if ( throwOnInvertible || false ) {
|
---|
639 |
|
---|
640 | throw new Error( msg );
|
---|
641 |
|
---|
642 | } else {
|
---|
643 |
|
---|
644 | console.warn( msg );
|
---|
645 |
|
---|
646 | }
|
---|
647 |
|
---|
648 | this.identity();
|
---|
649 |
|
---|
650 | return this;
|
---|
651 | }
|
---|
652 |
|
---|
653 | this.multiplyScalar( 1 / det );
|
---|
654 |
|
---|
655 | return this;
|
---|
656 |
|
---|
657 | },
|
---|
658 |
|
---|
659 | translate: function ( v ) {
|
---|
660 |
|
---|
661 | console.warn( 'DEPRECATED: Matrix4\'s .translate() has been removed.');
|
---|
662 |
|
---|
663 | },
|
---|
664 |
|
---|
665 | rotateX: function ( angle ) {
|
---|
666 |
|
---|
667 | console.warn( 'DEPRECATED: Matrix4\'s .rotateX() has been removed.');
|
---|
668 |
|
---|
669 | },
|
---|
670 |
|
---|
671 | rotateY: function ( angle ) {
|
---|
672 |
|
---|
673 | console.warn( 'DEPRECATED: Matrix4\'s .rotateY() has been removed.');
|
---|
674 |
|
---|
675 | },
|
---|
676 |
|
---|
677 | rotateZ: function ( angle ) {
|
---|
678 |
|
---|
679 | console.warn( 'DEPRECATED: Matrix4\'s .rotateZ() has been removed.');
|
---|
680 |
|
---|
681 | },
|
---|
682 |
|
---|
683 | rotateByAxis: function ( axis, angle ) {
|
---|
684 |
|
---|
685 | console.warn( 'DEPRECATED: Matrix4\'s .rotateByAxis() has been removed.');
|
---|
686 |
|
---|
687 | },
|
---|
688 |
|
---|
689 | scale: function ( v ) {
|
---|
690 |
|
---|
691 | var te = this.elements;
|
---|
692 | var x = v.x, y = v.y, z = v.z;
|
---|
693 |
|
---|
694 | te[0] *= x; te[4] *= y; te[8] *= z;
|
---|
695 | te[1] *= x; te[5] *= y; te[9] *= z;
|
---|
696 | te[2] *= x; te[6] *= y; te[10] *= z;
|
---|
697 | te[3] *= x; te[7] *= y; te[11] *= z;
|
---|
698 |
|
---|
699 | return this;
|
---|
700 |
|
---|
701 | },
|
---|
702 |
|
---|
703 | getMaxScaleOnAxis: function () {
|
---|
704 |
|
---|
705 | var te = this.elements;
|
---|
706 |
|
---|
707 | var scaleXSq = te[0] * te[0] + te[1] * te[1] + te[2] * te[2];
|
---|
708 | var scaleYSq = te[4] * te[4] + te[5] * te[5] + te[6] * te[6];
|
---|
709 | var scaleZSq = te[8] * te[8] + te[9] * te[9] + te[10] * te[10];
|
---|
710 |
|
---|
711 | return Math.sqrt( Math.max( scaleXSq, Math.max( scaleYSq, scaleZSq ) ) );
|
---|
712 |
|
---|
713 | },
|
---|
714 |
|
---|
715 | makeTranslation: function ( x, y, z ) {
|
---|
716 |
|
---|
717 | this.set(
|
---|
718 |
|
---|
719 | 1, 0, 0, x,
|
---|
720 | 0, 1, 0, y,
|
---|
721 | 0, 0, 1, z,
|
---|
722 | 0, 0, 0, 1
|
---|
723 |
|
---|
724 | );
|
---|
725 |
|
---|
726 | return this;
|
---|
727 |
|
---|
728 | },
|
---|
729 |
|
---|
730 | makeRotationX: function ( theta ) {
|
---|
731 |
|
---|
732 | var c = Math.cos( theta ), s = Math.sin( theta );
|
---|
733 |
|
---|
734 | this.set(
|
---|
735 |
|
---|
736 | 1, 0, 0, 0,
|
---|
737 | 0, c, -s, 0,
|
---|
738 | 0, s, c, 0,
|
---|
739 | 0, 0, 0, 1
|
---|
740 |
|
---|
741 | );
|
---|
742 |
|
---|
743 | return this;
|
---|
744 |
|
---|
745 | },
|
---|
746 |
|
---|
747 | makeRotationY: function ( theta ) {
|
---|
748 |
|
---|
749 | var c = Math.cos( theta ), s = Math.sin( theta );
|
---|
750 |
|
---|
751 | this.set(
|
---|
752 |
|
---|
753 | c, 0, s, 0,
|
---|
754 | 0, 1, 0, 0,
|
---|
755 | -s, 0, c, 0,
|
---|
756 | 0, 0, 0, 1
|
---|
757 |
|
---|
758 | );
|
---|
759 |
|
---|
760 | return this;
|
---|
761 |
|
---|
762 | },
|
---|
763 |
|
---|
764 | makeRotationZ: function ( theta ) {
|
---|
765 |
|
---|
766 | var c = Math.cos( theta ), s = Math.sin( theta );
|
---|
767 |
|
---|
768 | this.set(
|
---|
769 |
|
---|
770 | c, -s, 0, 0,
|
---|
771 | s, c, 0, 0,
|
---|
772 | 0, 0, 1, 0,
|
---|
773 | 0, 0, 0, 1
|
---|
774 |
|
---|
775 | );
|
---|
776 |
|
---|
777 | return this;
|
---|
778 |
|
---|
779 | },
|
---|
780 |
|
---|
781 | makeRotationAxis: function ( axis, angle ) {
|
---|
782 |
|
---|
783 | // Based on http://www.gamedev.net/reference/articles/article1199.asp
|
---|
784 |
|
---|
785 | var c = Math.cos( angle );
|
---|
786 | var s = Math.sin( angle );
|
---|
787 | var t = 1 - c;
|
---|
788 | var x = axis.x, y = axis.y, z = axis.z;
|
---|
789 | var tx = t * x, ty = t * y;
|
---|
790 |
|
---|
791 | this.set(
|
---|
792 |
|
---|
793 | tx * x + c, tx * y - s * z, tx * z + s * y, 0,
|
---|
794 | tx * y + s * z, ty * y + c, ty * z - s * x, 0,
|
---|
795 | tx * z - s * y, ty * z + s * x, t * z * z + c, 0,
|
---|
796 | 0, 0, 0, 1
|
---|
797 |
|
---|
798 | );
|
---|
799 |
|
---|
800 | return this;
|
---|
801 |
|
---|
802 | },
|
---|
803 |
|
---|
804 | makeScale: function ( x, y, z ) {
|
---|
805 |
|
---|
806 | this.set(
|
---|
807 |
|
---|
808 | x, 0, 0, 0,
|
---|
809 | 0, y, 0, 0,
|
---|
810 | 0, 0, z, 0,
|
---|
811 | 0, 0, 0, 1
|
---|
812 |
|
---|
813 | );
|
---|
814 |
|
---|
815 | return this;
|
---|
816 |
|
---|
817 | },
|
---|
818 |
|
---|
819 | compose: function ( position, quaternion, scale ) {
|
---|
820 |
|
---|
821 | this.makeRotationFromQuaternion( quaternion );
|
---|
822 | this.scale( scale );
|
---|
823 | this.setPosition( position );
|
---|
824 |
|
---|
825 | return this;
|
---|
826 |
|
---|
827 | },
|
---|
828 |
|
---|
829 | decompose: function () {
|
---|
830 |
|
---|
831 | var vector = new THREE.Vector3();
|
---|
832 | var matrix = new THREE.Matrix4();
|
---|
833 |
|
---|
834 | return function ( position, quaternion, scale ) {
|
---|
835 |
|
---|
836 | var te = this.elements;
|
---|
837 |
|
---|
838 | var sx = vector.set( te[0], te[1], te[2] ).length();
|
---|
839 | var sy = vector.set( te[4], te[5], te[6] ).length();
|
---|
840 | var sz = vector.set( te[8], te[9], te[10] ).length();
|
---|
841 |
|
---|
842 | // if determine is negative, we need to invert one scale
|
---|
843 | var det = this.determinant();
|
---|
844 | if( det < 0 ) {
|
---|
845 | sx = -sx;
|
---|
846 | }
|
---|
847 |
|
---|
848 | position.x = te[12];
|
---|
849 | position.y = te[13];
|
---|
850 | position.z = te[14];
|
---|
851 |
|
---|
852 | // scale the rotation part
|
---|
853 |
|
---|
854 | matrix.elements.set( this.elements ); // at this point matrix is incomplete so we can't use .copy()
|
---|
855 |
|
---|
856 | var invSX = 1 / sx;
|
---|
857 | var invSY = 1 / sy;
|
---|
858 | var invSZ = 1 / sz;
|
---|
859 |
|
---|
860 | matrix.elements[0] *= invSX;
|
---|
861 | matrix.elements[1] *= invSX;
|
---|
862 | matrix.elements[2] *= invSX;
|
---|
863 |
|
---|
864 | matrix.elements[4] *= invSY;
|
---|
865 | matrix.elements[5] *= invSY;
|
---|
866 | matrix.elements[6] *= invSY;
|
---|
867 |
|
---|
868 | matrix.elements[8] *= invSZ;
|
---|
869 | matrix.elements[9] *= invSZ;
|
---|
870 | matrix.elements[10] *= invSZ;
|
---|
871 |
|
---|
872 | quaternion.setFromRotationMatrix( matrix );
|
---|
873 |
|
---|
874 | scale.x = sx;
|
---|
875 | scale.y = sy;
|
---|
876 | scale.z = sz;
|
---|
877 |
|
---|
878 | return this;
|
---|
879 |
|
---|
880 | };
|
---|
881 |
|
---|
882 | }(),
|
---|
883 |
|
---|
884 | makeFrustum: function ( left, right, bottom, top, near, far ) {
|
---|
885 |
|
---|
886 | var te = this.elements;
|
---|
887 | var x = 2 * near / ( right - left );
|
---|
888 | var y = 2 * near / ( top - bottom );
|
---|
889 |
|
---|
890 | var a = ( right + left ) / ( right - left );
|
---|
891 | var b = ( top + bottom ) / ( top - bottom );
|
---|
892 | var c = - ( far + near ) / ( far - near );
|
---|
893 | var d = - 2 * far * near / ( far - near );
|
---|
894 |
|
---|
895 | te[0] = x; te[4] = 0; te[8] = a; te[12] = 0;
|
---|
896 | te[1] = 0; te[5] = y; te[9] = b; te[13] = 0;
|
---|
897 | te[2] = 0; te[6] = 0; te[10] = c; te[14] = d;
|
---|
898 | te[3] = 0; te[7] = 0; te[11] = - 1; te[15] = 0;
|
---|
899 |
|
---|
900 | return this;
|
---|
901 |
|
---|
902 | },
|
---|
903 |
|
---|
904 | makePerspective: function ( fov, aspect, near, far ) {
|
---|
905 |
|
---|
906 | var ymax = near * Math.tan( THREE.Math.degToRad( fov * 0.5 ) );
|
---|
907 | var ymin = - ymax;
|
---|
908 | var xmin = ymin * aspect;
|
---|
909 | var xmax = ymax * aspect;
|
---|
910 |
|
---|
911 | return this.makeFrustum( xmin, xmax, ymin, ymax, near, far );
|
---|
912 |
|
---|
913 | },
|
---|
914 |
|
---|
915 | makeOrthographic: function ( left, right, top, bottom, near, far ) {
|
---|
916 |
|
---|
917 | var te = this.elements;
|
---|
918 | var w = right - left;
|
---|
919 | var h = top - bottom;
|
---|
920 | var p = far - near;
|
---|
921 |
|
---|
922 | var x = ( right + left ) / w;
|
---|
923 | var y = ( top + bottom ) / h;
|
---|
924 | var z = ( far + near ) / p;
|
---|
925 |
|
---|
926 | te[0] = 2 / w; te[4] = 0; te[8] = 0; te[12] = -x;
|
---|
927 | te[1] = 0; te[5] = 2 / h; te[9] = 0; te[13] = -y;
|
---|
928 | te[2] = 0; te[6] = 0; te[10] = -2/p; te[14] = -z;
|
---|
929 | te[3] = 0; te[7] = 0; te[11] = 0; te[15] = 1;
|
---|
930 |
|
---|
931 | return this;
|
---|
932 |
|
---|
933 | },
|
---|
934 |
|
---|
935 | fromArray: function ( array ) {
|
---|
936 |
|
---|
937 | this.elements.set( array );
|
---|
938 |
|
---|
939 | return this;
|
---|
940 |
|
---|
941 | },
|
---|
942 |
|
---|
943 | toArray: function () {
|
---|
944 |
|
---|
945 | var te = this.elements;
|
---|
946 |
|
---|
947 | return [
|
---|
948 | te[ 0 ], te[ 1 ], te[ 2 ], te[ 3 ],
|
---|
949 | te[ 4 ], te[ 5 ], te[ 6 ], te[ 7 ],
|
---|
950 | te[ 8 ], te[ 9 ], te[ 10 ], te[ 11 ],
|
---|
951 | te[ 12 ], te[ 13 ], te[ 14 ], te[ 15 ]
|
---|
952 | ];
|
---|
953 |
|
---|
954 | },
|
---|
955 |
|
---|
956 | clone: function () {
|
---|
957 |
|
---|
958 | var te = this.elements;
|
---|
959 |
|
---|
960 | return new THREE.Matrix4(
|
---|
961 |
|
---|
962 | te[0], te[4], te[8], te[12],
|
---|
963 | te[1], te[5], te[9], te[13],
|
---|
964 | te[2], te[6], te[10], te[14],
|
---|
965 | te[3], te[7], te[11], te[15]
|
---|
966 |
|
---|
967 | );
|
---|
968 |
|
---|
969 | }
|
---|
970 |
|
---|
971 | };
|
---|