private List <Vector3> GetEvenlySpacedPoints(BezierSplineDataObject spline, ref float splineLength, float spacing) { List <Vector3> points = new List <Vector3> { spline[0] }; Vector3 prevPoint = spline[0]; float distanceSinceLastPoint = 0; for (int segmentIndex = 0; segmentIndex < spline.SegmentCount; segmentIndex++) { Vector3[] segment = spline.GetSegmentPoints(segmentIndex); float curveLength = Utils.Bezier3DUtility.Get3DCurveLength(segment[0], segment[1], segment[2], segment[3]); int divisions = Mathf.CeilToInt(curveLength * _Resolution * 10); splineLength += curveLength; float pointPos = 0; while (pointPos <= 1) { pointPos += 1f / divisions; Vector3 pointOnCurve = Utils.Bezier3DUtility.CubicCurveVector3(segment[0], segment[1], segment[2], segment[3], pointPos); distanceSinceLastPoint += Vector3.Distance(prevPoint, pointOnCurve); while (distanceSinceLastPoint >= spacing) { float overShootDistance = distanceSinceLastPoint - spacing; Vector3 newCorrectedPoint = pointOnCurve + (prevPoint - pointOnCurve).normalized * overShootDistance; points.Add(newCorrectedPoint); distanceSinceLastPoint = overShootDistance; prevPoint = newCorrectedPoint; } prevPoint = pointOnCurve; } } return(points); }
public Vector3[] CalculateEvenlySpacedPoints(BezierSplineDataObject spline) { float splineLength = 0; List <Vector3> points = GetEvenlySpacedPoints(spline, ref splineLength, _Spacing); float remainingPointLength = spline.IsClosed ? Mathf.Sqrt( (points[0].x - points[points.Count - 1].x) * (points[0].x - points[points.Count - 1].x) + (points[0].y - points[points.Count - 1].y) * (points[0].y - points[points.Count - 1].y) + (points[0].z - points[points.Count - 1].z) * (points[0].z - points[points.Count - 1].z)) : Mathf.Sqrt( (spline[spline.PointCount - 1].x - points[points.Count - 1].x) * (spline[spline.PointCount - 1].x - points[points.Count - 1].x) + (spline[spline.PointCount - 1].y - points[points.Count - 1].y) * (spline[spline.PointCount - 1].y - points[points.Count - 1].y) + (spline[spline.PointCount - 1].z - points[points.Count - 1].z) * (spline[spline.PointCount - 1].z - points[points.Count - 1].z)); float totalSpacing = _Spacing + remainingPointLength / points.Count; return(GetEvenlySpacedPoints(spline, ref splineLength, totalSpacing).ToArray()); }
public int EvenlySpacedSplinePointCount(BezierSplineDataObject spline) => _EvenlySpacedSplinePoints[spline].Length;
public Vector3 this[BezierSplineDataObject dataObject, int i] => _EvenlySpacedSplinePoints[dataObject][i];