MathUtils.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. import { warn } from '../utils.js';
  2. const _lut = [ '00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '0a', '0b', '0c', '0d', '0e', '0f', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '1a', '1b', '1c', '1d', '1e', '1f', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '2a', '2b', '2c', '2d', '2e', '2f', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '3a', '3b', '3c', '3d', '3e', '3f', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '4a', '4b', '4c', '4d', '4e', '4f', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '5a', '5b', '5c', '5d', '5e', '5f', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '6a', '6b', '6c', '6d', '6e', '6f', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '7a', '7b', '7c', '7d', '7e', '7f', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '8a', '8b', '8c', '8d', '8e', '8f', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99', '9a', '9b', '9c', '9d', '9e', '9f', 'a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'aa', 'ab', 'ac', 'ad', 'ae', 'af', 'b0', 'b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8', 'b9', 'ba', 'bb', 'bc', 'bd', 'be', 'bf', 'c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9', 'ca', 'cb', 'cc', 'cd', 'ce', 'cf', 'd0', 'd1', 'd2', 'd3', 'd4', 'd5', 'd6', 'd7', 'd8', 'd9', 'da', 'db', 'dc', 'dd', 'de', 'df', 'e0', 'e1', 'e2', 'e3', 'e4', 'e5', 'e6', 'e7', 'e8', 'e9', 'ea', 'eb', 'ec', 'ed', 'ee', 'ef', 'f0', 'f1', 'f2', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9', 'fa', 'fb', 'fc', 'fd', 'fe', 'ff' ];
  3. let _seed = 1234567;
  4. const DEG2RAD = Math.PI / 180;
  5. const RAD2DEG = 180 / Math.PI;
  6. /**
  7. * Generate a [UUID](https://en.wikipedia.org/wiki/Universally_unique_identifier)
  8. * (universally unique identifier).
  9. *
  10. * @return {string} The UUID.
  11. */
  12. function generateUUID() {
  13. // http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/21963136#21963136
  14. const d0 = Math.random() * 0xffffffff | 0;
  15. const d1 = Math.random() * 0xffffffff | 0;
  16. const d2 = Math.random() * 0xffffffff | 0;
  17. const d3 = Math.random() * 0xffffffff | 0;
  18. const uuid = _lut[ d0 & 0xff ] + _lut[ d0 >> 8 & 0xff ] + _lut[ d0 >> 16 & 0xff ] + _lut[ d0 >> 24 & 0xff ] + '-' +
  19. _lut[ d1 & 0xff ] + _lut[ d1 >> 8 & 0xff ] + '-' + _lut[ d1 >> 16 & 0x0f | 0x40 ] + _lut[ d1 >> 24 & 0xff ] + '-' +
  20. _lut[ d2 & 0x3f | 0x80 ] + _lut[ d2 >> 8 & 0xff ] + '-' + _lut[ d2 >> 16 & 0xff ] + _lut[ d2 >> 24 & 0xff ] +
  21. _lut[ d3 & 0xff ] + _lut[ d3 >> 8 & 0xff ] + _lut[ d3 >> 16 & 0xff ] + _lut[ d3 >> 24 & 0xff ];
  22. // .toLowerCase() here flattens concatenated strings to save heap memory space.
  23. return uuid.toLowerCase();
  24. }
  25. /**
  26. * Clamps the given value between min and max.
  27. *
  28. * @param {number} value - The value to clamp.
  29. * @param {number} min - The min value.
  30. * @param {number} max - The max value.
  31. * @return {number} The clamped value.
  32. */
  33. function clamp( value, min, max ) {
  34. return Math.max( min, Math.min( max, value ) );
  35. }
  36. /**
  37. * Computes the Euclidean modulo of the given parameters that
  38. * is `( ( n % m ) + m ) % m`.
  39. *
  40. * @param {number} n - The first parameter.
  41. * @param {number} m - The second parameter.
  42. * @return {number} The Euclidean modulo.
  43. */
  44. function euclideanModulo( n, m ) {
  45. // https://en.wikipedia.org/wiki/Modulo_operation
  46. return ( ( n % m ) + m ) % m;
  47. }
  48. /**
  49. * Performs a linear mapping from range `<a1, a2>` to range `<b1, b2>`
  50. * for the given value. `a2` must be greater than `a1`.
  51. *
  52. * @param {number} x - The value to be mapped.
  53. * @param {number} a1 - Minimum value for range A.
  54. * @param {number} a2 - Maximum value for range A.
  55. * @param {number} b1 - Minimum value for range B.
  56. * @param {number} b2 - Maximum value for range B.
  57. * @return {number} The mapped value.
  58. */
  59. function mapLinear( x, a1, a2, b1, b2 ) {
  60. return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
  61. }
  62. /**
  63. * Returns the percentage in the closed interval `[0, 1]` of the given value
  64. * between the start and end point.
  65. *
  66. * @param {number} x - The start point
  67. * @param {number} y - The end point.
  68. * @param {number} value - A value between start and end.
  69. * @return {number} The interpolation factor.
  70. */
  71. function inverseLerp( x, y, value ) {
  72. // https://www.gamedev.net/tutorials/programming/general-and-gameplay-programming/inverse-lerp-a-super-useful-yet-often-overlooked-function-r5230/
  73. if ( x !== y ) {
  74. return ( value - x ) / ( y - x );
  75. } else {
  76. return 0;
  77. }
  78. }
  79. /**
  80. * Returns a value linearly interpolated from two known points based on the given interval -
  81. * `t = 0` will return `x` and `t = 1` will return `y`.
  82. *
  83. * @param {number} x - The start point
  84. * @param {number} y - The end point.
  85. * @param {number} t - The interpolation factor in the closed interval `[0, 1]`.
  86. * @return {number} The interpolated value.
  87. */
  88. function lerp( x, y, t ) {
  89. return ( 1 - t ) * x + t * y;
  90. }
  91. /**
  92. * Smoothly interpolate a number from `x` to `y` in a spring-like manner using a delta
  93. * time to maintain frame rate independent movement. For details, see
  94. * [Frame rate independent damping using lerp](http://www.rorydriscoll.com/2016/03/07/frame-rate-independent-damping-using-lerp/).
  95. *
  96. * @param {number} x - The current point.
  97. * @param {number} y - The target point.
  98. * @param {number} lambda - A higher lambda value will make the movement more sudden,
  99. * and a lower value will make the movement more gradual.
  100. * @param {number} dt - Delta time in seconds.
  101. * @return {number} The interpolated value.
  102. */
  103. function damp( x, y, lambda, dt ) {
  104. return lerp( x, y, 1 - Math.exp( - lambda * dt ) );
  105. }
  106. /**
  107. * Returns a value that alternates between `0` and the given `length` parameter.
  108. *
  109. * @param {number} x - The value to pingpong.
  110. * @param {number} [length=1] - The positive value the function will pingpong to.
  111. * @return {number} The alternated value.
  112. */
  113. function pingpong( x, length = 1 ) {
  114. // https://www.desmos.com/calculator/vcsjnyz7x4
  115. return length - Math.abs( euclideanModulo( x, length * 2 ) - length );
  116. }
  117. /**
  118. * Returns a value in the range `[0,1]` that represents the percentage that `x` has
  119. * moved between `min` and `max`, but smoothed or slowed down the closer `x` is to
  120. * the `min` and `max`.
  121. *
  122. * See [Smoothstep](http://en.wikipedia.org/wiki/Smoothstep) for more details.
  123. *
  124. * @param {number} x - The value to evaluate based on its position between `min` and `max`.
  125. * @param {number} min - The min value. Any `x` value below `min` will be `0`. `min` must be lower than `max`.
  126. * @param {number} max - The max value. Any `x` value above `max` will be `1`. `max` must be greater than `min`.
  127. * @return {number} The alternated value.
  128. */
  129. function smoothstep( x, min, max ) {
  130. if ( x <= min ) return 0;
  131. if ( x >= max ) return 1;
  132. x = ( x - min ) / ( max - min );
  133. return x * x * ( 3 - 2 * x );
  134. }
  135. /**
  136. * A [variation on smoothstep](https://en.wikipedia.org/wiki/Smoothstep#Variations)
  137. * that has zero 1st and 2nd order derivatives at `x=0` and `x=1`.
  138. *
  139. * @param {number} x - The value to evaluate based on its position between `min` and `max`.
  140. * @param {number} min - The min value. Any `x` value below `min` will be `0`. `min` must be lower than `max`.
  141. * @param {number} max - The max value. Any `x` value above `max` will be `1`. `max` must be greater than `min`.
  142. * @return {number} The alternated value.
  143. */
  144. function smootherstep( x, min, max ) {
  145. if ( x <= min ) return 0;
  146. if ( x >= max ) return 1;
  147. x = ( x - min ) / ( max - min );
  148. return x * x * x * ( x * ( x * 6 - 15 ) + 10 );
  149. }
  150. /**
  151. * Returns a random integer from `<low, high>` interval.
  152. *
  153. * @param {number} low - The lower value boundary.
  154. * @param {number} high - The upper value boundary
  155. * @return {number} A random integer.
  156. */
  157. function randInt( low, high ) {
  158. return low + Math.floor( Math.random() * ( high - low + 1 ) );
  159. }
  160. /**
  161. * Returns a random float from `<low, high>` interval.
  162. *
  163. * @param {number} low - The lower value boundary.
  164. * @param {number} high - The upper value boundary
  165. * @return {number} A random float.
  166. */
  167. function randFloat( low, high ) {
  168. return low + Math.random() * ( high - low );
  169. }
  170. /**
  171. * Returns a random integer from `<-range/2, range/2>` interval.
  172. *
  173. * @param {number} range - Defines the value range.
  174. * @return {number} A random float.
  175. */
  176. function randFloatSpread( range ) {
  177. return range * ( 0.5 - Math.random() );
  178. }
  179. /**
  180. * Returns a deterministic pseudo-random float in the interval `[0, 1]`.
  181. *
  182. * @param {number} [s] - The integer seed.
  183. * @return {number} A random float.
  184. */
  185. function seededRandom( s ) {
  186. if ( s !== undefined ) _seed = s;
  187. // Mulberry32 generator
  188. let t = _seed += 0x6D2B79F5;
  189. t = Math.imul( t ^ t >>> 15, t | 1 );
  190. t ^= t + Math.imul( t ^ t >>> 7, t | 61 );
  191. return ( ( t ^ t >>> 14 ) >>> 0 ) / 4294967296;
  192. }
  193. /**
  194. * Converts degrees to radians.
  195. *
  196. * @param {number} degrees - A value in degrees.
  197. * @return {number} The converted value in radians.
  198. */
  199. function degToRad( degrees ) {
  200. return degrees * DEG2RAD;
  201. }
  202. /**
  203. * Converts radians to degrees.
  204. *
  205. * @param {number} radians - A value in radians.
  206. * @return {number} The converted value in degrees.
  207. */
  208. function radToDeg( radians ) {
  209. return radians * RAD2DEG;
  210. }
  211. /**
  212. * Returns `true` if the given number is a power of two.
  213. *
  214. * @param {number} value - The value to check.
  215. * @return {boolean} Whether the given number is a power of two or not.
  216. */
  217. function isPowerOfTwo( value ) {
  218. return ( value & ( value - 1 ) ) === 0 && value !== 0;
  219. }
  220. /**
  221. * Returns the smallest power of two that is greater than or equal to the given number.
  222. *
  223. * @param {number} value - The value to find a POT for. Must be greater than `0`.
  224. * @return {number} The smallest power of two that is greater than or equal to the given number.
  225. */
  226. function ceilPowerOfTwo( value ) {
  227. return Math.pow( 2, Math.ceil( Math.log( value ) / Math.LN2 ) );
  228. }
  229. /**
  230. * Returns the largest power of two that is less than or equal to the given number.
  231. *
  232. * @param {number} value - The value to find a POT for. Must be greater than `0`.
  233. * @return {number} The largest power of two that is less than or equal to the given number.
  234. */
  235. function floorPowerOfTwo( value ) {
  236. return Math.pow( 2, Math.floor( Math.log( value ) / Math.LN2 ) );
  237. }
  238. /**
  239. * Sets the given quaternion from the [Intrinsic Proper Euler Angles](https://en.wikipedia.org/wiki/Euler_angles)
  240. * defined by the given angles and order.
  241. *
  242. * Rotations are applied to the axes in the order specified by order:
  243. * rotation by angle `a` is applied first, then by angle `b`, then by angle `c`.
  244. *
  245. * @param {Quaternion} q - The quaternion to set.
  246. * @param {number} a - The rotation applied to the first axis, in radians.
  247. * @param {number} b - The rotation applied to the second axis, in radians.
  248. * @param {number} c - The rotation applied to the third axis, in radians.
  249. * @param {('XYX'|'XZX'|'YXY'|'YZY'|'ZXZ'|'ZYZ')} order - A string specifying the axes order.
  250. */
  251. function setQuaternionFromProperEuler( q, a, b, c, order ) {
  252. const cos = Math.cos;
  253. const sin = Math.sin;
  254. const c2 = cos( b / 2 );
  255. const s2 = sin( b / 2 );
  256. const c13 = cos( ( a + c ) / 2 );
  257. const s13 = sin( ( a + c ) / 2 );
  258. const c1_3 = cos( ( a - c ) / 2 );
  259. const s1_3 = sin( ( a - c ) / 2 );
  260. const c3_1 = cos( ( c - a ) / 2 );
  261. const s3_1 = sin( ( c - a ) / 2 );
  262. switch ( order ) {
  263. case 'XYX':
  264. q.set( c2 * s13, s2 * c1_3, s2 * s1_3, c2 * c13 );
  265. break;
  266. case 'YZY':
  267. q.set( s2 * s1_3, c2 * s13, s2 * c1_3, c2 * c13 );
  268. break;
  269. case 'ZXZ':
  270. q.set( s2 * c1_3, s2 * s1_3, c2 * s13, c2 * c13 );
  271. break;
  272. case 'XZX':
  273. q.set( c2 * s13, s2 * s3_1, s2 * c3_1, c2 * c13 );
  274. break;
  275. case 'YXY':
  276. q.set( s2 * c3_1, c2 * s13, s2 * s3_1, c2 * c13 );
  277. break;
  278. case 'ZYZ':
  279. q.set( s2 * s3_1, s2 * c3_1, c2 * s13, c2 * c13 );
  280. break;
  281. default:
  282. warn( 'MathUtils: .setQuaternionFromProperEuler() encountered an unknown order: ' + order );
  283. }
  284. }
  285. /**
  286. * Denormalizes the given value according to the given typed array.
  287. *
  288. * @param {number} value - The value to denormalize.
  289. * @param {TypedArray} array - The typed array that defines the data type of the value.
  290. * @return {number} The denormalize (float) value in the range `[0,1]`.
  291. */
  292. function denormalize( value, array ) {
  293. switch ( array.constructor ) {
  294. case Float32Array:
  295. return value;
  296. case Uint32Array:
  297. return value / 4294967295.0;
  298. case Uint16Array:
  299. return value / 65535.0;
  300. case Uint8Array:
  301. return value / 255.0;
  302. case Int32Array:
  303. return Math.max( value / 2147483647.0, - 1.0 );
  304. case Int16Array:
  305. return Math.max( value / 32767.0, - 1.0 );
  306. case Int8Array:
  307. return Math.max( value / 127.0, - 1.0 );
  308. default:
  309. throw new Error( 'Invalid component type.' );
  310. }
  311. }
  312. /**
  313. * Normalizes the given value according to the given typed array.
  314. *
  315. * @param {number} value - The float value in the range `[0,1]` to normalize.
  316. * @param {TypedArray} array - The typed array that defines the data type of the value.
  317. * @return {number} The normalize value.
  318. */
  319. function normalize( value, array ) {
  320. switch ( array.constructor ) {
  321. case Float32Array:
  322. return value;
  323. case Uint32Array:
  324. return Math.round( value * 4294967295.0 );
  325. case Uint16Array:
  326. return Math.round( value * 65535.0 );
  327. case Uint8Array:
  328. return Math.round( value * 255.0 );
  329. case Int32Array:
  330. return Math.round( value * 2147483647.0 );
  331. case Int16Array:
  332. return Math.round( value * 32767.0 );
  333. case Int8Array:
  334. return Math.round( value * 127.0 );
  335. default:
  336. throw new Error( 'Invalid component type.' );
  337. }
  338. }
  339. /**
  340. * @class
  341. * @classdesc A collection of math utility functions.
  342. * @hideconstructor
  343. */
  344. const MathUtils = {
  345. DEG2RAD: DEG2RAD,
  346. RAD2DEG: RAD2DEG,
  347. /**
  348. * Generate a [UUID](https://en.wikipedia.org/wiki/Universally_unique_identifier)
  349. * (universally unique identifier).
  350. *
  351. * @static
  352. * @method
  353. * @return {string} The UUID.
  354. */
  355. generateUUID: generateUUID,
  356. /**
  357. * Clamps the given value between min and max.
  358. *
  359. * @static
  360. * @method
  361. * @param {number} value - The value to clamp.
  362. * @param {number} min - The min value.
  363. * @param {number} max - The max value.
  364. * @return {number} The clamped value.
  365. */
  366. clamp: clamp,
  367. /**
  368. * Computes the Euclidean modulo of the given parameters that
  369. * is `( ( n % m ) + m ) % m`.
  370. *
  371. * @static
  372. * @method
  373. * @param {number} n - The first parameter.
  374. * @param {number} m - The second parameter.
  375. * @return {number} The Euclidean modulo.
  376. */
  377. euclideanModulo: euclideanModulo,
  378. /**
  379. * Performs a linear mapping from range `<a1, a2>` to range `<b1, b2>`
  380. * for the given value.
  381. *
  382. * @static
  383. * @method
  384. * @param {number} x - The value to be mapped.
  385. * @param {number} a1 - Minimum value for range A.
  386. * @param {number} a2 - Maximum value for range A.
  387. * @param {number} b1 - Minimum value for range B.
  388. * @param {number} b2 - Maximum value for range B.
  389. * @return {number} The mapped value.
  390. */
  391. mapLinear: mapLinear,
  392. /**
  393. * Returns the percentage in the closed interval `[0, 1]` of the given value
  394. * between the start and end point.
  395. *
  396. * @static
  397. * @method
  398. * @param {number} x - The start point
  399. * @param {number} y - The end point.
  400. * @param {number} value - A value between start and end.
  401. * @return {number} The interpolation factor.
  402. */
  403. inverseLerp: inverseLerp,
  404. /**
  405. * Returns a value linearly interpolated from two known points based on the given interval -
  406. * `t = 0` will return `x` and `t = 1` will return `y`.
  407. *
  408. * @static
  409. * @method
  410. * @param {number} x - The start point
  411. * @param {number} y - The end point.
  412. * @param {number} t - The interpolation factor in the closed interval `[0, 1]`.
  413. * @return {number} The interpolated value.
  414. */
  415. lerp: lerp,
  416. /**
  417. * Smoothly interpolate a number from `x` to `y` in a spring-like manner using a delta
  418. * time to maintain frame rate independent movement. For details, see
  419. * [Frame rate independent damping using lerp](http://www.rorydriscoll.com/2016/03/07/frame-rate-independent-damping-using-lerp/).
  420. *
  421. * @static
  422. * @method
  423. * @param {number} x - The current point.
  424. * @param {number} y - The target point.
  425. * @param {number} lambda - A higher lambda value will make the movement more sudden,
  426. * and a lower value will make the movement more gradual.
  427. * @param {number} dt - Delta time in seconds.
  428. * @return {number} The interpolated value.
  429. */
  430. damp: damp,
  431. /**
  432. * Returns a value that alternates between `0` and the given `length` parameter.
  433. *
  434. * @static
  435. * @method
  436. * @param {number} x - The value to pingpong.
  437. * @param {number} [length=1] - The positive value the function will pingpong to.
  438. * @return {number} The alternated value.
  439. */
  440. pingpong: pingpong,
  441. /**
  442. * Returns a value in the range `[0,1]` that represents the percentage that `x` has
  443. * moved between `min` and `max`, but smoothed or slowed down the closer `x` is to
  444. * the `min` and `max`.
  445. *
  446. * See [Smoothstep](http://en.wikipedia.org/wiki/Smoothstep) for more details.
  447. *
  448. * @static
  449. * @method
  450. * @param {number} x - The value to evaluate based on its position between min and max.
  451. * @param {number} min - The min value. Any x value below min will be `0`.
  452. * @param {number} max - The max value. Any x value above max will be `1`.
  453. * @return {number} The alternated value.
  454. */
  455. smoothstep: smoothstep,
  456. /**
  457. * A [variation on smoothstep](https://en.wikipedia.org/wiki/Smoothstep#Variations)
  458. * that has zero 1st and 2nd order derivatives at x=0 and x=1.
  459. *
  460. * @static
  461. * @method
  462. * @param {number} x - The value to evaluate based on its position between min and max.
  463. * @param {number} min - The min value. Any x value below min will be `0`.
  464. * @param {number} max - The max value. Any x value above max will be `1`.
  465. * @return {number} The alternated value.
  466. */
  467. smootherstep: smootherstep,
  468. /**
  469. * Returns a random integer from `<low, high>` interval.
  470. *
  471. * @static
  472. * @method
  473. * @param {number} low - The lower value boundary.
  474. * @param {number} high - The upper value boundary
  475. * @return {number} A random integer.
  476. */
  477. randInt: randInt,
  478. /**
  479. * Returns a random float from `<low, high>` interval.
  480. *
  481. * @static
  482. * @method
  483. * @param {number} low - The lower value boundary.
  484. * @param {number} high - The upper value boundary
  485. * @return {number} A random float.
  486. */
  487. randFloat: randFloat,
  488. /**
  489. * Returns a random integer from `<-range/2, range/2>` interval.
  490. *
  491. * @static
  492. * @method
  493. * @param {number} range - Defines the value range.
  494. * @return {number} A random float.
  495. */
  496. randFloatSpread: randFloatSpread,
  497. /**
  498. * Returns a deterministic pseudo-random float in the interval `[0, 1]`.
  499. *
  500. * @static
  501. * @method
  502. * @param {number} [s] - The integer seed.
  503. * @return {number} A random float.
  504. */
  505. seededRandom: seededRandom,
  506. /**
  507. * Converts degrees to radians.
  508. *
  509. * @static
  510. * @method
  511. * @param {number} degrees - A value in degrees.
  512. * @return {number} The converted value in radians.
  513. */
  514. degToRad: degToRad,
  515. /**
  516. * Converts radians to degrees.
  517. *
  518. * @static
  519. * @method
  520. * @param {number} radians - A value in radians.
  521. * @return {number} The converted value in degrees.
  522. */
  523. radToDeg: radToDeg,
  524. /**
  525. * Returns `true` if the given number is a power of two.
  526. *
  527. * @static
  528. * @method
  529. * @param {number} value - The value to check.
  530. * @return {boolean} Whether the given number is a power of two or not.
  531. */
  532. isPowerOfTwo: isPowerOfTwo,
  533. /**
  534. * Returns the smallest power of two that is greater than or equal to the given number.
  535. *
  536. * @static
  537. * @method
  538. * @param {number} value - The value to find a POT for.
  539. * @return {number} The smallest power of two that is greater than or equal to the given number.
  540. */
  541. ceilPowerOfTwo: ceilPowerOfTwo,
  542. /**
  543. * Returns the largest power of two that is less than or equal to the given number.
  544. *
  545. * @static
  546. * @method
  547. * @param {number} value - The value to find a POT for.
  548. * @return {number} The largest power of two that is less than or equal to the given number.
  549. */
  550. floorPowerOfTwo: floorPowerOfTwo,
  551. /**
  552. * Sets the given quaternion from the [Intrinsic Proper Euler Angles](https://en.wikipedia.org/wiki/Euler_angles)
  553. * defined by the given angles and order.
  554. *
  555. * Rotations are applied to the axes in the order specified by order:
  556. * rotation by angle `a` is applied first, then by angle `b`, then by angle `c`.
  557. *
  558. * @static
  559. * @method
  560. * @param {Quaternion} q - The quaternion to set.
  561. * @param {number} a - The rotation applied to the first axis, in radians.
  562. * @param {number} b - The rotation applied to the second axis, in radians.
  563. * @param {number} c - The rotation applied to the third axis, in radians.
  564. * @param {('XYX'|'XZX'|'YXY'|'YZY'|'ZXZ'|'ZYZ')} order - A string specifying the axes order.
  565. */
  566. setQuaternionFromProperEuler: setQuaternionFromProperEuler,
  567. /**
  568. * Normalizes the given value according to the given typed array.
  569. *
  570. * @static
  571. * @method
  572. * @param {number} value - The float value in the range `[0,1]` to normalize.
  573. * @param {TypedArray} array - The typed array that defines the data type of the value.
  574. * @return {number} The normalize value.
  575. */
  576. normalize: normalize,
  577. /**
  578. * Denormalizes the given value according to the given typed array.
  579. *
  580. * @static
  581. * @method
  582. * @param {number} value - The value to denormalize.
  583. * @param {TypedArray} array - The typed array that defines the data type of the value.
  584. * @return {number} The denormalize (float) value in the range `[0,1]`.
  585. */
  586. denormalize: denormalize
  587. };
  588. export {
  589. DEG2RAD,
  590. RAD2DEG,
  591. generateUUID,
  592. clamp,
  593. euclideanModulo,
  594. mapLinear,
  595. inverseLerp,
  596. lerp,
  597. damp,
  598. pingpong,
  599. smoothstep,
  600. smootherstep,
  601. randInt,
  602. randFloat,
  603. randFloatSpread,
  604. seededRandom,
  605. degToRad,
  606. radToDeg,
  607. isPowerOfTwo,
  608. ceilPowerOfTwo,
  609. floorPowerOfTwo,
  610. setQuaternionFromProperEuler,
  611. normalize,
  612. denormalize,
  613. MathUtils
  614. };
粤ICP备19079148号