LineManager.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class LineManager : MonoSingleton<LineManager>
  5. {
  6. private Vector2 Interval = new Vector2(0.05f, 0);
  7. private Vector2 offset;
  8. private void Start()
  9. {
  10. StartCoroutine(onDGTween());
  11. }
  12. IEnumerator onDGTween()
  13. {
  14. while(true)
  15. {
  16. yield return new WaitForSeconds(0.04f);
  17. if (line!=null)
  18. {
  19. offset -= Interval;
  20. line.materials[0].SetTextureOffset("_MainTex", offset);
  21. if(offset.x<=-1)
  22. {
  23. offset = Vector2.zero;
  24. }
  25. }
  26. if(isStart)
  27. {
  28. if (initList != null && initList.Count > 0)
  29. {
  30. Vector3 v3 = new Vector3(OpenXRCamera.Instance.head.position.x, initList[JGIndex].y, OpenXRCamera.Instance.head.position.z);
  31. if (Vector3.Distance(v3, initList[JGIndex]) <= 0.2f)
  32. {
  33. JGIndex++;
  34. if (JGIndex >= initList.Count)
  35. {
  36. JGIndex = initList.Count - 1;
  37. }
  38. }
  39. RoadManager.Instance.setFX(initList[JGIndex]);
  40. }
  41. }
  42. }
  43. }
  44. public void stop()
  45. {
  46. if(RoadManager.Instance!=null&& line!=null)
  47. {
  48. JGIndex = 0;
  49. isStart = false;
  50. RoadManager.Instance.close();
  51. line.positionCount = 0;
  52. }
  53. }
  54. public bool isStart;
  55. List<Vector3> initList;
  56. public LineRenderer line;
  57. float MaxL;
  58. public void setRoad(List<Vector3> lineList)
  59. {
  60. JGIndex = 0;
  61. isStart = true;
  62. if (!line)
  63. {
  64. line =GameObject.Instantiate( WindowsManager.Instance.GetPrefab(WindowConfig.windowType.XunJian,"Line")).GetComponent<LineRenderer>();
  65. }
  66. lineList.Insert(0,OpenXRCamera.Instance.head.position);
  67. initList = lineList;
  68. for (int i = 0; i < lineList.Count; i++)
  69. {
  70. lineList[i] = new Vector3(lineList[i].x, OpenXRCamera.Instance.head.position.y-0.3f, lineList[i].z);
  71. }
  72. line.startWidth = 0.025f;
  73. line.endWidth = 0.025f;
  74. if(lineList.Count>2)
  75. {
  76. List<Vector3> v3 = new List<Vector3>();
  77. for (int i = 2; i < lineList.Count; i+=2)
  78. {
  79. v3.Add(lineList[i - 2]);
  80. v3.AddRange(DrawCurve(GetPos(lineList[i - 1],lineList[i-2]), lineList[i - 1], GetPos(lineList[i - 1], lineList[i])));
  81. v3.Add(lineList[i]);
  82. }
  83. lineList = v3;
  84. }
  85. line.positionCount = lineList.Count;
  86. line.SetPositions(lineList.ToArray());
  87. MaxL = 0;
  88. for (int i = 1; i < lineList.Count; i++)
  89. {
  90. MaxL+= Vector3.Distance(lineList[i - 1], lineList[i]);
  91. }
  92. line.materials[0].SetTextureScale("_MainTex", new Vector2(MaxL * 10*4, 1));
  93. }
  94. public Vector3 GetPos(Vector3 post1, Vector3 post2)
  95. {
  96. Vector3 pos = Vector3.zero;
  97. float dis = Vector3.Distance(post1, post2);//计算距离
  98. Vector3 vector = (post2 - post1).normalized; //向量单位化
  99. float rand =0.2f;//随机距离
  100. pos = vector * rand; //得到新坐标
  101. pos += post1; //使新的坐标点在 post1与 post2之间
  102. return pos;
  103. }
  104. private int _segmentNum = 10;
  105. List<Vector3> DrawCurve(Vector3 pos1, Vector3 pos2, Vector3 pos3)
  106. {
  107. List<Vector3> listPos = new List<Vector3>();
  108. for (int i = 1; i <= _segmentNum; i++)
  109. {
  110. float t = i / (float)_segmentNum;
  111. int nodeIndex = 0;
  112. Vector3 pixel = CalculateCubicBezierPoint(t, pos1,
  113. pos2, pos3);
  114. listPos.Add(pixel);
  115. }
  116. return listPos;
  117. }
  118. Vector3 CalculateCubicBezierPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2)
  119. {
  120. float u = 1 - t;
  121. float tt = t * t;
  122. float uu = u * u;
  123. Vector3 p = uu * p0;
  124. p += 2 * u * t * p1;
  125. p += tt * p2;
  126. return p;
  127. }
  128. int JGIndex;
  129. void checkPos()
  130. {
  131. }
  132. }