Matrix3.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. class Matrix3 {
  2. constructor() {
  3. this.isMatrix3 = 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. invert() {
  96. const te = this.elements,
  97. n11 = te[ 0 ], n21 = te[ 1 ], n31 = te[ 2 ],
  98. n12 = te[ 3 ], n22 = te[ 4 ], n32 = te[ 5 ],
  99. n13 = te[ 6 ], n23 = te[ 7 ], n33 = te[ 8 ],
  100. t11 = n33 * n22 - n32 * n23,
  101. t12 = n32 * n13 - n33 * n12,
  102. t13 = n23 * n12 - n22 * n13,
  103. det = n11 * t11 + n21 * t12 + n31 * t13;
  104. if ( det === 0 ) return this.set( 0, 0, 0, 0, 0, 0, 0, 0, 0 );
  105. const detInv = 1 / det;
  106. te[ 0 ] = t11 * detInv;
  107. te[ 1 ] = ( n31 * n23 - n33 * n21 ) * detInv;
  108. te[ 2 ] = ( n32 * n21 - n31 * n22 ) * detInv;
  109. te[ 3 ] = t12 * detInv;
  110. te[ 4 ] = ( n33 * n11 - n31 * n13 ) * detInv;
  111. te[ 5 ] = ( n31 * n12 - n32 * n11 ) * detInv;
  112. te[ 6 ] = t13 * detInv;
  113. te[ 7 ] = ( n21 * n13 - n23 * n11 ) * detInv;
  114. te[ 8 ] = ( n22 * n11 - n21 * n12 ) * detInv;
  115. return this;
  116. }
  117. transpose() {
  118. let tmp;
  119. const m = this.elements;
  120. tmp = m[ 1 ]; m[ 1 ] = m[ 3 ]; m[ 3 ] = tmp;
  121. tmp = m[ 2 ]; m[ 2 ] = m[ 6 ]; m[ 6 ] = tmp;
  122. tmp = m[ 5 ]; m[ 5 ] = m[ 7 ]; m[ 7 ] = tmp;
  123. return this;
  124. }
  125. getNormalMatrix( matrix4 ) {
  126. return this.setFromMatrix4( matrix4 ).copy( this ).invert().transpose();
  127. }
  128. transposeIntoArray( r ) {
  129. const m = this.elements;
  130. r[ 0 ] = m[ 0 ];
  131. r[ 1 ] = m[ 3 ];
  132. r[ 2 ] = m[ 6 ];
  133. r[ 3 ] = m[ 1 ];
  134. r[ 4 ] = m[ 4 ];
  135. r[ 5 ] = m[ 7 ];
  136. r[ 6 ] = m[ 2 ];
  137. r[ 7 ] = m[ 5 ];
  138. r[ 8 ] = m[ 8 ];
  139. return this;
  140. }
  141. setUvTransform( tx, ty, sx, sy, rotation, cx, cy ) {
  142. const c = Math.cos( rotation );
  143. const s = Math.sin( rotation );
  144. this.set(
  145. sx * c, sx * s, - sx * ( c * cx + s * cy ) + cx + tx,
  146. - sy * s, sy * c, - sy * ( - s * cx + c * cy ) + cy + ty,
  147. 0, 0, 1
  148. );
  149. return this;
  150. }
  151. scale( sx, sy ) {
  152. const te = this.elements;
  153. te[ 0 ] *= sx; te[ 3 ] *= sx; te[ 6 ] *= sx;
  154. te[ 1 ] *= sy; te[ 4 ] *= sy; te[ 7 ] *= sy;
  155. return this;
  156. }
  157. rotate( theta ) {
  158. const c = Math.cos( theta );
  159. const s = Math.sin( theta );
  160. const te = this.elements;
  161. const a11 = te[ 0 ], a12 = te[ 3 ], a13 = te[ 6 ];
  162. const a21 = te[ 1 ], a22 = te[ 4 ], a23 = te[ 7 ];
  163. te[ 0 ] = c * a11 + s * a21;
  164. te[ 3 ] = c * a12 + s * a22;
  165. te[ 6 ] = c * a13 + s * a23;
  166. te[ 1 ] = - s * a11 + c * a21;
  167. te[ 4 ] = - s * a12 + c * a22;
  168. te[ 7 ] = - s * a13 + c * a23;
  169. return this;
  170. }
  171. translate( tx, ty ) {
  172. const te = this.elements;
  173. te[ 0 ] += tx * te[ 2 ]; te[ 3 ] += tx * te[ 5 ]; te[ 6 ] += tx * te[ 8 ];
  174. te[ 1 ] += ty * te[ 2 ]; te[ 4 ] += ty * te[ 5 ]; te[ 7 ] += ty * te[ 8 ];
  175. return this;
  176. }
  177. equals( matrix ) {
  178. const te = this.elements;
  179. const me = matrix.elements;
  180. for ( let i = 0; i < 9; i ++ ) {
  181. if ( te[ i ] !== me[ i ] ) return false;
  182. }
  183. return true;
  184. }
  185. fromArray( array, offset = 0 ) {
  186. for ( let i = 0; i < 9; i ++ ) {
  187. this.elements[ i ] = array[ i + offset ];
  188. }
  189. return this;
  190. }
  191. toArray( array = [], offset = 0 ) {
  192. const te = this.elements;
  193. array[ offset ] = te[ 0 ];
  194. array[ offset + 1 ] = te[ 1 ];
  195. array[ offset + 2 ] = te[ 2 ];
  196. array[ offset + 3 ] = te[ 3 ];
  197. array[ offset + 4 ] = te[ 4 ];
  198. array[ offset + 5 ] = te[ 5 ];
  199. array[ offset + 6 ] = te[ 6 ];
  200. array[ offset + 7 ] = te[ 7 ];
  201. array[ offset + 8 ] = te[ 8 ];
  202. return array;
  203. }
  204. }
  205. export { Matrix3 };