@@ -21,20 +21,31 @@ public struct PointIndexTuple
21
21
{
22
22
public readonly BezierPoint point1 , point2 ;
23
23
public readonly int index1 , index2 ;
24
- public readonly float t ;
24
+ public readonly float localT ;
25
25
26
- public PointIndexTuple ( BezierSpline spline , int index1 , int index2 , float t )
26
+ public PointIndexTuple ( BezierSpline spline , int index1 , int index2 , float localT )
27
27
{
28
28
this . point1 = spline [ index1 ] ;
29
29
this . point2 = spline [ index2 ] ;
30
30
this . index1 = index1 ;
31
31
this . index2 = index2 ;
32
- this . t = t ;
32
+ this . localT = localT ;
33
+ }
34
+
35
+ public float GetNormalizedT ( )
36
+ {
37
+ return GetNormalizedT ( localT ) ;
38
+ }
39
+
40
+ public float GetNormalizedT ( float localT )
41
+ {
42
+ BezierSpline spline = point1 . GetComponentInParent < BezierSpline > ( ) ;
43
+ return ( index1 + localT ) / ( spline . loop ? spline . Count : ( spline . Count - 1 ) ) ;
33
44
}
34
45
35
46
public Vector3 GetPoint ( )
36
47
{
37
- return GetPoint ( t ) ;
48
+ return GetPoint ( localT ) ;
38
49
}
39
50
40
51
public Vector3 GetPoint ( float localT )
@@ -49,7 +60,7 @@ public Vector3 GetPoint( float localT )
49
60
50
61
public Vector3 GetTangent ( )
51
62
{
52
- return GetTangent ( t ) ;
63
+ return GetTangent ( localT ) ;
53
64
}
54
65
55
66
public Vector3 GetTangent ( float localT )
@@ -63,7 +74,7 @@ public Vector3 GetTangent( float localT )
63
74
64
75
public Vector3 GetNormal ( )
65
76
{
66
- return GetNormal ( t ) ;
77
+ return GetNormal ( localT ) ;
67
78
}
68
79
69
80
public Vector3 GetNormal ( float localT )
@@ -85,7 +96,7 @@ public Vector3 GetNormal( float localT )
85
96
86
97
public BezierPoint . ExtraData GetExtraData ( )
87
98
{
88
- return defaultExtraDataLerpFunction ( point1 . extraData , point2 . extraData , t ) ;
99
+ return defaultExtraDataLerpFunction ( point1 . extraData , point2 . extraData , localT ) ;
89
100
}
90
101
91
102
public BezierPoint . ExtraData GetExtraData ( float localT )
@@ -95,7 +106,7 @@ public BezierPoint.ExtraData GetExtraData( float localT )
95
106
96
107
public BezierPoint . ExtraData GetExtraData ( ExtraDataLerpFunction lerpFunction )
97
108
{
98
- return lerpFunction ( point1 . extraData , point2 . extraData , t ) ;
109
+ return lerpFunction ( point1 . extraData , point2 . extraData , localT ) ;
99
110
}
100
111
101
112
public BezierPoint . ExtraData GetExtraData ( float localT , ExtraDataLerpFunction lerpFunction )
@@ -156,15 +167,20 @@ private void Awake()
156
167
Refresh ( ) ;
157
168
}
158
169
159
- #if UNITY_EDITOR
160
- private void OnTransformChildrenChanged ( )
170
+ private void LateUpdate ( )
161
171
{
162
- Refresh ( ) ;
172
+ for ( int i = 0 ; i < endPoints . Count ; i ++ )
173
+ endPoints [ i ] . RefreshIfChanged ( ) ;
174
+
175
+ #if UNITY_EDITOR
176
+ Internal_CheckDirty ( ) ;
177
+ #endif
163
178
}
164
179
165
- private void LateUpdate ( )
180
+ #if UNITY_EDITOR
181
+ private void OnTransformChildrenChanged ( )
166
182
{
167
- Internal_CheckDirty ( ) ;
183
+ Refresh ( ) ;
168
184
}
169
185
170
186
internal void Internal_CheckDirty ( )
@@ -650,6 +666,51 @@ public Vector3 FindNearestPointTo( Vector3 worldPos, out float normalizedT, floa
650
666
return result ;
651
667
}
652
668
669
+ public Vector3 FindNearestPointToLine ( Vector3 lineStart , Vector3 lineEnd , float accuracy = 100f )
670
+ {
671
+ Vector3 pointOnLine ;
672
+ float normalizedT ;
673
+ return FindNearestPointToLine ( lineStart , lineEnd , out pointOnLine , out normalizedT , accuracy ) ;
674
+ }
675
+
676
+ public Vector3 FindNearestPointToLine ( Vector3 lineStart , Vector3 lineEnd , out Vector3 pointOnLine , out float normalizedT , float accuracy = 100f )
677
+ {
678
+ Vector3 result = Vector3 . zero ;
679
+ pointOnLine = Vector3 . zero ;
680
+ normalizedT = - 1f ;
681
+
682
+ float step = AccuracyToStepSize ( accuracy ) ;
683
+
684
+ // Find closest point on line
685
+ // Credit: https://github.com/Unity-Technologies/UnityCsReference/blob/61f92bd79ae862c4465d35270f9d1d57befd1761/Editor/Mono/HandleUtility.cs#L115-L128
686
+ Vector3 lineDirection = lineEnd - lineStart ;
687
+ float length = lineDirection . magnitude ;
688
+ Vector3 normalizedLineDirection = lineDirection ;
689
+ if ( length > .000001f )
690
+ normalizedLineDirection /= length ;
691
+
692
+ float minDistance = Mathf . Infinity ;
693
+ for ( float i = 0f ; i < 1f ; i += step )
694
+ {
695
+ Vector3 thisPoint = GetPoint ( i ) ;
696
+
697
+ // Find closest point on line
698
+ // Credit: https://github.com/Unity-Technologies/UnityCsReference/blob/61f92bd79ae862c4465d35270f9d1d57befd1761/Editor/Mono/HandleUtility.cs#L115-L128
699
+ Vector3 closestPointOnLine = lineStart + normalizedLineDirection * Mathf . Clamp ( Vector3 . Dot ( normalizedLineDirection , thisPoint - lineStart ) , 0f , length ) ;
700
+
701
+ float thisDistance = ( closestPointOnLine - thisPoint ) . sqrMagnitude ;
702
+ if ( thisDistance < minDistance )
703
+ {
704
+ minDistance = thisDistance ;
705
+ result = thisPoint ;
706
+ pointOnLine = closestPointOnLine ;
707
+ normalizedT = i ;
708
+ }
709
+ }
710
+
711
+ return result ;
712
+ }
713
+
653
714
// Credit: https://gamedev.stackexchange.com/a/27138
654
715
public Vector3 MoveAlongSpline ( ref float normalizedT , float deltaMovement , int accuracy = 3 )
655
716
{
@@ -662,6 +723,9 @@ public Vector3 MoveAlongSpline( ref float normalizedT, float deltaMovement, int
662
723
663
724
public void ConstructLinearPath ( )
664
725
{
726
+ for ( int i = 0 ; i < endPoints . Count ; i ++ )
727
+ endPoints [ i ] . RefreshIfChanged ( ) ;
728
+
665
729
for ( int i = 0 ; i < endPoints . Count ; i ++ )
666
730
{
667
731
endPoints [ i ] . handleMode = BezierPoint . HandleMode . Free ;
@@ -685,7 +749,10 @@ public void ConstructLinearPath()
685
749
public void AutoConstructSpline ( )
686
750
{
687
751
for ( int i = 0 ; i < endPoints . Count ; i ++ )
752
+ {
688
753
endPoints [ i ] . handleMode = BezierPoint . HandleMode . Mirrored ;
754
+ endPoints [ i ] . RefreshIfChanged ( ) ;
755
+ }
689
756
690
757
int n = endPoints . Count - 1 ;
691
758
if ( n == 1 )
@@ -769,6 +836,9 @@ public void AutoConstructSpline2()
769
836
return ;
770
837
}
771
838
839
+ for ( int i = 0 ; i < endPoints . Count ; i ++ )
840
+ endPoints [ i ] . RefreshIfChanged ( ) ;
841
+
772
842
for ( int i = 0 ; i < endPoints . Count ; i ++ )
773
843
{
774
844
Vector3 pMinus1 , p1 , p2 ;
@@ -824,6 +894,9 @@ public void AutoCalculateNormals( bool flipNormals )
824
894
const float DELTA_T = 0.025f ;
825
895
const float ONE_MINUS_DELTA_T = 1f - DELTA_T ;
826
896
897
+ for ( int i = 0 ; i < endPoints . Count ; i ++ )
898
+ endPoints [ i ] . RefreshIfChanged ( ) ;
899
+
827
900
for ( int i = 0 ; i < endPoints . Count ; i ++ )
828
901
{
829
902
if ( i < endPoints . Count - 1 )
@@ -921,7 +994,7 @@ IEnumerator IEnumerable.GetEnumerator()
921
994
}
922
995
923
996
#if UNITY_EDITOR
924
- public void Reset ( )
997
+ internal void Reset ( )
925
998
{
926
999
for ( int i = endPoints . Count - 1 ; i >= 0 ; i -- )
927
1000
UnityEditor . Undo . DestroyObjectImmediate ( endPoints [ i ] . gameObject ) ;
0 commit comments