blob: 5abe769d4f3d4ad323d0ca0b23e21df14c6b11b7 (
plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
#region UsingStatements
using UnityEngine;
using System;
using System.Collections;
#endregion
/// <summary>
/// - Helper class for storing and manipulating Bezier Point data
/// - Ensures that handles are in correct relation to one another
/// - Handles adding/removing self from curve point lists
/// - Calls SetDirty() on curve when edited
/// </summary>
[Serializable]
public class BezierPoint : MonoBehaviour{
#region PublicEnumerations
/// <summary>
/// - Enumeration describing the relationship between a point's handles
/// - Connected : The point's handles are mirrored across the point
/// - Broken : Each handle moves independently of the other
/// - None : This point has no handles (both handles are located ON the point)
/// </summary>
public enum HandleStyle
{
Connected,
Broken,
None,
}
#endregion
#region PublicProperties
/// <summary>
/// - Curve this point belongs to
/// - Changing this value will automatically remove this point from the current curve and add it to the new one
/// </summary>
[SerializeField]
private BezierCurve _curve;
public BezierCurve curve
{
get{return _curve;}
set
{
if(_curve) _curve.RemovePoint(this);
_curve = value;
_curve.AddPoint(this);
}
}
/// <summary>
/// - Value describing the relationship between this point's handles
/// </summary>
public HandleStyle handleStyle;
/// <summary>
/// - Shortcut to transform.position
/// </summary>
/// <value>
/// - The point's world position
/// </value>
public Vector3 position
{
get { return transform.position; }
set { transform.position = value; }
}
/// <summary>
/// - Shortcut to transform.localPosition
/// </summary>
/// <value>
/// - The point's local position.
/// </value>
public Vector3 localPosition
{
get { return transform.localPosition; }
set { transform.localPosition = value; }
}
/// <summary>
/// - Local position of the first handle
/// - Setting this value will cause the curve to become dirty
/// - This handle effects the curve generated from this point and the point proceeding it in curve.points
/// </summary>
[SerializeField]
private Vector3 _handle1;
public Vector3 handle1
{
get { return _handle1; }
set
{
if(_handle1 == value) return;
_handle1 = value;
if(handleStyle == HandleStyle.None) handleStyle = HandleStyle.Broken;
else if(handleStyle == HandleStyle.Connected) _handle2 = -value;
_curve.SetDirty();
}
}
/// <summary>
/// - Global position of the first handle
/// - Ultimately stored in the 'handle1' variable
/// - Setting this value will cause the curve to become dirty
/// - This handle effects the curve generated from this point and the point proceeding it in curve.points
/// </summary>
public Vector3 globalHandle1
{
get{return transform.TransformPoint(handle1);}
set{handle1 = transform.InverseTransformPoint(value);}
}
/// <summary>
/// - Local position of the second handle
/// - Setting this value will cause the curve to become dirty
/// - This handle effects the curve generated from this point and the point coming after it in curve.points
/// </summary>
[SerializeField]
private Vector3 _handle2;
public Vector3 handle2
{
get { return _handle2; }
set
{
if(_handle2 == value) return;
_handle2 = value;
if(handleStyle == HandleStyle.None) handleStyle = HandleStyle.Broken;
else if(handleStyle == HandleStyle.Connected) _handle1 = -value;
_curve.SetDirty();
}
}
/// <summary>
/// - Global position of the second handle
/// - Ultimately stored in the 'handle2' variable
/// - Setting this value will cause the curve to become dirty
/// - This handle effects the curve generated from this point and the point coming after it in curve.points
/// </summary>
public Vector3 globalHandle2
{
get{return transform.TransformPoint(handle2);}
set{handle2 = transform.InverseTransformPoint(value);}
}
#endregion
#region PrivateVariables
/// <summary>
/// - Used to determine if this point has moved since the last frame
/// </summary>
private Vector3 lastPosition;
#endregion
#region MonoBehaviourFunctions
void Update()
{
if(!_curve.dirty && transform.position != lastPosition)
{
_curve.SetDirty();
lastPosition = transform.position;
}
}
#endregion
}
|