BezierCurve.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. /// <summary>
  6. /// 贝塞尔曲线
  7. /// </summary>
  8. [Serializable]
  9. public class BezierCurve
  10. {
  11. /// <summary>
  12. /// 段数
  13. /// </summary>
  14. [Range(1, 100)] public int segments = 10;
  15. /// <summary>
  16. /// 是否循环
  17. /// </summary>
  18. public bool loop;
  19. /// <summary>
  20. /// 点集合
  21. /// </summary>
  22. public List<BezierCurvePoint> points = new List<BezierCurvePoint>(2)
  23. {
  24. new BezierCurvePoint() { position = Vector3.back * 5f, tangent = Vector3.back * 5f + Vector3.left * 3f },
  25. new BezierCurvePoint() { position = Vector3.forward * 5f, tangent = Vector3.forward * 5f + Vector3.right * 3f }
  26. };
  27. /// <summary>
  28. /// 根据归一化位置值获取对应的贝塞尔曲线上的点
  29. /// </summary>
  30. /// <param name="t">归一化位置值 [0,1]</param>
  31. /// <returns></returns>
  32. public Vector3 EvaluatePosition(float t)
  33. {
  34. Vector3 retVal = Vector3.zero;
  35. if (points.Count > 0)
  36. {
  37. float max = points.Count - 1 < 1 ? 0 : (loop ? points.Count : points.Count - 1);
  38. float standardized = (loop && max > 0) ? ((t %= max) + (t < 0 ? max : 0)) : Mathf.Clamp(t, 0, max);
  39. int rounded = Mathf.RoundToInt(standardized);
  40. int i1, i2;
  41. if (Mathf.Abs(standardized - rounded) < Mathf.Epsilon)
  42. i1 = i2 = (rounded == points.Count) ? 0 : rounded;
  43. else
  44. {
  45. i1 = Mathf.FloorToInt(standardized);
  46. if (i1 >= points.Count)
  47. {
  48. standardized -= max;
  49. i1 = 0;
  50. }
  51. i2 = Mathf.CeilToInt(standardized);
  52. i2 = i2 >= points.Count ? 0 : i2;
  53. }
  54. retVal = i1 == i2 ? points[i1].position : BezierCurveUtility.Bezier3(points[i1].position,
  55. points[i1].position + points[i1].tangent, points[i2].position
  56. - points[i2].tangent, points[i2].position, standardized - i1);
  57. }
  58. return retVal;
  59. }
  60. }
  61. [Serializable]
  62. public struct BezierCurvePoint
  63. {
  64. /// <summary>
  65. /// 坐标点
  66. /// </summary>
  67. public Vector3 position;
  68. /// <summary>
  69. /// 控制点 与坐标点形成切线
  70. /// </summary>
  71. public Vector3 tangent;
  72. }
  73. public class BezierCurveUtility
  74. {
  75. /// <summary>
  76. /// 三阶贝塞尔曲线
  77. /// </summary>
  78. /// <param name="p0">起点</param>
  79. /// <param name="p1">控制点1</param>
  80. /// <param name="p2">控制点2</param>
  81. /// <param name="p3">终点</param>
  82. /// <param name="t">[0,1]</param>
  83. /// <returns></returns>
  84. public static Vector3 Bezier3(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t)
  85. {
  86. Vector3 p0p1 = (1 - t) * p0 + t * p1;
  87. Vector3 p1p2 = (1 - t) * p1 + t * p2;
  88. Vector3 p2p3 = (1 - t) * p2 + t * p3;
  89. Vector3 p0p1p2 = (1 - t) * p0p1 + t * p1p2;
  90. Vector3 p1p2p3 = (1 - t) * p1p2 + t * p2p3;
  91. return (1 - t) * p0p1p2 + t * p1p2p3;
  92. }
  93. }