@@ -56,19 +56,9 @@ public int gizmoSmoothness
56
56
#endif
57
57
58
58
public int Count { get { return endPoints . Count ; } }
59
- public float Length { get { return GetLengthApproximately ( 0f , 1f ) ; } }
59
+ public BezierPoint this [ int index ] { get { return endPoints [ index ] ; } }
60
60
61
- public BezierPoint this [ int index ]
62
- {
63
- get
64
- {
65
- if ( index < Count )
66
- return endPoints [ index ] ;
67
-
68
- Debug . LogError ( "Bezier index " + index + " is out of range: " + Count ) ;
69
- return null ;
70
- }
71
- }
61
+ public float Length { get { return GetLengthApproximately ( 0f , 1f ) ; } }
72
62
73
63
private void Awake ( )
74
64
{
@@ -157,7 +147,7 @@ public BezierPoint InsertNewPointAt( int index )
157
147
point . transform . SetParent ( parent , false ) ;
158
148
point . transform . SetSiblingIndex ( siblingIndex ) ;
159
149
160
- if ( endPoints . Count == prevCount ) // If spline is not automatically Refresh()'ed
150
+ if ( endPoints . Count == prevCount ) // If spline isn't automatically Refresh()'ed
161
151
endPoints . Insert ( index , point ) ;
162
152
163
153
return point ;
@@ -564,9 +554,7 @@ public void AutoConstructSpline()
564
554
rhs = new Vector3 [ n ] ;
565
555
566
556
for ( int i = 1 ; i < n - 1 ; i ++ )
567
- {
568
557
rhs [ i ] = 4 * endPoints [ i ] . position + 2 * endPoints [ i + 1 ] . position ;
569
- }
570
558
571
559
rhs [ 0 ] = endPoints [ 0 ] . position + 2 * endPoints [ 1 ] . position ;
572
560
@@ -579,17 +567,30 @@ public void AutoConstructSpline()
579
567
}
580
568
581
569
// Get first control points
582
- Vector3 [ ] controlPoints = GetFirstControlPoints ( rhs ) ;
570
+ int rhsLength = rhs . Length ;
571
+ Vector3 [ ] controlPoints = new Vector3 [ rhsLength ] ; // Solution vector
572
+ float [ ] tmp = new float [ rhsLength ] ; // Temp workspace
573
+
574
+ float b = 2f ;
575
+ controlPoints [ 0 ] = rhs [ 0 ] / b ;
576
+ for ( int i = 1 ; i < rhsLength ; i ++ ) // Decomposition and forward substitution
577
+ {
578
+ float val = 1f / b ;
579
+ tmp [ i ] = val ;
580
+ b = ( i < rhsLength - 1 ? 4f : 3.5f ) - val ;
581
+ controlPoints [ i ] = ( rhs [ i ] - controlPoints [ i - 1 ] ) / b ;
582
+ }
583
+
584
+ for ( int i = 1 ; i < rhsLength ; i ++ )
585
+ controlPoints [ rhsLength - i - 1 ] -= tmp [ rhsLength - i ] * controlPoints [ rhsLength - i ] ; // Backsubstitution
583
586
584
587
for ( int i = 0 ; i < n ; i ++ )
585
588
{
586
589
// First control point
587
590
endPoints [ i ] . followingControlPointPosition = controlPoints [ i ] ;
588
591
589
592
if ( loop )
590
- {
591
593
endPoints [ i + 1 ] . precedingControlPointPosition = 2 * endPoints [ i + 1 ] . position - controlPoints [ i + 1 ] ;
592
- }
593
594
else
594
595
{
595
596
// Second control point
@@ -609,32 +610,6 @@ public void AutoConstructSpline()
609
610
}
610
611
}
611
612
612
- private static Vector3 [ ] GetFirstControlPoints ( Vector3 [ ] rhs )
613
- {
614
- // Credit: http://www.codeproject.com/Articles/31859/Draw-a-Smooth-Curve-through-a-Set-of-2D-Points-wit
615
-
616
- int n = rhs . Length ;
617
- Vector3 [ ] x = new Vector3 [ n ] ; // Solution vector.
618
- float [ ] tmp = new float [ n ] ; // Temp workspace.
619
-
620
- float b = 2f ;
621
- x [ 0 ] = rhs [ 0 ] / b ;
622
- for ( int i = 1 ; i < n ; i ++ ) // Decomposition and forward substitution.
623
- {
624
- float val = 1f / b ;
625
- tmp [ i ] = val ;
626
- b = ( i < n - 1 ? 4f : 3.5f ) - val ;
627
- x [ i ] = ( rhs [ i ] - x [ i - 1 ] ) / b ;
628
- }
629
-
630
- for ( int i = 1 ; i < n ; i ++ )
631
- {
632
- x [ n - i - 1 ] -= tmp [ n - i ] * x [ n - i ] ; // Backsubstitution.
633
- }
634
-
635
- return x ;
636
- }
637
-
638
613
public void AutoConstructSpline2 ( )
639
614
{
640
615
// Credit: http://stackoverflow.com/questions/3526940/how-to-create-a-cubic-bezier-curve-when-given-n-points-in-3d
@@ -651,9 +626,7 @@ public void AutoConstructSpline2()
651
626
pMinus1 = endPoints [ 0 ] . position ;
652
627
}
653
628
else
654
- {
655
629
pMinus1 = endPoints [ i - 1 ] . position ;
656
- }
657
630
658
631
if ( loop )
659
632
{
@@ -689,11 +662,6 @@ public void AutoConstructSpline2()
689
662
}
690
663
}
691
664
692
- /*public void AutoConstructSpline3()
693
- {
694
- // Todo? http://www.math.ucla.edu/~baker/149.1.02w/handouts/dd_splines.pdf
695
- }*/
696
-
697
665
private float AccuracyToStepSize ( float accuracy )
698
666
{
699
667
if ( accuracy <= 0f )
0 commit comments