-
Notifications
You must be signed in to change notification settings - Fork 0
/
SplineTools.cs
268 lines (230 loc) · 9.6 KB
/
SplineTools.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
using UnityEngine;
namespace vSplines
{
public static class SplineTools
{
private static Matrix4x4 GetGlobalTRS(this Transform transform)
{
return Matrix4x4.TRS(transform.position, transform.rotation, transform.lossyScale);
}
private static Matrix4x4 GetLocalTRS(this Transform transform)
{
return Matrix4x4.TRS(transform.localPosition, transform.localRotation, transform.localScale);
}
private static Quaternion GetRotation(this Matrix4x4 m)
{
return Quaternion.LookRotation(m.GetColumn(2), m.GetColumn(1));
}
private static Vector2 xz(this Vector3 obj)
{
return new Vector2(obj.x, obj.z);
}
public static Vector3 GetTangent(this SplineSegment s, float naturalT)
{
var up = s.GetUpVector(naturalT);
var rot = s.GetRotation(naturalT);
up = rot * up;
return Vector3.Cross(s.GetDeltaOnSplineSegment(naturalT), up);
}
public static Quaternion GetRotation(this SplineSegment s, float naturalT)
{
return Quaternion.Euler(Vector3.Lerp(s.FirstControlPoint.Rotation, s.SecondControlPoint.Rotation, naturalT));
}
public static Vector3 GetUpVector(this SplineSegment s, float naturalT)
{
return Vector3.Lerp(s.FirstControlPoint.UpVector, s.SecondControlPoint.UpVector, naturalT);
}
public static Vector3 GetDeltaOnSplineSegment(this SplineSegment s, float naturalT)
{
var p0 = s.FirstControlPoint.Position;
var p1 = s.FirstControlPoint.Position + s.FirstControlPoint.Control;
var p2 = s.SecondControlPoint.Position + s.SecondControlPoint.Control;
var p3 = s.SecondControlPoint.Position;
var a = 3 * Mathf.Pow(1 - naturalT, 2) * (p1 - p0);
var b = 6 * (1 - naturalT) * naturalT * (p2 - p1);
var c = 3 * Mathf.Pow(naturalT, 2) * (p3 - p2);
return a + b + c;
}
public static float NaturalToUniformTime(this SplineSegment s, float naturalT)
{
for (var i = 1; i < s.Points.Count; i++)
{
var thisPoint = s.Points[i];
var thisNT = thisPoint.NaturalTime;
if (naturalT > thisNT)
{
continue;
}
var lastPoint = s.Points[i - 1];
var lastNT = lastPoint.NaturalTime;
var frac = naturalT - lastNT;
var lerp = frac / (thisNT - lastNT);
return Mathf.Lerp(lastPoint.AccumLength / s.Length, thisPoint.AccumLength / s.Length, lerp);
}
return 1;
}
public static float UniformToNaturalTime(this SplineSegment s, float uniformT)
{
var length = s.Length;
for (var i = 1; i < s.Points.Count; i++)
{
var thisPoint = s.Points[i];
var thisUt = thisPoint.AccumLength / length;
if (uniformT > thisUt)
{
continue;
}
var lastPoint = s.Points[i - 1];
var lastUt = lastPoint.AccumLength / length;
var frac = uniformT - lastUt;
var lerp = frac / (thisUt - lastUt);
return Mathf.Lerp(lastPoint.NaturalTime, thisPoint.NaturalTime, lerp);
}
return 1;
}
public static Vector3 GetNaturalPointOnSplineSegment(this SplineSegment s, float naturalT)
{
var p0 = s.FirstControlPoint.Position;
var p1 = s.FirstControlPoint.Position + s.FirstControlPoint.Control;
var p2 = s.SecondControlPoint.Position + s.SecondControlPoint.Control;
var p3 = s.SecondControlPoint.Position;
var t2 = naturalT * naturalT;
var t3 = naturalT * t2;
var mt = 1 - naturalT;
var mt2 = mt * mt;
var mt3 = mt * mt2;
return p0 * mt3 + 3 * p1 * mt2 * naturalT + 3 * p2 * mt * t2 + p3 * t3;
}
public static Vector3 GetUniformPointOnSplineSegment(this SplineSegment s, float uniformTime)
{
var totalDist = s.Length;
// March forward along the spline
for (var i = 0; i < s.Points.Count; i++)
{
var thisPoint = s.Points[i];
var percentageThrough = thisPoint.AccumLength / totalDist;
if (percentageThrough >= uniformTime)
{
// We've found our point...
if (i == 0)
{
return s.Points[i].Position;
}
// Interpolate - tricky!
var lastPoint = s.Points[i - 1];
var lastPercent = lastPoint.AccumLength / totalDist;
var percentDelta = (uniformTime - lastPercent) / (percentageThrough - lastPercent);
return Vector3.Lerp(lastPoint.Position, thisPoint.Position, percentDelta);
}
}
return s.Points[s.Points.Count - 1].Position;
}
public static void ApplyMatrix(this SplineSegment spline, Matrix4x4 mat)
{
spline.FirstControlPoint.ApplyMatrix(mat);
spline.SecondControlPoint.ApplyMatrix(mat);
for (int i = 0; i < spline.Points.Count; i++)
{
var splinePoint = spline.Points[i];
splinePoint.Position = mat.MultiplyPoint3x4(splinePoint.Position);
spline.Points[i] = splinePoint;
}
}
public static void ApplyMatrix(this SplineSegment.ControlPoint point, Matrix4x4 mat)
{
point.Position = mat.MultiplyPoint3x4(point.Position);
point.Control = mat.MultiplyVector(point.Control);
point.Rotation = (mat.GetRotation() * Quaternion.Euler(point.Rotation)).eulerAngles;
}
/// <summary>
/// kind of a complex problem - this is a relatively naive and slow solution that will only find an approximation
/// and will NOT work for extremely distorted curves
/// </summary>
/// <param name="s"></param>
/// <param name="worldPosxz"></param>
/// <returns></returns>
public static float GetClosestUniformTimeOnSplineSegmentXZ(this SplineSegment s, Vector2 worldPosxz/*, float threshold*/)
{
//Profiler.BeginSample("GetClosestUniformTimeOnSplineXZ");
float bestTime = 0;
float bestDist = float.MaxValue;
foreach (var splinePoint in s.Points)
{
var sqrDist = (splinePoint.Position.xz() - worldPosxz).sqrMagnitude;
if (sqrDist < bestDist)
{
bestDist = sqrDist;
bestTime = splinePoint.UniformTime;
}
}
//Profiler.EndSample();
return bestTime;
}
public static float GetClosestUniformTimeOnSplineSegment(this SplineSegment s, Vector3 worldPos, float threshold)
{
// Basically a binary search along a spline
var rangeT = new Vector2(0, 1);
while (true)
{
var midPointT = Mathf.Lerp(rangeT.x, rangeT.y, .5f);
var minPos = s.GetUniformPointOnSplineSegment(rangeT.x);
var midPos = s.GetUniformPointOnSplineSegment(midPointT);
var maxPos = s.GetUniformPointOnSplineSegment(rangeT.y);
var firstDist = (minPos - worldPos).magnitude;
var midDist = (midPos - worldPos).magnitude;
var secondDist = (maxPos - worldPos).magnitude;
var firstSegment = firstDist + midDist;
var secondSegment = midDist + secondDist;
// The current candidate is good enough
if (Mathf.Abs(firstDist - secondDist) < threshold)
{
return midPointT;
}
if (firstSegment < secondSegment)
{
rangeT = new Vector2(rangeT.x, midPointT);
}
else
{
rangeT = new Vector2(midPointT, rangeT.y);
}
}
}
public static Vector3 GetDistancePointAlongSplineSegment(this SplineSegment s, float distance)
{
if (distance >= s.Length)
{
return s.SecondControlPoint.Position;
}
else if (distance <= 0)
{
return s.FirstControlPoint.Position;
}
var t = distance / s.Length;
return s.GetUniformPointOnSplineSegment(t);
}
public static Vector3 GetDistancePointAlongSpline(this Spline s, float distance)
{
if (distance >= s.Length)
{
return s.End;
}
else if (distance <= 0)
{
return s.Start;
}
var lengthAccum = 0f;
foreach (var segment in s.Segments)
{
if (lengthAccum + segment.Length <= distance)
{
lengthAccum += segment.Length;
continue;
}
var fracLength = distance - lengthAccum;
return segment.GetDistancePointAlongSplineSegment(fracLength);
}
throw new System.Exception("Finding point on spline failed unexpectedly.");
}
}
}