123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class LineManager : MonoSingleton<LineManager>
- {
- private Vector2 Interval = new Vector2(0.05f, 0);
- private Vector2 offset;
- private void Start()
- {
- StartCoroutine(onDGTween());
- }
- IEnumerator onDGTween()
- {
- while(true)
- {
- yield return new WaitForSeconds(0.04f);
- if (line!=null)
- {
- offset -= Interval;
- line.materials[0].SetTextureOffset("_MainTex", offset);
- if(offset.x<=-1)
- {
- offset = Vector2.zero;
- }
- }
- if(isStart)
- {
- if (line != null && line.positionCount > JGIndex)
- {
- Vector3 v3 = new Vector3(OpenXRCamera.Instance.head.position.x, line.GetPosition(JGIndex).y, OpenXRCamera.Instance.head.position.z);
- if (Vector3.Distance(v3, line.GetPosition(JGIndex)) <= 1f)
- {
- JGIndex++;
- if (JGIndex >= line.positionCount)
- {
- JGIndex = 0;
- line.positionCount = 0;
- initList = null;
- RoadManager.Instance.close();
- JinRuRenwu.Instance.gotoNextItem();
- }
- }
- if(line.positionCount > JGIndex)
- RoadManager.Instance.setFX(line.GetPosition(JGIndex));
- }
- }
- }
- }
- public void stop()
- {
- if(RoadManager.Instance!=null&& line!=null)
- {
- JGIndex = 0;
- isStart = false;
- RoadManager.Instance.close();
- line.positionCount = 0;
- }
- }
- public bool isStart;
- List<Vector3> initList;
- public LineRenderer line;
- float MaxL;
- public void setRoad(List<Vector3> lineList)
- {
- JGIndex = 0;
- isStart = true;
- if (!line)
- {
- line =GameObject.Instantiate( WindowsManager.Instance.GetPrefab(WindowConfig.windowType.XunJian,"Line")).GetComponent<LineRenderer>();
- }
- lineList.Insert(0,OpenXRCamera.Instance.head.position);
- initList = lineList;
- for (int i = 0; i < lineList.Count; i++)
- {
- lineList[i] = new Vector3(lineList[i].x, OpenXRCamera.Instance.head.position.y-0.3f, lineList[i].z);
- }
- line.startWidth = 0.025f;
- line.endWidth = 0.025f;
- if(lineList.Count>2)
- {
- List<Vector3> v3 = new List<Vector3>();
- int indexNow=0;
- for (int i = 2; i < lineList.Count; i+=2)
- {
- v3.Add(lineList[i - 2]);
- v3.AddRange(DrawCurve(GetPos(lineList[i - 1],lineList[i-2]), lineList[i - 1], GetPos(lineList[i - 1], lineList[i])));
- v3.Add(lineList[i]);
- indexNow = i;
- }
- for (int j = indexNow+1; j < lineList.Count; j++)
- {
- v3.Add(lineList[j]);
- }
- lineList = v3;
- }
- line.positionCount = lineList.Count;
- line.SetPositions(lineList.ToArray());
- MaxL = 0;
- for (int i = 1; i < lineList.Count; i++)
- {
- MaxL+= Vector3.Distance(lineList[i - 1], lineList[i]);
- }
- initListLine();
- line.materials[0].SetTextureScale("_MainTex", new Vector2(MaxL * 10*4, 1));
- }
- public Vector3 GetPos(Vector3 post1, Vector3 post2)
- {
- Vector3 pos = Vector3.zero;
- float dis = Vector3.Distance(post1, post2);
- Vector3 vector = (post2 - post1).normalized;
- float rand =0.2f;
- pos = vector * rand;
- pos += post1;
- return pos;
- }
- private int _segmentNum = 10;
- List<Vector3> DrawCurve(Vector3 pos1, Vector3 pos2, Vector3 pos3)
- {
- List<Vector3> listPos = new List<Vector3>();
- for (int i = 1; i <= _segmentNum; i++)
- {
- float t = i / (float)_segmentNum;
- int nodeIndex = 0;
- Vector3 pixel = CalculateCubicBezierPoint(t, pos1,
- pos2, pos3);
- listPos.Add(pixel);
- }
- return listPos;
- }
- Vector3 CalculateCubicBezierPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2)
- {
- float u = 1 - t;
- float tt = t * t;
- float uu = u * u;
- Vector3 p = uu * p0;
- p += 2 * u * t * p1;
- p += tt * p2;
- return p;
- }
- int JGIndex;
- void checkPos()
- {
- }
- List<Vector3> poslist = new List<Vector3>();
- private void initListLine()
- {
- poslist = new List<Vector3>();
- LineRenderer lr = line;
- if (lr)
- {
- for (int i = 0; i < lr.positionCount; i++)
- {
- poslist.Add(lr.GetPosition(i));
- }
- }
- }
-
- void Update()
- {
- LineRenderer lr = line;
- if (lr)
- {
- for (int i = 1; i < lr.positionCount; i++)
- {
- Vector3 v3 = DianYunManager.Instance.TestGo.transform.TransformPoint(poslist[i]);
- lr.SetPosition(i, new Vector3(v3.x, OpenXRCamera.Instance.head.position.y - 0.3f, v3.z));
- }
- }
- }
- }
|