Matrix3.js 6.9 KB

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