Bezier.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. public class Bezier : MonoSingleton<Bezier>
  5. {
  6. // public Transform[] controlPoints;
  7. public LineRenderer lineRenderer;
  8. private int layerOrder = 0;
  9. private int _segmentNum = 50;
  10. private float rr = 0.2f;
  11. public Transform point;
  12. private Vector2 Interval = new Vector2(0.05f, 0);
  13. private Material material;
  14. private Vector2 offset;
  15. // 0.3 弧度半径
  16. void Start()
  17. {
  18. material = lineRenderer.materials[0];
  19. if (!lineRenderer)
  20. {
  21. lineRenderer = GetComponent<LineRenderer>();
  22. }
  23. // lineRenderer.sortingLayerID = layerOrder;
  24. // DrawCurve();
  25. StartCoroutine(SetOffset());
  26. }
  27. public void SetRoute(List<Vector3> listPos)
  28. {
  29. int index = 0;
  30. RouteClear();
  31. lineRenderer.positionCount = (listPos.Count - 2) * _segmentNum + listPos.Count;
  32. Debug.Log(lineRenderer.positionCount);
  33. lineRenderer.SetPosition(index++, listPos[0]);
  34. for (int i = 1; i < listPos.Count - 1; i++)
  35. {
  36. point.position = listPos[i];
  37. point.LookAt(listPos[i - 1]);
  38. point.Translate(0, 0, rr, Space.Self);
  39. Vector3 pos1 = point.position;
  40. lineRenderer.SetPosition(index++, pos1);
  41. //GameObject obj = new GameObject(i.ToString());
  42. //obj.transform.position = point.position;
  43. //GameObject obj2 = new GameObject(i.ToString());
  44. //obj2.transform.position = controlPoints[i].position;
  45. point.position = listPos[i];
  46. point.LookAt(listPos[i + 1]);
  47. point.Translate(0, 0, rr + 0.05f, Space.Self);
  48. Vector3 pos2 = point.position;
  49. Debug.Log(pos1 + " " + point.position + " " + pos2);
  50. List<Vector3> listLinePos = DrawCurve(pos1, listPos[i], pos2);
  51. for (int j = 0; j < listLinePos.Count; j++)
  52. {
  53. lineRenderer.SetPosition(index++, listLinePos[j]);
  54. }
  55. }
  56. lineRenderer.SetPosition(index++, listPos[listPos.Count - 1]);
  57. float dis = 0;
  58. for (int i = 0; i < lineRenderer.positionCount-1; i++)
  59. {
  60. dis += Vector3.Distance(lineRenderer.GetPosition(i), lineRenderer.GetPosition(i + 1));
  61. }
  62. material.SetTextureScale("_MainTex", new Vector2(dis * 8, 1f));
  63. }
  64. public void RouteClear()
  65. {
  66. lineRenderer.positionCount = 0;
  67. }
  68. private IEnumerator SetOffset()
  69. {
  70. while (true)
  71. {
  72. yield return new WaitForSeconds(0.04f);
  73. offset -= Interval;
  74. material.SetTextureOffset("_MainTex", offset);
  75. }
  76. }
  77. void Update()
  78. {
  79. // DrawCurve();
  80. }
  81. void DrawCurve()
  82. {
  83. //for (int i = 1; i <= _segmentNum; i++)
  84. //{
  85. // float t = i / (float)_segmentNum;
  86. // int nodeIndex = 0;
  87. // Vector3 pixel = CalculateCubicBezierPoint(t, controlPoints[nodeIndex].position,
  88. // controlPoints[nodeIndex + 1].position, controlPoints[nodeIndex + 2].position);
  89. // lineRenderer.positionCount = i;
  90. // lineRenderer.SetPosition(i - 1, pixel);
  91. //}
  92. }
  93. List<Vector3> DrawCurve( Vector3 pos1,Vector3 pos2 ,Vector3 pos3)
  94. {
  95. List<Vector3> listPos = new List<Vector3>();
  96. for (int i = 1; i <= _segmentNum; i++)
  97. {
  98. float t = i / (float)_segmentNum;
  99. int nodeIndex = 0;
  100. Vector3 pixel = CalculateCubicBezierPoint(t, pos1,
  101. pos2, pos3);
  102. listPos.Add(pixel);
  103. }
  104. return listPos;
  105. }
  106. Vector3 CalculateCubicBezierPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2)
  107. {
  108. float u = 1 - t;
  109. float tt = t * t;
  110. float uu = u * u;
  111. Vector3 p = uu * p0;
  112. p += 2 * u * t * p1;
  113. p += tt * p2;
  114. return p;
  115. }
  116. }