-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
LTBezierPath.cs
94 lines (81 loc) · 2 KB
/
LTBezierPath.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using UnityEngine;
public class LTBezierPath
{
public Vector3[] pts;
public float length;
public bool orientToPath;
private LTBezier[] beziers;
private float[] lengthRatio;
public LTBezierPath()
{
}
public LTBezierPath(Vector3[] pts_)
{
setPoints(pts_);
}
public void setPoints(Vector3[] pts_)
{
if (pts_.Length < 4)
{
LeanTween.logError("LeanTween - When passing values for a vector path, you must pass four or more values!");
}
if (pts_.Length % 4 != 0)
{
LeanTween.logError("LeanTween - When passing values for a vector path, they must be in sets of four: controlPoint1, controlPoint2, endPoint2, controlPoint2, controlPoint2...");
}
pts = pts_;
int num = 0;
beziers = new LTBezier[pts.Length / 4];
lengthRatio = new float[beziers.Length];
length = 0f;
for (int i = 0; i < pts.Length; i += 4)
{
beziers[num] = new LTBezier(pts[i], pts[i + 2], pts[i + 1], pts[i + 3], 0.05f);
length += beziers[num].length;
num++;
}
for (int i = 0; i < beziers.Length; i++)
{
lengthRatio[i] = beziers[i].length / length;
}
}
public Vector3 point(float ratio)
{
float num = 0f;
for (int i = 0; i < lengthRatio.Length; i++)
{
num += lengthRatio[i];
if (num >= ratio)
{
return beziers[i].point((ratio - (num - lengthRatio[i])) / lengthRatio[i]);
}
}
return beziers[lengthRatio.Length - 1].point(1f);
}
public void place(Transform transform, float ratio)
{
place(transform, ratio, Vector3.up);
}
public void place(Transform transform, float ratio, Vector3 worldUp)
{
transform.position = point(ratio);
ratio += 0.001f;
if (ratio <= 1f)
{
transform.LookAt(point(ratio), worldUp);
}
}
public void placeLocal(Transform transform, float ratio)
{
placeLocal(transform, ratio, Vector3.up);
}
public void placeLocal(Transform transform, float ratio, Vector3 worldUp)
{
transform.localPosition = point(ratio);
ratio += 0.001f;
if (ratio <= 1f)
{
transform.LookAt(transform.parent.TransformPoint(point(ratio)), worldUp);
}
}
}