From 72a80deb278ee5c4e23463cb9c76ce41b2233a62 Mon Sep 17 00:00:00 2001 From: Thor Brigsted Date: Sun, 3 Mar 2019 02:42:07 +0100 Subject: [PATCH] Improved bezier tangents --- Scripts/Editor/NodeEditorAction.cs | 4 ++-- Scripts/Editor/NodeEditorGUI.cs | 25 +++++++++++++++---------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/Scripts/Editor/NodeEditorAction.cs b/Scripts/Editor/NodeEditorAction.cs index b4f6562..c641b7d 100644 --- a/Scripts/Editor/NodeEditorAction.cs +++ b/Scripts/Editor/NodeEditorAction.cs @@ -431,11 +431,11 @@ namespace XNodeEditor { Vector2 to = Vector2.zero; for (int i = 0; i < draggedOutputReroutes.Count; i++) { to = draggedOutputReroutes[i]; - DrawConnection(from, to, col); + DrawNoodle(from, i == 0 ? draggedOutput : null, to, null, col); from = to; } to = draggedOutputTarget != null ? portConnectionPoints[draggedOutputTarget].center : WindowToGridPosition(Event.current.mousePosition); - DrawConnection(from, to, col); + DrawNoodle(from, draggedOutputReroutes.Any() ? null : draggedOutput, to, draggedOutputTarget, col); Color bgcol = Color.black; Color frcol = col; diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index e22a0b4..3033108 100644 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -117,20 +117,25 @@ namespace XNodeEditor { } /// Draw a bezier from startpoint to endpoint, both in grid coordinates - public void DrawConnection(Vector2 startPoint, Vector2 endPoint, Color col) { + public void DrawNoodle(Vector2 startPoint, XNode.NodePort startPort, Vector2 endPoint, XNode.NodePort endPort, Color col) { startPoint = GridToWindowPosition(startPoint); endPoint = GridToWindowPosition(endPoint); switch (NodeEditorPreferences.GetSettings().noodleType) { case NodeEditorPreferences.NoodleType.Curve: - Vector2 startTangent = startPoint; - if (startPoint.x < endPoint.x) startTangent.x = Mathf.LerpUnclamped(startPoint.x, endPoint.x, 0.7f); - else startTangent.x = Mathf.LerpUnclamped(startPoint.x, endPoint.x, -0.7f); + float minTangent = Vector2.Distance(endPoint, startPoint)*0.5f; - Vector2 endTangent = endPoint; - if (startPoint.x > endPoint.x) endTangent.x = Mathf.LerpUnclamped(endPoint.x, startPoint.x, -0.7f); - else endTangent.x = Mathf.LerpUnclamped(endPoint.x, startPoint.x, 0.7f); - Handles.DrawBezier(startPoint, endPoint, startTangent, endTangent, col, null, 4); + Vector2 startTangent = Vector2.zero; + startTangent.x = (endPoint.x - startPoint.x) * 0.7f; + startTangent.x = Mathf.Sign(startTangent.x) * Mathf.Max(Mathf.Abs(startTangent.x), minTangent); + if (startPort != null) startTangent.x = Mathf.Abs(startTangent.x); + + Vector2 endTangent = Vector2.zero; + endTangent.x = (startPoint.x - endPoint.x) * 0.7f; + endTangent.x = Mathf.Sign(endTangent.x) * Mathf.Max(Mathf.Abs(endTangent.x), minTangent); + if (endPort != null) endTangent.x = -Mathf.Abs(endTangent.x); + + Handles.DrawBezier(startPoint, endPoint, startPoint + startTangent, endPoint + endTangent, col, null, 4); break; case NodeEditorPreferences.NoodleType.Line: Handles.color = col; @@ -201,12 +206,12 @@ namespace XNodeEditor { // Loop through reroute points and draw the path for (int i = 0; i < reroutePoints.Count; i++) { to = reroutePoints[i]; - DrawConnection(from, to, connectionColor); + DrawNoodle(from, i == 0 ? output : null, to, null, connectionColor); from = to; } to = toRect.center; - DrawConnection(from, to, connectionColor); + DrawNoodle(from, reroutePoints.Any() ? null : output, to, input, connectionColor); // Loop through reroute points again and draw the points for (int i = 0; i < reroutePoints.Count; i++) {