math.js 953 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. export const math = (function() {
  2. return {
  3. rand_range: function(a, b) {
  4. return Math.random() * (b - a) + a;
  5. },
  6. rand_normalish: function() {
  7. const r = Math.random() + Math.random() + Math.random() + Math.random();
  8. return (r / 4.0) * 2.0 - 1;
  9. },
  10. rand_int: function(a, b) {
  11. return Math.round(Math.random() * (b - a) + a);
  12. },
  13. lerp: function(x, a, b) {
  14. return x * (b - a) + a;
  15. },
  16. smoothstep: function(x, a, b) {
  17. x = x * x * (3.0 - 2.0 * x);
  18. return x * (b - a) + a;
  19. },
  20. smootherstep: function(x, a, b) {
  21. x = x * x * x * (x * (x * 6 - 15) + 10);
  22. return x * (b - a) + a;
  23. },
  24. clamp: function(x, a, b) {
  25. return Math.min(Math.max(x, a), b);
  26. },
  27. sat: function(x) {
  28. return Math.min(Math.max(x, 0.0), 1.0);
  29. },
  30. in_range: (x, a, b) => {
  31. return x >= a && x <= b;
  32. },
  33. };
  34. })();