Matrix3.js 5.7 KB

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