Skip to content

Commit 99f3275

Browse files
committed
- Improved UX for transforming selected points together via Shift key
- Selected points can now be transformed together in 2 ways: in the same direction or in opposite directions (toggle with C key) - Added "Preserve Shape" option to "Insert Point Before" and "Insert Point After" actions (spline's shape will be preserved but if neighboring end points' Handle Modes are set to Mirrored, they will be changed to Aligned) - Added "Show Control Points" option to spline's Inspector to hide the control points completely, if desired
1 parent 54eded1 commit 99f3275

File tree

3 files changed

+348
-126
lines changed

3 files changed

+348
-126
lines changed

Plugins/BezierSolution/BezierSpline.cs

Lines changed: 19 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,9 @@ public int gizmoSmoothness
5656
#endif
5757

5858
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]; } }
6060

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 ); } }
7262

7363
private void Awake()
7464
{
@@ -157,7 +147,7 @@ public BezierPoint InsertNewPointAt( int index )
157147
point.transform.SetParent( parent, false );
158148
point.transform.SetSiblingIndex( siblingIndex );
159149

160-
if( endPoints.Count == prevCount ) // If spline is not automatically Refresh()'ed
150+
if( endPoints.Count == prevCount ) // If spline isn't automatically Refresh()'ed
161151
endPoints.Insert( index, point );
162152

163153
return point;
@@ -564,9 +554,7 @@ public void AutoConstructSpline()
564554
rhs = new Vector3[n];
565555

566556
for( int i = 1; i < n - 1; i++ )
567-
{
568557
rhs[i] = 4 * endPoints[i].position + 2 * endPoints[i + 1].position;
569-
}
570558

571559
rhs[0] = endPoints[0].position + 2 * endPoints[1].position;
572560

@@ -579,17 +567,30 @@ public void AutoConstructSpline()
579567
}
580568

581569
// 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
583586

584587
for( int i = 0; i < n; i++ )
585588
{
586589
// First control point
587590
endPoints[i].followingControlPointPosition = controlPoints[i];
588591

589592
if( loop )
590-
{
591593
endPoints[i + 1].precedingControlPointPosition = 2 * endPoints[i + 1].position - controlPoints[i + 1];
592-
}
593594
else
594595
{
595596
// Second control point
@@ -609,32 +610,6 @@ public void AutoConstructSpline()
609610
}
610611
}
611612

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-
638613
public void AutoConstructSpline2()
639614
{
640615
// 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()
651626
pMinus1 = endPoints[0].position;
652627
}
653628
else
654-
{
655629
pMinus1 = endPoints[i - 1].position;
656-
}
657630

658631
if( loop )
659632
{
@@ -689,11 +662,6 @@ public void AutoConstructSpline2()
689662
}
690663
}
691664

692-
/*public void AutoConstructSpline3()
693-
{
694-
// Todo? http://www.math.ucla.edu/~baker/149.1.02w/handouts/dd_splines.pdf
695-
}*/
696-
697665
private float AccuracyToStepSize( float accuracy )
698666
{
699667
if( accuracy <= 0f )

0 commit comments

Comments
 (0)