Matrix3.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. * @author WestLangley / http://github.com/WestLangley
  4. * @author bhouston / http://clara.io
  5. * @author tschw
  6. */
  7. function Matrix3() {
  8. this.elements = [
  9. 1, 0, 0,
  10. 0, 1, 0,
  11. 0, 0, 1
  12. ];
  13. if ( arguments.length > 0 ) {
  14. console.error( 'THREE.Matrix3: the constructor no longer reads arguments. use .set() instead.' );
  15. }
  16. }
  17. Object.assign( Matrix3.prototype, {
  18. isMatrix3: true,
  19. set: function ( n11, n12, n13, n21, n22, n23, n31, n32, n33 ) {
  20. const te = this.elements;
  21. te[ 0 ] = n11; te[ 1 ] = n21; te[ 2 ] = n31;
  22. te[ 3 ] = n12; te[ 4 ] = n22; te[ 5 ] = n32;
  23. te[ 6 ] = n13; te[ 7 ] = n23; te[ 8 ] = n33;
  24. return this;
  25. },
  26. identity: function () {
  27. this.set(
  28. 1, 0, 0,
  29. 0, 1, 0,
  30. 0, 0, 1
  31. );
  32. return this;
  33. },
  34. clone: function () {
  35. return new this.constructor().fromArray( this.elements );
  36. },
  37. copy: function ( m ) {
  38. const te = this.elements;
  39. const me = m.elements;
  40. te[ 0 ] = me[ 0 ]; te[ 1 ] = me[ 1 ]; te[ 2 ] = me[ 2 ];
  41. te[ 3 ] = me[ 3 ]; te[ 4 ] = me[ 4 ]; te[ 5 ] = me[ 5 ];
  42. te[ 6 ] = me[ 6 ]; te[ 7 ] = me[ 7 ]; te[ 8 ] = me[ 8 ];
  43. return this;
  44. },
  45. extractBasis: function ( xAxis, yAxis, zAxis ) {
  46. xAxis.setFromMatrix3Column( this, 0 );
  47. yAxis.setFromMatrix3Column( this, 1 );
  48. zAxis.setFromMatrix3Column( this, 2 );
  49. return this;
  50. },
  51. setFromMatrix4: function ( m ) {
  52. const me = m.elements;
  53. this.set(
  54. me[ 0 ], me[ 4 ], me[ 8 ],
  55. me[ 1 ], me[ 5 ], me[ 9 ],
  56. me[ 2 ], me[ 6 ], me[ 10 ]
  57. );
  58. return this;
  59. },
  60. multiply: function ( m ) {
  61. return this.multiplyMatrices( this, m );
  62. },
  63. premultiply: function ( m ) {
  64. return this.multiplyMatrices( m, this );
  65. },
  66. multiplyMatrices: function ( a, b ) {
  67. const ae = a.elements;
  68. const be = b.elements;
  69. const te = this.elements;
  70. const a11 = ae[ 0 ], a12 = ae[ 3 ], a13 = ae[ 6 ];
  71. const a21 = ae[ 1 ], a22 = ae[ 4 ], a23 = ae[ 7 ];
  72. const a31 = ae[ 2 ], a32 = ae[ 5 ], a33 = ae[ 8 ];
  73. const b11 = be[ 0 ], b12 = be[ 3 ], b13 = be[ 6 ];
  74. const b21 = be[ 1 ], b22 = be[ 4 ], b23 = be[ 7 ];
  75. const b31 = be[ 2 ], b32 = be[ 5 ], b33 = be[ 8 ];
  76. te[ 0 ] = a11 * b11 + a12 * b21 + a13 * b31;
  77. te[ 3 ] = a11 * b12 + a12 * b22 + a13 * b32;
  78. te[ 6 ] = a11 * b13 + a12 * b23 + a13 * b33;
  79. te[ 1 ] = a21 * b11 + a22 * b21 + a23 * b31;
  80. te[ 4 ] = a21 * b12 + a22 * b22 + a23 * b32;
  81. te[ 7 ] = a21 * b13 + a22 * b23 + a23 * b33;
  82. te[ 2 ] = a31 * b11 + a32 * b21 + a33 * b31;
  83. te[ 5 ] = a31 * b12 + a32 * b22 + a33 * b32;
  84. te[ 8 ] = a31 * b13 + a32 * b23 + a33 * b33;
  85. return this;
  86. },
  87. multiplyScalar: function ( s ) {
  88. const te = this.elements;
  89. te[ 0 ] *= s; te[ 3 ] *= s; te[ 6 ] *= s;
  90. te[ 1 ] *= s; te[ 4 ] *= s; te[ 7 ] *= s;
  91. te[ 2 ] *= s; te[ 5 ] *= s; te[ 8 ] *= s;
  92. return this;
  93. },
  94. determinant: function () {
  95. const te = this.elements;
  96. const a = te[ 0 ], b = te[ 1 ], c = te[ 2 ],
  97. d = te[ 3 ], e = te[ 4 ], f = te[ 5 ],
  98. g = te[ 6 ], h = te[ 7 ], i = te[ 8 ];
  99. return a * e * i - a * f * h - b * d * i + b * f * g + c * d * h - c * e * g;
  100. },
  101. getInverse: function ( matrix, throwOnDegenerate ) {
  102. if ( throwOnDegenerate !== undefined ) {
  103. console.warn( "THREE.Matrix3: .getInverse() can no longer be configured to throw on degenerate." );
  104. }
  105. const me = matrix.elements,
  106. te = this.elements,
  107. n11 = me[ 0 ], n21 = me[ 1 ], n31 = me[ 2 ],
  108. n12 = me[ 3 ], n22 = me[ 4 ], n32 = me[ 5 ],
  109. n13 = me[ 6 ], n23 = me[ 7 ], n33 = me[ 8 ],
  110. t11 = n33 * n22 - n32 * n23,
  111. t12 = n32 * n13 - n33 * n12,
  112. t13 = n23 * n12 - n22 * n13,
  113. det = n11 * t11 + n21 * t12 + n31 * t13;
  114. if ( det === 0 ) return this.set( 0, 0, 0, 0, 0, 0, 0, 0, 0 );
  115. const detInv = 1 / det;
  116. te[ 0 ] = t11 * detInv;
  117. te[ 1 ] = ( n31 * n23 - n33 * n21 ) * detInv;
  118. te[ 2 ] = ( n32 * n21 - n31 * n22 ) * detInv;
  119. te[ 3 ] = t12 * detInv;
  120. te[ 4 ] = ( n33 * n11 - n31 * n13 ) * detInv;
  121. te[ 5 ] = ( n31 * n12 - n32 * n11 ) * detInv;
  122. te[ 6 ] = t13 * detInv;
  123. te[ 7 ] = ( n21 * n13 - n23 * n11 ) * detInv;
  124. te[ 8 ] = ( n22 * n11 - n21 * n12 ) * detInv;
  125. return this;
  126. },
  127. transpose: function () {
  128. let tmp;
  129. const m = this.elements;
  130. tmp = m[ 1 ]; m[ 1 ] = m[ 3 ]; m[ 3 ] = tmp;
  131. tmp = m[ 2 ]; m[ 2 ] = m[ 6 ]; m[ 6 ] = tmp;
  132. tmp = m[ 5 ]; m[ 5 ] = m[ 7 ]; m[ 7 ] = tmp;
  133. return this;
  134. },
  135. getNormalMatrix: function ( matrix4 ) {
  136. return this.setFromMatrix4( matrix4 ).getInverse( this ).transpose();
  137. },
  138. transposeIntoArray: function ( r ) {
  139. const m = this.elements;
  140. r[ 0 ] = m[ 0 ];
  141. r[ 1 ] = m[ 3 ];
  142. r[ 2 ] = m[ 6 ];
  143. r[ 3 ] = m[ 1 ];
  144. r[ 4 ] = m[ 4 ];
  145. r[ 5 ] = m[ 7 ];
  146. r[ 6 ] = m[ 2 ];
  147. r[ 7 ] = m[ 5 ];
  148. r[ 8 ] = m[ 8 ];
  149. return this;
  150. },
  151. setUvTransform: function ( tx, ty, sx, sy, rotation, cx, cy ) {
  152. const c = Math.cos( rotation );
  153. const s = Math.sin( rotation );
  154. this.set(
  155. sx * c, sx * s, - sx * ( c * cx + s * cy ) + cx + tx,
  156. - sy * s, sy * c, - sy * ( - s * cx + c * cy ) + cy + ty,
  157. 0, 0, 1
  158. );
  159. },
  160. scale: function ( sx, sy ) {
  161. const te = this.elements;
  162. te[ 0 ] *= sx; te[ 3 ] *= sx; te[ 6 ] *= sx;
  163. te[ 1 ] *= sy; te[ 4 ] *= sy; te[ 7 ] *= sy;
  164. return this;
  165. },
  166. rotate: function ( theta ) {
  167. const c = Math.cos( theta );
  168. const s = Math.sin( theta );
  169. const te = this.elements;
  170. const a11 = te[ 0 ], a12 = te[ 3 ], a13 = te[ 6 ];
  171. const a21 = te[ 1 ], a22 = te[ 4 ], a23 = te[ 7 ];
  172. te[ 0 ] = c * a11 + s * a21;
  173. te[ 3 ] = c * a12 + s * a22;
  174. te[ 6 ] = c * a13 + s * a23;
  175. te[ 1 ] = - s * a11 + c * a21;
  176. te[ 4 ] = - s * a12 + c * a22;
  177. te[ 7 ] = - s * a13 + c * a23;
  178. return this;
  179. },
  180. translate: function ( tx, ty ) {
  181. const te = this.elements;
  182. te[ 0 ] += tx * te[ 2 ]; te[ 3 ] += tx * te[ 5 ]; te[ 6 ] += tx * te[ 8 ];
  183. te[ 1 ] += ty * te[ 2 ]; te[ 4 ] += ty * te[ 5 ]; te[ 7 ] += ty * te[ 8 ];
  184. return this;
  185. },
  186. equals: function ( matrix ) {
  187. const te = this.elements;
  188. const me = matrix.elements;
  189. for ( let i = 0; i < 9; i ++ ) {
  190. if ( te[ i ] !== me[ i ] ) return false;
  191. }
  192. return true;
  193. },
  194. fromArray: function ( array, offset ) {
  195. if ( offset === undefined ) offset = 0;
  196. for ( let i = 0; i < 9; i ++ ) {
  197. this.elements[ i ] = array[ i + offset ];
  198. }
  199. return this;
  200. },
  201. toArray: function ( array, offset ) {
  202. if ( array === undefined ) array = [];
  203. if ( offset === undefined ) offset = 0;
  204. const te = this.elements;
  205. array[ offset ] = te[ 0 ];
  206. array[ offset + 1 ] = te[ 1 ];
  207. array[ offset + 2 ] = te[ 2 ];
  208. array[ offset + 3 ] = te[ 3 ];
  209. array[ offset + 4 ] = te[ 4 ];
  210. array[ offset + 5 ] = te[ 5 ];
  211. array[ offset + 6 ] = te[ 6 ];
  212. array[ offset + 7 ] = te[ 7 ];
  213. array[ offset + 8 ] = te[ 8 ];
  214. return array;
  215. }
  216. } );
  217. export { Matrix3 };
粤ICP备19079148号