BezierUtils2.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System.Diagnostics;
  2. using System.Numerics;
  3. public class BezierUtils2
  4. {
  5. /// <summary>
  6. /// 根据T值,计算贝塞尔曲线上面相对应的点
  7. /// </summary>
  8. /// <param name="t"></param>T值
  9. /// <param name="p0"></param>起始点
  10. /// <param name="p1"></param>控制点
  11. /// <param name="p2"></param>目标点
  12. /// <returns></returns>根据T值计算出来的贝赛尔曲线点
  13. private static Vector3 CalculateCubicBezierPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2)
  14. {
  15. float u = 1 - t;
  16. float tt = t * t;
  17. float uu = u * u;
  18. Vector3 p = uu * p0;
  19. p += 2 * u * t * p1;
  20. p += tt * p2;
  21. return p;
  22. }
  23. /// <summary>
  24. /// 获取存储贝塞尔曲线点的数组
  25. /// </summary>
  26. /// <param name="startPoint"></param>起始点
  27. /// <param name="controlPoint"></param>控制点
  28. /// <param name="endPoint"></param>目标点
  29. /// <param name="segmentNum"></param>采样点的数量
  30. /// <returns></returns>存储贝塞尔曲线点的数组
  31. public static Vector3[] GetBeizerList(Vector3 startPoint, Vector3 controlPoint, Vector3 endPoint, int segmentNum)
  32. {
  33. Vector3[] path = new Vector3[segmentNum];
  34. for (int i = 1; i <= segmentNum; i++)
  35. {
  36. float t = i / (float)segmentNum;
  37. Vector3 pixel = CalculateCubicBezierPoint(t, startPoint,
  38. controlPoint, endPoint);
  39. path[i - 1] = pixel;
  40. // Debug.Log(path[i - 1]);
  41. }
  42. return path;
  43. }
  44. }