VibrationHelper.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using Unity.Burst;
  2. using Unity.Mathematics;
  3. using UnityEngine;
  4. namespace LitMotion
  5. {
  6. [BurstCompile]
  7. internal static class VibrationHelper
  8. {
  9. [BurstCompile]
  10. public static void EvaluateStrength(in float strength, in int frequency, in float dampingRatio, in float t, out float result)
  11. {
  12. if (t == 1f || t == 0f)
  13. {
  14. result = 0f;
  15. return;
  16. }
  17. float angularFrequency = (frequency - 0.5f) * math.PI;
  18. float dampingFactor = dampingRatio * frequency / (2f * math.PI);
  19. result = strength * math.pow(math.E, -dampingFactor * t) * math.cos(angularFrequency * t);
  20. }
  21. [BurstCompile]
  22. public static void EvaluateStrength(in Vector2 strength, in int frequency, in float dampingRatio, in float t, out Vector2 result)
  23. {
  24. if (t == 1f || t == 0f)
  25. {
  26. result = Vector2.zero;
  27. return;
  28. }
  29. float angularFrequency = (frequency - 0.5f) * math.PI;
  30. float dampingFactor = dampingRatio * frequency / (2f * math.PI);
  31. result = math.cos(angularFrequency * t) * math.pow(math.E, -dampingFactor * t) * strength;
  32. }
  33. [BurstCompile]
  34. public static void EvaluateStrength(in Vector3 strength, in int frequency, in float dampingRatio, in float t, out Vector3 result)
  35. {
  36. if (t == 1f || t == 0f)
  37. {
  38. result = Vector3.zero;
  39. return;
  40. }
  41. float angularFrequency = (frequency - 0.5f) * math.PI;
  42. float dampingFactor = dampingRatio * frequency / (2f * math.PI);
  43. result = math.cos(angularFrequency * t) * math.pow(math.E, -dampingFactor * t) * strength;
  44. }
  45. }
  46. }