void DrawBezierSegmentTest(BezierSegment seg) { //The start position of the line Vector3 lastPos = transform.TransformPoint(seg.A); //The resolution of the line //Make sure the resolution is adding up to 1, so 0.3 will give a gap at the end, but 0.2 will work float resolution = 0.02f; //How many loops? int loops = Mathf.FloorToInt(1f / resolution); for (int i = 1; i < loops; i++) { //Which t position are we at? float t = i * resolution; //Find the coordinates between the control points with a Catmull-Rom spline Vector3 newPos = SS_Common.DeCasteljausAlgorithm(t, seg); //Draw this line segment Gizmos.DrawLine(lastPos, newPos); //Save this pos so we can draw the next line segment lastPos = newPos; } }
/// <summary> /// /// </summary> /// <returns>a Vector3 in world coordinates </returns> public static Vector3 GetWorldPosAtCurvePoint(BezierSegment seg, float dist) { //Find the total length of the curve float totalLength = SS_Common.GetLengthSimpsons(0f, 1f, seg); //Use Newton–Raphsons method to find the t value from the start of the curve //to the end of the distance we have float t = SS_Common.FindTValue(dist, totalLength, seg); //Get the coordinate on the Bezier curve at this t value Vector3 pos = SS_Common.DeCasteljausAlgorithm(t, seg); return(pos); }
//Divide the curve into equal steps public void DivideSegmentIntoSteps(BezierSegment seg) { //Find the total length of the curve float totalLength = SS_Common.GetLengthSimpsons(0f, 1f, seg); //How many sections do we want to divide the curve into int parts = numSteps; //reset the curve steps Array <======= EDIT MIO segmentSteps.Clear(); segmentSteps.Add(transform.TransformPoint(seg.A)); //What's the length of one section? float sectionLength = totalLength / (float)parts; //Init the variables we need in the loop float currentDistance = 0f + sectionLength; //The curve's start position Vector3 lastPos = seg.A; for (int i = 1; i <= parts; i++) { //Use Newton–Raphsons method to find the t value from the start of the curve //to the end of the distance we have float t = SS_Common.FindTValue(currentDistance, totalLength, seg); //Get the coordinate on the Bezier curve at this t value Vector3 pos = SS_Common.DeCasteljausAlgorithm(t, seg); //Save the last position lastPos = pos; //Add current pos to vector list <======= EDIT MIO segmentSteps.Add(transform.TransformPoint(pos)); //Add to the distance traveled on the line so far currentDistance += sectionLength; } }