From 5e7282c27d6df67318e08a7b70c1d54d6c6717d9 Mon Sep 17 00:00:00 2001 From: victornor <1-1@hotmail.dk> Date: Mon, 20 Mar 2023 13:33:14 +0100 Subject: [PATCH 1/3] fix: GetPosition when Segments is null --- Runtime/Scripts/Primitives/UILineRenderer.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Runtime/Scripts/Primitives/UILineRenderer.cs b/Runtime/Scripts/Primitives/UILineRenderer.cs index d3b5d3d..b0db451 100644 --- a/Runtime/Scripts/Primitives/UILineRenderer.cs +++ b/Runtime/Scripts/Primitives/UILineRenderer.cs @@ -419,7 +419,7 @@ namespace UnityEngine.UI.Extensions { return Segments[segmentIndex - 1][index - 1]; } - else if (Segments.Count > 0) + else if (Segments?.Count > 0) { var segmentIndexCount = 0; var indexCount = index; @@ -480,4 +480,4 @@ namespace UnityEngine.UI.Extensions } } } -} \ No newline at end of file +} From ba7b21fdae7b6b86f46070738386c35bfc5d6aba Mon Sep 17 00:00:00 2001 From: Victor <1-1@hotmail.dk> Date: Mon, 20 Mar 2023 13:42:33 +0100 Subject: [PATCH 2/3] feat: add CalculatePointOnCurve for linerenderer --- Runtime/Scripts/Primitives/UILineRenderer.cs | 23 ++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Runtime/Scripts/Primitives/UILineRenderer.cs b/Runtime/Scripts/Primitives/UILineRenderer.cs index b0db451..902a40c 100644 --- a/Runtime/Scripts/Primitives/UILineRenderer.cs +++ b/Runtime/Scripts/Primitives/UILineRenderer.cs @@ -443,6 +443,29 @@ namespace UnityEngine.UI.Extensions } } + /// + /// Calculates the position of a point on the curve, given t (0-1), start point, control points and end point. + /// + /// Required Percentage between start and end point, in the range 0 to 1 + /// Required Starting point + /// Required Control point 1 + /// Required Control point 2 + /// Required End point + /// Vector2 position of point on curve at t percentage between p1 and p4 + private Vector2 CalculatePointOnCurve(float t, Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4) + { + var t2 = t * t; + var t3 = t2 * t; + + var x = p1.x + (-p1.x * 3 + t * (3 * p1.x - p1.x * t)) * t + (3 * p2.x + t * (-6 * p2.x + p2.x * 3 * t)) * t + + (p3.x * 3 - p3.x * 3 * t) * t2 + p4.x * t3; + + var y = p1.y + (-p1.y * 3 + t * (3 * p1.y - p1.y * t)) * t + (3 * p2.y + t * (-6 * p2.y + p2.y * 3 * t)) * t + + (p3.y * 3 - p3.y * 3 * t) * t2 + p4.y * t3; + + return new Vector2(x, y); + } + /// /// Get the Vector2 position of a line within a specific segment /// From 7127f8bca746df5ebd34d9f76b823beec3e1072b Mon Sep 17 00:00:00 2001 From: Victor <1-1@hotmail.dk> Date: Mon, 20 Mar 2023 13:43:43 +0100 Subject: [PATCH 3/3] chore: make CalculatePointOnCurve public --- Runtime/Scripts/Primitives/UILineRenderer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Runtime/Scripts/Primitives/UILineRenderer.cs b/Runtime/Scripts/Primitives/UILineRenderer.cs index 902a40c..7225145 100644 --- a/Runtime/Scripts/Primitives/UILineRenderer.cs +++ b/Runtime/Scripts/Primitives/UILineRenderer.cs @@ -452,7 +452,7 @@ namespace UnityEngine.UI.Extensions /// Required Control point 2 /// Required End point /// Vector2 position of point on curve at t percentage between p1 and p4 - private Vector2 CalculatePointOnCurve(float t, Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4) + public Vector2 CalculatePointOnCurve(float t, Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4) { var t2 = t * t; var t3 = t2 * t;