/Rendering/Basics/Drawing/Grid.cs
C# | 168 lines | 108 code | 20 blank | 40 comment | 13 complexity | ce47b8cd67106da1854dfd9dbc2d2c0c MD5 | raw file
1using Delta.Utilities.Datatypes; 2 3namespace Delta.Rendering.Basics.Drawing 4{ 5 /// <summary> 6 /// Helper class to draw a coordinate system grid and gizmo, mostly used 7 /// for debugging or some tools that display simple 3D data like the mesh 8 /// viewer in the ContentManager tool. 9 /// </summary> 10 public static class Grid 11 { 12 #region Draw (Static) 13 /// <summary> 14 /// Draw a 3D grid, default values are 20m x 20m with the Color Grey. 15 /// </summary> 16 public static void Draw() 17 { 18 Draw(20, 20, 20, 20, Color.Grey); 19 } 20 21 /// <summary> 22 /// Draws a grid with the given positions/sizes and colors. 23 /// </summary> 24 /// <param name="width">Grid width</param> 25 /// <param name="depth">Grid depth</param> 26 /// <param name="widthFaceCount">Number of faces in width</param> 27 /// <param name="depthFaceCount">Number of faces in depth</param> 28 /// <param name="gridColor">The color for the inner grid lines</param> 29 /// <param name="lineColor">The color for the marker lines</param> 30 /// <param name="drawCoordinateSystemGizmo">If tue, a coordinate 31 /// system gizmo will be drawn at (0f,0f,0.1f)</param> 32 public static void Draw(float width, float depth, int widthFaceCount, 33 int depthFaceCount, Color gridColor, Color lineColor, 34 bool drawCoordinateSystemGizmo = true) 35 { 36 int numberOfWidthLines = widthFaceCount; 37 if (numberOfWidthLines % 2 != 0) 38 { 39 numberOfWidthLines++; 40 } 41 int numberOfDepthLines = depthFaceCount; 42 if (numberOfDepthLines % 2 != 0) 43 { 44 numberOfDepthLines++; 45 } 46 float faceWidth = width / widthFaceCount; 47 float faceDepth = depth / depthFaceCount; 48 49 // Draw everything 1cm off the ground (needs pretty good depth buffer) 50 float offset = 0.01f; //0.0001f; 0.01cm is not enough! 51 52 #region WidthLines 53 Vector startPos = new Vector(0, -(depth * 0.5f), offset); 54 Vector endPos = new Vector(0, (depth * 0.5f), offset); 55 56 // Draw the middle line. 57 DrawManager drawInstance = DrawManager.Instance; 58 drawInstance.Draw3DLine(ref startPos, ref endPos, ref lineColor); 59 60 // Add the width lines. 61 for (int index = 0; index < numberOfWidthLines / 2; index++) 62 { 63 startPos += new Vector(faceWidth, 0, 0); 64 endPos += new Vector(faceWidth, 0, 0); 65 66 drawInstance.Draw3DLine(startPos, endPos, 67 (index + 1) % 5 == 0 68 ? lineColor 69 : gridColor); 70 } 71 72 // Reset the position. 73 startPos = new Vector(0, -(depth * 0.5f), offset); 74 endPos = new Vector(0, (depth * 0.5f), offset); 75 76 for (int index = 0; index < numberOfWidthLines / 2; index++) 77 { 78 startPos -= new Vector(faceWidth, 0, 0); 79 endPos -= new Vector(faceWidth, 0, 0); 80 81 drawInstance.Draw3DLine(startPos, endPos, 82 (index + 1) % 5 == 0 83 ? lineColor 84 : gridColor); 85 } 86 #endregion 87 88 #region DepthLines 89 startPos = new Vector(-(width * 0.5f), 0, offset); 90 endPos = new Vector((width * 0.5f), 0, offset); 91 drawInstance.Draw3DLine(ref startPos, ref endPos, ref lineColor); 92 93 // Add the depth lines. 94 for (int index = 0; index < numberOfDepthLines / 2; index++) 95 { 96 startPos += new Vector(0, faceDepth, 0); 97 endPos += new Vector(0, faceDepth, 0); 98 99 drawInstance.Draw3DLine(startPos, endPos, 100 (index + 1) % 5 == 0 101 ? lineColor 102 : gridColor); 103 } 104 105 startPos = new Vector(-(width * 0.5f), 0, offset); 106 endPos = new Vector((width * 0.5f), 0, offset); 107 108 for (int index = 0; index < numberOfDepthLines / 2; index++) 109 { 110 startPos -= new Vector(0, faceDepth, 0); 111 endPos -= new Vector(0, faceDepth, 0); 112 113 drawInstance.Draw3DLine(startPos, endPos, 114 (index + 1) % 5 == 0 115 ? lineColor 116 : gridColor); 117 } 118 #endregion 119 120 if (drawCoordinateSystemGizmo) 121 { 122 DrawCoordinateSystemGizmo(offset); 123 } 124 } 125 126 /// <summary> 127 /// Draws a 3-dimensional grid using "LineColor" as ambient color. 128 /// </summary> 129 /// <param name="width">Grid width.</param> 130 /// <param name="depth">Grid depth.</param> 131 /// <param name="widthFaceCount">Count of faces in width.</param> 132 /// <param name="depthFaceCount">Count of faces in depth.</param> 133 /// <param name="color">Grid color.</param> 134 public static void Draw(float width, float depth, int widthFaceCount, 135 int depthFaceCount, Color color) 136 { 137 Draw(width, depth, widthFaceCount, depthFaceCount, color, Color.White); 138 } 139 #endregion 140 141 #region DrawCoordinateSystemGizmo (Static) 142 /// <summary> 143 /// Draw gizmo with offset of 0.01f 144 /// </summary> 145 public static void DrawCoordinateSystemGizmo() 146 { 147 DrawCoordinateSystemGizmo(0.01f); 148 } 149 150 /// <summary> 151 /// DrawCoordinateSystemGizmo with given offset 152 /// </summary> 153 /// <param name="offset">The offset.</param> 154 public static void DrawCoordinateSystemGizmo(float offset) 155 { 156 // Draw the coordinate system gizmo 157 Vector vOffset = new Vector(0, 0, offset + 0.0025f); 158 DrawManager drawInstance = DrawManager.Instance; 159 drawInstance.Draw3DLine(vOffset, 160 Vector.UnitX + vOffset, Color.Red); 161 drawInstance.Draw3DLine(vOffset, 162 Vector.UnitY + vOffset, Color.Green); 163 drawInstance.Draw3DLine(vOffset, 164 Vector.UnitZ + vOffset, Color.Blue); 165 } 166 #endregion 167 } 168}