MathUtils.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. // http://en.wikipedia.org/wiki/Smoothstep
  39. smoothstep: function ( x, min, max ) {
  40. if ( x <= min ) return 0;
  41. if ( x >= max ) return 1;
  42. x = ( x - min ) / ( max - min );
  43. return x * x * ( 3 - 2 * x );
  44. },
  45. smootherstep: function ( x, min, max ) {
  46. if ( x <= min ) return 0;
  47. if ( x >= max ) return 1;
  48. x = ( x - min ) / ( max - min );
  49. return x * x * x * ( x * ( x * 6 - 15 ) + 10 );
  50. },
  51. // Random integer from <low, high> interval
  52. randInt: function ( low, high ) {
  53. return low + Math.floor( Math.random() * ( high - low + 1 ) );
  54. },
  55. // Random float from <low, high> interval
  56. randFloat: function ( low, high ) {
  57. return low + Math.random() * ( high - low );
  58. },
  59. // Random float from <-range/2, range/2> interval
  60. randFloatSpread: function ( range ) {
  61. return range * ( 0.5 - Math.random() );
  62. },
  63. // Deterministic pseudo-random float in the interval [ 0, 1 ]
  64. seededRandom: function ( s ) {
  65. if ( s !== undefined ) _seed = s % 2147483647;
  66. // Park-Miller algorithm
  67. _seed = _seed * 16807 % 2147483647;
  68. return ( _seed - 1 ) / 2147483646;
  69. },
  70. degToRad: function ( degrees ) {
  71. return degrees * MathUtils.DEG2RAD;
  72. },
  73. radToDeg: function ( radians ) {
  74. return radians * MathUtils.RAD2DEG;
  75. },
  76. isPowerOfTwo: function ( value ) {
  77. return ( value & ( value - 1 ) ) === 0 && value !== 0;
  78. },
  79. ceilPowerOfTwo: function ( value ) {
  80. return Math.pow( 2, Math.ceil( Math.log( value ) / Math.LN2 ) );
  81. },
  82. floorPowerOfTwo: function ( value ) {
  83. return Math.pow( 2, Math.floor( Math.log( value ) / Math.LN2 ) );
  84. },
  85. setQuaternionFromProperEuler: function ( q, a, b, c, order ) {
  86. // Intrinsic Proper Euler Angles - see https://en.wikipedia.org/wiki/Euler_angles
  87. // rotations are applied to the axes in the order specified by 'order'
  88. // rotation by angle 'a' is applied first, then by angle 'b', then by angle 'c'
  89. // angles are in radians
  90. const cos = Math.cos;
  91. const sin = Math.sin;
  92. const c2 = cos( b / 2 );
  93. const s2 = sin( b / 2 );
  94. const c13 = cos( ( a + c ) / 2 );
  95. const s13 = sin( ( a + c ) / 2 );
  96. const c1_3 = cos( ( a - c ) / 2 );
  97. const s1_3 = sin( ( a - c ) / 2 );
  98. const c3_1 = cos( ( c - a ) / 2 );
  99. const s3_1 = sin( ( c - a ) / 2 );
  100. switch ( order ) {
  101. case 'XYX':
  102. q.set( c2 * s13, s2 * c1_3, s2 * s1_3, c2 * c13 );
  103. break;
  104. case 'YZY':
  105. q.set( s2 * s1_3, c2 * s13, s2 * c1_3, c2 * c13 );
  106. break;
  107. case 'ZXZ':
  108. q.set( s2 * c1_3, s2 * s1_3, c2 * s13, c2 * c13 );
  109. break;
  110. case 'XZX':
  111. q.set( c2 * s13, s2 * s3_1, s2 * c3_1, c2 * c13 );
  112. break;
  113. case 'YXY':
  114. q.set( s2 * c3_1, c2 * s13, s2 * s3_1, c2 * c13 );
  115. break;
  116. case 'ZYZ':
  117. q.set( s2 * s3_1, s2 * c3_1, c2 * s13, c2 * c13 );
  118. break;
  119. default:
  120. console.warn( 'THREE.MathUtils: .setQuaternionFromProperEuler() encountered an unknown order: ' + order );
  121. }
  122. }
  123. };
  124. export { MathUtils };