|
@@ -4,7 +4,7 @@ using System.Collections;
|
|
|
|
|
|
public class Bezier : MonoSingleton<Bezier>
|
|
|
{
|
|
|
- public Transform[] controlPoints;
|
|
|
+
|
|
|
public LineRenderer lineRenderer;
|
|
|
|
|
|
private int layerOrder = 0;
|
|
@@ -33,18 +33,24 @@ public class Bezier : MonoSingleton<Bezier>
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
|
+ StartCoroutine(SetOffset());
|
|
|
+ }
|
|
|
+
|
|
|
+ public void SetRoute(List<Vector3> listPos)
|
|
|
+ {
|
|
|
int index = 0;
|
|
|
+
|
|
|
+ RouteClear();
|
|
|
+ lineRenderer.positionCount = (listPos.Count - 2) * _segmentNum + listPos.Count;
|
|
|
Debug.Log(lineRenderer.positionCount);
|
|
|
- lineRenderer.positionCount = (controlPoints.Length - 2) * _segmentNum + controlPoints.Length;
|
|
|
- lineRenderer.SetPosition(index++, controlPoints[0].position);
|
|
|
- for (int i = 1; i < controlPoints.Length-1; i++)
|
|
|
+ lineRenderer.SetPosition(index++, listPos[0]);
|
|
|
+ for (int i = 1; i < listPos.Count - 1; i++)
|
|
|
{
|
|
|
|
|
|
-
|
|
|
-
|
|
|
- point.position = controlPoints[i].position;
|
|
|
- point.LookAt(controlPoints[i-1].position);
|
|
|
- point.Translate(0,0, rr, Space.Self);
|
|
|
+ point.position = listPos[i];
|
|
|
+ point.LookAt(listPos[i - 1]);
|
|
|
+ point.Translate(0, 0, rr, Space.Self);
|
|
|
Vector3 pos1 = point.position;
|
|
|
lineRenderer.SetPosition(index++, pos1);
|
|
|
|
|
@@ -54,39 +60,38 @@ public class Bezier : MonoSingleton<Bezier>
|
|
|
|
|
|
|
|
|
|
|
|
- point.position = controlPoints[i].position;
|
|
|
- point.LookAt(controlPoints[i+1].position);
|
|
|
- point.Translate(0, 0, rr+0.05f, Space.Self);
|
|
|
+ point.position = listPos[i];
|
|
|
+ point.LookAt(listPos[i + 1]);
|
|
|
+ point.Translate(0, 0, rr + 0.05f, Space.Self);
|
|
|
Vector3 pos2 = point.position;
|
|
|
|
|
|
Debug.Log(pos1 + " " + point.position + " " + pos2);
|
|
|
|
|
|
- List<Vector3> listPos = DrawCurve(pos1, controlPoints[i].position, pos2);
|
|
|
- for (int j = 0; j < listPos.Count; j++)
|
|
|
+ List<Vector3> listLinePos = DrawCurve(pos1, listPos[i], pos2);
|
|
|
+ for (int j = 0; j < listLinePos.Count; j++)
|
|
|
{
|
|
|
- lineRenderer.SetPosition(index++, listPos[j]);
|
|
|
+ lineRenderer.SetPosition(index++, listLinePos[j]);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
- lineRenderer.SetPosition(index++, controlPoints[controlPoints.Length-1].position);
|
|
|
+ lineRenderer.SetPosition(index++, listPos[listPos.Count - 1]);
|
|
|
|
|
|
float dis = 0;
|
|
|
- for (int i = 0; i < lineRenderer.positionCount; i++)
|
|
|
+ for (int i = 0; i < lineRenderer.positionCount-1; i++)
|
|
|
{
|
|
|
dis += Vector3.Distance(lineRenderer.GetPosition(i), lineRenderer.GetPosition(i + 1));
|
|
|
}
|
|
|
|
|
|
- material.SetTextureScale("_MainTex", new Vector2(dis*4, 1f));
|
|
|
- StartCoroutine(SetOffset());
|
|
|
+ material.SetTextureScale("_MainTex", new Vector2(dis * 8, 1f));
|
|
|
+
|
|
|
}
|
|
|
|
|
|
- public void SetRoute()
|
|
|
+ public void RouteClear()
|
|
|
{
|
|
|
-
|
|
|
+ lineRenderer.positionCount = 0;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
private IEnumerator SetOffset()
|
|
|
{
|
|
|
while (true)
|
|
@@ -107,15 +112,15 @@ public class Bezier : MonoSingleton<Bezier>
|
|
|
|
|
|
void DrawCurve()
|
|
|
{
|
|
|
- for (int i = 1; i <= _segmentNum; i++)
|
|
|
- {
|
|
|
- float t = i / (float)_segmentNum;
|
|
|
- int nodeIndex = 0;
|
|
|
- Vector3 pixel = CalculateCubicBezierPoint(t, controlPoints[nodeIndex].position,
|
|
|
- controlPoints[nodeIndex + 1].position, controlPoints[nodeIndex + 2].position);
|
|
|
- lineRenderer.positionCount = i;
|
|
|
- lineRenderer.SetPosition(i - 1, pixel);
|
|
|
- }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
}
|
|
|
|