Matrix4.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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.__tmpVec1, y = THREE.Matrix4.__tmpVec2, z = THREE.Matrix4.__tmpVec3;
  48. z.sub( eye, center ).normalize();
  49. x.cross( up, z ).normalize();
  50. y.cross( z, x ).normalize();
  51. /* this.n11 = x.x; this.n12 = x.y; this.n13 = x.z; this.n14 = - x.dot( eye );
  52. this.n21 = y.x; this.n22 = y.y; this.n23 = y.z; this.n24 = - y.dot( eye );
  53. this.n31 = z.x; this.n32 = z.y; this.n33 = z.z; this.n34 = - z.dot( eye );
  54. this.n41 = 0; this.n42 = 0; this.n43 = 0; this.n44 = 1;
  55. */
  56. this.n11 = x.x; this.n12 = y.x; this.n13 = z.x; this.n14 = -eye.x;
  57. this.n21 = x.y; this.n22 = y.y; this.n23 = z.y; this.n24 = -eye.y;
  58. this.n31 = x.z; this.n32 = y.z; this.n33 = z.z; this.n34 = -eye.z;
  59. return this;
  60. },
  61. multiplyVector3: function ( v ) {
  62. var vx = v.x, vy = v.y, vz = v.z,
  63. d = 1 / ( this.n41 * vx + this.n42 * vy + this.n43 * vz + this.n44 );
  64. v.x = ( this.n11 * vx + this.n12 * vy + this.n13 * vz + this.n14 ) * d;
  65. v.y = ( this.n21 * vx + this.n22 * vy + this.n23 * vz + this.n24 ) * d;
  66. v.z = ( this.n31 * vx + this.n32 * vy + this.n33 * vz + this.n34 ) * d;
  67. return v;
  68. },
  69. multiplyVector3OnlyZ: function( v ) {
  70. var vx = v.x, vy = v.y, vz = v.z,
  71. d = 1 / ( this.n41 * vx + this.n42 * vy + this.n43 * vz + this.n44 );
  72. return ( this.n31 * vx + this.n32 * vy + this.n33 * vz + this.n34 ) * d;
  73. },
  74. multiplyVector4: function ( v ) {
  75. var vx = v.x, vy = v.y, vz = v.z, vw = v.w;
  76. v.x = this.n11 * vx + this.n12 * vy + this.n13 * vz + this.n14 * vw;
  77. v.y = this.n21 * vx + this.n22 * vy + this.n23 * vz + this.n24 * vw;
  78. v.z = this.n31 * vx + this.n32 * vy + this.n33 * vz + this.n34 * vw;
  79. v.w = this.n41 * vx + this.n42 * vy + this.n43 * vz + this.n44 * vw;
  80. return v;
  81. },
  82. crossVector: function ( a ) {
  83. var v = new THREE.Vector4();
  84. v.x = this.n11 * a.x + this.n12 * a.y + this.n13 * a.z + this.n14 * a.w;
  85. v.y = this.n21 * a.x + this.n22 * a.y + this.n23 * a.z + this.n24 * a.w;
  86. v.z = this.n31 * a.x + this.n32 * a.y + this.n33 * a.z + this.n34 * a.w;
  87. v.w = ( a.w ) ? this.n41 * a.x + this.n42 * a.y + this.n43 * a.z + this.n44 * a.w : 1;
  88. return v;
  89. },
  90. multiply: function ( a, b ) {
  91. var a11 = a.n11, a12 = a.n12, a13 = a.n13, a14 = a.n14,
  92. a21 = a.n21, a22 = a.n22, a23 = a.n23, a24 = a.n24,
  93. a31 = a.n31, a32 = a.n32, a33 = a.n33, a34 = a.n34,
  94. a41 = a.n41, a42 = a.n42, a43 = a.n43, a44 = a.n44,
  95. b11 = b.n11, b12 = b.n12, b13 = b.n13, b14 = b.n14,
  96. b21 = b.n21, b22 = b.n22, b23 = b.n23, b24 = b.n24,
  97. b31 = b.n31, b32 = b.n32, b33 = b.n33, b34 = b.n34,
  98. b41 = b.n41, b42 = b.n42, b43 = b.n43, b44 = b.n44;
  99. /*
  100. this.n11 = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
  101. this.n12 = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
  102. this.n13 = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
  103. this.n14 = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
  104. this.n21 = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
  105. this.n22 = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
  106. this.n23 = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
  107. this.n24 = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
  108. this.n31 = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
  109. this.n32 = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
  110. this.n33 = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
  111. this.n34 = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
  112. this.n41 = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
  113. this.n42 = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
  114. this.n43 = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
  115. this.n44 = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
  116. */
  117. this.n11 = a11 * b11 + a12 * b21 + a13 * b31;
  118. this.n12 = a11 * b12 + a12 * b22 + a13 * b32;
  119. this.n13 = a11 * b13 + a12 * b23 + a13 * b33;
  120. this.n14 = a11 * b14 + a12 * b24 + a13 * b34 + a14;
  121. this.n21 = a21 * b11 + a22 * b21 + a23 * b31;
  122. this.n22 = a21 * b12 + a22 * b22 + a23 * b32;
  123. this.n23 = a21 * b13 + a22 * b23 + a23 * b33;
  124. this.n24 = a21 * b14 + a22 * b24 + a23 * b34 + a24;
  125. this.n31 = a31 * b11 + a32 * b21 + a33 * b31;
  126. this.n32 = a31 * b12 + a32 * b22 + a33 * b32;
  127. this.n33 = a31 * b13 + a32 * b23 + a33 * b33;
  128. this.n34 = a31 * b14 + a32 * b24 + a33 * b34 + a34;
  129. this.n41 = a41 * b11 + a42 * b21 + a43 * b31;
  130. this.n42 = a41 * b12 + a42 * b22 + a43 * b32;
  131. this.n43 = a41 * b13 + a42 * b23 + a43 * b33;
  132. this.n44 = a41 * b14 + a42 * b24 + a43 * b34 + a44;
  133. return this;
  134. },
  135. multiplyToArray: function ( a, b, r ) {
  136. var a11 = a.n11, a12 = a.n12, a13 = a.n13, a14 = a.n14,
  137. a21 = a.n21, a22 = a.n22, a23 = a.n23, a24 = a.n24,
  138. a31 = a.n31, a32 = a.n32, a33 = a.n33, a34 = a.n34,
  139. a41 = a.n41, a42 = a.n42, a43 = a.n43, a44 = a.n44,
  140. b11 = b.n11, b12 = b.n12, b13 = b.n13, b14 = b.n14,
  141. b21 = b.n21, b22 = b.n22, b23 = b.n23, b24 = b.n24,
  142. b31 = b.n31, b32 = b.n32, b33 = b.n33, b34 = b.n34,
  143. b41 = b.n41, b42 = b.n42, b43 = b.n43, b44 = b.n44;
  144. this.n11 = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
  145. this.n12 = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
  146. this.n13 = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
  147. this.n14 = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
  148. this.n21 = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
  149. this.n22 = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
  150. this.n23 = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
  151. this.n24 = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
  152. this.n31 = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
  153. this.n32 = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
  154. this.n33 = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
  155. this.n34 = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
  156. this.n41 = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
  157. this.n42 = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
  158. this.n43 = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
  159. this.n44 = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
  160. r[ 0 ] = this.n11; r[ 1 ] = this.n21; r[ 2 ] = this.n31; r[ 3 ] = this.n41;
  161. r[ 4 ] = this.n12; r[ 5 ] = this.n22; r[ 6 ] = this.n32; r[ 7 ] = this.n42;
  162. r[ 8 ] = this.n13; r[ 9 ] = this.n23; r[ 10 ] = this.n33; r[ 11 ] = this.n43;
  163. r[ 12 ] = this.n14; r[ 13 ] = this.n24; r[ 14 ] = this.n34; r[ 15 ] = this.n44;
  164. return this;
  165. },
  166. multiplySelf: function ( m ) {
  167. var n11 = this.n11, n12 = this.n12, n13 = this.n13, n14 = this.n14,
  168. n21 = this.n21, n22 = this.n22, n23 = this.n23, n24 = this.n24,
  169. n31 = this.n31, n32 = this.n32, n33 = this.n33, n34 = this.n34,
  170. n41 = this.n41, n42 = this.n42, n43 = this.n43, n44 = this.n44,
  171. mn11 = m.n11, mn21 = m.n21, mn31 = m.n31, mn41 = m.n41,
  172. mn12 = m.n12, mn22 = m.n22, mn32 = m.n32, mn42 = m.n42,
  173. mn13 = m.n13, mn23 = m.n23, mn33 = m.n33, mn43 = m.n43,
  174. mn14 = m.n14, mn24 = m.n24, mn34 = m.n34, mn44 = m.n44;
  175. /*
  176. this.n11 = n11 * mn11 + n12 * mn21 + n13 * mn31 + n14 * mn41;
  177. this.n12 = n11 * mn12 + n12 * mn22 + n13 * mn32 + n14 * mn42;
  178. this.n13 = n11 * mn13 + n12 * mn23 + n13 * mn33 + n14 * mn43;
  179. this.n14 = n11 * mn14 + n12 * mn24 + n13 * mn34 + n14 * mn44;
  180. this.n21 = n21 * mn11 + n22 * mn21 + n23 * mn31 + n24 * mn41;
  181. this.n22 = n21 * mn12 + n22 * mn22 + n23 * mn32 + n24 * mn42;
  182. this.n23 = n21 * mn13 + n22 * mn23 + n23 * mn33 + n24 * mn43;
  183. this.n24 = n21 * mn14 + n22 * mn24 + n23 * mn34 + n24 * mn44;
  184. this.n31 = n31 * mn11 + n32 * mn21 + n33 * mn31 + n34 * mn41;
  185. this.n32 = n31 * mn12 + n32 * mn22 + n33 * mn32 + n34 * mn42;
  186. this.n33 = n31 * mn13 + n32 * mn23 + n33 * mn33 + n34 * mn43;
  187. this.n34 = n31 * mn14 + n32 * mn24 + n33 * mn34 + n34 * mn44;
  188. this.n41 = n41 * mn11 + n42 * mn21 + n43 * mn31 + n44 * mn41;
  189. this.n42 = n41 * mn12 + n42 * mn22 + n43 * mn32 + n44 * mn42;
  190. this.n43 = n41 * mn13 + n42 * mn23 + n43 * mn33 + n44 * mn43;
  191. this.n44 = n41 * mn14 + n42 * mn24 + n43 * mn34 + n44 * mn44;
  192. */
  193. this.n11 = n11 * mn11 + n12 * mn21 + n13 * mn31;
  194. this.n12 = n11 * mn12 + n12 * mn22 + n13 * mn32;
  195. this.n13 = n11 * mn13 + n12 * mn23 + n13 * mn33;
  196. this.n14 = n11 * mn14 + n12 * mn24 + n13 * mn34 + n14;
  197. this.n21 = n21 * mn11 + n22 * mn21 + n23 * mn31;
  198. this.n22 = n21 * mn12 + n22 * mn22 + n23 * mn32;
  199. this.n23 = n21 * mn13 + n22 * mn23 + n23 * mn33;
  200. this.n24 = n21 * mn14 + n22 * mn24 + n23 * mn34 + n24;
  201. this.n31 = n31 * mn11 + n32 * mn21 + n33 * mn31;
  202. this.n32 = n31 * mn12 + n32 * mn22 + n33 * mn32;
  203. this.n33 = n31 * mn13 + n32 * mn23 + n33 * mn33;
  204. this.n34 = n31 * mn14 + n32 * mn24 + n33 * mn34 + n34;
  205. this.n41 = n41 * mn11 + n42 * mn21 + n43 * mn31;
  206. this.n42 = n41 * mn12 + n42 * mn22 + n43 * mn32;
  207. this.n43 = n41 * mn13 + n42 * mn23 + n43 * mn33;
  208. this.n44 = n41 * mn14 + n42 * mn24 + n43 * mn34 + n44;
  209. return this;
  210. },
  211. multiplyScalar: function ( s ) {
  212. this.n11 *= s; this.n12 *= s; this.n13 *= s; this.n14 *= s;
  213. this.n21 *= s; this.n22 *= s; this.n23 *= s; this.n24 *= s;
  214. this.n31 *= s; this.n32 *= s; this.n33 *= s; this.n34 *= s;
  215. this.n41 *= s; this.n42 *= s; this.n43 *= s; this.n44 *= s;
  216. return this;
  217. },
  218. determinant: function () {
  219. var n11 = this.n11, n12 = this.n12, n13 = this.n13, n14 = this.n14,
  220. n21 = this.n21, n22 = this.n22, n23 = this.n23, n24 = this.n24,
  221. n31 = this.n31, n32 = this.n32, n33 = this.n33, n34 = this.n34,
  222. n41 = this.n41, n42 = this.n42, n43 = this.n43, n44 = this.n44;
  223. //TODO: make this more efficient
  224. //( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm )
  225. return (
  226. n14 * n23 * n32 * n41-
  227. n13 * n24 * n32 * n41-
  228. n14 * n22 * n33 * n41+
  229. n12 * n24 * n33 * n41+
  230. n13 * n22 * n34 * n41-
  231. n12 * n23 * n34 * n41-
  232. n14 * n23 * n31 * n42+
  233. n13 * n24 * n31 * n42+
  234. n14 * n21 * n33 * n42-
  235. n11 * n24 * n33 * n42-
  236. n13 * n21 * n34 * n42+
  237. n11 * n23 * n34 * n42+
  238. n14 * n22 * n31 * n43-
  239. n12 * n24 * n31 * n43-
  240. n14 * n21 * n32 * n43+
  241. n11 * n24 * n32 * n43+
  242. n12 * n21 * n34 * n43-
  243. n11 * n22 * n34 * n43-
  244. n13 * n22 * n31 * n44+
  245. n12 * n23 * n31 * n44+
  246. n13 * n21 * n32 * n44-
  247. n11 * n23 * n32 * n44-
  248. n12 * n21 * n33 * n44+
  249. n11 * n22 * n33 * n44
  250. );
  251. },
  252. transpose: function () {
  253. var tmp;
  254. tmp = this.n21; this.n21 = this.n12; this.n12 = tmp;
  255. tmp = this.n31; this.n31 = this.n13; this.n13 = tmp;
  256. tmp = this.n32; this.n32 = this.n23; this.n23 = tmp;
  257. tmp = this.n41; this.n41 = this.n14; this.n14 = tmp;
  258. tmp = this.n42; this.n42 = this.n24; this.n24 = tmp;
  259. tmp = this.n43; this.n43 = this.n34; this.n43 = tmp;
  260. return this;
  261. },
  262. clone: function () {
  263. var m = new THREE.Matrix4();
  264. m.n11 = this.n11; m.n12 = this.n12; m.n13 = this.n13; m.n14 = this.n14;
  265. m.n21 = this.n21; m.n22 = this.n22; m.n23 = this.n23; m.n24 = this.n24;
  266. m.n31 = this.n31; m.n32 = this.n32; m.n33 = this.n33; m.n34 = this.n34;
  267. m.n41 = this.n41; m.n42 = this.n42; m.n43 = this.n43; m.n44 = this.n44;
  268. return m;
  269. },
  270. flatten: function () {
  271. this.flattenToArray( this.flat );
  272. return this.flat;
  273. },
  274. flattenToArray: function ( flat ) {
  275. flat[ 0 ] = this.n11; flat[ 1 ] = this.n21; flat[ 2 ] = this.n31; flat[ 3 ] = this.n41;
  276. flat[ 4 ] = this.n12; flat[ 5 ] = this.n22; flat[ 6 ] = this.n32; flat[ 7 ] = this.n42;
  277. flat[ 8 ] = this.n13; flat[ 9 ] = this.n23; flat[ 10 ] = this.n33; flat[ 11 ] = this.n43;
  278. flat[ 12 ] = this.n14; flat[ 13 ] = this.n24; flat[ 14 ] = this.n34; flat[ 15 ] = this.n44;
  279. return flat;
  280. },
  281. flattenToArrayOffset: function( flat, offset ) {
  282. flat[ offset ] = this.n11;
  283. flat[ offset + 1 ] = this.n21;
  284. flat[ offset + 2 ] = this.n31;
  285. flat[ offset + 3 ] = this.n41;
  286. flat[ offset + 4 ] = this.n12;
  287. flat[ offset + 5 ] = this.n22;
  288. flat[ offset + 6 ] = this.n32;
  289. flat[ offset + 7 ] = this.n42;
  290. flat[ offset + 8 ] = this.n13;
  291. flat[ offset + 9 ] = this.n23;
  292. flat[ offset + 10 ] = this.n33;
  293. flat[ offset + 11 ] = this.n43;
  294. flat[ offset + 12 ] = this.n14;
  295. flat[ offset + 13 ] = this.n24;
  296. flat[ offset + 14 ] = this.n34;
  297. flat[ offset + 15 ] = this.n44;
  298. return flat;
  299. },
  300. setTranslation: function( x, y, z ) {
  301. this.set(
  302. 1, 0, 0, x,
  303. 0, 1, 0, y,
  304. 0, 0, 1, z,
  305. 0, 0, 0, 1
  306. );
  307. return this;
  308. },
  309. setScale: function ( x, y, z ) {
  310. this.set(
  311. x, 0, 0, 0,
  312. 0, y, 0, 0,
  313. 0, 0, z, 0,
  314. 0, 0, 0, 1
  315. );
  316. return this;
  317. },
  318. setRotX: function ( theta ) {
  319. var c = Math.cos( theta ), s = Math.sin( theta );
  320. this.set(
  321. 1, 0, 0, 0,
  322. 0, c, -s, 0,
  323. 0, s, c, 0,
  324. 0, 0, 0, 1
  325. );
  326. return this;
  327. },
  328. setRotY: function( theta ) {
  329. var c = Math.cos( theta ), s = Math.sin( theta );
  330. this.set(
  331. c, 0, s, 0,
  332. 0, 1, 0, 0,
  333. -s, 0, c, 0,
  334. 0, 0, 0, 1
  335. );
  336. return this;
  337. },
  338. setRotZ: function( theta ) {
  339. var c = Math.cos( theta ), s = Math.sin( theta );
  340. this.set(
  341. c, -s, 0, 0,
  342. s, c, 0, 0,
  343. 0, 0, 1, 0,
  344. 0, 0, 0, 1
  345. );
  346. return this;
  347. },
  348. setRotAxis: function( axis, angle ) {
  349. // Based on http://www.gamedev.net/reference/articles/article1199.asp
  350. var c = Math.cos( angle ),
  351. s = Math.sin( angle ),
  352. t = 1 - c,
  353. x = axis.x, y = axis.y, z = axis.z,
  354. tx = t * x, ty = t * y;
  355. this.set(
  356. tx * x + c, tx * y - s * z, tx * z + s * y, 0,
  357. tx * y + s * z, ty * y + c, ty * z - s * x, 0,
  358. tx * z - s * y, ty * z + s * x, t * z * z + c, 0,
  359. 0, 0, 0, 1
  360. );
  361. return this;
  362. },
  363. setPosition: function( vec3 ) {
  364. this.n14 = vec3.x;
  365. this.n24 = vec3.y;
  366. this.n34 = vec3.z;
  367. return this;
  368. },
  369. setRotationFromEuler: function( vec3 ) {
  370. var x = vec3.x, y = vec3.y, z = vec3.z,
  371. a = Math.cos( x ), b = Math.sin( x ),
  372. c = Math.cos( y ), d = Math.sin( y ),
  373. e = Math.cos( z ), f = Math.sin( z ),
  374. ad = a * d, bd = b * d;
  375. this.n11 = c * e;
  376. this.n12 = - c * f;
  377. this.n13 = d;
  378. this.n21 = bd * e + a * f;
  379. this.n22 = - bd * f + a * e;
  380. this.n23 = - b * c;
  381. this.n31 = - ad * e + b * f;
  382. this.n32 = ad * f + b * e;
  383. this.n33 = a * c;
  384. },
  385. setRotationFromQuaternion: function( quat ) {
  386. var x = quat.x, y = quat.y, z = quat.z, w = quat.w,
  387. x2 = x + x, y2 = y + y, z2 = z + z,
  388. xx = x * x2, xy = x * y2, xz = x * z2,
  389. yy = y * y2, yz = y * z2, zz = z * z2,
  390. wx = w * x2, wy = w * y2, wz = w * z2;
  391. this.n11 = 1 - ( yy + zz );
  392. this.n12 = xy - wz;
  393. this.n13 = xz + wy;
  394. this.n21 = xy + wz;
  395. this.n22 = 1 - ( xx + zz );
  396. this.n23 = yz - wx;
  397. this.n31 = xz - wy;
  398. this.n32 = yz + wx;
  399. this.n33 = 1 - ( xx + yy );
  400. },
  401. scale : function ( vec3 ) {
  402. var x = vec3.x, y = vec3.y, z = vec3.z;
  403. this.n11 *= x; this.n12 *= y; this.n13 *= z;
  404. this.n21 *= x; this.n22 *= y; this.n23 *= z;
  405. this.n31 *= x; this.n32 *= y; this.n33 *= z;
  406. this.n41 *= x; this.n42 *= y; this.n43 *= z;
  407. return this;
  408. }
  409. };
  410. THREE.Matrix4.translationMatrix = function ( x, y, z ) {
  411. var m = new THREE.Matrix4();
  412. m.setTranslation( x, y, z );
  413. return m;
  414. };
  415. THREE.Matrix4.scaleMatrix = function ( x, y, z ) {
  416. var m = new THREE.Matrix4();
  417. m.setScale( x, y, z );
  418. return m;
  419. };
  420. THREE.Matrix4.rotationXMatrix = function ( theta ) {
  421. var m = new THREE.Matrix4();
  422. m.setRotX( theta );
  423. return m;
  424. };
  425. THREE.Matrix4.rotationYMatrix = function ( theta ) {
  426. var m = new THREE.Matrix4();
  427. m.setRotY( theta );
  428. return m;
  429. };
  430. THREE.Matrix4.rotationZMatrix = function ( theta ) {
  431. var m = new THREE.Matrix4();
  432. m.setRotZ( theta );
  433. return m;
  434. };
  435. THREE.Matrix4.rotationAxisAngleMatrix = function ( axis, angle ) {
  436. var m = new THREE.Matrix4();
  437. m.setRotAxis( axis, angle );
  438. return m;
  439. };
  440. THREE.Matrix4.makeInvert = function ( m1, m2 ) {
  441. // based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
  442. var n11 = m1.n11, n12 = m1.n12, n13 = m1.n13, n14 = m1.n14,
  443. n21 = m1.n21, n22 = m1.n22, n23 = m1.n23, n24 = m1.n24,
  444. n31 = m1.n31, n32 = m1.n32, n33 = m1.n33, n34 = m1.n34,
  445. n41 = m1.n41, n42 = m1.n42, n43 = m1.n43, n44 = m1.n44;
  446. if( m2 === undefined ) m2 = new THREE.Matrix4();
  447. m2.n11 = n23*n34*n42 - n24*n33*n42 + n24*n32*n43 - n22*n34*n43 - n23*n32*n44 + n22*n33*n44;
  448. m2.n12 = n14*n33*n42 - n13*n34*n42 - n14*n32*n43 + n12*n34*n43 + n13*n32*n44 - n12*n33*n44;
  449. m2.n13 = n13*n24*n42 - n14*n23*n42 + n14*n22*n43 - n12*n24*n43 - n13*n22*n44 + n12*n23*n44;
  450. m2.n14 = n14*n23*n32 - n13*n24*n32 - n14*n22*n33 + n12*n24*n33 + n13*n22*n34 - n12*n23*n34;
  451. m2.n21 = n24*n33*n41 - n23*n34*n41 - n24*n31*n43 + n21*n34*n43 + n23*n31*n44 - n21*n33*n44;
  452. m2.n22 = n13*n34*n41 - n14*n33*n41 + n14*n31*n43 - n11*n34*n43 - n13*n31*n44 + n11*n33*n44;
  453. m2.n23 = n14*n23*n41 - n13*n24*n41 - n14*n21*n43 + n11*n24*n43 + n13*n21*n44 - n11*n23*n44;
  454. m2.n24 = n13*n24*n31 - n14*n23*n31 + n14*n21*n33 - n11*n24*n33 - n13*n21*n34 + n11*n23*n34;
  455. m2.n31 = n22*n34*n41 - n24*n32*n41 + n24*n31*n42 - n21*n34*n42 - n22*n31*n44 + n21*n32*n44;
  456. m2.n32 = n14*n32*n41 - n12*n34*n41 - n14*n31*n42 + n11*n34*n42 + n12*n31*n44 - n11*n32*n44;
  457. m2.n33 = n13*n24*n41 - n14*n22*n41 + n14*n21*n42 - n11*n24*n42 - n12*n21*n44 + n11*n22*n44;
  458. m2.n34 = n14*n22*n31 - n12*n24*n31 - n14*n21*n32 + n11*n24*n32 + n12*n21*n34 - n11*n22*n34;
  459. m2.n41 = n23*n32*n41 - n22*n33*n41 - n23*n31*n42 + n21*n33*n42 + n22*n31*n43 - n21*n32*n43;
  460. m2.n42 = n12*n33*n41 - n13*n32*n41 + n13*n31*n42 - n11*n33*n42 - n12*n31*n43 + n11*n32*n43;
  461. m2.n43 = n13*n22*n41 - n12*n23*n41 - n13*n21*n42 + n11*n23*n42 + n12*n21*n43 - n11*n22*n43;
  462. m2.n44 = n12*n23*n31 - n13*n22*n31 + n13*n21*n32 - n11*n23*n32 - n12*n21*n33 + n11*n22*n33;
  463. m2.multiplyScalar( 1 / m1.determinant() );
  464. return m2;
  465. };
  466. /*
  467. THREE.Matrix4.makeInvert = function ( m1 ) {
  468. var m2 = new THREE.Matrix4();
  469. return THREE.Matrix4.makeInvertTo( m1, m2 );
  470. };
  471. THREE.Matrix4.makeInvertTo = function ( m1, m2 ) {
  472. var n11 = m1.n11, n12 = m1.n12, n13 = m1.n13, n14 = m1.n14,
  473. n21 = m1.n21, n22 = m1.n22, n23 = m1.n23, n24 = m1.n24,
  474. n31 = m1.n31, n32 = m1.n32, n33 = m1.n33, n34 = m1.n34,
  475. n41 = m1.n41, n42 = m1.n42, n43 = m1.n43, n44 = m1.n44;
  476. //TODO: make this more efficient
  477. //( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm )
  478. m2.n11 = n23*n34*n42 - n24*n33*n42 + n24*n32*n43 - n22*n34*n43 - n23*n32*n44 + n22*n33*n44;
  479. m2.n12 = n14*n33*n42 - n13*n34*n42 - n14*n32*n43 + n12*n34*n43 + n13*n32*n44 - n12*n33*n44;
  480. m2.n13 = n13*n24*n42 - n14*n23*n42 + n14*n22*n43 - n12*n24*n43 - n13*n22*n44 + n12*n23*n44;
  481. m2.n14 = n14*n23*n32 - n13*n24*n32 - n14*n22*n33 + n12*n24*n33 + n13*n22*n34 - n12*n23*n34;
  482. m2.n21 = n24*n33*n41 - n23*n34*n41 - n24*n31*n43 + n21*n34*n43 + n23*n31*n44 - n21*n33*n44;
  483. m2.n22 = n13*n34*n41 - n14*n33*n41 + n14*n31*n43 - n11*n34*n43 - n13*n31*n44 + n11*n33*n44;
  484. m2.n23 = n14*n23*n41 - n13*n24*n41 - n14*n21*n43 + n11*n24*n43 + n13*n21*n44 - n11*n23*n44;
  485. m2.n24 = n13*n24*n31 - n14*n23*n31 + n14*n21*n33 - n11*n24*n33 - n13*n21*n34 + n11*n23*n34;
  486. m2.n31 = n22*n34*n41 - n24*n32*n41 + n24*n31*n42 - n21*n34*n42 - n22*n31*n44 + n21*n32*n44;
  487. m2.n32 = n14*n32*n41 - n12*n34*n41 - n14*n31*n42 + n11*n34*n42 + n12*n31*n44 - n11*n32*n44;
  488. m2.n33 = n13*n24*n41 - n14*n22*n41 + n14*n21*n42 - n11*n24*n42 - n12*n21*n44 + n11*n22*n44;
  489. m2.n34 = n14*n22*n31 - n12*n24*n31 - n14*n21*n32 + n11*n24*n32 + n12*n21*n34 - n11*n22*n34;
  490. m2.n41 = n23*n32*n41 - n22*n33*n41 - n23*n31*n42 + n21*n33*n42 + n22*n31*n43 - n21*n32*n43;
  491. m2.n42 = n12*n33*n41 - n13*n32*n41 + n13*n31*n42 - n11*n33*n42 - n12*n31*n43 + n11*n32*n43;
  492. m2.n43 = n13*n22*n41 - n12*n23*n41 - n13*n21*n42 + n11*n23*n42 + n12*n21*n43 - n11*n22*n43;
  493. m2.n44 = n12*n23*n31 - n13*n22*n31 + n13*n21*n32 - n11*n23*n32 - n12*n21*n33 + n11*n22*n33;
  494. m2.multiplyScalar( 1 / m1.determinant() );
  495. return m2;
  496. };
  497. */
  498. THREE.Matrix4.makeInvert3x3 = function ( m1 ) {
  499. // input: THREE.Matrix4, output: THREE.Matrix3
  500. // ( based on http://code.google.com/p/webgl-mjs/ )
  501. var m33 = m1.m33,
  502. m33m = m33.m,
  503. a11 = m1.n33 * m1.n22 - m1.n32 * m1.n23,
  504. a21 = - m1.n33 * m1.n21 + m1.n31 * m1.n23,
  505. a31 = m1.n32 * m1.n21 - m1.n31 * m1.n22,
  506. a12 = - m1.n33 * m1.n12 + m1.n32 * m1.n13,
  507. a22 = m1.n33 * m1.n11 - m1.n31 * m1.n13,
  508. a32 = - m1.n32 * m1.n11 + m1.n31 * m1.n12,
  509. a13 = m1.n23 * m1.n12 - m1.n22 * m1.n13,
  510. a23 = - m1.n23 * m1.n11 + m1.n21 * m1.n13,
  511. a33 = m1.n22 * m1.n11 - m1.n21 * m1.n12,
  512. det = m1.n11 * a11 + m1.n21 * a12 + m1.n31 * a13,
  513. idet;
  514. // no inverse
  515. if (det == 0) throw "matrix not invertible";
  516. idet = 1.0 / det;
  517. m33m[ 0 ] = idet * a11; m33m[ 1 ] = idet * a21; m33m[ 2 ] = idet * a31;
  518. m33m[ 3 ] = idet * a12; m33m[ 4 ] = idet * a22; m33m[ 5 ] = idet * a32;
  519. m33m[ 6 ] = idet * a13; m33m[ 7 ] = idet * a23; m33m[ 8 ] = idet * a33;
  520. return m33;
  521. }
  522. THREE.Matrix4.makeFrustum = function ( left, right, bottom, top, near, far ) {
  523. var m, x, y, a, b, c, d;
  524. m = new THREE.Matrix4();
  525. x = 2 * near / ( right - left );
  526. y = 2 * near / ( top - bottom );
  527. a = ( right + left ) / ( right - left );
  528. b = ( top + bottom ) / ( top - bottom );
  529. c = - ( far + near ) / ( far - near );
  530. d = - 2 * far * near / ( far - near );
  531. m.n11 = x; m.n12 = 0; m.n13 = a; m.n14 = 0;
  532. m.n21 = 0; m.n22 = y; m.n23 = b; m.n24 = 0;
  533. m.n31 = 0; m.n32 = 0; m.n33 = c; m.n34 = d;
  534. m.n41 = 0; m.n42 = 0; m.n43 = - 1; m.n44 = 0;
  535. return m;
  536. };
  537. THREE.Matrix4.makePerspective = function ( fov, aspect, near, far ) {
  538. var ymax, ymin, xmin, xmax;
  539. ymax = near * Math.tan( fov * Math.PI / 360 );
  540. ymin = - ymax;
  541. xmin = ymin * aspect;
  542. xmax = ymax * aspect;
  543. return THREE.Matrix4.makeFrustum( xmin, xmax, ymin, ymax, near, far );
  544. };
  545. THREE.Matrix4.makeOrtho = function ( left, right, top, bottom, near, far ) {
  546. var m, x, y, z, w, h, p;
  547. m = new THREE.Matrix4();
  548. w = right - left;
  549. h = top - bottom;
  550. p = far - near;
  551. x = ( right + left ) / w;
  552. y = ( top + bottom ) / h;
  553. z = ( far + near ) / p;
  554. m.n11 = 2 / w; m.n12 = 0; m.n13 = 0; m.n14 = -x;
  555. m.n21 = 0; m.n22 = 2 / h; m.n23 = 0; m.n24 = -y;
  556. m.n31 = 0; m.n32 = 0; m.n33 = -2 / p; m.n34 = -z;
  557. m.n41 = 0; m.n42 = 0; m.n43 = 0; m.n44 = 1;
  558. return m;
  559. };
  560. THREE.Matrix4.__tmpVec1 = new THREE.Vector3();
  561. THREE.Matrix4.__tmpVec2 = new THREE.Vector3();
  562. THREE.Matrix4.__tmpVec3 = new THREE.Vector3();