LineManager.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 (line != null && line.positionCount > JGIndex)
  29. {
  30. Vector3 v3 = new Vector3(OpenXRCamera.Instance.head.position.x, line.GetPosition(JGIndex).y, OpenXRCamera.Instance.head.position.z);
  31. if (Vector3.Distance(v3, line.GetPosition(JGIndex)) <= 1f)
  32. {
  33. JGIndex++;
  34. if (JGIndex >= line.positionCount)
  35. {
  36. JGIndex = 0;
  37. line.positionCount = 0;
  38. initList = null;
  39. RoadManager.Instance.close();
  40. JinRuRenwu.Instance.gotoNextItem();
  41. }
  42. }
  43. if(line.positionCount > JGIndex)
  44. RoadManager.Instance.setFX(line.GetPosition(JGIndex));
  45. }
  46. }
  47. }
  48. }
  49. public void stop()
  50. {
  51. if(RoadManager.Instance!=null&& line!=null)
  52. {
  53. JGIndex = 0;
  54. isStart = false;
  55. RoadManager.Instance.close();
  56. line.positionCount = 0;
  57. }
  58. }
  59. public bool isStart;
  60. List<Vector3> initList;
  61. public LineRenderer line;
  62. float MaxL;
  63. public void setRoad(List<Vector3> lineList)
  64. {
  65. JGIndex = 0;
  66. isStart = true;
  67. if (!line)
  68. {
  69. line =GameObject.Instantiate( WindowsManager.Instance.GetPrefab(WindowConfig.windowType.XunJian,"Line")).GetComponent<LineRenderer>();
  70. }
  71. lineList.Insert(0,OpenXRCamera.Instance.head.position);
  72. initList = lineList;
  73. for (int i = 0; i < lineList.Count; i++)
  74. {
  75. lineList[i] = new Vector3(lineList[i].x, OpenXRCamera.Instance.head.position.y-0.3f, lineList[i].z);
  76. }
  77. line.startWidth = 0.025f;
  78. line.endWidth = 0.025f;
  79. if(lineList.Count>2)
  80. {
  81. List<Vector3> v3 = new List<Vector3>();
  82. int indexNow=0;
  83. for (int i = 2; i < lineList.Count; i+=2)
  84. {
  85. v3.Add(lineList[i - 2]);
  86. v3.AddRange(DrawCurve(GetPos(lineList[i - 1],lineList[i-2]), lineList[i - 1], GetPos(lineList[i - 1], lineList[i])));
  87. v3.Add(lineList[i]);
  88. indexNow = i;
  89. }
  90. for (int j = indexNow+1; j < lineList.Count; j++)
  91. {
  92. v3.Add(lineList[j]);
  93. }
  94. lineList = v3;
  95. }
  96. line.positionCount = lineList.Count;
  97. line.SetPositions(lineList.ToArray());
  98. MaxL = 0;
  99. for (int i = 1; i < lineList.Count; i++)
  100. {
  101. MaxL+= Vector3.Distance(lineList[i - 1], lineList[i]);
  102. }
  103. initListLine();
  104. line.materials[0].SetTextureScale("_MainTex", new Vector2(MaxL * 10*4, 1));
  105. }
  106. public Vector3 GetPos(Vector3 post1, Vector3 post2)
  107. {
  108. Vector3 pos = Vector3.zero;
  109. float dis = Vector3.Distance(post1, post2);//计算距离
  110. Vector3 vector = (post2 - post1).normalized; //向量单位化
  111. float rand =0.2f;//随机距离
  112. pos = vector * rand; //得到新坐标
  113. pos += post1; //使新的坐标点在 post1与 post2之间
  114. return pos;
  115. }
  116. private int _segmentNum = 10;
  117. List<Vector3> DrawCurve(Vector3 pos1, Vector3 pos2, Vector3 pos3)
  118. {
  119. List<Vector3> listPos = new List<Vector3>();
  120. for (int i = 1; i <= _segmentNum; i++)
  121. {
  122. float t = i / (float)_segmentNum;
  123. int nodeIndex = 0;
  124. Vector3 pixel = CalculateCubicBezierPoint(t, pos1,
  125. pos2, pos3);
  126. listPos.Add(pixel);
  127. }
  128. return listPos;
  129. }
  130. Vector3 CalculateCubicBezierPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2)
  131. {
  132. float u = 1 - t;
  133. float tt = t * t;
  134. float uu = u * u;
  135. Vector3 p = uu * p0;
  136. p += 2 * u * t * p1;
  137. p += tt * p2;
  138. return p;
  139. }
  140. int JGIndex;
  141. void checkPos()
  142. {
  143. }
  144. List<Vector3> poslist = new List<Vector3>();
  145. private void initListLine()
  146. {
  147. poslist = new List<Vector3>();
  148. LineRenderer lr = line;
  149. if (lr)
  150. {
  151. for (int i = 0; i < lr.positionCount; i++)
  152. {
  153. poslist.Add(lr.GetPosition(i));
  154. }
  155. }
  156. }
  157. // Update is called once per frame
  158. void Update()
  159. {
  160. LineRenderer lr = line;
  161. if (lr)
  162. {
  163. for (int i = 1; i < lr.positionCount; i++)
  164. {
  165. Vector3 v3 = DianYunManager.Instance.TestGo.transform.TransformPoint(poslist[i]);
  166. lr.SetPosition(i, new Vector3(v3.x, OpenXRCamera.Instance.head.position.y - 0.3f, v3.z));
  167. }
  168. }
  169. }
  170. }