Matrix4.js 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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. THREE.Matrix4 = function () {
  14. this.elements = new Float32Array( [
  15. 1, 0, 0, 0,
  16. 0, 1, 0, 0,
  17. 0, 0, 1, 0,
  18. 0, 0, 0, 1
  19. ] );
  20. if ( arguments.length > 0 ) {
  21. console.error( 'THREE.Matrix4: the constructor no longer reads arguments. use .set() instead.' );
  22. }
  23. };
  24. THREE.Matrix4.prototype = {
  25. constructor: THREE.Matrix4,
  26. set: function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
  27. var te = this.elements;
  28. te[ 0 ] = n11; te[ 4 ] = n12; te[ 8 ] = n13; te[ 12 ] = n14;
  29. te[ 1 ] = n21; te[ 5 ] = n22; te[ 9 ] = n23; te[ 13 ] = n24;
  30. te[ 2 ] = n31; te[ 6 ] = n32; te[ 10 ] = n33; te[ 14 ] = n34;
  31. te[ 3 ] = n41; te[ 7 ] = n42; te[ 11 ] = n43; te[ 15 ] = n44;
  32. return this;
  33. },
  34. identity: function () {
  35. this.set(
  36. 1, 0, 0, 0,
  37. 0, 1, 0, 0,
  38. 0, 0, 1, 0,
  39. 0, 0, 0, 1
  40. );
  41. return this;
  42. },
  43. clone: function () {
  44. return new THREE.Matrix4().fromArray( this.elements );
  45. },
  46. copy: function ( m ) {
  47. this.elements.set( m.elements );
  48. return this;
  49. },
  50. extractPosition: function ( m ) {
  51. console.warn( 'THREE.Matrix4: .extractPosition() has been renamed to .copyPosition().' );
  52. return this.copyPosition( m );
  53. },
  54. copyPosition: function ( m ) {
  55. var te = this.elements;
  56. var me = m.elements;
  57. te[ 12 ] = me[ 12 ];
  58. te[ 13 ] = me[ 13 ];
  59. te[ 14 ] = me[ 14 ];
  60. return this;
  61. },
  62. extractBasis: function ( xAxis, yAxis, zAxis ) {
  63. var te = this.elements;
  64. xAxis.set( te[ 0 ], te[ 1 ], te[ 2 ] );
  65. yAxis.set( te[ 4 ], te[ 5 ], te[ 6 ] );
  66. zAxis.set( te[ 8 ], te[ 9 ], te[ 10 ] );
  67. return this;
  68. },
  69. makeBasis: function ( xAxis, yAxis, zAxis ) {
  70. this.set(
  71. xAxis.x, yAxis.x, zAxis.x, 0,
  72. xAxis.y, yAxis.y, zAxis.y, 0,
  73. xAxis.z, yAxis.z, zAxis.z, 0,
  74. 0, 0, 0, 1
  75. );
  76. return this;
  77. },
  78. extractRotation: function () {
  79. var v1;
  80. return function ( m ) {
  81. if ( v1 === undefined ) v1 = new THREE.Vector3();
  82. var te = this.elements;
  83. var me = m.elements;
  84. var scaleX = 1 / v1.set( me[ 0 ], me[ 1 ], me[ 2 ] ).length();
  85. var scaleY = 1 / v1.set( me[ 4 ], me[ 5 ], me[ 6 ] ).length();
  86. var scaleZ = 1 / v1.set( me[ 8 ], me[ 9 ], me[ 10 ] ).length();
  87. te[ 0 ] = me[ 0 ] * scaleX;
  88. te[ 1 ] = me[ 1 ] * scaleX;
  89. te[ 2 ] = me[ 2 ] * scaleX;
  90. te[ 4 ] = me[ 4 ] * scaleY;
  91. te[ 5 ] = me[ 5 ] * scaleY;
  92. te[ 6 ] = me[ 6 ] * scaleY;
  93. te[ 8 ] = me[ 8 ] * scaleZ;
  94. te[ 9 ] = me[ 9 ] * scaleZ;
  95. te[ 10 ] = me[ 10 ] * scaleZ;
  96. return this;
  97. };
  98. }(),
  99. makeRotationFromEuler: function ( euler ) {
  100. if ( euler instanceof THREE.Euler === false ) {
  101. console.error( 'THREE.Matrix: .makeRotationFromEuler() now expects a Euler rotation rather than a Vector3 and order.' );
  102. }
  103. var te = this.elements;
  104. var x = euler.x, y = euler.y, z = euler.z;
  105. var a = Math.cos( x ), b = Math.sin( x );
  106. var c = Math.cos( y ), d = Math.sin( y );
  107. var e = Math.cos( z ), f = Math.sin( z );
  108. if ( euler.order === 'XYZ' ) {
  109. var ae = a * e, af = a * f, be = b * e, bf = b * f;
  110. te[ 0 ] = c * e;
  111. te[ 4 ] = - c * f;
  112. te[ 8 ] = d;
  113. te[ 1 ] = af + be * d;
  114. te[ 5 ] = ae - bf * d;
  115. te[ 9 ] = - b * c;
  116. te[ 2 ] = bf - ae * d;
  117. te[ 6 ] = be + af * d;
  118. te[ 10 ] = a * c;
  119. } else if ( euler.order === 'YXZ' ) {
  120. var ce = c * e, cf = c * f, de = d * e, df = d * f;
  121. te[ 0 ] = ce + df * b;
  122. te[ 4 ] = de * b - cf;
  123. te[ 8 ] = a * d;
  124. te[ 1 ] = a * f;
  125. te[ 5 ] = a * e;
  126. te[ 9 ] = - b;
  127. te[ 2 ] = cf * b - de;
  128. te[ 6 ] = df + ce * b;
  129. te[ 10 ] = a * c;
  130. } else if ( euler.order === 'ZXY' ) {
  131. var ce = c * e, cf = c * f, de = d * e, df = d * f;
  132. te[ 0 ] = ce - df * b;
  133. te[ 4 ] = - a * f;
  134. te[ 8 ] = de + cf * b;
  135. te[ 1 ] = cf + de * b;
  136. te[ 5 ] = a * e;
  137. te[ 9 ] = df - ce * b;
  138. te[ 2 ] = - a * d;
  139. te[ 6 ] = b;
  140. te[ 10 ] = a * c;
  141. } else if ( euler.order === 'ZYX' ) {
  142. var ae = a * e, af = a * f, be = b * e, bf = b * f;
  143. te[ 0 ] = c * e;
  144. te[ 4 ] = be * d - af;
  145. te[ 8 ] = ae * d + bf;
  146. te[ 1 ] = c * f;
  147. te[ 5 ] = bf * d + ae;
  148. te[ 9 ] = af * d - be;
  149. te[ 2 ] = - d;
  150. te[ 6 ] = b * c;
  151. te[ 10 ] = a * c;
  152. } else if ( euler.order === 'YZX' ) {
  153. var ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  154. te[ 0 ] = c * e;
  155. te[ 4 ] = bd - ac * f;
  156. te[ 8 ] = bc * f + ad;
  157. te[ 1 ] = f;
  158. te[ 5 ] = a * e;
  159. te[ 9 ] = - b * e;
  160. te[ 2 ] = - d * e;
  161. te[ 6 ] = ad * f + bc;
  162. te[ 10 ] = ac - bd * f;
  163. } else if ( euler.order === 'XZY' ) {
  164. var ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  165. te[ 0 ] = c * e;
  166. te[ 4 ] = - f;
  167. te[ 8 ] = d * e;
  168. te[ 1 ] = ac * f + bd;
  169. te[ 5 ] = a * e;
  170. te[ 9 ] = ad * f - bc;
  171. te[ 2 ] = bc * f - ad;
  172. te[ 6 ] = b * e;
  173. te[ 10 ] = bd * f + ac;
  174. }
  175. // last column
  176. te[ 3 ] = 0;
  177. te[ 7 ] = 0;
  178. te[ 11 ] = 0;
  179. // bottom row
  180. te[ 12 ] = 0;
  181. te[ 13 ] = 0;
  182. te[ 14 ] = 0;
  183. te[ 15 ] = 1;
  184. return this;
  185. },
  186. setRotationFromQuaternion: function ( q ) {
  187. console.warn( 'THREE.Matrix4: .setRotationFromQuaternion() has been renamed to .makeRotationFromQuaternion().' );
  188. return this.makeRotationFromQuaternion( q );
  189. },
  190. makeRotationFromQuaternion: function ( q ) {
  191. var te = this.elements;
  192. var x = q.x, y = q.y, z = q.z, w = q.w;
  193. var x2 = x + x, y2 = y + y, z2 = z + z;
  194. var xx = x * x2, xy = x * y2, xz = x * z2;
  195. var yy = y * y2, yz = y * z2, zz = z * z2;
  196. var wx = w * x2, wy = w * y2, wz = w * z2;
  197. te[ 0 ] = 1 - ( yy + zz );
  198. te[ 4 ] = xy - wz;
  199. te[ 8 ] = xz + wy;
  200. te[ 1 ] = xy + wz;
  201. te[ 5 ] = 1 - ( xx + zz );
  202. te[ 9 ] = yz - wx;
  203. te[ 2 ] = xz - wy;
  204. te[ 6 ] = yz + wx;
  205. te[ 10 ] = 1 - ( xx + yy );
  206. // last column
  207. te[ 3 ] = 0;
  208. te[ 7 ] = 0;
  209. te[ 11 ] = 0;
  210. // bottom row
  211. te[ 12 ] = 0;
  212. te[ 13 ] = 0;
  213. te[ 14 ] = 0;
  214. te[ 15 ] = 1;
  215. return this;
  216. },
  217. lookAt: function () {
  218. var x, y, z;
  219. return function ( eye, target, up ) {
  220. if ( x === undefined ) x = new THREE.Vector3();
  221. if ( y === undefined ) y = new THREE.Vector3();
  222. if ( z === undefined ) z = new THREE.Vector3();
  223. var te = this.elements;
  224. z.subVectors( eye, target ).normalize();
  225. if ( z.length() === 0 ) {
  226. z.z = 1;
  227. }
  228. x.crossVectors( up, z ).normalize();
  229. if ( x.length() === 0 ) {
  230. z.x += 0.0001;
  231. x.crossVectors( up, z ).normalize();
  232. }
  233. y.crossVectors( z, x );
  234. te[ 0 ] = x.x; te[ 4 ] = y.x; te[ 8 ] = z.x;
  235. te[ 1 ] = x.y; te[ 5 ] = y.y; te[ 9 ] = z.y;
  236. te[ 2 ] = x.z; te[ 6 ] = y.z; te[ 10 ] = z.z;
  237. return this;
  238. };
  239. }(),
  240. multiply: function ( m, n ) {
  241. if ( n !== undefined ) {
  242. console.warn( 'THREE.Matrix4: .multiply() now only accepts one argument. Use .multiplyMatrices( a, b ) instead.' );
  243. return this.multiplyMatrices( m, n );
  244. }
  245. return this.multiplyMatrices( this, m );
  246. },
  247. multiplyMatrices: function ( a, b ) {
  248. var ae = a.elements;
  249. var be = b.elements;
  250. var te = this.elements;
  251. var a11 = ae[ 0 ], a12 = ae[ 4 ], a13 = ae[ 8 ], a14 = ae[ 12 ];
  252. var a21 = ae[ 1 ], a22 = ae[ 5 ], a23 = ae[ 9 ], a24 = ae[ 13 ];
  253. var a31 = ae[ 2 ], a32 = ae[ 6 ], a33 = ae[ 10 ], a34 = ae[ 14 ];
  254. var a41 = ae[ 3 ], a42 = ae[ 7 ], a43 = ae[ 11 ], a44 = ae[ 15 ];
  255. var b11 = be[ 0 ], b12 = be[ 4 ], b13 = be[ 8 ], b14 = be[ 12 ];
  256. var b21 = be[ 1 ], b22 = be[ 5 ], b23 = be[ 9 ], b24 = be[ 13 ];
  257. var b31 = be[ 2 ], b32 = be[ 6 ], b33 = be[ 10 ], b34 = be[ 14 ];
  258. var b41 = be[ 3 ], b42 = be[ 7 ], b43 = be[ 11 ], b44 = be[ 15 ];
  259. te[ 0 ] = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
  260. te[ 4 ] = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
  261. te[ 8 ] = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
  262. te[ 12 ] = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
  263. te[ 1 ] = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
  264. te[ 5 ] = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
  265. te[ 9 ] = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
  266. te[ 13 ] = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
  267. te[ 2 ] = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
  268. te[ 6 ] = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
  269. te[ 10 ] = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
  270. te[ 14 ] = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
  271. te[ 3 ] = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
  272. te[ 7 ] = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
  273. te[ 11 ] = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
  274. te[ 15 ] = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
  275. return this;
  276. },
  277. multiplyToArray: function ( a, b, r ) {
  278. var te = this.elements;
  279. this.multiplyMatrices( a, b );
  280. r[ 0 ] = te[ 0 ]; r[ 1 ] = te[ 1 ]; r[ 2 ] = te[ 2 ]; r[ 3 ] = te[ 3 ];
  281. r[ 4 ] = te[ 4 ]; r[ 5 ] = te[ 5 ]; r[ 6 ] = te[ 6 ]; r[ 7 ] = te[ 7 ];
  282. r[ 8 ] = te[ 8 ]; r[ 9 ] = te[ 9 ]; r[ 10 ] = te[ 10 ]; r[ 11 ] = te[ 11 ];
  283. r[ 12 ] = te[ 12 ]; r[ 13 ] = te[ 13 ]; r[ 14 ] = te[ 14 ]; r[ 15 ] = te[ 15 ];
  284. return this;
  285. },
  286. multiplyScalar: function ( s ) {
  287. var te = this.elements;
  288. te[ 0 ] *= s; te[ 4 ] *= s; te[ 8 ] *= s; te[ 12 ] *= s;
  289. te[ 1 ] *= s; te[ 5 ] *= s; te[ 9 ] *= s; te[ 13 ] *= s;
  290. te[ 2 ] *= s; te[ 6 ] *= s; te[ 10 ] *= s; te[ 14 ] *= s;
  291. te[ 3 ] *= s; te[ 7 ] *= s; te[ 11 ] *= s; te[ 15 ] *= s;
  292. return this;
  293. },
  294. multiplyVector3: function ( vector ) {
  295. console.warn( 'THREE.Matrix4: .multiplyVector3() has been removed. Use vector.applyMatrix4( matrix ) or vector.applyProjection( matrix ) instead.' );
  296. return vector.applyProjection( this );
  297. },
  298. multiplyVector4: function ( vector ) {
  299. console.warn( 'THREE.Matrix4: .multiplyVector4() has been removed. Use vector.applyMatrix4( matrix ) instead.' );
  300. return vector.applyMatrix4( this );
  301. },
  302. multiplyVector3Array: function ( a ) {
  303. console.warn( 'THREE.Matrix4: .multiplyVector3Array() has been renamed. Use matrix.applyToVector3Array( array ) instead.' );
  304. return this.applyToVector3Array( a );
  305. },
  306. applyToVector3Array: function () {
  307. var v1;
  308. return function ( array, offset, length ) {
  309. if ( v1 === undefined ) v1 = new THREE.Vector3();
  310. if ( offset === undefined ) offset = 0;
  311. if ( length === undefined ) length = array.length;
  312. for ( var i = 0, j = offset; i < length; i += 3, j += 3 ) {
  313. v1.fromArray( array, j );
  314. v1.applyMatrix4( this );
  315. v1.toArray( array, j );
  316. }
  317. return array;
  318. };
  319. }(),
  320. applyToBuffer: function () {
  321. var v1;
  322. return function applyToBuffer( buffer, offset, length ) {
  323. if ( v1 === undefined ) v1 = new THREE.Vector3();
  324. if ( offset === undefined ) offset = 0;
  325. if ( length === undefined ) length = buffer.length / buffer.itemSize;
  326. for ( var i = 0, j = offset; i < length; i ++, j ++ ) {
  327. v1.x = buffer.getX( j );
  328. v1.y = buffer.getY( j );
  329. v1.z = buffer.getZ( j );
  330. v1.applyMatrix4( this );
  331. buffer.setXYZ( v1.x, v1.y, v1.z );
  332. }
  333. return buffer;
  334. };
  335. }(),
  336. rotateAxis: function ( v ) {
  337. console.warn( 'THREE.Matrix4: .rotateAxis() has been removed. Use Vector3.transformDirection( matrix ) instead.' );
  338. v.transformDirection( this );
  339. },
  340. crossVector: function ( vector ) {
  341. console.warn( 'THREE.Matrix4: .crossVector() has been removed. Use vector.applyMatrix4( matrix ) instead.' );
  342. return vector.applyMatrix4( this );
  343. },
  344. determinant: function () {
  345. var te = this.elements;
  346. var n11 = te[ 0 ], n12 = te[ 4 ], n13 = te[ 8 ], n14 = te[ 12 ];
  347. var n21 = te[ 1 ], n22 = te[ 5 ], n23 = te[ 9 ], n24 = te[ 13 ];
  348. var n31 = te[ 2 ], n32 = te[ 6 ], n33 = te[ 10 ], n34 = te[ 14 ];
  349. var n41 = te[ 3 ], n42 = te[ 7 ], n43 = te[ 11 ], n44 = te[ 15 ];
  350. //TODO: make this more efficient
  351. //( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm )
  352. return (
  353. n41 * (
  354. + n14 * n23 * n32
  355. - n13 * n24 * n32
  356. - n14 * n22 * n33
  357. + n12 * n24 * n33
  358. + n13 * n22 * n34
  359. - n12 * n23 * n34
  360. ) +
  361. n42 * (
  362. + n11 * n23 * n34
  363. - n11 * n24 * n33
  364. + n14 * n21 * n33
  365. - n13 * n21 * n34
  366. + n13 * n24 * n31
  367. - n14 * n23 * n31
  368. ) +
  369. n43 * (
  370. + n11 * n24 * n32
  371. - n11 * n22 * n34
  372. - n14 * n21 * n32
  373. + n12 * n21 * n34
  374. + n14 * n22 * n31
  375. - n12 * n24 * n31
  376. ) +
  377. n44 * (
  378. - n13 * n22 * n31
  379. - n11 * n23 * n32
  380. + n11 * n22 * n33
  381. + n13 * n21 * n32
  382. - n12 * n21 * n33
  383. + n12 * n23 * n31
  384. )
  385. );
  386. },
  387. transpose: function () {
  388. var te = this.elements;
  389. var tmp;
  390. tmp = te[ 1 ]; te[ 1 ] = te[ 4 ]; te[ 4 ] = tmp;
  391. tmp = te[ 2 ]; te[ 2 ] = te[ 8 ]; te[ 8 ] = tmp;
  392. tmp = te[ 6 ]; te[ 6 ] = te[ 9 ]; te[ 9 ] = tmp;
  393. tmp = te[ 3 ]; te[ 3 ] = te[ 12 ]; te[ 12 ] = tmp;
  394. tmp = te[ 7 ]; te[ 7 ] = te[ 13 ]; te[ 13 ] = tmp;
  395. tmp = te[ 11 ]; te[ 11 ] = te[ 14 ]; te[ 14 ] = tmp;
  396. return this;
  397. },
  398. flattenToArrayOffset: function ( array, offset ) {
  399. var te = this.elements;
  400. array[ offset ] = te[ 0 ];
  401. array[ offset + 1 ] = te[ 1 ];
  402. array[ offset + 2 ] = te[ 2 ];
  403. array[ offset + 3 ] = te[ 3 ];
  404. array[ offset + 4 ] = te[ 4 ];
  405. array[ offset + 5 ] = te[ 5 ];
  406. array[ offset + 6 ] = te[ 6 ];
  407. array[ offset + 7 ] = te[ 7 ];
  408. array[ offset + 8 ] = te[ 8 ];
  409. array[ offset + 9 ] = te[ 9 ];
  410. array[ offset + 10 ] = te[ 10 ];
  411. array[ offset + 11 ] = te[ 11 ];
  412. array[ offset + 12 ] = te[ 12 ];
  413. array[ offset + 13 ] = te[ 13 ];
  414. array[ offset + 14 ] = te[ 14 ];
  415. array[ offset + 15 ] = te[ 15 ];
  416. return array;
  417. },
  418. getPosition: function () {
  419. var v1;
  420. return function () {
  421. if ( v1 === undefined ) v1 = new THREE.Vector3();
  422. console.warn( 'THREE.Matrix4: .getPosition() has been removed. Use Vector3.setFromMatrixPosition( matrix ) instead.' );
  423. var te = this.elements;
  424. return v1.set( te[ 12 ], te[ 13 ], te[ 14 ] );
  425. };
  426. }(),
  427. setPosition: function ( v ) {
  428. var te = this.elements;
  429. te[ 12 ] = v.x;
  430. te[ 13 ] = v.y;
  431. te[ 14 ] = v.z;
  432. return this;
  433. },
  434. getInverse: function ( m, throwOnInvertible ) {
  435. // based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
  436. var te = this.elements;
  437. var me = m.elements;
  438. var n11 = me[ 0 ], n12 = me[ 4 ], n13 = me[ 8 ], n14 = me[ 12 ];
  439. var n21 = me[ 1 ], n22 = me[ 5 ], n23 = me[ 9 ], n24 = me[ 13 ];
  440. var n31 = me[ 2 ], n32 = me[ 6 ], n33 = me[ 10 ], n34 = me[ 14 ];
  441. var n41 = me[ 3 ], n42 = me[ 7 ], n43 = me[ 11 ], n44 = me[ 15 ];
  442. te[ 0 ] = n23 * n34 * n42 - n24 * n33 * n42 + n24 * n32 * n43 - n22 * n34 * n43 - n23 * n32 * n44 + n22 * n33 * n44;
  443. te[ 4 ] = n14 * n33 * n42 - n13 * n34 * n42 - n14 * n32 * n43 + n12 * n34 * n43 + n13 * n32 * n44 - n12 * n33 * n44;
  444. te[ 8 ] = n13 * n24 * n42 - n14 * n23 * n42 + n14 * n22 * n43 - n12 * n24 * n43 - n13 * n22 * n44 + n12 * n23 * n44;
  445. te[ 12 ] = n14 * n23 * n32 - n13 * n24 * n32 - n14 * n22 * n33 + n12 * n24 * n33 + n13 * n22 * n34 - n12 * n23 * n34;
  446. te[ 1 ] = n24 * n33 * n41 - n23 * n34 * n41 - n24 * n31 * n43 + n21 * n34 * n43 + n23 * n31 * n44 - n21 * n33 * n44;
  447. te[ 5 ] = n13 * n34 * n41 - n14 * n33 * n41 + n14 * n31 * n43 - n11 * n34 * n43 - n13 * n31 * n44 + n11 * n33 * n44;
  448. te[ 9 ] = n14 * n23 * n41 - n13 * n24 * n41 - n14 * n21 * n43 + n11 * n24 * n43 + n13 * n21 * n44 - n11 * n23 * n44;
  449. te[ 13 ] = n13 * n24 * n31 - n14 * n23 * n31 + n14 * n21 * n33 - n11 * n24 * n33 - n13 * n21 * n34 + n11 * n23 * n34;
  450. te[ 2 ] = n22 * n34 * n41 - n24 * n32 * n41 + n24 * n31 * n42 - n21 * n34 * n42 - n22 * n31 * n44 + n21 * n32 * n44;
  451. te[ 6 ] = n14 * n32 * n41 - n12 * n34 * n41 - n14 * n31 * n42 + n11 * n34 * n42 + n12 * n31 * n44 - n11 * n32 * n44;
  452. te[ 10 ] = n12 * n24 * n41 - n14 * n22 * n41 + n14 * n21 * n42 - n11 * n24 * n42 - n12 * n21 * n44 + n11 * n22 * n44;
  453. te[ 14 ] = n14 * n22 * n31 - n12 * n24 * n31 - n14 * n21 * n32 + n11 * n24 * n32 + n12 * n21 * n34 - n11 * n22 * n34;
  454. te[ 3 ] = n23 * n32 * n41 - n22 * n33 * n41 - n23 * n31 * n42 + n21 * n33 * n42 + n22 * n31 * n43 - n21 * n32 * n43;
  455. te[ 7 ] = n12 * n33 * n41 - n13 * n32 * n41 + n13 * n31 * n42 - n11 * n33 * n42 - n12 * n31 * n43 + n11 * n32 * n43;
  456. te[ 11 ] = n13 * n22 * n41 - n12 * n23 * n41 - n13 * n21 * n42 + n11 * n23 * n42 + n12 * n21 * n43 - n11 * n22 * n43;
  457. te[ 15 ] = n12 * n23 * n31 - n13 * n22 * n31 + n13 * n21 * n32 - n11 * n23 * n32 - n12 * n21 * n33 + n11 * n22 * n33;
  458. var det = n11 * te[ 0 ] + n21 * te[ 4 ] + n31 * te[ 8 ] + n41 * te[ 12 ];
  459. if ( det === 0 ) {
  460. var msg = "THREE.Matrix4.getInverse(): can't invert matrix, determinant is 0";
  461. if ( throwOnInvertible || false ) {
  462. throw new Error( msg );
  463. } else {
  464. console.warn( msg );
  465. }
  466. this.identity();
  467. return this;
  468. }
  469. this.multiplyScalar( 1 / det );
  470. return this;
  471. },
  472. translate: function ( v ) {
  473. console.error( 'THREE.Matrix4: .translate() has been removed.' );
  474. },
  475. rotateX: function ( angle ) {
  476. console.error( 'THREE.Matrix4: .rotateX() has been removed.' );
  477. },
  478. rotateY: function ( angle ) {
  479. console.error( 'THREE.Matrix4: .rotateY() has been removed.' );
  480. },
  481. rotateZ: function ( angle ) {
  482. console.error( 'THREE.Matrix4: .rotateZ() has been removed.' );
  483. },
  484. rotateByAxis: function ( axis, angle ) {
  485. console.error( 'THREE.Matrix4: .rotateByAxis() has been removed.' );
  486. },
  487. scale: function ( v ) {
  488. var te = this.elements;
  489. var x = v.x, y = v.y, z = v.z;
  490. te[ 0 ] *= x; te[ 4 ] *= y; te[ 8 ] *= z;
  491. te[ 1 ] *= x; te[ 5 ] *= y; te[ 9 ] *= z;
  492. te[ 2 ] *= x; te[ 6 ] *= y; te[ 10 ] *= z;
  493. te[ 3 ] *= x; te[ 7 ] *= y; te[ 11 ] *= z;
  494. return this;
  495. },
  496. getMaxScaleOnAxis: function () {
  497. var te = this.elements;
  498. var scaleXSq = te[ 0 ] * te[ 0 ] + te[ 1 ] * te[ 1 ] + te[ 2 ] * te[ 2 ];
  499. var scaleYSq = te[ 4 ] * te[ 4 ] + te[ 5 ] * te[ 5 ] + te[ 6 ] * te[ 6 ];
  500. var scaleZSq = te[ 8 ] * te[ 8 ] + te[ 9 ] * te[ 9 ] + te[ 10 ] * te[ 10 ];
  501. return Math.sqrt( Math.max( scaleXSq, Math.max( scaleYSq, scaleZSq ) ) );
  502. },
  503. makeTranslation: function ( x, y, z ) {
  504. this.set(
  505. 1, 0, 0, x,
  506. 0, 1, 0, y,
  507. 0, 0, 1, z,
  508. 0, 0, 0, 1
  509. );
  510. return this;
  511. },
  512. makeRotationX: function ( theta ) {
  513. var c = Math.cos( theta ), s = Math.sin( theta );
  514. this.set(
  515. 1, 0, 0, 0,
  516. 0, c, - s, 0,
  517. 0, s, c, 0,
  518. 0, 0, 0, 1
  519. );
  520. return this;
  521. },
  522. makeRotationY: function ( theta ) {
  523. var c = Math.cos( theta ), s = Math.sin( theta );
  524. this.set(
  525. c, 0, s, 0,
  526. 0, 1, 0, 0,
  527. - s, 0, c, 0,
  528. 0, 0, 0, 1
  529. );
  530. return this;
  531. },
  532. makeRotationZ: function ( theta ) {
  533. var c = Math.cos( theta ), s = Math.sin( theta );
  534. this.set(
  535. c, - s, 0, 0,
  536. s, c, 0, 0,
  537. 0, 0, 1, 0,
  538. 0, 0, 0, 1
  539. );
  540. return this;
  541. },
  542. makeRotationAxis: function ( axis, angle ) {
  543. // Based on http://www.gamedev.net/reference/articles/article1199.asp
  544. var c = Math.cos( angle );
  545. var s = Math.sin( angle );
  546. var t = 1 - c;
  547. var x = axis.x, y = axis.y, z = axis.z;
  548. var tx = t * x, ty = t * y;
  549. this.set(
  550. tx * x + c, tx * y - s * z, tx * z + s * y, 0,
  551. tx * y + s * z, ty * y + c, ty * z - s * x, 0,
  552. tx * z - s * y, ty * z + s * x, t * z * z + c, 0,
  553. 0, 0, 0, 1
  554. );
  555. return this;
  556. },
  557. makeScale: function ( x, y, z ) {
  558. this.set(
  559. x, 0, 0, 0,
  560. 0, y, 0, 0,
  561. 0, 0, z, 0,
  562. 0, 0, 0, 1
  563. );
  564. return this;
  565. },
  566. compose: function ( position, quaternion, scale ) {
  567. this.makeRotationFromQuaternion( quaternion );
  568. this.scale( scale );
  569. this.setPosition( position );
  570. return this;
  571. },
  572. decompose: function () {
  573. var vector, matrix;
  574. return function ( position, quaternion, scale ) {
  575. if ( vector === undefined ) vector = new THREE.Vector3();
  576. if ( matrix === undefined ) matrix = new THREE.Matrix4();
  577. var te = this.elements;
  578. var sx = vector.set( te[ 0 ], te[ 1 ], te[ 2 ] ).length();
  579. var sy = vector.set( te[ 4 ], te[ 5 ], te[ 6 ] ).length();
  580. var sz = vector.set( te[ 8 ], te[ 9 ], te[ 10 ] ).length();
  581. // if determine is negative, we need to invert one scale
  582. var det = this.determinant();
  583. if ( det < 0 ) {
  584. sx = - sx;
  585. }
  586. position.x = te[ 12 ];
  587. position.y = te[ 13 ];
  588. position.z = te[ 14 ];
  589. // scale the rotation part
  590. matrix.elements.set( this.elements ); // at this point matrix is incomplete so we can't use .copy()
  591. var invSX = 1 / sx;
  592. var invSY = 1 / sy;
  593. var invSZ = 1 / sz;
  594. matrix.elements[ 0 ] *= invSX;
  595. matrix.elements[ 1 ] *= invSX;
  596. matrix.elements[ 2 ] *= invSX;
  597. matrix.elements[ 4 ] *= invSY;
  598. matrix.elements[ 5 ] *= invSY;
  599. matrix.elements[ 6 ] *= invSY;
  600. matrix.elements[ 8 ] *= invSZ;
  601. matrix.elements[ 9 ] *= invSZ;
  602. matrix.elements[ 10 ] *= invSZ;
  603. quaternion.setFromRotationMatrix( matrix );
  604. scale.x = sx;
  605. scale.y = sy;
  606. scale.z = sz;
  607. return this;
  608. };
  609. }(),
  610. makeFrustum: function ( left, right, bottom, top, near, far ) {
  611. var te = this.elements;
  612. var x = 2 * near / ( right - left );
  613. var y = 2 * near / ( top - bottom );
  614. var a = ( right + left ) / ( right - left );
  615. var b = ( top + bottom ) / ( top - bottom );
  616. var c = - ( far + near ) / ( far - near );
  617. var d = - 2 * far * near / ( far - near );
  618. te[ 0 ] = x; te[ 4 ] = 0; te[ 8 ] = a; te[ 12 ] = 0;
  619. te[ 1 ] = 0; te[ 5 ] = y; te[ 9 ] = b; te[ 13 ] = 0;
  620. te[ 2 ] = 0; te[ 6 ] = 0; te[ 10 ] = c; te[ 14 ] = d;
  621. te[ 3 ] = 0; te[ 7 ] = 0; te[ 11 ] = - 1; te[ 15 ] = 0;
  622. return this;
  623. },
  624. makePerspective: function ( fov, aspect, near, far ) {
  625. var ymax = near * Math.tan( THREE.Math.degToRad( fov * 0.5 ) );
  626. var ymin = - ymax;
  627. var xmin = ymin * aspect;
  628. var xmax = ymax * aspect;
  629. return this.makeFrustum( xmin, xmax, ymin, ymax, near, far );
  630. },
  631. makeOrthographic: function ( left, right, top, bottom, near, far ) {
  632. var te = this.elements;
  633. var w = right - left;
  634. var h = top - bottom;
  635. var p = far - near;
  636. var x = ( right + left ) / w;
  637. var y = ( top + bottom ) / h;
  638. var z = ( far + near ) / p;
  639. te[ 0 ] = 2 / w; te[ 4 ] = 0; te[ 8 ] = 0; te[ 12 ] = - x;
  640. te[ 1 ] = 0; te[ 5 ] = 2 / h; te[ 9 ] = 0; te[ 13 ] = - y;
  641. te[ 2 ] = 0; te[ 6 ] = 0; te[ 10 ] = - 2 / p; te[ 14 ] = - z;
  642. te[ 3 ] = 0; te[ 7 ] = 0; te[ 11 ] = 0; te[ 15 ] = 1;
  643. return this;
  644. },
  645. equals: function ( matrix ) {
  646. var te = this.elements;
  647. var me = matrix.elements;
  648. for ( var i = 0; i < 16; i ++ ) {
  649. if ( te[ i ] !== me[ i ] ) return false;
  650. }
  651. return true;
  652. },
  653. fromArray: function ( array ) {
  654. this.elements.set( array );
  655. return this;
  656. },
  657. toArray: function () {
  658. var te = this.elements;
  659. return [
  660. te[ 0 ], te[ 1 ], te[ 2 ], te[ 3 ],
  661. te[ 4 ], te[ 5 ], te[ 6 ], te[ 7 ],
  662. te[ 8 ], te[ 9 ], te[ 10 ], te[ 11 ],
  663. te[ 12 ], te[ 13 ], te[ 14 ], te[ 15 ]
  664. ];
  665. }
  666. };