Tanoda
CUIBezierCurve.cs
Go to the documentation of this file.
1
3
5{
9 public class CUIBezierCurve : MonoBehaviour
10 {
11 public readonly static int CubicBezierCurvePtNum = 4;
12
13 #region Descriptions
14
15 [SerializeField]
16 protected Vector3[] controlPoints;
17
18 public Vector3[] ControlPoints
19 {
20 get
21 {
22 return controlPoints;
23 }
24
25 }
26
27#if UNITY_EDITOR
31 public Vector3[] EDITOR_ControlPoints
32 {
33 set
34 {
35 controlPoints = value;
36 }
37 }
38#endif
39
40 #endregion
41
42 #region Events
43
44#if UNITY_EDITOR
45 protected void OnValidate()
46 {
47 Refresh();
48 }
49#endif
50
51 public void Refresh()
52 {
53
54 if (OnRefresh != null)
55 OnRefresh();
56 }
57
58 #endregion
59 #region Services
60
66 public Vector3 GetPoint(float _time)
67 {
68 float oneMinusTime = 1 - _time;
69
70 return oneMinusTime * oneMinusTime * oneMinusTime * controlPoints[0] +
71 3 * oneMinusTime * oneMinusTime * _time * controlPoints[1] +
72 3 * oneMinusTime * _time * _time * controlPoints[2] +
73 _time * _time * _time * controlPoints[3];
74 }
75
76 public Vector3 GetTangent(float _time)
77 {
78 float oneMinusTime = 1 - _time;
79
80 return 3 * oneMinusTime * oneMinusTime * (controlPoints[1] - controlPoints[0]) +
81 6 * oneMinusTime * _time * (controlPoints[2] - controlPoints[1]) +
82 3 * _time * _time * (controlPoints[3] - controlPoints[2]);
83 }
84
85 #endregion
86
87 #region Configurations
88
89 public void ReportSet()
90 {
91 if (controlPoints == null)
92 {
94 controlPoints[0] = new Vector3(0, 0, 0);
95 controlPoints[1] = new Vector3(0, 1, 0);
96 controlPoints[2] = new Vector3(1, 1, 0);
97 controlPoints[3] = new Vector3(1, 0, 0);
98 }
99
100 bool isPointsReady = true;
101
102 isPointsReady = isPointsReady & controlPoints.Length == CUIBezierCurve.CubicBezierCurvePtNum;
103 }
104 #endregion
105
106
107 #region Services
108
109 public System.Action OnRefresh;
110
111
112
113 #endregion
114
115 }
116}
Assume to be a cubic bezier curve at the moment.
Vector3 GetPoint(float _time)
call this to get a sample
Credit Erdener Gonenc - @PixelEnvision.