@@ -120,39 +120,56 @@ public void AddPhysics()
120
120
/// <remarks>Based on the Catmull-Rom spline algorithm. Use these points to create and initialize nodes.</remarks>
121
121
private Vector3 [ ] GeneratePointsOnSpline ( int numPointsPerSegment , int numSegments )
122
122
{
123
+ // Create an array to store the spline points.
123
124
var splinePoints = new Vector3 [ numPointsPerSegment * numSegments ] ;
125
+
126
+ // Calculate the step size between points on the spline based on the number of points per segment.
124
127
var tStep = 1f / numPointsPerSegment ;
125
128
129
+ // Iterate through each segment of the spline.
126
130
for ( var i = 0 ; i < numSegments - 1 ; i ++ )
127
131
{
132
+ // Determine the indices of the control points for the spline segment.
128
133
var p0 = i == 0 ? 0 : i - 1 ;
129
134
var p2 = i == numSegments - 1 ? numSegments - 1 : i + 1 ;
130
135
var p3 = i == numSegments - 2 ? numSegments - 1 : i + 2 ;
131
-
136
+
137
+ // Generate points for the current spline segment.
132
138
for ( var j = 0 ; j < numPointsPerSegment ; j ++ )
133
139
{
140
+ // Calculate the parameter t for the current point on the spline.
134
141
var t = j * tStep ;
142
+
143
+ // Calculate t^2 and t^3 for optimization.
135
144
var t2 = t * t ;
136
145
var t3 = t2 * t ;
137
-
138
- var pointOnSpline = 0.5f * ( ( 2 * controlPoints [ i ] . position ) +
139
- ( - controlPoints [ p0 ] . position + controlPoints [ p2 ] . position ) * t +
140
- ( 2 * controlPoints [ p0 ] . position - 5 * controlPoints [ i ] . position + 4 * controlPoints [ p2 ] . position - controlPoints [ p3 ] . position ) * t2 +
141
- ( - controlPoints [ p0 ] . position + 3 * controlPoints [ i ] . position - 3 * controlPoints [ p2 ] . position + controlPoints [ p3 ] . position ) * t3 ) ;
142
-
143
-
144
-
146
+
147
+ // Calculate the point on the spline using the Catmull-Rom formula.
148
+ var pointOnSpline = 0.5f * (
149
+ ( 2 * controlPoints [ i ] . position ) +
150
+ ( - controlPoints [ p0 ] . position + controlPoints [ p2 ] . position ) * t +
151
+ ( 2 * controlPoints [ p0 ] . position - 5 * controlPoints [ i ] . position + 4 * controlPoints [ p2 ] . position - controlPoints [ p3 ] . position ) * t2 +
152
+ ( - controlPoints [ p0 ] . position + 3 * controlPoints [ i ] . position - 3 * controlPoints [ p2 ] . position + controlPoints [ p3 ] . position ) * t3
153
+ ) ;
154
+
155
+ // Draw a magenta debug line from the point to the point with an offset upward.
145
156
Debug . DrawLine ( pointOnSpline , pointOnSpline + Vector3 . up , Color . magenta , 10.0f ) ;
146
- if ( pointOnSpline != Vector3 . zero )
157
+
158
+ // Store the point in the splinePoints array if it is not at the origin (Vector3.zero).
159
+ if ( pointOnSpline != Vector3 . zero )
147
160
splinePoints [ i * numPointsPerSegment + j ] = pointOnSpline ;
148
161
}
149
162
}
163
+
164
+ // Remove any points that are at the origin (Vector3.zero) from the array.
150
165
splinePoints = splinePoints . Where ( x => x != Vector3 . zero ) . ToArray ( ) ;
151
-
166
+
167
+ // Update the line renderer with the spline points and return the points array.
152
168
_lineRenderer . UpdateSplineRenderer ( splinePoints ) ;
153
169
return splinePoints ;
154
170
}
155
171
172
+
156
173
[ Button ( ButtonSizes . Medium ) ]
157
174
public void DebugSpline ( )
158
175
{
0 commit comments