mirror of
https://github.com/Siccity/xNode.git
synced 2025-12-20 09:16:01 +08:00
Merge pull request #207 from nostek/feature/masterfix
Optimize GC allocations in NodeEditorWindow
This commit is contained in:
commit
30b825ff1c
@ -15,6 +15,7 @@ namespace XNodeEditor {
|
|||||||
private int topPadding { get { return isDocked() ? 19 : 22; } }
|
private int topPadding { get { return isDocked() ? 19 : 22; } }
|
||||||
/// <summary> Executed after all other window GUI. Useful if Zoom is ruining your day. Automatically resets after being run.</summary>
|
/// <summary> Executed after all other window GUI. Useful if Zoom is ruining your day. Automatically resets after being run.</summary>
|
||||||
public event Action onLateGUI;
|
public event Action onLateGUI;
|
||||||
|
private static readonly Vector3[] polyLineTempArray = new Vector3[2];
|
||||||
|
|
||||||
private void OnGUI() {
|
private void OnGUI() {
|
||||||
Event e = Event.current;
|
Event e = Event.current;
|
||||||
@ -116,23 +117,45 @@ namespace XNodeEditor {
|
|||||||
if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
|
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;
|
||||||
|
float uuu = uu * u, ttt = tt * t;
|
||||||
|
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)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary> Draws a line segment without allocating temporary arrays </summary>
|
||||||
|
static void DrawAAPolyLineNonAlloc(float thickness, Vector2 p0, Vector2 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);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary> Draw a bezier from output to input in grid coordinates </summary>
|
/// <summary> Draw a bezier from output to input in grid coordinates </summary>
|
||||||
public void DrawNoodle(Gradient gradient, NoodlePath path, NoodleStroke stroke, float thickness, List<Vector2> gridPoints) {
|
public void DrawNoodle(Gradient gradient, NoodlePath path, NoodleStroke stroke, float thickness, List<Vector2> 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);
|
Handles.color = gradient.Evaluate(0f);
|
||||||
int length = gridPoints.Count;
|
int length = gridPoints.Count;
|
||||||
switch (path) {
|
switch (path) {
|
||||||
case NoodlePath.Curvy:
|
case NoodlePath.Curvy:
|
||||||
Vector2 outputTangent = Vector2.right;
|
Vector2 outputTangent = Vector2.right;
|
||||||
for (int i = 0; i < length - 1; i++) {
|
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
|
// Cached most variables that repeat themselves here to avoid so many indexer calls :p
|
||||||
Vector2 point_a = windowPoints[i];
|
Vector2 point_a = gridPoints[i];
|
||||||
Vector2 point_b = windowPoints[i + 1];
|
Vector2 point_b = gridPoints[i + 1];
|
||||||
float dist_ab = Vector2.Distance(point_a, point_b);
|
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) {
|
if (i < length - 2) {
|
||||||
Vector2 point_c = windowPoints[i + 2];
|
Vector2 point_c = gridPoints[i + 2];
|
||||||
Vector2 ab = (point_b - point_a).normalized;
|
Vector2 ab = (point_b - point_a).normalized;
|
||||||
Vector2 cb = (point_b - point_c).normalized;
|
Vector2 cb = (point_b - point_c).normalized;
|
||||||
Vector2 ac = (point_c - point_a).normalized;
|
Vector2 ac = (point_c - point_a).normalized;
|
||||||
@ -140,37 +163,41 @@ namespace XNodeEditor {
|
|||||||
float tangentLength = (dist_ab + Vector2.Distance(point_b, point_c)) * 0.005f * zoom;
|
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)));
|
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;
|
inputTangent = p;
|
||||||
} else {
|
} 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.
|
// Calculates the tangents for the bezier's curves.
|
||||||
Vector2 tangent_a = point_a + outputTangent * 50 / zoom;
|
float zoomCoef = 50 / zoom;
|
||||||
Vector2 tangent_b = point_b + inputTangent * 50 / zoom;
|
Vector2 tangent_a = point_a + outputTangent * zoomCoef;
|
||||||
|
Vector2 tangent_b = point_b + inputTangent * zoomCoef;
|
||||||
// Hover effect.
|
// Hover effect.
|
||||||
int division = Mathf.RoundToInt(.2f * dist_ab) + 3;
|
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.
|
// Coloring and bezier drawing.
|
||||||
for (int j = 0; j < points.Length - 1; j++) {
|
int draw = 0;
|
||||||
|
Vector2 bezierPrevious = point_a;
|
||||||
|
for (int j = 1; j <= division; ++j) {
|
||||||
if (stroke == NoodleStroke.Dashed) {
|
if (stroke == NoodleStroke.Dashed) {
|
||||||
draw++;
|
draw++;
|
||||||
if (draw >= 2) draw = -2;
|
if (draw >= 2) draw = -2;
|
||||||
if (draw < 0) continue;
|
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)
|
||||||
if (i == gridPoints.Count - 2) Handles.color = gradient.Evaluate((j + 1f) / points.Length);
|
Handles.color = gradient.Evaluate((j + 1f) / division);
|
||||||
Handles.DrawAAPolyLine(thickness, points[j], points[j + 1]);
|
Vector2 bezierNext = CalculateBezierPoint(point_a, tangent_a, tangent_b, point_b, j / (float) division);
|
||||||
|
DrawAAPolyLineNonAlloc(thickness, bezierPrevious, bezierNext);
|
||||||
|
bezierPrevious = bezierNext;
|
||||||
}
|
}
|
||||||
outputTangent = -inputTangent;
|
outputTangent = -inputTangent;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case NoodlePath.Straight:
|
case NoodlePath.Straight:
|
||||||
for (int i = 0; i < length - 1; i++) {
|
for (int i = 0; i < length - 1; i++) {
|
||||||
Vector2 point_a = windowPoints[i];
|
Vector2 point_a = gridPoints[i];
|
||||||
Vector2 point_b = windowPoints[i + 1];
|
Vector2 point_b = gridPoints[i + 1];
|
||||||
// Draws the line with the coloring.
|
// Draws the line with the coloring.
|
||||||
Vector2 prev_point = point_a;
|
Vector2 prev_point = point_a;
|
||||||
// Approximately one segment per 5 pixels
|
// Approximately one segment per 5 pixels
|
||||||
@ -182,8 +209,8 @@ namespace XNodeEditor {
|
|||||||
float t = j / (float) segments;
|
float t = j / (float) segments;
|
||||||
Vector2 lerp = Vector2.Lerp(point_a, point_b, t);
|
Vector2 lerp = Vector2.Lerp(point_a, point_b, t);
|
||||||
if (draw > 0) {
|
if (draw > 0) {
|
||||||
if (i == gridPoints.Count - 2) Handles.color = gradient.Evaluate(t);
|
if (i == length - 2) Handles.color = gradient.Evaluate(t);
|
||||||
Handles.DrawAAPolyLine(thickness, prev_point, lerp);
|
DrawAAPolyLineNonAlloc(thickness, prev_point, lerp);
|
||||||
}
|
}
|
||||||
prev_point = lerp;
|
prev_point = lerp;
|
||||||
if (stroke == NoodleStroke.Dashed && draw >= 2) draw = -2;
|
if (stroke == NoodleStroke.Dashed && draw >= 2) draw = -2;
|
||||||
@ -193,49 +220,49 @@ namespace XNodeEditor {
|
|||||||
case NoodlePath.Angled:
|
case NoodlePath.Angled:
|
||||||
for (int i = 0; i < length - 1; i++) {
|
for (int i = 0; i < length - 1; i++) {
|
||||||
if (i == length - 1) continue; // Skip last index
|
if (i == length - 1) continue; // Skip last index
|
||||||
if (windowPoints[i].x <= windowPoints[i + 1].x - (50 / zoom)) {
|
if (gridPoints[i].x <= gridPoints[i + 1].x - (50 / zoom)) {
|
||||||
float midpoint = (windowPoints[i].x + windowPoints[i + 1].x) * 0.5f;
|
float midpoint = (gridPoints[i].x + gridPoints[i + 1].x) * 0.5f;
|
||||||
Vector2 start_1 = windowPoints[i];
|
Vector2 start_1 = gridPoints[i];
|
||||||
Vector2 end_1 = windowPoints[i + 1];
|
Vector2 end_1 = gridPoints[i + 1];
|
||||||
start_1.x = midpoint;
|
start_1.x = midpoint;
|
||||||
end_1.x = midpoint;
|
end_1.x = midpoint;
|
||||||
if (i == gridPoints.Count - 2) {
|
if (i == length - 2) {
|
||||||
Handles.DrawAAPolyLine(thickness, windowPoints[i], start_1);
|
DrawAAPolyLineNonAlloc(thickness, gridPoints[i], start_1);
|
||||||
Handles.color = gradient.Evaluate(0.5f);
|
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.color = gradient.Evaluate(1f);
|
||||||
Handles.DrawAAPolyLine(thickness, end_1, windowPoints[i + 1]);
|
DrawAAPolyLineNonAlloc(thickness, end_1, gridPoints[i + 1]);
|
||||||
} else {
|
} else {
|
||||||
Handles.DrawAAPolyLine(thickness, windowPoints[i], start_1);
|
DrawAAPolyLineNonAlloc(thickness, gridPoints[i], start_1);
|
||||||
Handles.DrawAAPolyLine(thickness, start_1, end_1);
|
DrawAAPolyLineNonAlloc(thickness, start_1, end_1);
|
||||||
Handles.DrawAAPolyLine(thickness, end_1, windowPoints[i + 1]);
|
DrawAAPolyLineNonAlloc(thickness, end_1, gridPoints[i + 1]);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
float midpoint = (windowPoints[i].y + windowPoints[i + 1].y) * 0.5f;
|
float midpoint = (gridPoints[i].y + gridPoints[i + 1].y) * 0.5f;
|
||||||
Vector2 start_1 = windowPoints[i];
|
Vector2 start_1 = gridPoints[i];
|
||||||
Vector2 end_1 = windowPoints[i + 1];
|
Vector2 end_1 = gridPoints[i + 1];
|
||||||
start_1.x += 25 / zoom;
|
start_1.x += 25 / zoom;
|
||||||
end_1.x -= 25 / zoom;
|
end_1.x -= 25 / zoom;
|
||||||
Vector2 start_2 = start_1;
|
Vector2 start_2 = start_1;
|
||||||
Vector2 end_2 = end_1;
|
Vector2 end_2 = end_1;
|
||||||
start_2.y = midpoint;
|
start_2.y = midpoint;
|
||||||
end_2.y = midpoint;
|
end_2.y = midpoint;
|
||||||
if (i == gridPoints.Count - 2) {
|
if (i == length - 2) {
|
||||||
Handles.DrawAAPolyLine(thickness, windowPoints[i], start_1);
|
DrawAAPolyLineNonAlloc(thickness, gridPoints[i], start_1);
|
||||||
Handles.color = gradient.Evaluate(0.25f);
|
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.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.color = gradient.Evaluate(0.75f);
|
||||||
Handles.DrawAAPolyLine(thickness, end_2, end_1);
|
DrawAAPolyLineNonAlloc(thickness, end_2, end_1);
|
||||||
Handles.color = gradient.Evaluate(1f);
|
Handles.color = gradient.Evaluate(1f);
|
||||||
Handles.DrawAAPolyLine(thickness, end_1, windowPoints[i + 1]);
|
DrawAAPolyLineNonAlloc(thickness, end_1, gridPoints[i + 1]);
|
||||||
} else {
|
} else {
|
||||||
Handles.DrawAAPolyLine(thickness, windowPoints[i], start_1);
|
DrawAAPolyLineNonAlloc(thickness, gridPoints[i], start_1);
|
||||||
Handles.DrawAAPolyLine(thickness, start_1, start_2);
|
DrawAAPolyLineNonAlloc(thickness, start_1, start_2);
|
||||||
Handles.DrawAAPolyLine(thickness, start_2, end_2);
|
DrawAAPolyLineNonAlloc(thickness, start_2, end_2);
|
||||||
Handles.DrawAAPolyLine(thickness, end_2, end_1);
|
DrawAAPolyLineNonAlloc(thickness, end_2, end_1);
|
||||||
Handles.DrawAAPolyLine(thickness, end_1, windowPoints[i + 1]);
|
DrawAAPolyLineNonAlloc(thickness, end_1, gridPoints[i + 1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -249,6 +276,8 @@ namespace XNodeEditor {
|
|||||||
List<RerouteReference> selection = preBoxSelectionReroute != null ? new List<RerouteReference>(preBoxSelectionReroute) : new List<RerouteReference>();
|
List<RerouteReference> selection = preBoxSelectionReroute != null ? new List<RerouteReference>(preBoxSelectionReroute) : new List<RerouteReference>();
|
||||||
hoveredReroute = new RerouteReference();
|
hoveredReroute = new RerouteReference();
|
||||||
|
|
||||||
|
List<Vector2> gridPoints = new List<Vector2>(2);
|
||||||
|
|
||||||
Color col = GUI.color;
|
Color col = GUI.color;
|
||||||
foreach (XNode.Node node in graph.nodes) {
|
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.
|
//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.
|
||||||
@ -277,7 +306,7 @@ namespace XNodeEditor {
|
|||||||
|
|
||||||
List<Vector2> reroutePoints = output.GetReroutePoints(k);
|
List<Vector2> reroutePoints = output.GetReroutePoints(k);
|
||||||
|
|
||||||
List<Vector2> gridPoints = new List<Vector2>();
|
gridPoints.Clear();
|
||||||
gridPoints.Add(fromRect.center);
|
gridPoints.Add(fromRect.center);
|
||||||
gridPoints.AddRange(reroutePoints);
|
gridPoints.AddRange(reroutePoints);
|
||||||
gridPoints.Add(toRect.center);
|
gridPoints.Add(toRect.center);
|
||||||
@ -343,6 +372,8 @@ namespace XNodeEditor {
|
|||||||
//Save guiColor so we can revert it
|
//Save guiColor so we can revert it
|
||||||
Color guiColor = GUI.color;
|
Color guiColor = GUI.color;
|
||||||
|
|
||||||
|
List<XNode.NodePort> removeEntries = new List<XNode.NodePort>();
|
||||||
|
|
||||||
if (e.type == EventType.Layout) culledNodes = new List<XNode.Node>();
|
if (e.type == EventType.Layout) culledNodes = new List<XNode.Node>();
|
||||||
for (int n = 0; n < graph.nodes.Count; n++) {
|
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.
|
// Skip null nodes. The user could be in the process of renaming scripts, so removing them at this point is not advisable.
|
||||||
@ -360,7 +391,10 @@ namespace XNodeEditor {
|
|||||||
} else if (culledNodes.Contains(node)) continue;
|
} else if (culledNodes.Contains(node)) continue;
|
||||||
|
|
||||||
if (e.type == EventType.Repaint) {
|
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);
|
NodeEditor nodeEditor = NodeEditor.GetEditor(node, this);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user