MathUtils.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. const _lut = [];
  2. for ( let i = 0; i < 256; i ++ ) {
  3. _lut[ i ] = ( i < 16 ? '0' : '' ) + ( i ).toString( 16 );
  4. }
  5. let _seed = 1234567;
  6. const MathUtils = {
  7. DEG2RAD: Math.PI / 180,
  8. RAD2DEG: 180 / Math.PI,
  9. generateUUID: function () {
  10. // http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/21963136#21963136
  11. const d0 = Math.random() * 0xffffffff | 0;
  12. const d1 = Math.random() * 0xffffffff | 0;
  13. const d2 = Math.random() * 0xffffffff | 0;
  14. const d3 = Math.random() * 0xffffffff | 0;
  15. const uuid = _lut[ d0 & 0xff ] + _lut[ d0 >> 8 & 0xff ] + _lut[ d0 >> 16 & 0xff ] + _lut[ d0 >> 24 & 0xff ] + '-' +
  16. _lut[ d1 & 0xff ] + _lut[ d1 >> 8 & 0xff ] + '-' + _lut[ d1 >> 16 & 0x0f | 0x40 ] + _lut[ d1 >> 24 & 0xff ] + '-' +
  17. _lut[ d2 & 0x3f | 0x80 ] + _lut[ d2 >> 8 & 0xff ] + '-' + _lut[ d2 >> 16 & 0xff ] + _lut[ d2 >> 24 & 0xff ] +
  18. _lut[ d3 & 0xff ] + _lut[ d3 >> 8 & 0xff ] + _lut[ d3 >> 16 & 0xff ] + _lut[ d3 >> 24 & 0xff ];
  19. // .toUpperCase() here flattens concatenated strings to save heap memory space.
  20. return uuid.toUpperCase();
  21. },
  22. clamp: function ( value, min, max ) {
  23. return Math.max( min, Math.min( max, value ) );
  24. },
  25. // compute euclidian modulo of m % n
  26. // https://en.wikipedia.org/wiki/Modulo_operation
  27. euclideanModulo: function ( n, m ) {
  28. return ( ( n % m ) + m ) % m;
  29. },
  30. // Linear mapping from range <a1, a2> to range <b1, b2>
  31. mapLinear: function ( x, a1, a2, b1, b2 ) {
  32. return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
  33. },
  34. // https://en.wikipedia.org/wiki/Linear_interpolation
  35. lerp: function ( x, y, t ) {
  36. return ( 1 - t ) * x + t * y;
  37. },
  38. // https://www.desmos.com/calculator/vcsjnyz7x4
  39. pingpong: function ( x, length = 1 ) {
  40. return length - Math.abs( MathUtils.euclideanModulo( x, length * 2 ) - length );
  41. },
  42. // http://en.wikipedia.org/wiki/Smoothstep
  43. smoothstep: function ( x, min, max ) {
  44. if ( x <= min ) return 0;
  45. if ( x >= max ) return 1;
  46. x = ( x - min ) / ( max - min );
  47. return x * x * ( 3 - 2 * x );
  48. },
  49. smootherstep: function ( x, min, max ) {
  50. if ( x <= min ) return 0;
  51. if ( x >= max ) return 1;
  52. x = ( x - min ) / ( max - min );
  53. return x * x * x * ( x * ( x * 6 - 15 ) + 10 );
  54. },
  55. // Random integer from <low, high> interval
  56. randInt: function ( low, high ) {
  57. return low + Math.floor( Math.random() * ( high - low + 1 ) );
  58. },
  59. // Random float from <low, high> interval
  60. randFloat: function ( low, high ) {
  61. return low + Math.random() * ( high - low );
  62. },
  63. // Random float from <-range/2, range/2> interval
  64. randFloatSpread: function ( range ) {
  65. return range * ( 0.5 - Math.random() );
  66. },
  67. // Deterministic pseudo-random float in the interval [ 0, 1 ]
  68. seededRandom: function ( s ) {
  69. if ( s !== undefined ) _seed = s % 2147483647;
  70. // Park-Miller algorithm
  71. _seed = _seed * 16807 % 2147483647;
  72. return ( _seed - 1 ) / 2147483646;
  73. },
  74. degToRad: function ( degrees ) {
  75. return degrees * MathUtils.DEG2RAD;
  76. },
  77. radToDeg: function ( radians ) {
  78. return radians * MathUtils.RAD2DEG;
  79. },
  80. isPowerOfTwo: function ( value ) {
  81. return ( value & ( value - 1 ) ) === 0 && value !== 0;
  82. },
  83. ceilPowerOfTwo: function ( value ) {
  84. return Math.pow( 2, Math.ceil( Math.log( value ) / Math.LN2 ) );
  85. },
  86. floorPowerOfTwo: function ( value ) {
  87. return Math.pow( 2, Math.floor( Math.log( value ) / Math.LN2 ) );
  88. },
  89. setQuaternionFromProperEuler: function ( q, a, b, c, order ) {
  90. // Intrinsic Proper Euler Angles - see https://en.wikipedia.org/wiki/Euler_angles
  91. // rotations are applied to the axes in the order specified by 'order'
  92. // rotation by angle 'a' is applied first, then by angle 'b', then by angle 'c'
  93. // angles are in radians
  94. const cos = Math.cos;
  95. const sin = Math.sin;
  96. const c2 = cos( b / 2 );
  97. const s2 = sin( b / 2 );
  98. const c13 = cos( ( a + c ) / 2 );
  99. const s13 = sin( ( a + c ) / 2 );
  100. const c1_3 = cos( ( a - c ) / 2 );
  101. const s1_3 = sin( ( a - c ) / 2 );
  102. const c3_1 = cos( ( c - a ) / 2 );
  103. const s3_1 = sin( ( c - a ) / 2 );
  104. switch ( order ) {
  105. case 'XYX':
  106. q.set( c2 * s13, s2 * c1_3, s2 * s1_3, c2 * c13 );
  107. break;
  108. case 'YZY':
  109. q.set( s2 * s1_3, c2 * s13, s2 * c1_3, c2 * c13 );
  110. break;
  111. case 'ZXZ':
  112. q.set( s2 * c1_3, s2 * s1_3, c2 * s13, c2 * c13 );
  113. break;
  114. case 'XZX':
  115. q.set( c2 * s13, s2 * s3_1, s2 * c3_1, c2 * c13 );
  116. break;
  117. case 'YXY':
  118. q.set( s2 * c3_1, c2 * s13, s2 * s3_1, c2 * c13 );
  119. break;
  120. case 'ZYZ':
  121. q.set( s2 * s3_1, s2 * c3_1, c2 * s13, c2 * c13 );
  122. break;
  123. default:
  124. console.warn( 'THREE.MathUtils: .setQuaternionFromProperEuler() encountered an unknown order: ' + order );
  125. }
  126. }
  127. };
  128. export { MathUtils };