Matrix3.js 6.1 KB

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