LineManager.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. JGIndex = 0;
  47. isStart = false;
  48. RoadManager.Instance.close();
  49. line.positionCount = 0;
  50. }
  51. public bool isStart;
  52. List<Vector3> initList;
  53. public LineRenderer line;
  54. float MaxL;
  55. public void setRoad(List<Vector3> lineList)
  56. {
  57. JGIndex = 0;
  58. isStart = true;
  59. if (!line)
  60. {
  61. line =GameObject.Instantiate( WindowsManager.Instance.GetPrefab(WindowConfig.windowType.XunJian,"Line")).GetComponent<LineRenderer>();
  62. }
  63. lineList.Insert(0,OpenXRCamera.Instance.head.position);
  64. initList = lineList;
  65. for (int i = 0; i < lineList.Count; i++)
  66. {
  67. lineList[i] = new Vector3(lineList[i].x, OpenXRCamera.Instance.head.position.y-0.3f, lineList[i].z);
  68. }
  69. line.startWidth = 0.025f;
  70. line.endWidth = 0.025f;
  71. if(lineList.Count>2)
  72. {
  73. List<Vector3> v3 = new List<Vector3>();
  74. for (int i = 2; i < lineList.Count; i+=2)
  75. {
  76. v3.Add(lineList[i - 2]);
  77. v3.AddRange(DrawCurve(GetPos(lineList[i - 1],lineList[i-2]), lineList[i - 1], GetPos(lineList[i - 1], lineList[i])));
  78. v3.Add(lineList[i]);
  79. }
  80. lineList = v3;
  81. }
  82. line.positionCount = lineList.Count;
  83. line.SetPositions(lineList.ToArray());
  84. MaxL = 0;
  85. for (int i = 1; i < lineList.Count; i++)
  86. {
  87. MaxL+= Vector3.Distance(lineList[i - 1], lineList[i]);
  88. }
  89. line.materials[0].SetTextureScale("_MainTex", new Vector2(MaxL * 10*4, 1));
  90. }
  91. public Vector3 GetPos(Vector3 post1, Vector3 post2)
  92. {
  93. Vector3 pos = Vector3.zero;
  94. float dis = Vector3.Distance(post1, post2);//计算距离
  95. Vector3 vector = (post2 - post1).normalized; //向量单位化
  96. float rand =0.2f;//随机距离
  97. pos = vector * rand; //得到新坐标
  98. pos += post1; //使新的坐标点在 post1与 post2之间
  99. return pos;
  100. }
  101. private int _segmentNum = 10;
  102. List<Vector3> DrawCurve(Vector3 pos1, Vector3 pos2, Vector3 pos3)
  103. {
  104. List<Vector3> listPos = new List<Vector3>();
  105. for (int i = 1; i <= _segmentNum; i++)
  106. {
  107. float t = i / (float)_segmentNum;
  108. int nodeIndex = 0;
  109. Vector3 pixel = CalculateCubicBezierPoint(t, pos1,
  110. pos2, pos3);
  111. listPos.Add(pixel);
  112. }
  113. return listPos;
  114. }
  115. Vector3 CalculateCubicBezierPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2)
  116. {
  117. float u = 1 - t;
  118. float tt = t * t;
  119. float uu = u * u;
  120. Vector3 p = uu * p0;
  121. p += 2 * u * t * p1;
  122. p += tt * p2;
  123. return p;
  124. }
  125. int JGIndex;
  126. void checkPos()
  127. {
  128. }
  129. }