Matrix3.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. for ( var i = 0; i < 9; i ++ ) te[ i ] = me[ i ];
  42. return this;
  43. },
  44. setFromMatrix4: function ( m ) {
  45. var me = m.elements;
  46. this.set(
  47. me[ 0 ], me[ 4 ], me[ 8 ],
  48. me[ 1 ], me[ 5 ], me[ 9 ],
  49. me[ 2 ], me[ 6 ], me[ 10 ]
  50. );
  51. return this;
  52. },
  53. applyToBufferAttribute: function () {
  54. var v1 = new Vector3();
  55. return function applyToBufferAttribute( attribute ) {
  56. for ( var i = 0, l = attribute.count; i < l; i ++ ) {
  57. v1.x = attribute.getX( i );
  58. v1.y = attribute.getY( i );
  59. v1.z = attribute.getZ( i );
  60. v1.applyMatrix3( this );
  61. attribute.setXYZ( i, v1.x, v1.y, v1.z );
  62. }
  63. return attribute;
  64. };
  65. }(),
  66. multiplyScalar: function ( s ) {
  67. var te = this.elements;
  68. te[ 0 ] *= s; te[ 3 ] *= s; te[ 6 ] *= s;
  69. te[ 1 ] *= s; te[ 4 ] *= s; te[ 7 ] *= s;
  70. te[ 2 ] *= s; te[ 5 ] *= s; te[ 8 ] *= s;
  71. return this;
  72. },
  73. determinant: function () {
  74. var te = this.elements;
  75. var a = te[ 0 ], b = te[ 1 ], c = te[ 2 ],
  76. d = te[ 3 ], e = te[ 4 ], f = te[ 5 ],
  77. g = te[ 6 ], h = te[ 7 ], i = te[ 8 ];
  78. return a * e * i - a * f * h - b * d * i + b * f * g + c * d * h - c * e * g;
  79. },
  80. getInverse: function ( matrix, throwOnDegenerate ) {
  81. if ( matrix && matrix.isMatrix4 ) {
  82. console.error( "THREE.Matrix3.getInverse no longer takes a Matrix4 argument." );
  83. }
  84. var me = matrix.elements,
  85. te = this.elements,
  86. n11 = me[ 0 ], n21 = me[ 1 ], n31 = me[ 2 ],
  87. n12 = me[ 3 ], n22 = me[ 4 ], n32 = me[ 5 ],
  88. n13 = me[ 6 ], n23 = me[ 7 ], n33 = me[ 8 ],
  89. t11 = n33 * n22 - n32 * n23,
  90. t12 = n32 * n13 - n33 * n12,
  91. t13 = n23 * n12 - n22 * n13,
  92. det = n11 * t11 + n21 * t12 + n31 * t13;
  93. if ( det === 0 ) {
  94. var msg = "THREE.Matrix3.getInverse(): can't invert matrix, determinant is 0";
  95. if ( throwOnDegenerate === true ) {
  96. throw new Error( msg );
  97. } else {
  98. console.warn( msg );
  99. }
  100. return this.identity();
  101. }
  102. var detInv = 1 / det;
  103. te[ 0 ] = t11 * detInv;
  104. te[ 1 ] = ( n31 * n23 - n33 * n21 ) * detInv;
  105. te[ 2 ] = ( n32 * n21 - n31 * n22 ) * detInv;
  106. te[ 3 ] = t12 * detInv;
  107. te[ 4 ] = ( n33 * n11 - n31 * n13 ) * detInv;
  108. te[ 5 ] = ( n31 * n12 - n32 * n11 ) * detInv;
  109. te[ 6 ] = t13 * detInv;
  110. te[ 7 ] = ( n21 * n13 - n23 * n11 ) * detInv;
  111. te[ 8 ] = ( n22 * n11 - n21 * n12 ) * detInv;
  112. return this;
  113. },
  114. transpose: function () {
  115. var tmp, m = this.elements;
  116. tmp = m[ 1 ]; m[ 1 ] = m[ 3 ]; m[ 3 ] = tmp;
  117. tmp = m[ 2 ]; m[ 2 ] = m[ 6 ]; m[ 6 ] = tmp;
  118. tmp = m[ 5 ]; m[ 5 ] = m[ 7 ]; m[ 7 ] = tmp;
  119. return this;
  120. },
  121. getNormalMatrix: function ( matrix4 ) {
  122. return this.setFromMatrix4( matrix4 ).getInverse( this ).transpose();
  123. },
  124. transposeIntoArray: function ( r ) {
  125. var m = this.elements;
  126. r[ 0 ] = m[ 0 ];
  127. r[ 1 ] = m[ 3 ];
  128. r[ 2 ] = m[ 6 ];
  129. r[ 3 ] = m[ 1 ];
  130. r[ 4 ] = m[ 4 ];
  131. r[ 5 ] = m[ 7 ];
  132. r[ 6 ] = m[ 2 ];
  133. r[ 7 ] = m[ 5 ];
  134. r[ 8 ] = m[ 8 ];
  135. return this;
  136. },
  137. fromArray: function ( array, offset ) {
  138. if ( offset === undefined ) offset = 0;
  139. for ( var i = 0; i < 9; i ++ ) {
  140. this.elements[ i ] = array[ i + offset ];
  141. }
  142. return this;
  143. },
  144. toArray: function ( array, offset ) {
  145. if ( array === undefined ) array = [];
  146. if ( offset === undefined ) offset = 0;
  147. var te = this.elements;
  148. array[ offset ] = te[ 0 ];
  149. array[ offset + 1 ] = te[ 1 ];
  150. array[ offset + 2 ] = te[ 2 ];
  151. array[ offset + 3 ] = te[ 3 ];
  152. array[ offset + 4 ] = te[ 4 ];
  153. array[ offset + 5 ] = te[ 5 ];
  154. array[ offset + 6 ] = te[ 6 ];
  155. array[ offset + 7 ] = te[ 7 ];
  156. array[ offset + 8 ] = te[ 8 ];
  157. return array;
  158. }
  159. } );
  160. export { Matrix3 };