Matrix4.js 21 KB

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