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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
|
#region UsingStatements
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
#endregion
/// <summary>
/// - Class for describing and drawing Bezier Curves
/// - Efficiently handles approximate length calculation through 'dirty' system
/// - Has static functions for getting points on curves constructed by Vector3 parameters (GetPoint, GetCubicPoint, GetQuadraticPoint, and GetLinearPoint)
/// </summary>
[ExecuteInEditMode]
[Serializable]
public class BezierCurve : MonoBehaviour {
#region PublicVariables
/// <summary>
/// - the number of mid-points calculated for each pair of bezier points
/// - used for drawing the curve in the editor
/// - used for calculating the "length" variable
/// </summary>
public int resolution = 30;
/// <summary>
/// Gets or sets a value indicating whether this <see cref="BezierCurve"/> is dirty.
/// </summary>
/// <value>
/// <c>true</c> if dirty; otherwise, <c>false</c>.
/// </value>
public bool dirty { get; private set; }
/// <summary>
/// - color this curve will be drawn with in the editor
/// - set in the editor
/// </summary>
public Color drawColor = Color.white;
#endregion
#region PublicProperties
/// <summary>
/// - set in the editor
/// - used to determine if the curve should be drawn as "closed" in the editor
/// - used to determine if the curve's length should include the curve between the first and the last points in "points" array
/// - setting this value will cause the curve to become dirty
/// </summary>
[SerializeField] private bool _close;
public bool close
{
get { return _close; }
set
{
if(_close == value) return;
_close = value;
dirty = true;
}
}
/// <summary>
/// - set internally
/// - gets point corresponding to "index" in "points" array
/// - does not allow direct set
/// </summary>
/// <param name='index'>
/// - the index
/// </param>
public BezierPoint this[int index]
{
get { return points[index]; }
}
/// <summary>
/// - number of points stored in 'points' variable
/// - set internally
/// - does not include "handles"
/// </summary>
/// <value>
/// - The point count
/// </value>
public int pointCount
{
get { return points.Length; }
}
/// <summary>
/// - The approximate length of the curve
/// - recalculates if the curve is "dirty"
/// </summary>
private float _length;
public float length
{
get
{
if(dirty)
{
_length = 0;
for(int i = 0; i < points.Length - 1; i++){
_length += ApproximateLength(points[i], points[i + 1], resolution);
}
if(close) _length += ApproximateLength(points[points.Length - 1], points[0], resolution);
dirty = false;
}
return _length;
}
}
#endregion
#region PrivateVariables
/// <summary>
/// - Array of point objects that make up this curve
/// - Populated through editor
/// </summary>
[SerializeField] private BezierPoint[] points = new BezierPoint[0];
#endregion
#region UnityFunctions
void OnDrawGizmos () {
Gizmos.color = drawColor;
if(points.Length > 1){
for(int i = 0; i < points.Length - 1; i++){
DrawCurve(points[i], points[i+1], resolution);
}
if (close) DrawCurve(points[points.Length - 1], points[0], resolution);
}
}
void Awake(){
dirty = true;
}
#endregion
#region PublicFunctions
/// <summary>
/// - Adds the given point to the end of the curve ("points" array)
/// </summary>
/// <param name='point'>
/// - The point to add.
/// </param>
public void AddPoint(BezierPoint point)
{
List<BezierPoint> tempArray = new List<BezierPoint>(points);
tempArray.Add(point);
points = tempArray.ToArray();
dirty = true;
}
/// <summary>
/// - Adds a point at position
/// </summary>
/// <returns>
/// - The point object
/// </returns>
/// <param name='position'>
/// - Where to add the point
/// </param>
public BezierPoint AddPointAt(Vector3 position)
{
GameObject pointObject = new GameObject("Point "+pointCount);
pointObject.transform.parent = transform;
pointObject.transform.position = position;
BezierPoint newPoint = pointObject.AddComponent<BezierPoint>();
newPoint.curve = this;
return newPoint;
}
/// <summary>
/// - Removes the given point from the curve ("points" array)
/// </summary>
/// <param name='point'>
/// - The point to remove
/// </param>
public void RemovePoint(BezierPoint point)
{
List<BezierPoint> tempArray = new List<BezierPoint>(points);
tempArray.Remove(point);
points = tempArray.ToArray();
dirty = false;
}
/// <summary>
/// - Gets a copy of the bezier point array used to define this curve
/// </summary>
/// <returns>
/// - The cloned array of points
/// </returns>
public BezierPoint[] GetAnchorPoints()
{
return (BezierPoint[])points.Clone();
}
/// <summary>
/// - Gets the point at 't' percent along this curve
/// </summary>
/// <returns>
/// - Returns the point at 't' percent
/// </returns>
/// <param name='t'>
/// - Value between 0 and 1 representing the percent along the curve (0 = 0%, 1 = 100%)
/// </param>
public Vector3 GetPointAt(float t)
{
if(t <= 0) return points[0].position;
else if (t >= 1) return points[points.Length - 1].position;
float totalPercent = 0;
float curvePercent = 0;
BezierPoint p1 = null;
BezierPoint p2 = null;
for(int i = 0; i < points.Length - 1; i++)
{
curvePercent = ApproximateLength(points[i], points[i + 1], 10) / length;
if(totalPercent + curvePercent > t)
{
p1 = points[i];
p2 = points[i + 1];
break;
}
else totalPercent += curvePercent;
}
if(close && p1 == null)
{
p1 = points[points.Length - 1];
p2 = points[0];
}
t -= totalPercent;
return GetPoint(p1, p2, t / curvePercent);
}
/// <summary>
/// - Get the index of the given point in this curve
/// </summary>
/// <returns>
/// - The index, or -1 if the point is not found
/// </returns>
/// <param name='point'>
/// - Point to search for
/// </param>
public int GetPointIndex(BezierPoint point)
{
int result = -1;
for(int i = 0; i < points.Length; i++)
{
if(points[i] == point)
{
result = i;
break;
}
}
return result;
}
/// <summary>
/// - Sets this curve to 'dirty'
/// - Forces the curve to recalculate its length
/// </summary>
public void SetDirty()
{
dirty = true;
}
#endregion
#region PublicStaticFunctions
/// <summary>
/// - Draws the curve in the Editor
/// </summary>
/// <param name='p1'>
/// - The bezier point at the beginning of the curve
/// </param>
/// <param name='p2'>
/// - The bezier point at the end of the curve
/// </param>
/// <param name='resolution'>
/// - The number of segments along the curve to draw
/// </param>
public static void DrawCurve(BezierPoint p1, BezierPoint p2, int resolution)
{
int limit = resolution+1;
float _res = resolution;
Vector3 lastPoint = p1.position;
Vector3 currentPoint = Vector3.zero;
for(int i = 1; i < limit; i++){
currentPoint = GetPoint(p1, p2, i/_res);
Gizmos.DrawLine(lastPoint, currentPoint);
lastPoint = currentPoint;
}
}
/// <summary>
/// - Gets the point 't' percent along a curve
/// - Automatically calculates for the number of relevant points
/// </summary>
/// <returns>
/// - The point 't' percent along the curve
/// </returns>
/// <param name='p1'>
/// - The bezier point at the beginning of the curve
/// </param>
/// <param name='p2'>
/// - The bezier point at the end of the curve
/// </param>
/// <param name='t'>
/// - Value between 0 and 1 representing the percent along the curve (0 = 0%, 1 = 100%)
/// </param>
public static Vector3 GetPoint(BezierPoint p1, BezierPoint p2, float t)
{
if(p1.handle2 != Vector3.zero)
{
if(p2.handle1 != Vector3.zero) return GetCubicCurvePoint(p1.position, p1.globalHandle2, p2.globalHandle1, p2.position, t);
else return GetQuadraticCurvePoint(p1.position, p1.globalHandle2, p2.position, t);
}
else
{
if(p2.handle1 != Vector3.zero) return GetQuadraticCurvePoint(p1.position, p2.globalHandle1, p2.position, t);
else return GetLinearPoint(p1.position, p2.position, t);
}
}
/// <summary>
/// - Gets the point 't' percent along a third-order curve
/// </summary>
/// <returns>
/// - The point 't' percent along the curve
/// </returns>
/// <param name='p1'>
/// - The point at the beginning of the curve
/// </param>
/// <param name='p2'>
/// - The second point along the curve
/// </param>
/// <param name='p3'>
/// - The third point along the curve
/// </param>
/// <param name='p4'>
/// - The point at the end of the curve
/// </param>
/// <param name='t'>
/// - Value between 0 and 1 representing the percent along the curve (0 = 0%, 1 = 100%)
/// </param>
public static Vector3 GetCubicCurvePoint(Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4, float t)
{
t = Mathf.Clamp01(t);
Vector3 part1 = Mathf.Pow(1 - t, 3) * p1;
Vector3 part2 = 3 * Mathf.Pow(1 - t, 2) * t * p2;
Vector3 part3 = 3 * (1 - t) * Mathf.Pow(t, 2) * p3;
Vector3 part4 = Mathf.Pow(t, 3) * p4;
return part1 + part2 + part3 + part4;
}
/// <summary>
/// - Gets the point 't' percent along a second-order curve
/// </summary>
/// <returns>
/// - The point 't' percent along the curve
/// </returns>
/// <param name='p1'>
/// - The point at the beginning of the curve
/// </param>
/// <param name='p2'>
/// - The second point along the curve
/// </param>
/// <param name='p3'>
/// - The point at the end of the curve
/// </param>
/// <param name='t'>
/// - Value between 0 and 1 representing the percent along the curve (0 = 0%, 1 = 100%)
/// </param>
public static Vector3 GetQuadraticCurvePoint(Vector3 p1, Vector3 p2, Vector3 p3, float t)
{
t = Mathf.Clamp01(t);
Vector3 part1 = Mathf.Pow(1 - t, 2) * p1;
Vector3 part2 = 2 * (1 - t) * t * p2;
Vector3 part3 = Mathf.Pow(t, 2) * p3;
return part1 + part2 + part3;
}
/// <summary>
/// - Gets point 't' percent along a linear "curve" (line)
/// - This is exactly equivalent to Vector3.Lerp
/// </summary>
/// <returns>
/// - The point 't' percent along the curve
/// </returns>
/// <param name='p1'>
/// - The point at the beginning of the line
/// </param>
/// <param name='p2'>
/// - The point at the end of the line
/// </param>
/// <param name='t'>
/// - Value between 0 and 1 representing the percent along the line (0 = 0%, 1 = 100%)
/// </param>
public static Vector3 GetLinearPoint(Vector3 p1, Vector3 p2, float t)
{
return p1 + ((p2 - p1) * t);
}
/// <summary>
/// - Gets point 't' percent along n-order curve
/// </summary>
/// <returns>
/// - The point 't' percent along the curve
/// </returns>
/// <param name='t'>
/// - Value between 0 and 1 representing the percent along the curve (0 = 0%, 1 = 100%)
/// </param>
/// <param name='points'>
/// - The points used to define the curve
/// </param>
public static Vector3 GetPoint(float t, params Vector3[] points){
t = Mathf.Clamp01(t);
int order = points.Length-1;
Vector3 point = Vector3.zero;
Vector3 vectorToAdd;
for(int i = 0; i < points.Length; i++){
vectorToAdd = points[points.Length-i-1] * (BinomialCoefficient(i, order) * Mathf.Pow(t, order-i) * Mathf.Pow((1-t), i));
point += vectorToAdd;
}
return point;
}
/// <summary>
/// - Approximates the length
/// </summary>
/// <returns>
/// - The approximate length
/// </returns>
/// <param name='p1'>
/// - The bezier point at the start of the curve
/// </param>
/// <param name='p2'>
/// - The bezier point at the end of the curve
/// </param>
/// <param name='resolution'>
/// - The number of points along the curve used to create measurable segments
/// </param>
public static float ApproximateLength(BezierPoint p1, BezierPoint p2, int resolution = 10)
{
float _res = resolution;
float total = 0;
Vector3 lastPosition = p1.position;
Vector3 currentPosition;
for(int i = 0; i < resolution + 1; i++)
{
currentPosition = GetPoint(p1, p2, i / _res);
total += (currentPosition - lastPosition).magnitude;
lastPosition = currentPosition;
}
return total;
}
#endregion
#region UtilityFunctions
private static int BinomialCoefficient(int i, int n){
return Factoral(n)/(Factoral(i)*Factoral(n-i));
}
private static int Factoral(int i){
if(i == 0) return 1;
int total = 1;
while(i-1 >= 0){
total *= i;
i--;
}
return total;
}
#endregion
/* needs testing
public Vector3 GetPointAtDistance(float distance)
{
if(close)
{
if(distance < 0) while(distance < 0) { distance += length; }
else if(distance > length) while(distance > length) { distance -= length; }
}
else
{
if(distance <= 0) return points[0].position;
else if(distance >= length) return points[points.Length - 1].position;
}
float totalLength = 0;
float curveLength = 0;
BezierPoint firstPoint = null;
BezierPoint secondPoint = null;
for(int i = 0; i < points.Length - 1; i++)
{
curveLength = ApproximateLength(points[i], points[i + 1], resolution);
if(totalLength + curveLength >= distance)
{
firstPoint = points[i];
secondPoint = points[i+1];
break;
}
else totalLength += curveLength;
}
if(firstPoint == null)
{
firstPoint = points[points.Length - 1];
secondPoint = points[0];
curveLength = ApproximateLength(firstPoint, secondPoint, resolution);
}
distance -= totalLength;
return GetPoint(distance / curveLength, firstPoint, secondPoint);
}
*/
}
|