Forums › 💬 NodeCanvas › 🗨️ General Discussion › GraphEditor.DrawGrid fix performance
Hello,
I have huge perfomance issues cause by GraphEditor.DrawGrid so I rewrited the DrawGrid function which have execution time divided by 10:
Replace in GraphEditor.cs DrawGrid function by this piece of code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
private static MethodInfo s_applyWireMaterialMethod; public static void ApplyWireMaterial() { if (s_applyWireMaterialMethod == null) s_applyWireMaterialMethod = typeof(HandleUtility).GetMethod("ApplyWireMaterial", BindingFlags.NonPublic | BindingFlags.Static, null, new Type[]{ typeof(UnityEngine.Rendering.CompareFunction) }, null); s_applyWireMaterialMethod.Invoke(null, new object[] { Handles.zTest }); } //Draw a simple grid void DrawGrid(Rect container, Vector2 offset, float zoomFactor) { if (Event.current.type != EventType.Repaint) return; var scaledX = (container.width - offset.x) / zoomFactor; var scaledY = (container.height - offset.y) / zoomFactor; ApplyWireMaterial(); GL.PushMatrix(); GL.MultMatrix(Handles.matrix); GL.Begin(1); for (var i = 0 - (int)offset.x; i < scaledX; i++) { if (i % gridSize == 0) { GL.Color(new Color(0, 0, 0, i % (gridSize * 5) == 0 ? 0.2f : 0.1f)); GL.Vertex(new Vector3(i, 0, 0)); GL.Vertex(new Vector3(i, scaledY, 0)); } } for (var i = 0 - (int)offset.y; i < scaledY; i++) { if (i % gridSize == 0) { GL.Color(new Color(0, 0, 0, i % (gridSize * 5) == 0 ? 0.2f : 0.1f)); GL.Vertex(new Vector3(0, i, 0)); GL.Vertex(new Vector3(scaledX, i, 0)); } } GL.End(); GL.PopMatrix(); } |
Of course @Gavalakis you can use it for next update if you want!
Hello,
Indeed the grid function was way too slow because of many lines drawn and being part of the zoom group.
I have already changed the way it works to be faster, but of course also using GL directly is probably going to be further better as well, so thank you for that!
What version of unity is this code on? I am asking because it seems that the method “s_applyWireMaterialMethod” can not be found.
Thank you!
Oh :(. So try something like that:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
private static MethodInfo s_beginLineDrawingMethod; public static bool BeginLineDrawing(Matrix4x4 _matrix, bool _dottedLines, int _mode) { if(s_beginLineDrawingMethod == null) s_beginLineDrawingMethod = typeof(Handles).GetMethod("BeginLineDrawing", BindingFlags.NonPublic | BindingFlags.Static); return (bool)s_beginLineDrawingMethod.Invoke(null, new object[] {_matrix, _dottedLines, _mode}); } private static MethodInfo s_endLineDrawinggMethod; public static void EndLineDrawing() { if(s_endLineDrawinggMethod == null) s_endLineDrawinggMethod = typeof(Handles).GetMethod("EndLineDrawing", BindingFlags.NonPublic | BindingFlags.Static); s_endLineDrawinggMethod.Invoke(null, null); } //Draw a simple grid void DrawGrid(Rect container, Vector2 offset, float zoomFactor) { if (Event.current.type != EventType.Repaint) return; var scaledX = (container.width - offset.x) / zoomFactor; var scaledY = (container.height - offset.y) / zoomFactor; BeginLineDrawing(Handles.matrix, false,1); for (var i = 0 - (int)offset.x; i < scaledX; i++) { if (i % gridSize == 0) { GL.Color(new Color(0, 0, 0, i % (gridSize * 5) == 0 ? 0.2f : 0.1f)); GL.Vertex(new Vector3(i, 0, 0)); GL.Vertex(new Vector3(i, scaledY, 0)); } } for (var i = 0 - (int)offset.y; i < scaledY; i++) { if (i % gridSize == 0) { GL.Color(new Color(0, 0, 0, i % (gridSize * 5) == 0 ? 0.2f : 0.1f)); GL.Vertex(new Vector3(0, i, 0)); GL.Vertex(new Vector3(scaledX, i, 0)); } } EndLineDrawing(); } |
It should works!
Thanks for the follow up!
