123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- 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 (initList != null && initList.Count > 0)
- {
- Vector3 v3 = new Vector3(OpenXRCamera.Instance.head.position.x, initList[JGIndex].y, OpenXRCamera.Instance.head.position.z);
- if (Vector3.Distance(v3, initList[JGIndex]) <= 0.2f)
- {
- JGIndex++;
- if (JGIndex >= initList.Count)
- {
- JGIndex = initList.Count - 1;
- }
- }
- RoadManager.Instance.setFX(initList[JGIndex]);
- }
- }
- }
- }
- public void stop()
- {
- 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>();
- 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]);
- }
- 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]);
- }
- 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()
- {
- }
- }
|