Matrix4.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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. };
  19. THREE.Matrix4.prototype = {
  20. constructor: THREE.Matrix4,
  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, target, up ) {
  47. var x = THREE.Matrix4.__v1;
  48. var y = THREE.Matrix4.__v2;
  49. var z = THREE.Matrix4.__v3;
  50. z.sub( eye, target ).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 );
  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. var a21 = a.n21, a22 = a.n22, a23 = a.n23, a24 = a.n24;
  68. var a31 = a.n31, a32 = a.n32, a33 = a.n33, a34 = a.n34;
  69. var a41 = a.n41, a42 = a.n42, a43 = a.n43, a44 = a.n44;
  70. var b11 = b.n11, b12 = b.n12, b13 = b.n13, b14 = b.n14;
  71. var b21 = b.n21, b22 = b.n22, b23 = b.n23, b24 = b.n24;
  72. var b31 = b.n31, b32 = b.n32, b33 = b.n33, b34 = b.n34;
  73. var 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. var 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. var n21 = this.n21, n22 = this.n22, n23 = this.n23, n24 = this.n24;
  145. var n31 = this.n31, n32 = this.n32, n33 = this.n33, n34 = this.n34;
  146. var 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. flattenToArray: function ( flat ) {
  187. flat[ 0 ] = this.n11; flat[ 1 ] = this.n21; flat[ 2 ] = this.n31; flat[ 3 ] = this.n41;
  188. flat[ 4 ] = this.n12; flat[ 5 ] = this.n22; flat[ 6 ] = this.n32; flat[ 7 ] = this.n42;
  189. flat[ 8 ] = this.n13; flat[ 9 ] = this.n23; flat[ 10 ] = this.n33; flat[ 11 ] = this.n43;
  190. flat[ 12 ] = this.n14; flat[ 13 ] = this.n24; flat[ 14 ] = this.n34; flat[ 15 ] = this.n44;
  191. return flat;
  192. },
  193. flattenToArrayOffset: function( flat, offset ) {
  194. flat[ offset ] = this.n11;
  195. flat[ offset + 1 ] = this.n21;
  196. flat[ offset + 2 ] = this.n31;
  197. flat[ offset + 3 ] = this.n41;
  198. flat[ offset + 4 ] = this.n12;
  199. flat[ offset + 5 ] = this.n22;
  200. flat[ offset + 6 ] = this.n32;
  201. flat[ offset + 7 ] = this.n42;
  202. flat[ offset + 8 ] = this.n13;
  203. flat[ offset + 9 ] = this.n23;
  204. flat[ offset + 10 ] = this.n33;
  205. flat[ offset + 11 ] = this.n43;
  206. flat[ offset + 12 ] = this.n14;
  207. flat[ offset + 13 ] = this.n24;
  208. flat[ offset + 14 ] = this.n34;
  209. flat[ offset + 15 ] = this.n44;
  210. return flat;
  211. },
  212. getPosition: function () {
  213. return THREE.Matrix4.__v1.set( this.n14, this.n24, this.n34 );
  214. },
  215. setPosition: function ( v ) {
  216. this.n14 = v.x;
  217. this.n24 = v.y;
  218. this.n34 = v.z;
  219. return this;
  220. },
  221. getColumnX: function () {
  222. return THREE.Matrix4.__v1.set( this.n11, this.n21, this.n31 );
  223. },
  224. getColumnY: function () {
  225. return THREE.Matrix4.__v1.set( this.n12, this.n22, this.n32 );
  226. },
  227. getColumnZ: function() {
  228. return THREE.Matrix4.__v1.set( this.n13, this.n23, this.n33 );
  229. },
  230. getInverse: function ( m ) {
  231. // based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
  232. var n11 = m.n11, n12 = m.n12, n13 = m.n13, n14 = m.n14;
  233. var n21 = m.n21, n22 = m.n22, n23 = m.n23, n24 = m.n24;
  234. var n31 = m.n31, n32 = m.n32, n33 = m.n33, n34 = m.n34;
  235. var n41 = m.n41, n42 = m.n42, n43 = m.n43, n44 = m.n44;
  236. this.n11 = n23*n34*n42 - n24*n33*n42 + n24*n32*n43 - n22*n34*n43 - n23*n32*n44 + n22*n33*n44;
  237. this.n12 = n14*n33*n42 - n13*n34*n42 - n14*n32*n43 + n12*n34*n43 + n13*n32*n44 - n12*n33*n44;
  238. this.n13 = n13*n24*n42 - n14*n23*n42 + n14*n22*n43 - n12*n24*n43 - n13*n22*n44 + n12*n23*n44;
  239. this.n14 = n14*n23*n32 - n13*n24*n32 - n14*n22*n33 + n12*n24*n33 + n13*n22*n34 - n12*n23*n34;
  240. this.n21 = n24*n33*n41 - n23*n34*n41 - n24*n31*n43 + n21*n34*n43 + n23*n31*n44 - n21*n33*n44;
  241. this.n22 = n13*n34*n41 - n14*n33*n41 + n14*n31*n43 - n11*n34*n43 - n13*n31*n44 + n11*n33*n44;
  242. this.n23 = n14*n23*n41 - n13*n24*n41 - n14*n21*n43 + n11*n24*n43 + n13*n21*n44 - n11*n23*n44;
  243. this.n24 = n13*n24*n31 - n14*n23*n31 + n14*n21*n33 - n11*n24*n33 - n13*n21*n34 + n11*n23*n34;
  244. this.n31 = n22*n34*n41 - n24*n32*n41 + n24*n31*n42 - n21*n34*n42 - n22*n31*n44 + n21*n32*n44;
  245. this.n32 = n14*n32*n41 - n12*n34*n41 - n14*n31*n42 + n11*n34*n42 + n12*n31*n44 - n11*n32*n44;
  246. this.n33 = n12*n24*n41 - n14*n22*n41 + n14*n21*n42 - n11*n24*n42 - n12*n21*n44 + n11*n22*n44;
  247. this.n34 = n14*n22*n31 - n12*n24*n31 - n14*n21*n32 + n11*n24*n32 + n12*n21*n34 - n11*n22*n34;
  248. this.n41 = n23*n32*n41 - n22*n33*n41 - n23*n31*n42 + n21*n33*n42 + n22*n31*n43 - n21*n32*n43;
  249. this.n42 = n12*n33*n41 - n13*n32*n41 + n13*n31*n42 - n11*n33*n42 - n12*n31*n43 + n11*n32*n43;
  250. this.n43 = n13*n22*n41 - n12*n23*n41 - n13*n21*n42 + n11*n23*n42 + n12*n21*n43 - n11*n22*n43;
  251. this.n44 = n12*n23*n31 - n13*n22*n31 + n13*n21*n32 - n11*n23*n32 - n12*n21*n33 + n11*n22*n33;
  252. this.multiplyScalar( 1 / m.determinant() );
  253. return this;
  254. },
  255. setRotationFromEuler: function( v, order ) {
  256. var x = v.x, y = v.y, z = v.z;
  257. var a = Math.cos( x ), b = Math.sin( x );
  258. var c = Math.cos( y ), d = Math.sin( y );
  259. var e = Math.cos( z ), f = Math.sin( z );
  260. switch ( order ) {
  261. case 'YXZ':
  262. var ce = c * e, cf = c * f, de = d * e, df = d * f;
  263. this.n11 = ce + df * b;
  264. this.n12 = de * b - cf;
  265. this.n13 = a * d;
  266. this.n21 = a * f;
  267. this.n22 = a * e;
  268. this.n23 = - b;
  269. this.n31 = cf * b - de;
  270. this.n32 = df + ce * b;
  271. this.n33 = a * c;
  272. break;
  273. case 'ZXY':
  274. var ce = c * e, cf = c * f, de = d * e, df = d * f;
  275. this.n11 = ce - df * b;
  276. this.n12 = - a * f;
  277. this.n13 = de + cf * b;
  278. this.n21 = cf + de * b;
  279. this.n22 = a * e;
  280. this.n23 = df - ce * b;
  281. this.n31 = - a * d;
  282. this.n32 = b;
  283. this.n33 = a * c;
  284. break;
  285. case 'ZYX':
  286. var ae = a * e, af = a * f, be = b * e, bf = b * f;
  287. this.n11 = c * e;
  288. this.n12 = be * d - af;
  289. this.n13 = ae * d + bf;
  290. this.n21 = c * f;
  291. this.n22 = bf * d + ae;
  292. this.n23 = af * d - be;
  293. this.n31 = - d;
  294. this.n32 = b * c;
  295. this.n33 = a * c;
  296. break;
  297. case 'YZX':
  298. var ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  299. this.n11 = c * e;
  300. this.n12 = bd - ac * f;
  301. this.n13 = bc * f + ad;
  302. this.n21 = f;
  303. this.n22 = a * e;
  304. this.n23 = - b * e;
  305. this.n31 = - d * e;
  306. this.n32 = ad * f + bc;
  307. this.n33 = ac - bd * f;
  308. break;
  309. case 'XZY':
  310. var ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  311. this.n11 = c * e;
  312. this.n12 = - f;
  313. this.n13 = d * e;
  314. this.n21 = ac * f + bd;
  315. this.n22 = a * e;
  316. this.n23 = ad * f - bc;
  317. this.n31 = bc * f - ad;
  318. this.n32 = b * e;
  319. this.n33 = bd * f + ac;
  320. break;
  321. default: // 'XYZ'
  322. var ae = a * e, af = a * f, be = b * e, bf = b * f;
  323. this.n11 = c * e;
  324. this.n12 = - c * f;
  325. this.n13 = d;
  326. this.n21 = af + be * d;
  327. this.n22 = ae - bf * d;
  328. this.n23 = - b * c;
  329. this.n31 = bf - ae * d;
  330. this.n32 = be + af * d;
  331. this.n33 = a * c;
  332. break;
  333. }
  334. return this;
  335. },
  336. setRotationFromQuaternion: function( q ) {
  337. var x = q.x, y = q.y, z = q.z, w = q.w;
  338. var x2 = x + x, y2 = y + y, z2 = z + z;
  339. var xx = x * x2, xy = x * y2, xz = x * z2;
  340. var yy = y * y2, yz = y * z2, zz = z * z2;
  341. var wx = w * x2, wy = w * y2, wz = w * z2;
  342. this.n11 = 1 - ( yy + zz );
  343. this.n12 = xy - wz;
  344. this.n13 = xz + wy;
  345. this.n21 = xy + wz;
  346. this.n22 = 1 - ( xx + zz );
  347. this.n23 = yz - wx;
  348. this.n31 = xz - wy;
  349. this.n32 = yz + wx;
  350. this.n33 = 1 - ( xx + yy );
  351. return this;
  352. },
  353. compose: function ( translation, rotation, scale ) {
  354. var mRotation = THREE.Matrix4.__m1;
  355. var mScale = THREE.Matrix4.__m2;
  356. mRotation.identity();
  357. mRotation.setRotationFromQuaternion( rotation );
  358. mScale.makeScale( scale.x, scale.y, scale.z );
  359. this.multiply( mRotation, mScale );
  360. this.n14 = translation.x;
  361. this.n24 = translation.y;
  362. this.n34 = translation.z;
  363. return this;
  364. },
  365. decompose: function ( translation, rotation, scale ) {
  366. // grab the axis vectors
  367. var x = THREE.Matrix4.__v1;
  368. var y = THREE.Matrix4.__v2;
  369. var z = THREE.Matrix4.__v3;
  370. x.set( this.n11, this.n21, this.n31 );
  371. y.set( this.n12, this.n22, this.n32 );
  372. z.set( this.n13, this.n23, this.n33 );
  373. translation = ( translation instanceof THREE.Vector3 ) ? translation : new THREE.Vector3();
  374. rotation = ( rotation instanceof THREE.Quaternion ) ? rotation : new THREE.Quaternion();
  375. scale = ( scale instanceof THREE.Vector3 ) ? scale : new THREE.Vector3();
  376. scale.x = x.length();
  377. scale.y = y.length();
  378. scale.z = z.length();
  379. translation.x = this.n14;
  380. translation.y = this.n24;
  381. translation.z = this.n34;
  382. // scale the rotation part
  383. var matrix = THREE.Matrix4.__m1;
  384. matrix.copy( this );
  385. matrix.n11 /= scale.x;
  386. matrix.n21 /= scale.x;
  387. matrix.n31 /= scale.x;
  388. matrix.n12 /= scale.y;
  389. matrix.n22 /= scale.y;
  390. matrix.n32 /= scale.y;
  391. matrix.n13 /= scale.z;
  392. matrix.n23 /= scale.z;
  393. matrix.n33 /= scale.z;
  394. rotation.setFromRotationMatrix( matrix );
  395. return [ translation, rotation, scale ];
  396. },
  397. extractPosition: function ( m ) {
  398. this.n14 = m.n14;
  399. this.n24 = m.n24;
  400. this.n34 = m.n34;
  401. return this;
  402. },
  403. extractRotation: function ( m ) {
  404. var vector = THREE.Matrix4.__v1;
  405. var scaleX = 1 / vector.set( m.n11, m.n21, m.n31 ).length();
  406. var scaleY = 1 / vector.set( m.n12, m.n22, m.n32 ).length();
  407. var scaleZ = 1 / vector.set( m.n13, m.n23, m.n33 ).length();
  408. this.n11 = m.n11 * scaleX;
  409. this.n21 = m.n21 * scaleX;
  410. this.n31 = m.n31 * scaleX;
  411. this.n12 = m.n12 * scaleY;
  412. this.n22 = m.n22 * scaleY;
  413. this.n32 = m.n32 * scaleY;
  414. this.n13 = m.n13 * scaleZ;
  415. this.n23 = m.n23 * scaleZ;
  416. this.n33 = m.n33 * scaleZ;
  417. return this;
  418. },
  419. //
  420. translate: function ( v ) {
  421. var x = v.x, y = v.y, z = v.z;
  422. this.n14 = this.n11 * x + this.n12 * y + this.n13 * z + this.n14;
  423. this.n24 = this.n21 * x + this.n22 * y + this.n23 * z + this.n24;
  424. this.n34 = this.n31 * x + this.n32 * y + this.n33 * z + this.n34;
  425. this.n44 = this.n41 * x + this.n42 * y + this.n43 * z + this.n44;
  426. return this;
  427. },
  428. rotateX: function ( angle ) {
  429. var m12 = this.n12;
  430. var m22 = this.n22;
  431. var m32 = this.n32;
  432. var m42 = this.n42;
  433. var m13 = this.n13;
  434. var m23 = this.n23;
  435. var m33 = this.n33;
  436. var m43 = this.n43;
  437. var c = Math.cos( angle );
  438. var s = Math.sin( angle );
  439. this.n12 = c * m12 + s * m13;
  440. this.n22 = c * m22 + s * m23;
  441. this.n32 = c * m32 + s * m33;
  442. this.n42 = c * m42 + s * m43;
  443. this.n13 = c * m13 - s * m12;
  444. this.n23 = c * m23 - s * m22;
  445. this.n33 = c * m33 - s * m32;
  446. this.n43 = c * m43 - s * m42;
  447. return this;
  448. },
  449. rotateY: function ( angle ) {
  450. var m11 = this.n11;
  451. var m21 = this.n21;
  452. var m31 = this.n31;
  453. var m41 = this.n41;
  454. var m13 = this.n13;
  455. var m23 = this.n23;
  456. var m33 = this.n33;
  457. var m43 = this.n43;
  458. var c = Math.cos( angle );
  459. var s = Math.sin( angle );
  460. this.n11 = c * m11 - s * m13;
  461. this.n21 = c * m21 - s * m23;
  462. this.n31 = c * m31 - s * m33;
  463. this.n41 = c * m41 - s * m43;
  464. this.n13 = c * m13 + s * m11;
  465. this.n23 = c * m23 + s * m21;
  466. this.n33 = c * m33 + s * m31;
  467. this.n43 = c * m43 + s * m41;
  468. return this;
  469. },
  470. rotateZ: function ( angle ) {
  471. var m11 = this.n11;
  472. var m21 = this.n21;
  473. var m31 = this.n31;
  474. var m41 = this.n41;
  475. var m12 = this.n12;
  476. var m22 = this.n22;
  477. var m32 = this.n32;
  478. var m42 = this.n42;
  479. var c = Math.cos( angle );
  480. var s = Math.sin( angle );
  481. this.n11 = c * m11 + s * m12;
  482. this.n21 = c * m21 + s * m22;
  483. this.n31 = c * m31 + s * m32;
  484. this.n41 = c * m41 + s * m42;
  485. this.n12 = c * m12 - s * m11;
  486. this.n22 = c * m22 - s * m21;
  487. this.n32 = c * m32 - s * m31;
  488. this.n42 = c * m42 - s * m41;
  489. return this;
  490. },
  491. rotateByAxis: function ( axis, angle ) {
  492. // optimize by checking axis
  493. if ( axis.x === 1 && axis.y === 0 && axis.z === 0 ) {
  494. return this.rotateX( angle );
  495. } else if ( axis.x === 0 && axis.y === 1 && axis.z === 0 ) {
  496. return this.rotateY( angle );
  497. } else if ( axis.x === 0 && axis.y === 0 && axis.z === 1 ) {
  498. return this.rotateZ( angle );
  499. }
  500. var x = axis.x, y = axis.y, z = axis.z;
  501. var n = Math.sqrt(x * x + y * y + z * z);
  502. x /= n;
  503. y /= n;
  504. z /= n;
  505. var xx = x * x, yy = y * y, zz = z * z;
  506. var c = Math.cos( angle );
  507. var s = Math.sin( angle );
  508. var oneMinusCosine = 1 - c;
  509. var xy = x * y * oneMinusCosine;
  510. var xz = x * z * oneMinusCosine;
  511. var yz = y * z * oneMinusCosine;
  512. var xs = x * s;
  513. var ys = y * s;
  514. var zs = z * s;
  515. var r11 = xx + (1 - xx) * c;
  516. var r21 = xy + zs;
  517. var r31 = xz - ys;
  518. var r12 = xy - zs;
  519. var r22 = yy + (1 - yy) * c;
  520. var r32 = yz + xs;
  521. var r13 = xz + ys;
  522. var r23 = yz - xs;
  523. var r33 = zz + (1 - zz) * c;
  524. var m11 = this.n11, m21 = this.n21, m31 = this.n31, m41 = this.n41;
  525. var m12 = this.n12, m22 = this.n22, m32 = this.n32, m42 = this.n42;
  526. var m13 = this.n13, m23 = this.n23, m33 = this.n33, m43 = this.n43;
  527. var m14 = this.n14, m24 = this.n24, m34 = this.n34, m44 = this.n44;
  528. this.n11 = r11 * m11 + r21 * m12 + r31 * m13;
  529. this.n21 = r11 * m21 + r21 * m22 + r31 * m23;
  530. this.n31 = r11 * m31 + r21 * m32 + r31 * m33;
  531. this.n41 = r11 * m41 + r21 * m42 + r31 * m43;
  532. this.n12 = r12 * m11 + r22 * m12 + r32 * m13;
  533. this.n22 = r12 * m21 + r22 * m22 + r32 * m23;
  534. this.n32 = r12 * m31 + r22 * m32 + r32 * m33;
  535. this.n42 = r12 * m41 + r22 * m42 + r32 * m43;
  536. this.n13 = r13 * m11 + r23 * m12 + r33 * m13;
  537. this.n23 = r13 * m21 + r23 * m22 + r33 * m23;
  538. this.n33 = r13 * m31 + r23 * m32 + r33 * m33;
  539. this.n43 = r13 * m41 + r23 * m42 + r33 * m43;
  540. return this;
  541. },
  542. scale: function ( v ) {
  543. var x = v.x, y = v.y, z = v.z;
  544. this.n11 *= x; this.n12 *= y; this.n13 *= z;
  545. this.n21 *= x; this.n22 *= y; this.n23 *= z;
  546. this.n31 *= x; this.n32 *= y; this.n33 *= z;
  547. this.n41 *= x; this.n42 *= y; this.n43 *= z;
  548. return this;
  549. },
  550. getMaxScaleOnAxis : function () {
  551. var scaleXSq = this.n11 * this.n11 + this.n21 * this.n21 + this.n31 * this.n31;
  552. var scaleYSq = this.n12 * this.n12 + this.n22 * this.n22 + this.n32 * this.n32;
  553. var scaleZSq = this.n13 * this.n13 + this.n23 * this.n23 + this.n33 * this.n33;
  554. return Math.sqrt(Math.max( scaleXSq , Math.max( scaleYSq , scaleZSq )));
  555. },
  556. //
  557. makeTranslation: function ( x, y, z ) {
  558. this.set(
  559. 1, 0, 0, x,
  560. 0, 1, 0, y,
  561. 0, 0, 1, z,
  562. 0, 0, 0, 1
  563. );
  564. return this;
  565. },
  566. makeRotationX: function ( theta ) {
  567. var c = Math.cos( theta ), s = Math.sin( theta );
  568. this.set(
  569. 1, 0, 0, 0,
  570. 0, c, -s, 0,
  571. 0, s, c, 0,
  572. 0, 0, 0, 1
  573. );
  574. return this;
  575. },
  576. makeRotationY: function ( theta ) {
  577. var c = Math.cos( theta ), s = Math.sin( theta );
  578. this.set(
  579. c, 0, s, 0,
  580. 0, 1, 0, 0,
  581. -s, 0, c, 0,
  582. 0, 0, 0, 1
  583. );
  584. return this;
  585. },
  586. makeRotationZ: function ( theta ) {
  587. var c = Math.cos( theta ), s = Math.sin( theta );
  588. this.set(
  589. c, -s, 0, 0,
  590. s, c, 0, 0,
  591. 0, 0, 1, 0,
  592. 0, 0, 0, 1
  593. );
  594. return this;
  595. },
  596. makeRotationAxis: function ( axis, angle ) {
  597. // Based on http://www.gamedev.net/reference/articles/article1199.asp
  598. var c = Math.cos( angle );
  599. var s = Math.sin( angle );
  600. var t = 1 - c;
  601. var x = axis.x, y = axis.y, z = axis.z;
  602. var tx = t * x, ty = t * y;
  603. this.set(
  604. tx * x + c, tx * y - s * z, tx * z + s * y, 0,
  605. tx * y + s * z, ty * y + c, ty * z - s * x, 0,
  606. tx * z - s * y, ty * z + s * x, t * z * z + c, 0,
  607. 0, 0, 0, 1
  608. );
  609. return this;
  610. },
  611. makeScale: function ( x, y, z ) {
  612. this.set(
  613. x, 0, 0, 0,
  614. 0, y, 0, 0,
  615. 0, 0, z, 0,
  616. 0, 0, 0, 1
  617. );
  618. return this;
  619. },
  620. makeFrustum: function ( left, right, bottom, top, near, far ) {
  621. var x = 2 * near / ( right - left );
  622. var y = 2 * near / ( top - bottom );
  623. var a = ( right + left ) / ( right - left );
  624. var b = ( top + bottom ) / ( top - bottom );
  625. var c = - ( far + near ) / ( far - near );
  626. var d = - 2 * far * near / ( far - near );
  627. this.n11 = x; this.n12 = 0; this.n13 = a; this.n14 = 0;
  628. this.n21 = 0; this.n22 = y; this.n23 = b; this.n24 = 0;
  629. this.n31 = 0; this.n32 = 0; this.n33 = c; this.n34 = d;
  630. this.n41 = 0; this.n42 = 0; this.n43 = - 1; this.n44 = 0;
  631. return this;
  632. },
  633. makePerspective: function ( fov, aspect, near, far ) {
  634. var ymax = near * Math.tan( fov * Math.PI / 360 );
  635. var ymin = - ymax;
  636. var xmin = ymin * aspect;
  637. var xmax = ymax * aspect;
  638. return this.makeFrustum( xmin, xmax, ymin, ymax, near, far );
  639. },
  640. makeOrthographic: function ( left, right, top, bottom, near, far ) {
  641. var w = right - left;
  642. var h = top - bottom;
  643. var p = far - near;
  644. var x = ( right + left ) / w;
  645. var y = ( top + bottom ) / h;
  646. var z = ( far + near ) / p;
  647. this.n11 = 2 / w; this.n12 = 0; this.n13 = 0; this.n14 = -x;
  648. this.n21 = 0; this.n22 = 2 / h; this.n23 = 0; this.n24 = -y;
  649. this.n31 = 0; this.n32 = 0; this.n33 = -2 / p; this.n34 = -z;
  650. this.n41 = 0; this.n42 = 0; this.n43 = 0; this.n44 = 1;
  651. return this;
  652. },
  653. clone: function () {
  654. return new THREE.Matrix4(
  655. this.n11, this.n12, this.n13, this.n14,
  656. this.n21, this.n22, this.n23, this.n24,
  657. this.n31, this.n32, this.n33, this.n34,
  658. this.n41, this.n42, this.n43, this.n44
  659. );
  660. }
  661. };
  662. THREE.Matrix4.__v1 = new THREE.Vector3();
  663. THREE.Matrix4.__v2 = new THREE.Vector3();
  664. THREE.Matrix4.__v3 = new THREE.Vector3();
  665. THREE.Matrix4.__m1 = new THREE.Matrix4();
  666. THREE.Matrix4.__m2 = new THREE.Matrix4();
粤ICP备19079148号