Matrix4.js 21 KB

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