Matrix4.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. /**
  2. * @author mr.doob / 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. */
  10. THREE.Matrix4 = function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
  11. this.set(
  12. n11 || 1, n12 || 0, n13 || 0, n14 || 0,
  13. n21 || 0, n22 || 1, n23 || 0, n24 || 0,
  14. n31 || 0, n32 || 0, n33 || 1, n34 || 0,
  15. n41 || 0, n42 || 0, n43 || 0, n44 || 1
  16. );
  17. this.flat = new Array( 16 );
  18. this.m33 = new THREE.Matrix3();
  19. };
  20. THREE.Matrix4.prototype = {
  21. set : function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
  22. this.n11 = n11; this.n12 = n12; this.n13 = n13; this.n14 = n14;
  23. this.n21 = n21; this.n22 = n22; this.n23 = n23; this.n24 = n24;
  24. this.n31 = n31; this.n32 = n32; this.n33 = n33; this.n34 = n34;
  25. this.n41 = n41; this.n42 = n42; this.n43 = n43; this.n44 = n44;
  26. return this;
  27. },
  28. identity : function () {
  29. this.set(
  30. 1, 0, 0, 0,
  31. 0, 1, 0, 0,
  32. 0, 0, 1, 0,
  33. 0, 0, 0, 1
  34. );
  35. return this;
  36. },
  37. copy : function ( m ) {
  38. this.set(
  39. m.n11, m.n12, m.n13, m.n14,
  40. m.n21, m.n22, m.n23, m.n24,
  41. m.n31, m.n32, m.n33, m.n34,
  42. m.n41, m.n42, m.n43, m.n44
  43. );
  44. return this;
  45. },
  46. lookAt : function ( eye, center, up ) {
  47. var x = THREE.Matrix4.__v1, y = THREE.Matrix4.__v2, z = THREE.Matrix4.__v3;
  48. z.sub( eye, center ).normalize();
  49. if ( z.length() === 0 ) {
  50. z.z = 1;
  51. }
  52. x.cross( up, z ).normalize();
  53. if ( x.length() === 0 ) {
  54. z.x += 0.0001;
  55. x.cross( up, z ).normalize();
  56. }
  57. y.cross( z, x ).normalize();
  58. this.n11 = x.x; this.n12 = y.x; this.n13 = z.x;
  59. this.n21 = x.y; this.n22 = y.y; this.n23 = z.y;
  60. this.n31 = x.z; this.n32 = y.z; this.n33 = z.z;
  61. return this;
  62. },
  63. multiplyVector3 : function ( v ) {
  64. var vx = v.x, vy = v.y, vz = v.z,
  65. d = 1 / ( this.n41 * vx + this.n42 * vy + this.n43 * vz + this.n44 );
  66. v.x = ( this.n11 * vx + this.n12 * vy + this.n13 * vz + this.n14 ) * d;
  67. v.y = ( this.n21 * vx + this.n22 * vy + this.n23 * vz + this.n24 ) * d;
  68. v.z = ( this.n31 * vx + this.n32 * vy + this.n33 * vz + this.n34 ) * d;
  69. return v;
  70. },
  71. multiplyVector4 : function ( v ) {
  72. var vx = v.x, vy = v.y, vz = v.z, vw = v.w;
  73. v.x = this.n11 * vx + this.n12 * vy + this.n13 * vz + this.n14 * vw;
  74. v.y = this.n21 * vx + this.n22 * vy + this.n23 * vz + this.n24 * vw;
  75. v.z = this.n31 * vx + this.n32 * vy + this.n33 * vz + this.n34 * vw;
  76. v.w = this.n41 * vx + this.n42 * vy + this.n43 * vz + this.n44 * vw;
  77. return v;
  78. },
  79. rotateAxis : function ( v ) {
  80. var vx = v.x, vy = v.y, vz = v.z;
  81. v.x = vx * this.n11 + vy * this.n12 + vz * this.n13;
  82. v.y = vx * this.n21 + vy * this.n22 + vz * this.n23;
  83. v.z = vx * this.n31 + vy * this.n32 + vz * this.n33;
  84. v.normalize();
  85. return v;
  86. },
  87. crossVector : function ( a ) {
  88. var v = new THREE.Vector4();
  89. v.x = this.n11 * a.x + this.n12 * a.y + this.n13 * a.z + this.n14 * a.w;
  90. v.y = this.n21 * a.x + this.n22 * a.y + this.n23 * a.z + this.n24 * a.w;
  91. v.z = this.n31 * a.x + this.n32 * a.y + this.n33 * a.z + this.n34 * a.w;
  92. v.w = ( a.w ) ? this.n41 * a.x + this.n42 * a.y + this.n43 * a.z + this.n44 * a.w : 1;
  93. return v;
  94. },
  95. multiply : function ( a, b ) {
  96. var a11 = a.n11, a12 = a.n12, a13 = a.n13, a14 = a.n14,
  97. a21 = a.n21, a22 = a.n22, a23 = a.n23, a24 = a.n24,
  98. a31 = a.n31, a32 = a.n32, a33 = a.n33, a34 = a.n34,
  99. a41 = a.n41, a42 = a.n42, a43 = a.n43, a44 = a.n44,
  100. b11 = b.n11, b12 = b.n12, b13 = b.n13, b14 = b.n14,
  101. b21 = b.n21, b22 = b.n22, b23 = b.n23, b24 = b.n24,
  102. b31 = b.n31, b32 = b.n32, b33 = b.n33, b34 = b.n34,
  103. b41 = b.n41, b42 = b.n42, b43 = b.n43, b44 = b.n44;
  104. this.n11 = a11 * b11 + a12 * b21 + a13 * b31;
  105. this.n12 = a11 * b12 + a12 * b22 + a13 * b32;
  106. this.n13 = a11 * b13 + a12 * b23 + a13 * b33;
  107. this.n14 = a11 * b14 + a12 * b24 + a13 * b34 + a14;
  108. this.n21 = a21 * b11 + a22 * b21 + a23 * b31;
  109. this.n22 = a21 * b12 + a22 * b22 + a23 * b32;
  110. this.n23 = a21 * b13 + a22 * b23 + a23 * b33;
  111. this.n24 = a21 * b14 + a22 * b24 + a23 * b34 + a24;
  112. this.n31 = a31 * b11 + a32 * b21 + a33 * b31;
  113. this.n32 = a31 * b12 + a32 * b22 + a33 * b32;
  114. this.n33 = a31 * b13 + a32 * b23 + a33 * b33;
  115. this.n34 = a31 * b14 + a32 * b24 + a33 * b34 + a34;
  116. this.n41 = a41 * b11 + a42 * b21 + a43 * b31;
  117. this.n42 = a41 * b12 + a42 * b22 + a43 * b32;
  118. this.n43 = a41 * b13 + a42 * b23 + a43 * b33;
  119. this.n44 = a41 * b14 + a42 * b24 + a43 * b34 + a44;
  120. /*
  121. this.n11 = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
  122. this.n12 = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
  123. this.n13 = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
  124. this.n14 = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
  125. this.n21 = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
  126. this.n22 = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
  127. this.n23 = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
  128. this.n24 = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
  129. this.n31 = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
  130. this.n32 = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
  131. this.n33 = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
  132. this.n34 = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
  133. this.n41 = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
  134. this.n42 = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
  135. this.n43 = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
  136. this.n44 = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
  137. */
  138. return this;
  139. },
  140. multiplyToArray : function ( a, b, r ) {
  141. this.multiply( a, b );
  142. r[ 0 ] = this.n11; r[ 1 ] = this.n21; r[ 2 ] = this.n31; r[ 3 ] = this.n41;
  143. r[ 4 ] = this.n12; r[ 5 ] = this.n22; r[ 6 ] = this.n32; r[ 7 ] = this.n42;
  144. r[ 8 ] = this.n13; r[ 9 ] = this.n23; r[ 10 ] = this.n33; r[ 11 ] = this.n43;
  145. r[ 12 ] = this.n14; r[ 13 ] = this.n24; r[ 14 ] = this.n34; r[ 15 ] = this.n44;
  146. return this;
  147. },
  148. multiplySelf : function ( m ) {
  149. this.multiply( this, m );
  150. return this;
  151. },
  152. multiplyScalar : function ( s ) {
  153. this.n11 *= s; this.n12 *= s; this.n13 *= s; this.n14 *= s;
  154. this.n21 *= s; this.n22 *= s; this.n23 *= s; this.n24 *= s;
  155. this.n31 *= s; this.n32 *= s; this.n33 *= s; this.n34 *= s;
  156. this.n41 *= s; this.n42 *= s; this.n43 *= s; this.n44 *= s;
  157. return this;
  158. },
  159. determinant : function () {
  160. var n11 = this.n11, n12 = this.n12, n13 = this.n13, n14 = this.n14,
  161. n21 = this.n21, n22 = this.n22, n23 = this.n23, n24 = this.n24,
  162. n31 = this.n31, n32 = this.n32, n33 = this.n33, n34 = this.n34,
  163. n41 = this.n41, n42 = this.n42, n43 = this.n43, n44 = this.n44;
  164. //TODO: make this more efficient
  165. //( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm )
  166. return (
  167. n14 * n23 * n32 * n41-
  168. n13 * n24 * n32 * n41-
  169. n14 * n22 * n33 * n41+
  170. n12 * n24 * n33 * n41+
  171. n13 * n22 * n34 * n41-
  172. n12 * n23 * n34 * n41-
  173. n14 * n23 * n31 * n42+
  174. n13 * n24 * n31 * n42+
  175. n14 * n21 * n33 * n42-
  176. n11 * n24 * n33 * n42-
  177. n13 * n21 * n34 * n42+
  178. n11 * n23 * n34 * n42+
  179. n14 * n22 * n31 * n43-
  180. n12 * n24 * n31 * n43-
  181. n14 * n21 * n32 * n43+
  182. n11 * n24 * n32 * n43+
  183. n12 * n21 * n34 * n43-
  184. n11 * n22 * n34 * n43-
  185. n13 * n22 * n31 * n44+
  186. n12 * n23 * n31 * n44+
  187. n13 * n21 * n32 * n44-
  188. n11 * n23 * n32 * n44-
  189. n12 * n21 * n33 * n44+
  190. n11 * n22 * n33 * n44
  191. );
  192. },
  193. transpose : function () {
  194. var tmp;
  195. tmp = this.n21; this.n21 = this.n12; this.n12 = tmp;
  196. tmp = this.n31; this.n31 = this.n13; this.n13 = tmp;
  197. tmp = this.n32; this.n32 = this.n23; this.n23 = tmp;
  198. tmp = this.n41; this.n41 = this.n14; this.n14 = tmp;
  199. tmp = this.n42; this.n42 = this.n24; this.n24 = tmp;
  200. tmp = this.n43; this.n43 = this.n34; this.n43 = tmp;
  201. return this;
  202. },
  203. clone : function () {
  204. var m = new THREE.Matrix4();
  205. m.n11 = this.n11; m.n12 = this.n12; m.n13 = this.n13; m.n14 = this.n14;
  206. m.n21 = this.n21; m.n22 = this.n22; m.n23 = this.n23; m.n24 = this.n24;
  207. m.n31 = this.n31; m.n32 = this.n32; m.n33 = this.n33; m.n34 = this.n34;
  208. m.n41 = this.n41; m.n42 = this.n42; m.n43 = this.n43; m.n44 = this.n44;
  209. return m;
  210. },
  211. flatten : function () {
  212. this.flat[ 0 ] = this.n11; this.flat[ 1 ] = this.n21; this.flat[ 2 ] = this.n31; this.flat[ 3 ] = this.n41;
  213. this.flat[ 4 ] = this.n12; this.flat[ 5 ] = this.n22; this.flat[ 6 ] = this.n32; this.flat[ 7 ] = this.n42;
  214. this.flat[ 8 ] = this.n13; this.flat[ 9 ] = this.n23; this.flat[ 10 ] = this.n33; this.flat[ 11 ] = this.n43;
  215. this.flat[ 12 ] = this.n14; this.flat[ 13 ] = this.n24; this.flat[ 14 ] = this.n34; this.flat[ 15 ] = this.n44;
  216. return this.flat;
  217. },
  218. flattenToArray : function ( flat ) {
  219. flat[ 0 ] = this.n11; flat[ 1 ] = this.n21; flat[ 2 ] = this.n31; flat[ 3 ] = this.n41;
  220. flat[ 4 ] = this.n12; flat[ 5 ] = this.n22; flat[ 6 ] = this.n32; flat[ 7 ] = this.n42;
  221. flat[ 8 ] = this.n13; flat[ 9 ] = this.n23; flat[ 10 ] = this.n33; flat[ 11 ] = this.n43;
  222. flat[ 12 ] = this.n14; flat[ 13 ] = this.n24; flat[ 14 ] = this.n34; flat[ 15 ] = this.n44;
  223. return flat;
  224. },
  225. flattenToArrayOffset : function( flat, offset ) {
  226. flat[ offset ] = this.n11;
  227. flat[ offset + 1 ] = this.n21;
  228. flat[ offset + 2 ] = this.n31;
  229. flat[ offset + 3 ] = this.n41;
  230. flat[ offset + 4 ] = this.n12;
  231. flat[ offset + 5 ] = this.n22;
  232. flat[ offset + 6 ] = this.n32;
  233. flat[ offset + 7 ] = this.n42;
  234. flat[ offset + 8 ] = this.n13;
  235. flat[ offset + 9 ] = this.n23;
  236. flat[ offset + 10 ] = this.n33;
  237. flat[ offset + 11 ] = this.n43;
  238. flat[ offset + 12 ] = this.n14;
  239. flat[ offset + 13 ] = this.n24;
  240. flat[ offset + 14 ] = this.n34;
  241. flat[ offset + 15 ] = this.n44;
  242. return flat;
  243. },
  244. setTranslation : function( x, y, z ) {
  245. this.set(
  246. 1, 0, 0, x,
  247. 0, 1, 0, y,
  248. 0, 0, 1, z,
  249. 0, 0, 0, 1
  250. );
  251. return this;
  252. },
  253. setScale : function ( x, y, z ) {
  254. this.set(
  255. x, 0, 0, 0,
  256. 0, y, 0, 0,
  257. 0, 0, z, 0,
  258. 0, 0, 0, 1
  259. );
  260. return this;
  261. },
  262. setRotationX : function ( theta ) {
  263. var c = Math.cos( theta ), s = Math.sin( theta );
  264. this.set(
  265. 1, 0, 0, 0,
  266. 0, c, -s, 0,
  267. 0, s, c, 0,
  268. 0, 0, 0, 1
  269. );
  270. return this;
  271. },
  272. setRotationY : function( theta ) {
  273. var c = Math.cos( theta ), s = Math.sin( theta );
  274. this.set(
  275. c, 0, s, 0,
  276. 0, 1, 0, 0,
  277. -s, 0, c, 0,
  278. 0, 0, 0, 1
  279. );
  280. return this;
  281. },
  282. setRotationZ : function( theta ) {
  283. var c = Math.cos( theta ), s = Math.sin( theta );
  284. this.set(
  285. c, -s, 0, 0,
  286. s, c, 0, 0,
  287. 0, 0, 1, 0,
  288. 0, 0, 0, 1
  289. );
  290. return this;
  291. },
  292. setRotationAxis : function( axis, angle ) {
  293. // Based on http://www.gamedev.net/reference/articles/article1199.asp
  294. var c = Math.cos( angle ),
  295. s = Math.sin( angle ),
  296. t = 1 - c,
  297. x = axis.x, y = axis.y, z = axis.z,
  298. tx = t * x, ty = t * y;
  299. this.set(
  300. tx * x + c, tx * y - s * z, tx * z + s * y, 0,
  301. tx * y + s * z, ty * y + c, ty * z - s * x, 0,
  302. tx * z - s * y, ty * z + s * x, t * z * z + c, 0,
  303. 0, 0, 0, 1
  304. );
  305. return this;
  306. },
  307. setPosition : function( v ) {
  308. this.n14 = v.x;
  309. this.n24 = v.y;
  310. this.n34 = v.z;
  311. return this;
  312. },
  313. getPosition : function() {
  314. if( !this.position ) {
  315. this.position = new THREE.Vector3();
  316. }
  317. this.position.set( this.n14, this.n24, this.n34 );
  318. return this.position;
  319. },
  320. getColumnX : function() {
  321. if( !this.columnX ) {
  322. this.columnX = new THREE.Vector3();
  323. }
  324. this.columnX.set( this.n11, this.n21, this.n31 );
  325. return this.columnX;
  326. },
  327. getColumnY : function() {
  328. if( !this.columnY ) {
  329. this.columnY = new THREE.Vector3();
  330. }
  331. this.columnY.set( this.n12, this.n22, this.n32 );
  332. return this.columnY;
  333. },
  334. getColumnZ : function() {
  335. if( !this.columnZ ) {
  336. this.columnZ = new THREE.Vector3();
  337. }
  338. this.columnZ.set( this.n13, this.n23, this.n33 );
  339. return this.columnZ;
  340. },
  341. setRotationFromEuler : function( v ) {
  342. var x = v.x, y = v.y, z = v.z,
  343. a = Math.cos( x ), b = Math.sin( x ),
  344. c = Math.cos( y ), d = Math.sin( y ),
  345. e = Math.cos( z ), f = Math.sin( z ),
  346. ad = a * d, bd = b * d;
  347. this.n11 = c * e;
  348. this.n12 = - c * f;
  349. this.n13 = d;
  350. this.n21 = bd * e + a * f;
  351. this.n22 = - bd * f + a * e;
  352. this.n23 = - b * c;
  353. this.n31 = - ad * e + b * f;
  354. this.n32 = ad * f + b * e;
  355. this.n33 = a * c;
  356. return this;
  357. },
  358. setRotationFromQuaternion : function( q ) {
  359. var x = q.x, y = q.y, z = q.z, w = q.w,
  360. x2 = x + x, y2 = y + y, z2 = z + z,
  361. xx = x * x2, xy = x * y2, xz = x * z2,
  362. yy = y * y2, yz = y * z2, zz = z * z2,
  363. wx = w * x2, wy = w * y2, wz = w * z2;
  364. this.n11 = 1 - ( yy + zz );
  365. this.n12 = xy - wz;
  366. this.n13 = xz + wy;
  367. this.n21 = xy + wz;
  368. this.n22 = 1 - ( xx + zz );
  369. this.n23 = yz - wx;
  370. this.n31 = xz - wy;
  371. this.n32 = yz + wx;
  372. this.n33 = 1 - ( xx + yy );
  373. return this;
  374. },
  375. scale : function ( v ) {
  376. var x = v.x, y = v.y, z = v.z;
  377. this.n11 *= x; this.n12 *= y; this.n13 *= z;
  378. this.n21 *= x; this.n22 *= y; this.n23 *= z;
  379. this.n31 *= x; this.n32 *= y; this.n33 *= z;
  380. this.n41 *= x; this.n42 *= y; this.n43 *= z;
  381. return this;
  382. },
  383. extractPosition : function ( m ) {
  384. this.n14 = m.n14;
  385. this.n24 = m.n24;
  386. this.n34 = m.n34;
  387. },
  388. extractRotation : function ( m, s ) {
  389. var invScaleX = 1 / s.x, invScaleY = 1 / s.y, invScaleZ = 1 / s.z;
  390. this.n11 = m.n11 * invScaleX;
  391. this.n21 = m.n21 * invScaleX;
  392. this.n31 = m.n31 * invScaleX;
  393. this.n12 = m.n12 * invScaleY;
  394. this.n22 = m.n22 * invScaleY;
  395. this.n32 = m.n32 * invScaleY;
  396. this.n13 = m.n13 * invScaleZ;
  397. this.n23 = m.n23 * invScaleZ;
  398. this.n33 = m.n33 * invScaleZ;
  399. }
  400. };
  401. THREE.Matrix4.makeInvert = function ( m1, m2 ) {
  402. // based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
  403. var n11 = m1.n11, n12 = m1.n12, n13 = m1.n13, n14 = m1.n14,
  404. n21 = m1.n21, n22 = m1.n22, n23 = m1.n23, n24 = m1.n24,
  405. n31 = m1.n31, n32 = m1.n32, n33 = m1.n33, n34 = m1.n34,
  406. n41 = m1.n41, n42 = m1.n42, n43 = m1.n43, n44 = m1.n44;
  407. if( m2 === undefined ) m2 = new THREE.Matrix4();
  408. m2.n11 = n23*n34*n42 - n24*n33*n42 + n24*n32*n43 - n22*n34*n43 - n23*n32*n44 + n22*n33*n44;
  409. m2.n12 = n14*n33*n42 - n13*n34*n42 - n14*n32*n43 + n12*n34*n43 + n13*n32*n44 - n12*n33*n44;
  410. m2.n13 = n13*n24*n42 - n14*n23*n42 + n14*n22*n43 - n12*n24*n43 - n13*n22*n44 + n12*n23*n44;
  411. m2.n14 = n14*n23*n32 - n13*n24*n32 - n14*n22*n33 + n12*n24*n33 + n13*n22*n34 - n12*n23*n34;
  412. m2.n21 = n24*n33*n41 - n23*n34*n41 - n24*n31*n43 + n21*n34*n43 + n23*n31*n44 - n21*n33*n44;
  413. m2.n22 = n13*n34*n41 - n14*n33*n41 + n14*n31*n43 - n11*n34*n43 - n13*n31*n44 + n11*n33*n44;
  414. m2.n23 = n14*n23*n41 - n13*n24*n41 - n14*n21*n43 + n11*n24*n43 + n13*n21*n44 - n11*n23*n44;
  415. m2.n24 = n13*n24*n31 - n14*n23*n31 + n14*n21*n33 - n11*n24*n33 - n13*n21*n34 + n11*n23*n34;
  416. m2.n31 = n22*n34*n41 - n24*n32*n41 + n24*n31*n42 - n21*n34*n42 - n22*n31*n44 + n21*n32*n44;
  417. m2.n32 = n14*n32*n41 - n12*n34*n41 - n14*n31*n42 + n11*n34*n42 + n12*n31*n44 - n11*n32*n44;
  418. m2.n33 = n13*n24*n41 - n14*n22*n41 + n14*n21*n42 - n11*n24*n42 - n12*n21*n44 + n11*n22*n44;
  419. m2.n34 = n14*n22*n31 - n12*n24*n31 - n14*n21*n32 + n11*n24*n32 + n12*n21*n34 - n11*n22*n34;
  420. m2.n41 = n23*n32*n41 - n22*n33*n41 - n23*n31*n42 + n21*n33*n42 + n22*n31*n43 - n21*n32*n43;
  421. m2.n42 = n12*n33*n41 - n13*n32*n41 + n13*n31*n42 - n11*n33*n42 - n12*n31*n43 + n11*n32*n43;
  422. m2.n43 = n13*n22*n41 - n12*n23*n41 - n13*n21*n42 + n11*n23*n42 + n12*n21*n43 - n11*n22*n43;
  423. m2.n44 = n12*n23*n31 - n13*n22*n31 + n13*n21*n32 - n11*n23*n32 - n12*n21*n33 + n11*n22*n33;
  424. m2.multiplyScalar( 1 / m1.determinant() );
  425. return m2;
  426. };
  427. THREE.Matrix4.makeInvert3x3 = function ( m1 ) {
  428. // input: THREE.Matrix4, output: THREE.Matrix3
  429. // ( based on http://code.google.com/p/webgl-mjs/ )
  430. var m33 = m1.m33, m33m = m33.m,
  431. a11 = m1.n33 * m1.n22 - m1.n32 * m1.n23,
  432. a21 = - m1.n33 * m1.n21 + m1.n31 * m1.n23,
  433. a31 = m1.n32 * m1.n21 - m1.n31 * m1.n22,
  434. a12 = - m1.n33 * m1.n12 + m1.n32 * m1.n13,
  435. a22 = m1.n33 * m1.n11 - m1.n31 * m1.n13,
  436. a32 = - m1.n32 * m1.n11 + m1.n31 * m1.n12,
  437. a13 = m1.n23 * m1.n12 - m1.n22 * m1.n13,
  438. a23 = - m1.n23 * m1.n11 + m1.n21 * m1.n13,
  439. a33 = m1.n22 * m1.n11 - m1.n21 * m1.n12,
  440. det = m1.n11 * a11 + m1.n21 * a12 + m1.n31 * a13,
  441. idet;
  442. // no inverse
  443. if (det == 0) {
  444. throw "matrix not invertible";
  445. }
  446. idet = 1.0 / det;
  447. m33m[ 0 ] = idet * a11; m33m[ 1 ] = idet * a21; m33m[ 2 ] = idet * a31;
  448. m33m[ 3 ] = idet * a12; m33m[ 4 ] = idet * a22; m33m[ 5 ] = idet * a32;
  449. m33m[ 6 ] = idet * a13; m33m[ 7 ] = idet * a23; m33m[ 8 ] = idet * a33;
  450. return m33;
  451. }
  452. THREE.Matrix4.makeFrustum = function ( left, right, bottom, top, near, far ) {
  453. var m, x, y, a, b, c, d;
  454. m = new THREE.Matrix4();
  455. x = 2 * near / ( right - left );
  456. y = 2 * near / ( top - bottom );
  457. a = ( right + left ) / ( right - left );
  458. b = ( top + bottom ) / ( top - bottom );
  459. c = - ( far + near ) / ( far - near );
  460. d = - 2 * far * near / ( far - near );
  461. m.n11 = x; m.n12 = 0; m.n13 = a; m.n14 = 0;
  462. m.n21 = 0; m.n22 = y; m.n23 = b; m.n24 = 0;
  463. m.n31 = 0; m.n32 = 0; m.n33 = c; m.n34 = d;
  464. m.n41 = 0; m.n42 = 0; m.n43 = - 1; m.n44 = 0;
  465. return m;
  466. };
  467. THREE.Matrix4.makePerspective = function ( fov, aspect, near, far ) {
  468. var ymax, ymin, xmin, xmax;
  469. ymax = near * Math.tan( fov * Math.PI / 360 );
  470. ymin = - ymax;
  471. xmin = ymin * aspect;
  472. xmax = ymax * aspect;
  473. return THREE.Matrix4.makeFrustum( xmin, xmax, ymin, ymax, near, far );
  474. };
  475. THREE.Matrix4.makeOrtho = function ( left, right, top, bottom, near, far ) {
  476. var m, x, y, z, w, h, p;
  477. m = new THREE.Matrix4();
  478. w = right - left;
  479. h = top - bottom;
  480. p = far - near;
  481. x = ( right + left ) / w;
  482. y = ( top + bottom ) / h;
  483. z = ( far + near ) / p;
  484. m.n11 = 2 / w; m.n12 = 0; m.n13 = 0; m.n14 = -x;
  485. m.n21 = 0; m.n22 = 2 / h; m.n23 = 0; m.n24 = -y;
  486. m.n31 = 0; m.n32 = 0; m.n33 = -2 / p; m.n34 = -z;
  487. m.n41 = 0; m.n42 = 0; m.n43 = 0; m.n44 = 1;
  488. return m;
  489. };
  490. THREE.Matrix4.__v1 = new THREE.Vector3();
  491. THREE.Matrix4.__v2 = new THREE.Vector3();
  492. THREE.Matrix4.__v3 = new THREE.Vector3();
粤ICP备19079148号