PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Rendering/Basics/Drawing/Grid.cs

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