/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
- using Delta.Utilities.Datatypes;
-
- namespace Delta.Rendering.Basics.Drawing
- {
- /// <summary>
- /// Helper class to draw a coordinate system grid and gizmo, mostly used
- /// for debugging or some tools that display simple 3D data like the mesh
- /// viewer in the ContentManager tool.
- /// </summary>
- public static class Grid
- {
- #region Draw (Static)
- /// <summary>
- /// Draw a 3D grid, default values are 20m x 20m with the Color Grey.
- /// </summary>
- public static void Draw()
- {
- Draw(20, 20, 20, 20, Color.Grey);
- }
-
- /// <summary>
- /// Draws a grid with the given positions/sizes and colors.
- /// </summary>
- /// <param name="width">Grid width</param>
- /// <param name="depth">Grid depth</param>
- /// <param name="widthFaceCount">Number of faces in width</param>
- /// <param name="depthFaceCount">Number of faces in depth</param>
- /// <param name="gridColor">The color for the inner grid lines</param>
- /// <param name="lineColor">The color for the marker lines</param>
- /// <param name="drawCoordinateSystemGizmo">If tue, a coordinate
- /// system gizmo will be drawn at (0f,0f,0.1f)</param>
- public static void Draw(float width, float depth, int widthFaceCount,
- int depthFaceCount, Color gridColor, Color lineColor,
- bool drawCoordinateSystemGizmo = true)
- {
- int numberOfWidthLines = widthFaceCount;
- if (numberOfWidthLines % 2 != 0)
- {
- numberOfWidthLines++;
- }
- int numberOfDepthLines = depthFaceCount;
- if (numberOfDepthLines % 2 != 0)
- {
- numberOfDepthLines++;
- }
- float faceWidth = width / widthFaceCount;
- float faceDepth = depth / depthFaceCount;
-
- // Draw everything 1cm off the ground (needs pretty good depth buffer)
- float offset = 0.01f; //0.0001f; 0.01cm is not enough!
-
- #region WidthLines
- Vector startPos = new Vector(0, -(depth * 0.5f), offset);
- Vector endPos = new Vector(0, (depth * 0.5f), offset);
-
- // Draw the middle line.
- DrawManager drawInstance = DrawManager.Instance;
- drawInstance.Draw3DLine(ref startPos, ref endPos, ref lineColor);
-
- // Add the width lines.
- for (int index = 0; index < numberOfWidthLines / 2; index++)
- {
- startPos += new Vector(faceWidth, 0, 0);
- endPos += new Vector(faceWidth, 0, 0);
-
- drawInstance.Draw3DLine(startPos, endPos,
- (index + 1) % 5 == 0
- ? lineColor
- : gridColor);
- }
-
- // Reset the position.
- startPos = new Vector(0, -(depth * 0.5f), offset);
- endPos = new Vector(0, (depth * 0.5f), offset);
-
- for (int index = 0; index < numberOfWidthLines / 2; index++)
- {
- startPos -= new Vector(faceWidth, 0, 0);
- endPos -= new Vector(faceWidth, 0, 0);
-
- drawInstance.Draw3DLine(startPos, endPos,
- (index + 1) % 5 == 0
- ? lineColor
- : gridColor);
- }
- #endregion
-
- #region DepthLines
- startPos = new Vector(-(width * 0.5f), 0, offset);
- endPos = new Vector((width * 0.5f), 0, offset);
- drawInstance.Draw3DLine(ref startPos, ref endPos, ref lineColor);
-
- // Add the depth lines.
- for (int index = 0; index < numberOfDepthLines / 2; index++)
- {
- startPos += new Vector(0, faceDepth, 0);
- endPos += new Vector(0, faceDepth, 0);
-
- drawInstance.Draw3DLine(startPos, endPos,
- (index + 1) % 5 == 0
- ? lineColor
- : gridColor);
- }
-
- startPos = new Vector(-(width * 0.5f), 0, offset);
- endPos = new Vector((width * 0.5f), 0, offset);
-
- for (int index = 0; index < numberOfDepthLines / 2; index++)
- {
- startPos -= new Vector(0, faceDepth, 0);
- endPos -= new Vector(0, faceDepth, 0);
-
- drawInstance.Draw3DLine(startPos, endPos,
- (index + 1) % 5 == 0
- ? lineColor
- : gridColor);
- }
- #endregion
-
- if (drawCoordinateSystemGizmo)
- {
- DrawCoordinateSystemGizmo(offset);
- }
- }
-
- /// <summary>
- /// Draws a 3-dimensional grid using "LineColor" as ambient color.
- /// </summary>
- /// <param name="width">Grid width.</param>
- /// <param name="depth">Grid depth.</param>
- /// <param name="widthFaceCount">Count of faces in width.</param>
- /// <param name="depthFaceCount">Count of faces in depth.</param>
- /// <param name="color">Grid color.</param>
- public static void Draw(float width, float depth, int widthFaceCount,
- int depthFaceCount, Color color)
- {
- Draw(width, depth, widthFaceCount, depthFaceCount, color, Color.White);
- }
- #endregion
-
- #region DrawCoordinateSystemGizmo (Static)
- /// <summary>
- /// Draw gizmo with offset of 0.01f
- /// </summary>
- public static void DrawCoordinateSystemGizmo()
- {
- DrawCoordinateSystemGizmo(0.01f);
- }
-
- /// <summary>
- /// DrawCoordinateSystemGizmo with given offset
- /// </summary>
- /// <param name="offset">The offset.</param>
- public static void DrawCoordinateSystemGizmo(float offset)
- {
- // Draw the coordinate system gizmo
- Vector vOffset = new Vector(0, 0, offset + 0.0025f);
- DrawManager drawInstance = DrawManager.Instance;
- drawInstance.Draw3DLine(vOffset,
- Vector.UnitX + vOffset, Color.Red);
- drawInstance.Draw3DLine(vOffset,
- Vector.UnitY + vOffset, Color.Green);
- drawInstance.Draw3DLine(vOffset,
- Vector.UnitZ + vOffset, Color.Blue);
- }
- #endregion
- }
- }