Skip to content

Commit 275eddc

Browse files
authored
documented generate points fn
1 parent ec6fe87 commit 275eddc

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

Diff for: SplineEngine/SplineGenerator.cs

+28-11
Original file line numberDiff line numberDiff line change
@@ -120,39 +120,56 @@ public void AddPhysics()
120120
/// <remarks>Based on the Catmull-Rom spline algorithm. Use these points to create and initialize nodes.</remarks>
121121
private Vector3[] GeneratePointsOnSpline(int numPointsPerSegment, int numSegments)
122122
{
123+
// Create an array to store the spline points.
123124
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.
124127
var tStep = 1f / numPointsPerSegment;
125128

129+
// Iterate through each segment of the spline.
126130
for (var i = 0; i < numSegments - 1; i++)
127131
{
132+
// Determine the indices of the control points for the spline segment.
128133
var p0 = i == 0 ? 0 : i - 1;
129134
var p2 = i == numSegments - 1 ? numSegments - 1 : i + 1;
130135
var p3 = i == numSegments - 2 ? numSegments - 1 : i + 2;
131-
136+
137+
// Generate points for the current spline segment.
132138
for (var j = 0; j < numPointsPerSegment; j++)
133139
{
140+
// Calculate the parameter t for the current point on the spline.
134141
var t = j * tStep;
142+
143+
// Calculate t^2 and t^3 for optimization.
135144
var t2 = t * t;
136145
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.
145156
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)
147160
splinePoints[i * numPointsPerSegment + j] = pointOnSpline;
148161
}
149162
}
163+
164+
// Remove any points that are at the origin (Vector3.zero) from the array.
150165
splinePoints = splinePoints.Where(x => x != Vector3.zero).ToArray();
151-
166+
167+
// Update the line renderer with the spline points and return the points array.
152168
_lineRenderer.UpdateSplineRenderer(splinePoints);
153169
return splinePoints;
154170
}
155171

172+
156173
[Button(ButtonSizes.Medium)]
157174
public void DebugSpline()
158175
{

0 commit comments

Comments
 (0)