BesselPath.cs 1020 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace Core.Utility
  4. {
  5. /// <summary>
  6. /// 贝塞尔曲线
  7. /// </summary>
  8. public class BesselPath
  9. {
  10. public List<Vector3> PosList;
  11. public void SetPos(List<Vector3> PosList)
  12. {
  13. this.PosList = PosList;
  14. }
  15. public float GetPathCount(int count)
  16. {
  17. float step = 1f / count;
  18. Vector3 startPos = GetValue(0);
  19. float d = 0;
  20. for (int i = 1; i < count; i++)
  21. {
  22. Vector3 currPos = GetValue(step * i);
  23. d += Vector3.Distance(startPos, currPos);
  24. }
  25. return d;
  26. }
  27. public Vector3 GetValue(float t)
  28. {
  29. Vector2 a = PosList[0];
  30. for (int i = 1; i < PosList.Count; i++)
  31. {
  32. Vector2 b = PosList[i];
  33. Vector2 c = Vector2.Lerp(a, b, t);
  34. a = c;
  35. }
  36. return a;
  37. }
  38. }
  39. }