From 455107aabbf5a458fc914b3f32ab6ca87542cc07 Mon Sep 17 00:00:00 2001 From: apkd Date: Sun, 27 Oct 2019 16:30:05 +0100 Subject: [PATCH 1/8] Optimize NodeEditorGUI.DrawNoodle --- Scripts/Editor/NodeEditorGUI.cs | 112 +++++++++++++++++++------------- 1 file changed, 67 insertions(+), 45 deletions(-) diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index cae1f15..b8f55d1 100644 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -15,6 +15,7 @@ namespace XNodeEditor { private int topPadding { get { return isDocked() ? 19 : 22; } } /// Executed after all other window GUI. Useful if Zoom is ruining your day. Automatically resets after being run. public event Action onLateGUI; + private static readonly Vector3[] polyLineTempArray = new Vector3[2]; private void OnGUI() { Event e = Event.current; @@ -115,24 +116,41 @@ namespace XNodeEditor { contextMenu.DropDown(new Rect(Event.current.mousePosition, Vector2.zero)); if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets(); } + + static Vector3 CalculateBezierPoint(in Vector3 p0, in Vector3 p1, in Vector3 p2, in Vector3 p3, float t) { + float u = 1 - t; + float tt = t * t, uu = u * u; + float uuu = uu * u, ttt = tt * t; + return (uuu * p0) + (3 * uu * t * p1) + (3 * u * tt * p2) + (ttt * p3); + } + /// Draws a line segment without allocating temporary arrays + static void DrawAAPolyLineNonAlloc(float thickness, in Vector3 p0, in Vector3 p1) { + polyLineTempArray[0] = p0; + polyLineTempArray[1] = p1; + Handles.DrawAAPolyLine(thickness, polyLineTempArray); + } + /// Draw a bezier from output to input in grid coordinates public void DrawNoodle(Gradient gradient, NoodlePath path, NoodleStroke stroke, float thickness, List gridPoints) { - Vector2[] windowPoints = gridPoints.Select(x => GridToWindowPosition(x)).ToArray(); + // convert grid points to window points + for (int i = 0; i < gridPoints.Count; ++i) + gridPoints[i] = GridToWindowPosition(gridPoints[i]); + Handles.color = gradient.Evaluate(0f); int length = gridPoints.Count; switch (path) { case NoodlePath.Curvy: Vector2 outputTangent = Vector2.right; for (int i = 0; i < length - 1; i++) { - Vector2 inputTangent = Vector2.left; + Vector2 inputTangent; // Cached most variables that repeat themselves here to avoid so many indexer calls :p - Vector2 point_a = windowPoints[i]; - Vector2 point_b = windowPoints[i + 1]; + Vector2 point_a = gridPoints[i]; + Vector2 point_b = gridPoints[i + 1]; float dist_ab = Vector2.Distance(point_a, point_b); - if (i == 0) outputTangent = Vector2.right * dist_ab * 0.01f * zoom; + if (i == 0) outputTangent = zoom * dist_ab * 0.01f * Vector2.right; if (i < length - 2) { - Vector2 point_c = windowPoints[i + 2]; + Vector2 point_c = gridPoints[i + 2]; Vector2 ab = (point_b - point_a).normalized; Vector2 cb = (point_b - point_c).normalized; Vector2 ac = (point_c - point_a).normalized; @@ -140,37 +158,41 @@ namespace XNodeEditor { float tangentLength = (dist_ab + Vector2.Distance(point_b, point_c)) * 0.005f * zoom; float side = ((ac.x * (point_b.y - point_a.y)) - (ac.y * (point_b.x - point_a.x))); - p = new Vector2(-p.y, p.x) * Mathf.Sign(side) * tangentLength; + p = tangentLength * Mathf.Sign(side) * new Vector2(-p.y, p.x); inputTangent = p; } else { - inputTangent = Vector2.left * dist_ab * 0.01f * zoom; + inputTangent = zoom * dist_ab * 0.01f * Vector2.left; } // Calculates the tangents for the bezier's curves. - Vector2 tangent_a = point_a + outputTangent * 50 / zoom; - Vector2 tangent_b = point_b + inputTangent * 50 / zoom; + float zoomCoef = 50 / zoom; + Vector2 tangent_a = point_a + outputTangent * zoomCoef; + Vector2 tangent_b = point_b + inputTangent * zoomCoef; // Hover effect. int division = Mathf.RoundToInt(.2f * dist_ab) + 3; - Vector3[] points = Handles.MakeBezierPoints(point_a, point_b, tangent_a, tangent_b, division); - int draw = 0; // Coloring and bezier drawing. - for (int j = 0; j < points.Length - 1; j++) { + int draw = 0; + Vector3 bezierPrevious = point_a; + for (int j = 1; j < division; ++j) + { if (stroke == NoodleStroke.Dashed) { draw++; if (draw >= 2) draw = -2; if (draw < 0) continue; } - - if (i == gridPoints.Count - 2) Handles.color = gradient.Evaluate((j + 1f) / points.Length); - Handles.DrawAAPolyLine(thickness, points[j], points[j + 1]); + if (i == length - 2) + Handles.color = gradient.Evaluate((j + 1f) / division); + Vector3 bezierNext = CalculateBezierPoint(point_a, tangent_a, tangent_b, point_b, j / (float)division); + DrawAAPolyLineNonAlloc(thickness, bezierPrevious, bezierNext); + bezierPrevious = bezierNext; } outputTangent = -inputTangent; } break; case NoodlePath.Straight: for (int i = 0; i < length - 1; i++) { - Vector2 point_a = windowPoints[i]; - Vector2 point_b = windowPoints[i + 1]; + Vector2 point_a = gridPoints[i]; + Vector2 point_b = gridPoints[i + 1]; // Draws the line with the coloring. Vector2 prev_point = point_a; // Approximately one segment per 5 pixels @@ -182,8 +204,8 @@ namespace XNodeEditor { float t = j / (float) segments; Vector2 lerp = Vector2.Lerp(point_a, point_b, t); if (draw > 0) { - if (i == gridPoints.Count - 2) Handles.color = gradient.Evaluate(t); - Handles.DrawAAPolyLine(thickness, prev_point, lerp); + if (i == length - 2) Handles.color = gradient.Evaluate(t); + DrawAAPolyLineNonAlloc(thickness, prev_point, lerp); } prev_point = lerp; if (stroke == NoodleStroke.Dashed && draw >= 2) draw = -2; @@ -193,49 +215,49 @@ namespace XNodeEditor { case NoodlePath.Angled: for (int i = 0; i < length - 1; i++) { if (i == length - 1) continue; // Skip last index - if (windowPoints[i].x <= windowPoints[i + 1].x - (50 / zoom)) { - float midpoint = (windowPoints[i].x + windowPoints[i + 1].x) * 0.5f; - Vector2 start_1 = windowPoints[i]; - Vector2 end_1 = windowPoints[i + 1]; + if (gridPoints[i].x <= gridPoints[i + 1].x - (50 / zoom)) { + float midpoint = (gridPoints[i].x + gridPoints[i + 1].x) * 0.5f; + Vector2 start_1 = gridPoints[i]; + Vector2 end_1 = gridPoints[i + 1]; start_1.x = midpoint; end_1.x = midpoint; - if (i == gridPoints.Count - 2) { - Handles.DrawAAPolyLine(thickness, windowPoints[i], start_1); + if (i == length - 2) { + DrawAAPolyLineNonAlloc(thickness, gridPoints[i], start_1); Handles.color = gradient.Evaluate(0.5f); - Handles.DrawAAPolyLine(thickness, start_1, end_1); + DrawAAPolyLineNonAlloc(thickness, start_1, end_1); Handles.color = gradient.Evaluate(1f); - Handles.DrawAAPolyLine(thickness, end_1, windowPoints[i + 1]); + DrawAAPolyLineNonAlloc(thickness, end_1, gridPoints[i + 1]); } else { - Handles.DrawAAPolyLine(thickness, windowPoints[i], start_1); - Handles.DrawAAPolyLine(thickness, start_1, end_1); - Handles.DrawAAPolyLine(thickness, end_1, windowPoints[i + 1]); + DrawAAPolyLineNonAlloc(thickness, gridPoints[i], start_1); + DrawAAPolyLineNonAlloc(thickness, start_1, end_1); + DrawAAPolyLineNonAlloc(thickness, end_1, gridPoints[i + 1]); } } else { - float midpoint = (windowPoints[i].y + windowPoints[i + 1].y) * 0.5f; - Vector2 start_1 = windowPoints[i]; - Vector2 end_1 = windowPoints[i + 1]; + float midpoint = (gridPoints[i].y + gridPoints[i + 1].y) * 0.5f; + Vector2 start_1 = gridPoints[i]; + Vector2 end_1 = gridPoints[i + 1]; start_1.x += 25 / zoom; end_1.x -= 25 / zoom; Vector2 start_2 = start_1; Vector2 end_2 = end_1; start_2.y = midpoint; end_2.y = midpoint; - if (i == gridPoints.Count - 2) { - Handles.DrawAAPolyLine(thickness, windowPoints[i], start_1); + if (i == length - 2) { + DrawAAPolyLineNonAlloc(thickness, gridPoints[i], start_1); Handles.color = gradient.Evaluate(0.25f); - Handles.DrawAAPolyLine(thickness, start_1, start_2); + DrawAAPolyLineNonAlloc(thickness, start_1, start_2); Handles.color = gradient.Evaluate(0.5f); - Handles.DrawAAPolyLine(thickness, start_2, end_2); + DrawAAPolyLineNonAlloc(thickness, start_2, end_2); Handles.color = gradient.Evaluate(0.75f); - Handles.DrawAAPolyLine(thickness, end_2, end_1); + DrawAAPolyLineNonAlloc(thickness, end_2, end_1); Handles.color = gradient.Evaluate(1f); - Handles.DrawAAPolyLine(thickness, end_1, windowPoints[i + 1]); + DrawAAPolyLineNonAlloc(thickness, end_1, gridPoints[i + 1]); } else { - Handles.DrawAAPolyLine(thickness, windowPoints[i], start_1); - Handles.DrawAAPolyLine(thickness, start_1, start_2); - Handles.DrawAAPolyLine(thickness, start_2, end_2); - Handles.DrawAAPolyLine(thickness, end_2, end_1); - Handles.DrawAAPolyLine(thickness, end_1, windowPoints[i + 1]); + DrawAAPolyLineNonAlloc(thickness, gridPoints[i], start_1); + DrawAAPolyLineNonAlloc(thickness, start_1, start_2); + DrawAAPolyLineNonAlloc(thickness, start_2, end_2); + DrawAAPolyLineNonAlloc(thickness, end_2, end_1); + DrawAAPolyLineNonAlloc(thickness, end_1, gridPoints[i + 1]); } } } From e15076b34fecc178906770b8d207f350a5131f47 Mon Sep 17 00:00:00 2001 From: apkd Date: Sun, 27 Oct 2019 16:37:31 +0100 Subject: [PATCH 2/8] Use Vector2 in bezier calculations --- Scripts/Editor/NodeEditorGUI.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index b8f55d1..9bb6ab7 100644 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -117,7 +117,7 @@ namespace XNodeEditor { if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets(); } - static Vector3 CalculateBezierPoint(in Vector3 p0, in Vector3 p1, in Vector3 p2, in Vector3 p3, float t) { + static Vector2 CalculateBezierPoint(in Vector2 p0, in Vector2 p1, in Vector2 p2, in Vector2 p3, float t) { float u = 1 - t; float tt = t * t, uu = u * u; float uuu = uu * u, ttt = tt * t; @@ -125,7 +125,7 @@ namespace XNodeEditor { } /// Draws a line segment without allocating temporary arrays - static void DrawAAPolyLineNonAlloc(float thickness, in Vector3 p0, in Vector3 p1) { + static void DrawAAPolyLineNonAlloc(float thickness, in Vector2 p0, in Vector2 p1) { polyLineTempArray[0] = p0; polyLineTempArray[1] = p1; Handles.DrawAAPolyLine(thickness, polyLineTempArray); @@ -172,8 +172,8 @@ namespace XNodeEditor { int division = Mathf.RoundToInt(.2f * dist_ab) + 3; // Coloring and bezier drawing. int draw = 0; - Vector3 bezierPrevious = point_a; - for (int j = 1; j < division; ++j) + Vector2 bezierPrevious = point_a; + for (int j = 1; j <= division; ++j) { if (stroke == NoodleStroke.Dashed) { draw++; @@ -182,7 +182,7 @@ namespace XNodeEditor { } if (i == length - 2) Handles.color = gradient.Evaluate((j + 1f) / division); - Vector3 bezierNext = CalculateBezierPoint(point_a, tangent_a, tangent_b, point_b, j / (float)division); + Vector2 bezierNext = CalculateBezierPoint(point_a, tangent_a, tangent_b, point_b, j / (float)division); DrawAAPolyLineNonAlloc(thickness, bezierPrevious, bezierNext); bezierPrevious = bezierNext; } From 8b99a34a6347a3e6f4d766c71fd69f51481e8584 Mon Sep 17 00:00:00 2001 From: Simon Rodriguez Date: Tue, 12 Nov 2019 23:05:33 +0100 Subject: [PATCH 3/8] The in parameter was is available in C# 7.2 and later. This breaks compatibility with earlier versions. --- Scripts/Editor/NodeEditorGUI.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index 9bb6ab7..5ff178f 100644 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -117,7 +117,7 @@ namespace XNodeEditor { if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets(); } - static Vector2 CalculateBezierPoint(in Vector2 p0, in Vector2 p1, in Vector2 p2, in Vector2 p3, float t) { + static Vector2 CalculateBezierPoint(Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3, float t) { float u = 1 - t; float tt = t * t, uu = u * u; float uuu = uu * u, ttt = tt * t; @@ -125,7 +125,7 @@ namespace XNodeEditor { } /// Draws a line segment without allocating temporary arrays - static void DrawAAPolyLineNonAlloc(float thickness, in Vector2 p0, in Vector2 p1) { + static void DrawAAPolyLineNonAlloc(float thickness, Vector2 p0, Vector2 p1) { polyLineTempArray[0] = p0; polyLineTempArray[1] = p1; Handles.DrawAAPolyLine(thickness, polyLineTempArray); From 92ebd5953999d79b0231a054a6439f076c1509f9 Mon Sep 17 00:00:00 2001 From: Simon Rodriguez Date: Tue, 12 Nov 2019 23:19:51 +0100 Subject: [PATCH 4/8] reuse the list of grid points instead of creating a new one for each node->port --- Scripts/Editor/NodeEditorGUI.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index 5ff178f..1feec0e 100644 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -271,6 +271,8 @@ namespace XNodeEditor { List selection = preBoxSelectionReroute != null ? new List(preBoxSelectionReroute) : new List(); hoveredReroute = new RerouteReference(); + List gridPoints = new List(2); + Color col = GUI.color; foreach (XNode.Node node in graph.nodes) { //If a null node is found, return. This can happen if the nodes associated script is deleted. It is currently not possible in Unity to delete a null asset. @@ -299,7 +301,7 @@ namespace XNodeEditor { List reroutePoints = output.GetReroutePoints(k); - List gridPoints = new List(); + gridPoints.Clear(); gridPoints.Add(fromRect.center); gridPoints.AddRange(reroutePoints); gridPoints.Add(toRect.center); From 91eafcc47d08689b560f0f642c3e2e36c8dc85ce Mon Sep 17 00:00:00 2001 From: Simon Rodriguez Date: Wed, 13 Nov 2019 00:10:45 +0100 Subject: [PATCH 5/8] the + and * was creating a lot of new vector2s, went down 80% in call time with this --- Scripts/Editor/NodeEditorGUI.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index 1feec0e..30c26b4 100644 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -121,7 +121,10 @@ namespace XNodeEditor { float u = 1 - t; float tt = t * t, uu = u * u; float uuu = uu * u, ttt = tt * t; - return (uuu * p0) + (3 * uu * t * p1) + (3 * u * tt * p2) + (ttt * p3); + return new Vector2( + (uuu * p0.x) + (3 * uu * t * p1.x) + (3 * u * tt * p2.x) + (ttt * p3.x), + (uuu * p0.y) + (3 * uu * t * p1.y) + (3 * u * tt * p2.y) + (ttt * p3.y) + ); } /// Draws a line segment without allocating temporary arrays From 71023e1d58c9aa4ad1ce51aa1c73a9d623f0f5fa Mon Sep 17 00:00:00 2001 From: Simon Rodriguez Date: Wed, 13 Nov 2019 00:14:29 +0100 Subject: [PATCH 6/8] microoptimization, removes implicit case from vec2 to vec3. --- Scripts/Editor/NodeEditorGUI.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index 30c26b4..2f605a8 100644 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -129,8 +129,10 @@ namespace XNodeEditor { /// Draws a line segment without allocating temporary arrays static void DrawAAPolyLineNonAlloc(float thickness, Vector2 p0, Vector2 p1) { - polyLineTempArray[0] = p0; - polyLineTempArray[1] = p1; + polyLineTempArray[0].x = p0.x; + polyLineTempArray[0].y = p0.y; + polyLineTempArray[1].x = p1.x; + polyLineTempArray[1].y = p1.y; Handles.DrawAAPolyLine(thickness, polyLineTempArray); } From ec9c5a99de7d148bfc0594a182d50e066c021471 Mon Sep 17 00:00:00 2001 From: Simon Rodriguez Date: Wed, 13 Nov 2019 01:14:09 +0100 Subject: [PATCH 7/8] optimized linq function --- Scripts/Editor/NodeEditorGUI.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index 2f605a8..6cb17cb 100644 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -372,6 +372,8 @@ namespace XNodeEditor { //Save guiColor so we can revert it Color guiColor = GUI.color; + List removeEntries = new List(); + if (e.type == EventType.Layout) culledNodes = new List(); for (int n = 0; n < graph.nodes.Count; n++) { // Skip null nodes. The user could be in the process of renaming scripts, so removing them at this point is not advisable. @@ -389,7 +391,10 @@ namespace XNodeEditor { } else if (culledNodes.Contains(node)) continue; if (e.type == EventType.Repaint) { - _portConnectionPoints = _portConnectionPoints.Where(x => x.Key.node != node).ToDictionary(kvp => kvp.Key, kvp => kvp.Value); + removeEntries.Clear(); + foreach (var kvp in _portConnectionPoints) + if (kvp.Key.node == node) removeEntries.Add(kvp.Key); + foreach (var k in removeEntries) _portConnectionPoints.Remove(k); } NodeEditor nodeEditor = NodeEditor.GetEditor(node, this); From 4edcc3f0999bbee52e066f1a06551c3e0a64d35e Mon Sep 17 00:00:00 2001 From: Thor Brigsted Date: Thu, 14 Nov 2019 17:40:09 +0100 Subject: [PATCH 8/8] Fixed formatting, fixed dashed stroke style --- Scripts/Editor/NodeEditorGUI.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index 6cb17cb..c41afd0 100644 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -116,7 +116,7 @@ namespace XNodeEditor { contextMenu.DropDown(new Rect(Event.current.mousePosition, Vector2.zero)); if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets(); } - + static Vector2 CalculateBezierPoint(Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3, float t) { float u = 1 - t; float tt = t * t, uu = u * u; @@ -135,7 +135,7 @@ namespace XNodeEditor { polyLineTempArray[1].y = p1.y; Handles.DrawAAPolyLine(thickness, polyLineTempArray); } - + /// Draw a bezier from output to input in grid coordinates public void DrawNoodle(Gradient gradient, NoodlePath path, NoodleStroke stroke, float thickness, List gridPoints) { // convert grid points to window points @@ -178,16 +178,16 @@ namespace XNodeEditor { // Coloring and bezier drawing. int draw = 0; Vector2 bezierPrevious = point_a; - for (int j = 1; j <= division; ++j) - { + for (int j = 1; j <= division; ++j) { if (stroke == NoodleStroke.Dashed) { draw++; if (draw >= 2) draw = -2; if (draw < 0) continue; + if (draw == 0) bezierPrevious = CalculateBezierPoint(point_a, tangent_a, tangent_b, point_b, (j - 1f) / (float) division); } if (i == length - 2) Handles.color = gradient.Evaluate((j + 1f) / division); - Vector2 bezierNext = CalculateBezierPoint(point_a, tangent_a, tangent_b, point_b, j / (float)division); + Vector2 bezierNext = CalculateBezierPoint(point_a, tangent_a, tangent_b, point_b, j / (float) division); DrawAAPolyLineNonAlloc(thickness, bezierPrevious, bezierNext); bezierPrevious = bezierNext; }