Matrix4.js 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  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. * @author timknip / http://www.floorplanner.com/
  10. */
  11. THREE.Matrix4 = function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
  12. this.set(
  13. ( n11 !== undefined ) ? n11 : 1, n12 || 0, n13 || 0, n14 || 0,
  14. n21 || 0, ( n22 !== undefined ) ? n22 : 1, n23 || 0, n24 || 0,
  15. n31 || 0, n32 || 0, ( n33 !== undefined ) ? n33 : 1, n34 || 0,
  16. n41 || 0, n42 || 0, n43 || 0, ( n44 !== undefined ) ? n44 : 1
  17. );
  18. this.flat = new Array( 16 );
  19. this.m33 = new THREE.Matrix3();
  20. };
  21. THREE.Matrix4.prototype = {
  22. constructor: THREE.Matrix4,
  23. set: function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
  24. this.n11 = n11; this.n12 = n12; this.n13 = n13; this.n14 = n14;
  25. this.n21 = n21; this.n22 = n22; this.n23 = n23; this.n24 = n24;
  26. this.n31 = n31; this.n32 = n32; this.n33 = n33; this.n34 = n34;
  27. this.n41 = n41; this.n42 = n42; this.n43 = n43; this.n44 = n44;
  28. return this;
  29. },
  30. identity: function () {
  31. this.set(
  32. 1, 0, 0, 0,
  33. 0, 1, 0, 0,
  34. 0, 0, 1, 0,
  35. 0, 0, 0, 1
  36. );
  37. return this;
  38. },
  39. copy: function ( m ) {
  40. this.set(
  41. m.n11, m.n12, m.n13, m.n14,
  42. m.n21, m.n22, m.n23, m.n24,
  43. m.n31, m.n32, m.n33, m.n34,
  44. m.n41, m.n42, m.n43, m.n44
  45. );
  46. return this;
  47. },
  48. lookAt: function ( eye, center, up ) {
  49. var x = THREE.Matrix4.__v1, y = THREE.Matrix4.__v2, z = THREE.Matrix4.__v3;
  50. z.sub( eye, center ).normalize();
  51. if ( z.length() === 0 ) {
  52. z.z = 1;
  53. }
  54. x.cross( up, z ).normalize();
  55. if ( x.length() === 0 ) {
  56. z.x += 0.0001;
  57. x.cross( up, z ).normalize();
  58. }
  59. y.cross( z, x ).normalize();
  60. this.n11 = x.x; this.n12 = y.x; this.n13 = z.x;
  61. this.n21 = x.y; this.n22 = y.y; this.n23 = z.y;
  62. this.n31 = x.z; this.n32 = y.z; this.n33 = z.z;
  63. return this;
  64. },
  65. multiply: function ( a, b ) {
  66. var a11 = a.n11, a12 = a.n12, a13 = a.n13, a14 = a.n14,
  67. a21 = a.n21, a22 = a.n22, a23 = a.n23, a24 = a.n24,
  68. a31 = a.n31, a32 = a.n32, a33 = a.n33, a34 = a.n34,
  69. a41 = a.n41, a42 = a.n42, a43 = a.n43, a44 = a.n44,
  70. b11 = b.n11, b12 = b.n12, b13 = b.n13, b14 = b.n14,
  71. b21 = b.n21, b22 = b.n22, b23 = b.n23, b24 = b.n24,
  72. b31 = b.n31, b32 = b.n32, b33 = b.n33, b34 = b.n34,
  73. b41 = b.n41, b42 = b.n42, b43 = b.n43, b44 = b.n44;
  74. this.n11 = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
  75. this.n12 = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
  76. this.n13 = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
  77. this.n14 = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
  78. this.n21 = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
  79. this.n22 = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
  80. this.n23 = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
  81. this.n24 = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
  82. this.n31 = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
  83. this.n32 = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
  84. this.n33 = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
  85. this.n34 = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
  86. this.n41 = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
  87. this.n42 = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
  88. this.n43 = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
  89. this.n44 = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
  90. return this;
  91. },
  92. multiplySelf: function ( m ) {
  93. return this.multiply( this, m );
  94. },
  95. multiplyToArray: function ( a, b, r ) {
  96. this.multiply( a, b );
  97. r[ 0 ] = this.n11; r[ 1 ] = this.n21; r[ 2 ] = this.n31; r[ 3 ] = this.n41;
  98. r[ 4 ] = this.n12; r[ 5 ] = this.n22; r[ 6 ] = this.n32; r[ 7 ] = this.n42;
  99. r[ 8 ] = this.n13; r[ 9 ] = this.n23; r[ 10 ] = this.n33; r[ 11 ] = this.n43;
  100. r[ 12 ] = this.n14; r[ 13 ] = this.n24; r[ 14 ] = this.n34; r[ 15 ] = this.n44;
  101. return this;
  102. },
  103. multiplyScalar: function ( s ) {
  104. this.n11 *= s; this.n12 *= s; this.n13 *= s; this.n14 *= s;
  105. this.n21 *= s; this.n22 *= s; this.n23 *= s; this.n24 *= s;
  106. this.n31 *= s; this.n32 *= s; this.n33 *= s; this.n34 *= s;
  107. this.n41 *= s; this.n42 *= s; this.n43 *= s; this.n44 *= s;
  108. return this;
  109. },
  110. multiplyVector3: function ( v ) {
  111. var vx = v.x, vy = v.y, vz = v.z,
  112. d = 1 / ( this.n41 * vx + this.n42 * vy + this.n43 * vz + this.n44 );
  113. v.x = ( this.n11 * vx + this.n12 * vy + this.n13 * vz + this.n14 ) * d;
  114. v.y = ( this.n21 * vx + this.n22 * vy + this.n23 * vz + this.n24 ) * d;
  115. v.z = ( this.n31 * vx + this.n32 * vy + this.n33 * vz + this.n34 ) * d;
  116. return v;
  117. },
  118. multiplyVector4: function ( v ) {
  119. var vx = v.x, vy = v.y, vz = v.z, vw = v.w;
  120. v.x = this.n11 * vx + this.n12 * vy + this.n13 * vz + this.n14 * vw;
  121. v.y = this.n21 * vx + this.n22 * vy + this.n23 * vz + this.n24 * vw;
  122. v.z = this.n31 * vx + this.n32 * vy + this.n33 * vz + this.n34 * vw;
  123. v.w = this.n41 * vx + this.n42 * vy + this.n43 * vz + this.n44 * vw;
  124. return v;
  125. },
  126. rotateAxis: function ( v ) {
  127. var vx = v.x, vy = v.y, vz = v.z;
  128. v.x = vx * this.n11 + vy * this.n12 + vz * this.n13;
  129. v.y = vx * this.n21 + vy * this.n22 + vz * this.n23;
  130. v.z = vx * this.n31 + vy * this.n32 + vz * this.n33;
  131. v.normalize();
  132. return v;
  133. },
  134. crossVector: function ( a ) {
  135. var v = new THREE.Vector4();
  136. v.x = this.n11 * a.x + this.n12 * a.y + this.n13 * a.z + this.n14 * a.w;
  137. v.y = this.n21 * a.x + this.n22 * a.y + this.n23 * a.z + this.n24 * a.w;
  138. v.z = this.n31 * a.x + this.n32 * a.y + this.n33 * a.z + this.n34 * a.w;
  139. v.w = ( a.w ) ? this.n41 * a.x + this.n42 * a.y + this.n43 * a.z + this.n44 * a.w : 1;
  140. return v;
  141. },
  142. determinant: function () {
  143. var n11 = this.n11, n12 = this.n12, n13 = this.n13, n14 = this.n14,
  144. n21 = this.n21, n22 = this.n22, n23 = this.n23, n24 = this.n24,
  145. n31 = this.n31, n32 = this.n32, n33 = this.n33, n34 = this.n34,
  146. n41 = this.n41, n42 = this.n42, n43 = this.n43, n44 = this.n44;
  147. //TODO: make this more efficient
  148. //( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm )
  149. return (
  150. n14 * n23 * n32 * n41-
  151. n13 * n24 * n32 * n41-
  152. n14 * n22 * n33 * n41+
  153. n12 * n24 * n33 * n41+
  154. n13 * n22 * n34 * n41-
  155. n12 * n23 * n34 * n41-
  156. n14 * n23 * n31 * n42+
  157. n13 * n24 * n31 * n42+
  158. n14 * n21 * n33 * n42-
  159. n11 * n24 * n33 * n42-
  160. n13 * n21 * n34 * n42+
  161. n11 * n23 * n34 * n42+
  162. n14 * n22 * n31 * n43-
  163. n12 * n24 * n31 * n43-
  164. n14 * n21 * n32 * n43+
  165. n11 * n24 * n32 * n43+
  166. n12 * n21 * n34 * n43-
  167. n11 * n22 * n34 * n43-
  168. n13 * n22 * n31 * n44+
  169. n12 * n23 * n31 * n44+
  170. n13 * n21 * n32 * n44-
  171. n11 * n23 * n32 * n44-
  172. n12 * n21 * n33 * n44+
  173. n11 * n22 * n33 * n44
  174. );
  175. },
  176. transpose: function () {
  177. var tmp;
  178. tmp = this.n21; this.n21 = this.n12; this.n12 = tmp;
  179. tmp = this.n31; this.n31 = this.n13; this.n13 = tmp;
  180. tmp = this.n32; this.n32 = this.n23; this.n23 = tmp;
  181. tmp = this.n41; this.n41 = this.n14; this.n14 = tmp;
  182. tmp = this.n42; this.n42 = this.n24; this.n24 = tmp;
  183. tmp = this.n43; this.n43 = this.n34; this.n34 = tmp;
  184. return this;
  185. },
  186. clone: function () {
  187. var m = new THREE.Matrix4();
  188. m.n11 = this.n11; m.n12 = this.n12; m.n13 = this.n13; m.n14 = this.n14;
  189. m.n21 = this.n21; m.n22 = this.n22; m.n23 = this.n23; m.n24 = this.n24;
  190. m.n31 = this.n31; m.n32 = this.n32; m.n33 = this.n33; m.n34 = this.n34;
  191. m.n41 = this.n41; m.n42 = this.n42; m.n43 = this.n43; m.n44 = this.n44;
  192. return m;
  193. },
  194. flatten: function () {
  195. this.flat[ 0 ] = this.n11; this.flat[ 1 ] = this.n21; this.flat[ 2 ] = this.n31; this.flat[ 3 ] = this.n41;
  196. this.flat[ 4 ] = this.n12; this.flat[ 5 ] = this.n22; this.flat[ 6 ] = this.n32; this.flat[ 7 ] = this.n42;
  197. this.flat[ 8 ] = this.n13; this.flat[ 9 ] = this.n23; this.flat[ 10 ] = this.n33; this.flat[ 11 ] = this.n43;
  198. this.flat[ 12 ] = this.n14; this.flat[ 13 ] = this.n24; this.flat[ 14 ] = this.n34; this.flat[ 15 ] = this.n44;
  199. return this.flat;
  200. },
  201. flattenToArray: function ( flat ) {
  202. flat[ 0 ] = this.n11; flat[ 1 ] = this.n21; flat[ 2 ] = this.n31; flat[ 3 ] = this.n41;
  203. flat[ 4 ] = this.n12; flat[ 5 ] = this.n22; flat[ 6 ] = this.n32; flat[ 7 ] = this.n42;
  204. flat[ 8 ] = this.n13; flat[ 9 ] = this.n23; flat[ 10 ] = this.n33; flat[ 11 ] = this.n43;
  205. flat[ 12 ] = this.n14; flat[ 13 ] = this.n24; flat[ 14 ] = this.n34; flat[ 15 ] = this.n44;
  206. return flat;
  207. },
  208. flattenToArrayOffset: function( flat, offset ) {
  209. flat[ offset ] = this.n11;
  210. flat[ offset + 1 ] = this.n21;
  211. flat[ offset + 2 ] = this.n31;
  212. flat[ offset + 3 ] = this.n41;
  213. flat[ offset + 4 ] = this.n12;
  214. flat[ offset + 5 ] = this.n22;
  215. flat[ offset + 6 ] = this.n32;
  216. flat[ offset + 7 ] = this.n42;
  217. flat[ offset + 8 ] = this.n13;
  218. flat[ offset + 9 ] = this.n23;
  219. flat[ offset + 10 ] = this.n33;
  220. flat[ offset + 11 ] = this.n43;
  221. flat[ offset + 12 ] = this.n14;
  222. flat[ offset + 13 ] = this.n24;
  223. flat[ offset + 14 ] = this.n34;
  224. flat[ offset + 15 ] = this.n44;
  225. return flat;
  226. },
  227. setTranslation: function( x, y, z ) {
  228. this.set(
  229. 1, 0, 0, x,
  230. 0, 1, 0, y,
  231. 0, 0, 1, z,
  232. 0, 0, 0, 1
  233. );
  234. return this;
  235. },
  236. setScale: function ( x, y, z ) {
  237. this.set(
  238. x, 0, 0, 0,
  239. 0, y, 0, 0,
  240. 0, 0, z, 0,
  241. 0, 0, 0, 1
  242. );
  243. return this;
  244. },
  245. setRotationX: function ( theta ) {
  246. var c = Math.cos( theta ), s = Math.sin( theta );
  247. this.set(
  248. 1, 0, 0, 0,
  249. 0, c, -s, 0,
  250. 0, s, c, 0,
  251. 0, 0, 0, 1
  252. );
  253. return this;
  254. },
  255. setRotationY: function( theta ) {
  256. var c = Math.cos( theta ), s = Math.sin( theta );
  257. this.set(
  258. c, 0, s, 0,
  259. 0, 1, 0, 0,
  260. -s, 0, c, 0,
  261. 0, 0, 0, 1
  262. );
  263. return this;
  264. },
  265. setRotationZ: function( theta ) {
  266. var c = Math.cos( theta ), s = Math.sin( theta );
  267. this.set(
  268. c, -s, 0, 0,
  269. s, c, 0, 0,
  270. 0, 0, 1, 0,
  271. 0, 0, 0, 1
  272. );
  273. return this;
  274. },
  275. setRotationAxis: function( axis, angle ) {
  276. // Based on http://www.gamedev.net/reference/articles/article1199.asp
  277. var c = Math.cos( angle ),
  278. s = Math.sin( angle ),
  279. t = 1 - c,
  280. x = axis.x, y = axis.y, z = axis.z,
  281. tx = t * x, ty = t * y;
  282. this.set(
  283. tx * x + c, tx * y - s * z, tx * z + s * y, 0,
  284. tx * y + s * z, ty * y + c, ty * z - s * x, 0,
  285. tx * z - s * y, ty * z + s * x, t * z * z + c, 0,
  286. 0, 0, 0, 1
  287. );
  288. return this;
  289. },
  290. setPosition: function ( v ) {
  291. this.n14 = v.x;
  292. this.n24 = v.y;
  293. this.n34 = v.z;
  294. return this;
  295. },
  296. getPosition: function () {
  297. return THREE.Matrix4.__v1.set( this.n14, this.n24, this.n34 );
  298. },
  299. getColumnX: function () {
  300. return THREE.Matrix4.__v1.set( this.n11, this.n21, this.n31 );
  301. },
  302. getColumnY: function () {
  303. return THREE.Matrix4.__v1.set( this.n12, this.n22, this.n32 );
  304. },
  305. getColumnZ: function() {
  306. return THREE.Matrix4.__v1.set( this.n13, this.n23, this.n33 );
  307. },
  308. getInverse: function ( m ) {
  309. // based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
  310. var n11 = m.n11, n12 = m.n12, n13 = m.n13, n14 = m.n14,
  311. n21 = m.n21, n22 = m.n22, n23 = m.n23, n24 = m.n24,
  312. n31 = m.n31, n32 = m.n32, n33 = m.n33, n34 = m.n34,
  313. n41 = m.n41, n42 = m.n42, n43 = m.n43, n44 = m.n44;
  314. this.n11 = n23*n34*n42 - n24*n33*n42 + n24*n32*n43 - n22*n34*n43 - n23*n32*n44 + n22*n33*n44;
  315. this.n12 = n14*n33*n42 - n13*n34*n42 - n14*n32*n43 + n12*n34*n43 + n13*n32*n44 - n12*n33*n44;
  316. this.n13 = n13*n24*n42 - n14*n23*n42 + n14*n22*n43 - n12*n24*n43 - n13*n22*n44 + n12*n23*n44;
  317. this.n14 = n14*n23*n32 - n13*n24*n32 - n14*n22*n33 + n12*n24*n33 + n13*n22*n34 - n12*n23*n34;
  318. this.n21 = n24*n33*n41 - n23*n34*n41 - n24*n31*n43 + n21*n34*n43 + n23*n31*n44 - n21*n33*n44;
  319. this.n22 = n13*n34*n41 - n14*n33*n41 + n14*n31*n43 - n11*n34*n43 - n13*n31*n44 + n11*n33*n44;
  320. this.n23 = n14*n23*n41 - n13*n24*n41 - n14*n21*n43 + n11*n24*n43 + n13*n21*n44 - n11*n23*n44;
  321. this.n24 = n13*n24*n31 - n14*n23*n31 + n14*n21*n33 - n11*n24*n33 - n13*n21*n34 + n11*n23*n34;
  322. this.n31 = n22*n34*n41 - n24*n32*n41 + n24*n31*n42 - n21*n34*n42 - n22*n31*n44 + n21*n32*n44;
  323. this.n32 = n14*n32*n41 - n12*n34*n41 - n14*n31*n42 + n11*n34*n42 + n12*n31*n44 - n11*n32*n44;
  324. this.n33 = n12*n24*n41 - n14*n22*n41 + n14*n21*n42 - n11*n24*n42 - n12*n21*n44 + n11*n22*n44;
  325. this.n34 = n14*n22*n31 - n12*n24*n31 - n14*n21*n32 + n11*n24*n32 + n12*n21*n34 - n11*n22*n34;
  326. this.n41 = n23*n32*n41 - n22*n33*n41 - n23*n31*n42 + n21*n33*n42 + n22*n31*n43 - n21*n32*n43;
  327. this.n42 = n12*n33*n41 - n13*n32*n41 + n13*n31*n42 - n11*n33*n42 - n12*n31*n43 + n11*n32*n43;
  328. this.n43 = n13*n22*n41 - n12*n23*n41 - n13*n21*n42 + n11*n23*n42 + n12*n21*n43 - n11*n22*n43;
  329. this.n44 = n12*n23*n31 - n13*n22*n31 + n13*n21*n32 - n11*n23*n32 - n12*n21*n33 + n11*n22*n33;
  330. this.multiplyScalar( 1 / m.determinant() );
  331. return this;
  332. },
  333. setRotationFromEuler: function( v, order ) {
  334. var x = v.x, y = v.y, z = v.z,
  335. a = Math.cos( x ), b = Math.sin( x ),
  336. c = Math.cos( y ), d = Math.sin( y ),
  337. e = Math.cos( z ), f = Math.sin( z );
  338. switch ( order ) {
  339. case 'YXZ':
  340. var ce = c * e, cf = c * f, de = d * e, df = d * f;
  341. this.n11 = ce + df * b;
  342. this.n12 = de * b - cf;
  343. this.n13 = a * d;
  344. this.n21 = a * f;
  345. this.n22 = a * e;
  346. this.n23 = - b;
  347. this.n31 = cf * b - de;
  348. this.n32 = df + ce * b;
  349. this.n33 = a * c;
  350. break;
  351. case 'ZXY':
  352. var ce = c * e, cf = c * f, de = d * e, df = d * f;
  353. this.n11 = ce - df * b;
  354. this.n12 = - a * f;
  355. this.n13 = de + cf * b;
  356. this.n21 = cf + de * b;
  357. this.n22 = a * e;
  358. this.n23 = df - ce * b;
  359. this.n31 = - a * d;
  360. this.n32 = b;
  361. this.n33 = a * c;
  362. break;
  363. case 'ZYX':
  364. var ae = a * e, af = a * f, be = b * e, bf = b * f;
  365. this.n11 = c * e;
  366. this.n12 = be * d - af;
  367. this.n13 = ae * d + bf;
  368. this.n21 = c * f;
  369. this.n22 = bf * d + ae;
  370. this.n23 = af * d - be;
  371. this.n31 = - d;
  372. this.n32 = b * c;
  373. this.n33 = a * c;
  374. break;
  375. case 'YZX':
  376. var ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  377. this.n11 = c * e;
  378. this.n12 = bd - ac * f;
  379. this.n13 = bc * f + ad;
  380. this.n21 = f;
  381. this.n22 = a * e;
  382. this.n23 = - b * e;
  383. this.n31 = - d * e;
  384. this.n32 = ad * f + bc;
  385. this.n33 = ac - bd * f;
  386. break;
  387. case 'XZY':
  388. var ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  389. this.n11 = c * e;
  390. this.n12 = - f;
  391. this.n13 = d * e;
  392. this.n21 = ac * f + bd;
  393. this.n22 = a * e;
  394. this.n23 = ad * f - bc;
  395. this.n31 = bc * f - ad;
  396. this.n32 = b * e;
  397. this.n33 = bd * f + ac;
  398. break;
  399. default: // 'XYZ'
  400. var ae = a * e, af = a * f, be = b * e, bf = b * f;
  401. this.n11 = c * e;
  402. this.n12 = - c * f;
  403. this.n13 = d;
  404. this.n21 = af + be * d;
  405. this.n22 = ae - bf * d;
  406. this.n23 = - b * c;
  407. this.n31 = bf - ae * d;
  408. this.n32 = be + af * d;
  409. this.n33 = a * c;
  410. break;
  411. }
  412. return this;
  413. },
  414. setRotationFromQuaternion: function( q ) {
  415. var x = q.x, y = q.y, z = q.z, w = q.w,
  416. x2 = x + x, y2 = y + y, z2 = z + z,
  417. xx = x * x2, xy = x * y2, xz = x * z2,
  418. yy = y * y2, yz = y * z2, zz = z * z2,
  419. wx = w * x2, wy = w * y2, wz = w * z2;
  420. this.n11 = 1 - ( yy + zz );
  421. this.n12 = xy - wz;
  422. this.n13 = xz + wy;
  423. this.n21 = xy + wz;
  424. this.n22 = 1 - ( xx + zz );
  425. this.n23 = yz - wx;
  426. this.n31 = xz - wy;
  427. this.n32 = yz + wx;
  428. this.n33 = 1 - ( xx + yy );
  429. return this;
  430. },
  431. scale: function ( v ) {
  432. var x = v.x, y = v.y, z = v.z;
  433. this.n11 *= x; this.n12 *= y; this.n13 *= z;
  434. this.n21 *= x; this.n22 *= y; this.n23 *= z;
  435. this.n31 *= x; this.n32 *= y; this.n33 *= z;
  436. this.n41 *= x; this.n42 *= y; this.n43 *= z;
  437. return this;
  438. },
  439. compose: function ( translation, rotation, scale ) {
  440. var mRotation = THREE.Matrix4.__m1;
  441. var mScale = THREE.Matrix4.__m2;
  442. mRotation.identity();
  443. mRotation.setRotationFromQuaternion( rotation );
  444. mScale.setScale( scale.x, scale.y, scale.z );
  445. this.multiply( mRotation, mScale );
  446. this.n14 = translation.x;
  447. this.n24 = translation.y;
  448. this.n34 = translation.z;
  449. return this;
  450. },
  451. decompose: function ( translation, rotation, scale ) {
  452. // grab the axis vectors
  453. var x = THREE.Matrix4.__v1;
  454. var y = THREE.Matrix4.__v2;
  455. var z = THREE.Matrix4.__v3;
  456. x.set( this.n11, this.n21, this.n31 );
  457. y.set( this.n12, this.n22, this.n32 );
  458. z.set( this.n13, this.n23, this.n33 );
  459. translation = ( translation instanceof THREE.Vector3 ) ? translation : new THREE.Vector3();
  460. rotation = ( rotation instanceof THREE.Quaternion ) ? rotation : new THREE.Quaternion();
  461. scale = ( scale instanceof THREE.Vector3 ) ? scale : new THREE.Vector3();
  462. scale.x = x.length();
  463. scale.y = y.length();
  464. scale.z = z.length();
  465. translation.x = this.n14;
  466. translation.y = this.n24;
  467. translation.z = this.n34;
  468. // scale the rotation part
  469. var matrix = THREE.Matrix4.__m1;
  470. matrix.copy( this );
  471. matrix.n11 /= scale.x;
  472. matrix.n21 /= scale.x;
  473. matrix.n31 /= scale.x;
  474. matrix.n12 /= scale.y;
  475. matrix.n22 /= scale.y;
  476. matrix.n32 /= scale.y;
  477. matrix.n13 /= scale.z;
  478. matrix.n23 /= scale.z;
  479. matrix.n33 /= scale.z;
  480. rotation.setFromRotationMatrix( matrix );
  481. return [ translation, rotation, scale ];
  482. },
  483. extractPosition: function ( m ) {
  484. this.n14 = m.n14;
  485. this.n24 = m.n24;
  486. this.n34 = m.n34;
  487. return this;
  488. },
  489. extractRotation: function ( m ) {
  490. var vector = THREE.Matrix4.__v1;
  491. var scaleX = 1 / vector.set( m.n11, m.n21, m.n31 ).length();
  492. var scaleY = 1 / vector.set( m.n12, m.n22, m.n32 ).length();
  493. var scaleZ = 1 / vector.set( m.n13, m.n23, m.n33 ).length();
  494. this.n11 = m.n11 * scaleX;
  495. this.n21 = m.n21 * scaleX;
  496. this.n31 = m.n31 * scaleX;
  497. this.n12 = m.n12 * scaleY;
  498. this.n22 = m.n22 * scaleY;
  499. this.n32 = m.n32 * scaleY;
  500. this.n13 = m.n13 * scaleZ;
  501. this.n23 = m.n23 * scaleZ;
  502. this.n33 = m.n33 * scaleZ;
  503. return this;
  504. },
  505. rotateByAxis: function ( axis, angle ) {
  506. // optimize by checking axis
  507. if ( axis.x === 1 && axis.y === 0 && axis.z === 0 ) {
  508. return this.rotateX( angle );
  509. } else if ( axis.x === 0 && axis.y === 1 && axis.z === 0 ) {
  510. return this.rotateY( angle );
  511. } else if ( axis.x === 0 && axis.y === 0 && axis.z === 1 ) {
  512. return this.rotateZ( angle );
  513. }
  514. var x = axis.x,
  515. y = axis.y,
  516. z = axis.z,
  517. n = Math.sqrt(x * x + y * y + z * z);
  518. x /= n;
  519. y /= n;
  520. z /= n;
  521. var xx = x * x,
  522. yy = y * y,
  523. zz = z * z,
  524. c = Math.cos(angle),
  525. s = Math.sin(angle),
  526. oneMinusCosine = 1 - c,
  527. xy = x * y * oneMinusCosine,
  528. xz = x * z * oneMinusCosine,
  529. yz = y * z * oneMinusCosine,
  530. xs = x * s,
  531. ys = y * s,
  532. zs = z * s,
  533. r11 = xx + (1 - xx) * c,
  534. r21 = xy + zs,
  535. r31 = xz - ys,
  536. r12 = xy - zs,
  537. r22 = yy + (1 - yy) * c,
  538. r32 = yz + xs,
  539. r13 = xz + ys,
  540. r23 = yz - xs,
  541. r33 = zz + (1 - zz) * c,
  542. m11 = this.n11,
  543. m21 = this.n21,
  544. m31 = this.n31,
  545. m41 = this.n41,
  546. m12 = this.n12,
  547. m22 = this.n22,
  548. m32 = this.n32,
  549. m42 = this.n42,
  550. m13 = this.n13,
  551. m23 = this.n23,
  552. m33 = this.n33,
  553. m43 = this.n43,
  554. m14 = this.n14,
  555. m24 = this.n24,
  556. m34 = this.n34,
  557. m44 = this.n44;
  558. this.n11 = r11 * m11 + r21 * m12 + r31 * m13;
  559. this.n21 = r11 * m21 + r21 * m22 + r31 * m23;
  560. this.n31 = r11 * m31 + r21 * m32 + r31 * m33;
  561. this.n41 = r11 * m41 + r21 * m42 + r31 * m43;
  562. this.n12 = r12 * m11 + r22 * m12 + r32 * m13;
  563. this.n22 = r12 * m21 + r22 * m22 + r32 * m23;
  564. this.n32 = r12 * m31 + r22 * m32 + r32 * m33;
  565. this.n42 = r12 * m41 + r22 * m42 + r32 * m43;
  566. this.n13 = r13 * m11 + r23 * m12 + r33 * m13;
  567. this.n23 = r13 * m21 + r23 * m22 + r33 * m23;
  568. this.n33 = r13 * m31 + r23 * m32 + r33 * m33;
  569. this.n43 = r13 * m41 + r23 * m42 + r33 * m43;
  570. return this;
  571. },
  572. rotateX: function ( angle ) {
  573. var m12 = this.n12,
  574. m22 = this.n22,
  575. m32 = this.n32,
  576. m42 = this.n42,
  577. m13 = this.n13,
  578. m23 = this.n23,
  579. m33 = this.n33,
  580. m43 = this.n43,
  581. c = Math.cos(angle),
  582. s = Math.sin(angle);
  583. this.n12 = c * m12 + s * m13;
  584. this.n22 = c * m22 + s * m23;
  585. this.n32 = c * m32 + s * m33;
  586. this.n42 = c * m42 + s * m43;
  587. this.n13 = c * m13 - s * m12;
  588. this.n23 = c * m23 - s * m22;
  589. this.n33 = c * m33 - s * m32;
  590. this.n43 = c * m43 - s * m42;
  591. return this;
  592. },
  593. rotateY: function ( angle ) {
  594. var m11 = this.n11,
  595. m21 = this.n21,
  596. m31 = this.n31,
  597. m41 = this.n41,
  598. m13 = this.n13,
  599. m23 = this.n23,
  600. m33 = this.n33,
  601. m43 = this.n43,
  602. c = Math.cos(angle),
  603. s = Math.sin(angle);
  604. this.n11 = c * m11 - s * m13;
  605. this.n21 = c * m21 - s * m23;
  606. this.n31 = c * m31 - s * m33;
  607. this.n41 = c * m41 - s * m43;
  608. this.n13 = c * m13 + s * m11;
  609. this.n23 = c * m23 + s * m21;
  610. this.n33 = c * m33 + s * m31;
  611. this.n43 = c * m43 + s * m41;
  612. return this;
  613. },
  614. rotateZ: function ( angle ) {
  615. var m11 = this.n11,
  616. m21 = this.n21,
  617. m31 = this.n31,
  618. m41 = this.n41,
  619. m12 = this.n12,
  620. m22 = this.n22,
  621. m32 = this.n32,
  622. m42 = this.n42,
  623. c = Math.cos(angle),
  624. s = Math.sin(angle);
  625. this.n11 = c * m11 + s * m12;
  626. this.n21 = c * m21 + s * m22;
  627. this.n31 = c * m31 + s * m32;
  628. this.n41 = c * m41 + s * m42;
  629. this.n12 = c * m12 - s * m11;
  630. this.n22 = c * m22 - s * m21;
  631. this.n32 = c * m32 - s * m31;
  632. this.n42 = c * m42 - s * m41;
  633. return this;
  634. },
  635. translate: function ( v ) {
  636. var x = v.x, y = v.y, z = v.z;
  637. this.n14 = this.n11 * x + this.n12 * y + this.n13 * z + this.n14;
  638. this.n24 = this.n21 * x + this.n22 * y + this.n23 * z + this.n24;
  639. this.n34 = this.n31 * x + this.n32 * y + this.n33 * z + this.n34;
  640. this.n44 = this.n41 * x + this.n42 * y + this.n43 * z + this.n44;
  641. return this;
  642. }
  643. };
  644. THREE.Matrix4.makeInvert3x3 = function ( m1 ) {
  645. // input: THREE.Matrix4, output: THREE.Matrix3
  646. // ( based on http://code.google.com/p/webgl-mjs/ )
  647. var m33 = m1.m33, m33m = m33.m,
  648. a11 = m1.n33 * m1.n22 - m1.n32 * m1.n23,
  649. a21 = - m1.n33 * m1.n21 + m1.n31 * m1.n23,
  650. a31 = m1.n32 * m1.n21 - m1.n31 * m1.n22,
  651. a12 = - m1.n33 * m1.n12 + m1.n32 * m1.n13,
  652. a22 = m1.n33 * m1.n11 - m1.n31 * m1.n13,
  653. a32 = - m1.n32 * m1.n11 + m1.n31 * m1.n12,
  654. a13 = m1.n23 * m1.n12 - m1.n22 * m1.n13,
  655. a23 = - m1.n23 * m1.n11 + m1.n21 * m1.n13,
  656. a33 = m1.n22 * m1.n11 - m1.n21 * m1.n12,
  657. det = m1.n11 * a11 + m1.n21 * a12 + m1.n31 * a13,
  658. idet;
  659. // no inverse
  660. if ( det === 0 ) {
  661. console.error( 'THREE.Matrix4.makeInvert3x3: Matrix not invertible.' );
  662. }
  663. idet = 1.0 / det;
  664. m33m[ 0 ] = idet * a11; m33m[ 1 ] = idet * a21; m33m[ 2 ] = idet * a31;
  665. m33m[ 3 ] = idet * a12; m33m[ 4 ] = idet * a22; m33m[ 5 ] = idet * a32;
  666. m33m[ 6 ] = idet * a13; m33m[ 7 ] = idet * a23; m33m[ 8 ] = idet * a33;
  667. return m33;
  668. }
  669. THREE.Matrix4.makeFrustum = function ( left, right, bottom, top, near, far ) {
  670. var m, x, y, a, b, c, d;
  671. m = new THREE.Matrix4();
  672. x = 2 * near / ( right - left );
  673. y = 2 * near / ( top - bottom );
  674. a = ( right + left ) / ( right - left );
  675. b = ( top + bottom ) / ( top - bottom );
  676. c = - ( far + near ) / ( far - near );
  677. d = - 2 * far * near / ( far - near );
  678. m.n11 = x; m.n12 = 0; m.n13 = a; m.n14 = 0;
  679. m.n21 = 0; m.n22 = y; m.n23 = b; m.n24 = 0;
  680. m.n31 = 0; m.n32 = 0; m.n33 = c; m.n34 = d;
  681. m.n41 = 0; m.n42 = 0; m.n43 = - 1; m.n44 = 0;
  682. return m;
  683. };
  684. THREE.Matrix4.makePerspective = function ( fov, aspect, near, far ) {
  685. var ymax, ymin, xmin, xmax;
  686. ymax = near * Math.tan( fov * Math.PI / 360 );
  687. ymin = - ymax;
  688. xmin = ymin * aspect;
  689. xmax = ymax * aspect;
  690. return THREE.Matrix4.makeFrustum( xmin, xmax, ymin, ymax, near, far );
  691. };
  692. THREE.Matrix4.makeOrtho = function ( left, right, top, bottom, near, far ) {
  693. var m, x, y, z, w, h, p;
  694. m = new THREE.Matrix4();
  695. w = right - left;
  696. h = top - bottom;
  697. p = far - near;
  698. x = ( right + left ) / w;
  699. y = ( top + bottom ) / h;
  700. z = ( far + near ) / p;
  701. m.n11 = 2 / w; m.n12 = 0; m.n13 = 0; m.n14 = -x;
  702. m.n21 = 0; m.n22 = 2 / h; m.n23 = 0; m.n24 = -y;
  703. m.n31 = 0; m.n32 = 0; m.n33 = -2 / p; m.n34 = -z;
  704. m.n41 = 0; m.n42 = 0; m.n43 = 0; m.n44 = 1;
  705. return m;
  706. };
  707. THREE.Matrix4.__v1 = new THREE.Vector3();
  708. THREE.Matrix4.__v2 = new THREE.Vector3();
  709. THREE.Matrix4.__v3 = new THREE.Vector3();
  710. THREE.Matrix4.__m1 = new THREE.Matrix4();
  711. THREE.Matrix4.__m2 = new THREE.Matrix4();