Matrix4.js 22 KB

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