1
1
using UnityEngine ;
2
2
using System . Collections . Generic ;
3
- using UnityX . Geometry ;
4
3
5
4
namespace SplineSystem {
6
5
public delegate void OnSplineChangeEvent ( Spline spline ) ;
@@ -50,7 +49,10 @@ public float length {
50
49
}
51
50
}
52
51
public Bounds bounds ;
53
-
52
+
53
+ public Spline ( ) {
54
+ Validate ( ) ;
55
+ }
54
56
public Spline ( params SplineBezierPoint [ ] bezierPoints ) {
55
57
this . bezierPoints = bezierPoints ;
56
58
RefreshCurveData ( ) ;
@@ -148,7 +150,7 @@ public static Spline CreateFromPoints (IList<Vector3> points, IList<Quaternion>
148
150
}
149
151
150
152
public void RefreshCurveData ( ) {
151
- if ( bezierPoints . Length <= 1 ) return ;
153
+ if ( bezierPoints == null || bezierPoints . Length <= 1 ) return ;
152
154
if ( curves == null || curves . Length != bezierPoints . Length - 1 ) curves = new SplineBezierCurve [ bezierPoints . Length - 1 ] ;
153
155
for ( int i = 0 ; i < bezierPoints . Length - 1 ; i ++ ) curves [ i ] = new SplineBezierCurve ( bezierPoints [ i ] , bezierPoints [ i + 1 ] ) ;
154
156
@@ -392,8 +394,8 @@ float EstimateArcLengthAlongCurve (Vector3 position, SplineBezierCurve bestCurve
392
394
var leftPoint = bestCurve . GetPointAtT ( leftT ) ;
393
395
var rightPoint = bestCurve . GetPointAtT ( rightT ) ;
394
396
395
- closestDistanceAlongLeftLine = Line3D . GetNormalizedDistanceOnLine ( leftPoint , centerPoint , position , clampAtStart ) ;
396
- closestDistanceAlongRightLine = Line3D . GetNormalizedDistanceOnLine ( centerPoint , rightPoint , position , clampAtEnd ) ;
397
+ closestDistanceAlongLeftLine = GetNormalizedDistanceOnLine ( leftPoint , centerPoint , position , clampAtStart ) ;
398
+ closestDistanceAlongRightLine = GetNormalizedDistanceOnLine ( centerPoint , rightPoint , position , clampAtEnd ) ;
397
399
float closestDistanceOnLineA = SqrDistance ( position , Vector3 . LerpUnclamped ( leftPoint , centerPoint , closestDistanceAlongLeftLine ) ) ;
398
400
float closestDistanceOnLineB = SqrDistance ( position , Vector3 . LerpUnclamped ( centerPoint , rightPoint , closestDistanceAlongRightLine ) ) ;
399
401
var arcLengthOffsetMultiplier = 0f ;
@@ -478,8 +480,8 @@ public bool Validate () {
478
480
quality = defaultQuality ;
479
481
didChange = true ;
480
482
}
481
- if ( bezierPoints . Length < 2 ) {
482
- this . bezierPoints = new SplineBezierPoint [ ] {
483
+ if ( bezierPoints == null || bezierPoints . Length < 2 ) {
484
+ bezierPoints = new SplineBezierPoint [ ] {
483
485
new SplineBezierPoint ( new Vector3 ( - 1 , 1 , 0 ) , Quaternion . LookRotation ( Vector3 . right , Vector3 . forward ) , 1f , 1f ) ,
484
486
new SplineBezierPoint ( new Vector3 ( 1 , - 1 , 0 ) , Quaternion . LookRotation ( Vector3 . right , Vector3 . forward ) , 1f , 1f )
485
487
} ;
@@ -547,5 +549,19 @@ public static void DrawCurveLineGizmos (Spline spline, SplineBezierCurve curve,
547
549
static float SqrDistance ( Vector3 a , Vector3 b ) {
548
550
return ( a . x - b . x ) * ( a . x - b . x ) + ( a . y - b . y ) * ( a . y - b . y ) + ( a . z - b . z ) * ( a . z - b . z ) ;
549
551
}
552
+
553
+ static float GetNormalizedDistanceOnLine ( Vector3 start , Vector3 end , Vector3 p , bool clamped = true ) {
554
+ float sqrLength = SqrDistance ( start , end ) ;
555
+ return GetNormalizedDistanceOnLineInternal ( start , end , p , sqrLength , clamped ) ;
556
+ }
557
+
558
+ static float GetNormalizedDistanceOnLineInternal ( Vector3 start , Vector3 end , Vector3 p , float sqrLength , bool clamped = true ) {
559
+ if ( sqrLength == 0f ) return 0 ;
560
+ // Divide by length squared so that we can save on normalising (end-start), since
561
+ // we're effectively dividing by the length an extra time.
562
+ float n = Vector3 . Dot ( p - start , end - start ) / sqrLength ;
563
+ if ( ! clamped ) return n ;
564
+ return Mathf . Clamp01 ( n ) ;
565
+ }
550
566
}
551
567
}
0 commit comments