public Vector3[] CalculateEvenlySpacedPoints(float spacing, float resolution = 1) { List <Vector3> evenlySpacedPoints = new List <Vector3>(); evenlySpacedPoints.Add(points[0]); Vector3 previousPoint = points[0]; float dstSinceLastEvenPoint = 0; for (int segmentIndex = 0; segmentIndex < NumSegments; segmentIndex++) { Vector3[] p = GetPointsInSegment(segmentIndex); float controlNetLength = Vector3.Distance(p[0], p[1]) + Vector3.Distance(p[1], p[2]) + Vector3.Distance(p[2], p[3]); float estimedCurveLength = Vector3.Distance(p[0], p[3]) + controlNetLength / 2f; int divisions = Mathf.CeilToInt(estimedCurveLength * resolution * 10); float t = 0; while (t <= 1) { t += 1f / divisions; Vector3 pointOnCurve = Bezier.Evaluate(p[0], p[1], p[2], p[3], t); dstSinceLastEvenPoint += Vector3.Distance(previousPoint, pointOnCurve); while (dstSinceLastEvenPoint >= spacing) { float overshootDst = dstSinceLastEvenPoint - spacing; Vector3 newEvenlySpacedPoint = pointOnCurve + (previousPoint - pointOnCurve).normalized * overshootDst; evenlySpacedPoints.Add(newEvenlySpacedPoint); dstSinceLastEvenPoint = overshootDst; previousPoint = newEvenlySpacedPoint; } previousPoint = pointOnCurve; } } return(evenlySpacedPoints.ToArray()); }
float GetPercentOnBezier(Vector3[] points, Vector2 mousePos, float t, float offset, int count) { if (count <= 0) { return(t); } Vector2 bezierPosLeft = HandleUtility.WorldToGUIPoint(Bezier.Evaluate(points[0], points[1], points[2], points[3], t - offset)); Vector2 bezierPosRight = HandleUtility.WorldToGUIPoint(Bezier.Evaluate(points[0], points[1], points[2], points[3], t + offset)); float dstLeft = Vector2.Distance(mousePos, bezierPosLeft); float dstRight = Vector2.Distance(mousePos, bezierPosRight); return(GetPercentOnBezier(points, mousePos, (dstLeft < dstRight) ? t - offset : t + offset, offset / 2, --count)); }
Vector3 GetCloserPointInSegment(Vector2 mousePos, int segmentIndex) { Vector3[] points = LocalToWorld(Path.GetPointsInSegment(segmentIndex)); return(Bezier.Evaluate(points[0], points[1], points[2], points[3], GetPercentOnBezier(points, mousePos, 0.5f, 0.25f, 10))); }
Vector3 GetCurvesVector(int index, float keyValue) { Vector3[] points = path.GetPointsInSegment(index); return(creator.startTransform.TransformPoint(Bezier.Evaluate(points[0], points[1], points[2], points[3], keyValue))); }