@@ -44,5 +44,94 @@ public static implicit operator Ray(RayStep r)
4444 {
4545 return new Ray ( r . origin , r . direction ) ;
4646 }
47+
48+ #region static utility functions
49+
50+ /// <summary>
51+ /// Returns a point along an array of RaySteps by distance
52+ /// </summary>
53+ /// <param name="steps"></param>
54+ /// <param name="distance"></param>
55+ /// <returns></returns>
56+ public static Vector3 GetPointByDistance ( RayStep [ ] steps , float distance )
57+ {
58+ Debug . Assert ( steps != null ) ;
59+ Debug . Assert ( steps . Length > 0 ) ;
60+
61+ Vector3 point = Vector3 . zero ;
62+ float remainingDistance = distance ;
63+
64+ for ( int i = 0 ; i < steps . Length ; i ++ )
65+ {
66+ if ( remainingDistance > steps [ i ] . length )
67+ {
68+ remainingDistance -= steps [ i ] . length ;
69+ }
70+ else
71+ {
72+ point = Vector3 . Lerp ( steps [ i ] . origin , steps [ i ] . terminus , remainingDistance / steps [ i ] . length ) ;
73+ remainingDistance = 0 ;
74+ break ;
75+ }
76+ }
77+
78+ if ( remainingDistance > 0 )
79+ {
80+ // If we reach the end and still have distance left, set the point to the terminus of the last step
81+ point = steps [ steps . Length - 1 ] . terminus ;
82+ }
83+
84+ return point ;
85+ }
86+
87+ /// <summary>
88+ /// Returns a RayStep along an array of RaySteps by distance
89+ /// </summary>
90+ /// <param name="steps"></param>
91+ /// <param name="distance"></param>
92+ /// <returns></returns>
93+ public static RayStep GetStepByDistance ( RayStep [ ] steps , float distance )
94+ {
95+ Debug . Assert ( steps != null ) ;
96+ Debug . Assert ( steps . Length > 0 ) ;
97+
98+ RayStep step = new RayStep ( ) ;
99+ float remainingDistance = distance ;
100+
101+ for ( int i = 0 ; i < steps . Length ; i ++ )
102+ {
103+ if ( remainingDistance > steps [ i ] . length )
104+ {
105+ remainingDistance -= steps [ i ] . length ;
106+ }
107+ else
108+ {
109+ step = steps [ i ] ;
110+ remainingDistance = 0 ;
111+ break ;
112+ }
113+ }
114+
115+ if ( remainingDistance > 0 )
116+ {
117+ // If we reach the end and still have distance left, return the last step
118+ step = steps [ steps . Length - 1 ] ;
119+ }
120+
121+ return step ;
122+ }
123+
124+ /// <summary>
125+ /// Returns a direction along an array of RaySteps by distance
126+ /// </summary>
127+ /// <param name="steps"></param>
128+ /// <param name="distance"></param>
129+ /// <returns></returns>
130+ public static Vector3 GetDirectionByDistance ( RayStep [ ] steps , float distance )
131+ {
132+ return GetStepByDistance ( steps , distance ) . direction ;
133+ }
134+
135+ #endregion
47136 }
48137}
0 commit comments