Skip to content

Commit f9805de

Browse files
committed
Added styling system.
1 parent 68bbb55 commit f9805de

File tree

9 files changed

+396
-5
lines changed

9 files changed

+396
-5
lines changed

Example/Program.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ private static void Main(string[] args)
3737
DebugDraw.SetViewport(new Viewport(0, 0, s.X, s.Y));
3838
};
3939

40-
Vector3 position = new(0, 5, -1);
41-
Quaternion rotation = new Vector3(0, 0, 0).ToRad().ToQuaternion();
40+
Vector3 position = new(-5, 5, -5);
41+
Quaternion rotation = new Vector3(45, 45, 0).ToRad().ToQuaternion();
4242
Matrix4x4 projection = MathUtil.PerspectiveFovLH(
43-
MathF.PI / 2, window.Size.X / window.Size.Y, 0.1f, 1000f
43+
100f.ToRad(), window.Size.X / window.Size.Y, 0.1f, 1000f
4444
);
4545
Vector3 target = Vector3.Transform(Vector3.UnitZ, rotation);
4646
Vector3 up = Vector3.Transform(Vector3.UnitY, rotation);
@@ -63,7 +63,9 @@ private static void Main(string[] args)
6363
gl.ClearDepth(1.0f);
6464
gl.Clear((uint)ClearBufferMask.DepthBufferBit);
6565

66-
DebugDraw.DrawGrid(Matrix4x4.Identity, 100, Vector4.One);
66+
DebugDraw.PushStyleVar(DebugDrawStyleVar.GridAxisSize, 5);
67+
DebugDraw.DrawGrid(Matrix4x4.Identity, GridFlags.DrawAxis);
68+
DebugDraw.PopStyleVar();
6769

6870
DebugDraw.DrawBox(new(2, 4, 5), Quaternion.Identity, 1, 1, 1, new(1, 0, 0, 1));
6971

Hexa.NET.DebugDraw/DebugDraw.cs

Lines changed: 166 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,72 @@ public static DebugDrawCommandList CurrentList
9191
}
9292
}
9393

94+
public static void PushStyleColor(DebugDrawCol col, Vector4 color)
95+
{
96+
if (currentContext == null)
97+
{
98+
throw new InvalidOperationException("DebugDraw context is not set. Call DebugDraw.SetContext() before drawing.");
99+
}
100+
101+
currentContext.Style.PushStyleColor(col, color);
102+
}
103+
104+
public static void PopStyleColor()
105+
{
106+
if (currentContext == null)
107+
{
108+
throw new InvalidOperationException("DebugDraw context is not set. Call DebugDraw.SetContext() before drawing.");
109+
}
110+
111+
currentContext.Style.PopStyleColor();
112+
}
113+
114+
public static void PopStyleColor(int count)
115+
{
116+
if (currentContext == null)
117+
{
118+
throw new InvalidOperationException("DebugDraw context is not set. Call DebugDraw.SetContext() before drawing.");
119+
}
120+
121+
while (count-- != 0)
122+
{
123+
currentContext.Style.PopStyleColor();
124+
}
125+
}
126+
127+
public static void PushStyleVar(DebugDrawStyleVar var, float value)
128+
{
129+
if (currentContext == null)
130+
{
131+
throw new InvalidOperationException("DebugDraw context is not set. Call DebugDraw.SetContext() before drawing.");
132+
}
133+
134+
currentContext.Style.PushStyleVar(var, value);
135+
}
136+
137+
public static void PopStyleVar()
138+
{
139+
if (currentContext == null)
140+
{
141+
throw new InvalidOperationException("DebugDraw context is not set. Call DebugDraw.SetContext() before drawing.");
142+
}
143+
144+
currentContext.Style.PopStyleVar();
145+
}
146+
147+
public static void PopStyleVar(int count)
148+
{
149+
if (currentContext == null)
150+
{
151+
throw new InvalidOperationException("DebugDraw context is not set. Call DebugDraw.SetContext() before drawing.");
152+
}
153+
154+
while (count-- != 0)
155+
{
156+
currentContext.Style.PopStyleVar();
157+
}
158+
}
159+
94160
/// <summary>
95161
/// Saturates a float value to the range [0, 1].
96162
/// </summary>
@@ -1437,7 +1503,6 @@ public static void DrawQuadBillboard(Vector3 origin, Vector3 camOrigin, Vector3
14371503
/// <param name="matrix">The transformation matrix defining the orientation and position of the grid.</param>
14381504
/// <param name="size">The size of the grid (half-extent in each dimension).</param>
14391505
/// <param name="col">The color of the grid lines.</param>
1440-
///
14411506
public static void DrawGrid(Matrix4x4 matrix, int size, Vector4 col)
14421507
{
14431508
CurrentList.BeginDraw();
@@ -1476,5 +1541,105 @@ public static void DrawGrid(Matrix4x4 matrix, int size, Vector4 col)
14761541

14771542
CurrentList.RecordCmd(DebugDrawPrimitiveTopology.LineList);
14781543
}
1544+
1545+
/// <summary>
1546+
/// Draws a 3D grid in world space.
1547+
/// </summary>
1548+
/// <param name="matrix"> The transformation matrix defining the orientation and position of the grid.</param>
1549+
/// <param name="flags"> The flags that determine which elements of the grid to draw.</param>
1550+
/// <exception cref="InvalidOperationException"> The DebugDraw context is not set. Call DebugDraw.SetContext() before drawing.</exception>
1551+
public static void DrawGrid(Matrix4x4 matrix, GridFlags flags)
1552+
{
1553+
if (currentContext == null)
1554+
{
1555+
throw new InvalidOperationException("DebugDraw context is not set. Call DebugDraw.SetContext() before drawing.");
1556+
}
1557+
1558+
var style = currentContext.Style;
1559+
var size = (int)style.GridSize;
1560+
var spacing = style.GridSpacing;
1561+
1562+
CurrentList.BeginDraw();
1563+
bool axis = (flags & GridFlags.DrawAxis) != 0;
1564+
1565+
uint vertexCount = 2u * (uint)size * 2u + 4;
1566+
1567+
if (axis)
1568+
{
1569+
vertexCount += 6;
1570+
}
1571+
1572+
uint color = style.GetColorU32(DebugDrawCol.Grid);
1573+
1574+
CurrentList.ReserveGeometry(vertexCount, vertexCount);
1575+
var indices = CurrentList.Indices + CurrentList.IndexCount;
1576+
var vertices = CurrentList.Vertices + CurrentList.VertexCount;
1577+
1578+
int half = size / 2;
1579+
1580+
uint i = 0;
1581+
for (int x = -half; x <= half; x++)
1582+
{
1583+
var pos0 = Vector3.Transform(new Vector3(x * spacing, 0, -half), matrix);
1584+
var pos1 = Vector3.Transform(new Vector3(x * spacing, 0, half), matrix);
1585+
vertices[i] = new(pos0, default, color);
1586+
vertices[i + 1] = new(pos1, default, color);
1587+
indices[i] = i;
1588+
indices[i + 1] = i + 1;
1589+
i += 2;
1590+
}
1591+
1592+
for (int z = -half; z <= half; z++)
1593+
{
1594+
var pos0 = Vector3.Transform(new Vector3(-half, 0, z * spacing), matrix);
1595+
var pos1 = Vector3.Transform(new Vector3(half, 0, z * spacing), matrix);
1596+
vertices[i] = new(pos0, default, color);
1597+
vertices[i + 1] = new(pos1, default, color);
1598+
indices[i] = i;
1599+
indices[i + 1] = i + 1;
1600+
i += 2;
1601+
}
1602+
if (axis)
1603+
{
1604+
var colX = style.GetColorU32(DebugDrawCol.GridAxisX);
1605+
var colY = style.GetColorU32(DebugDrawCol.GridAxisY);
1606+
var colZ = style.GetColorU32(DebugDrawCol.GridAxisZ);
1607+
var axisSize = style.GridAxisSize;
1608+
1609+
{
1610+
var pos0 = Vector3.Transform(new Vector3(-axisSize, 0, 0), matrix);
1611+
var pos1 = Vector3.Transform(new Vector3(axisSize, 0, 0), matrix);
1612+
1613+
vertices[i] = new(pos0, default, colX);
1614+
vertices[i + 1] = new(pos1, default, colX);
1615+
indices[i] = i;
1616+
indices[i + 1] = i + 1;
1617+
i += 2;
1618+
}
1619+
1620+
{
1621+
var pos0 = Vector3.Transform(new Vector3(0, -axisSize, 0), matrix);
1622+
var pos1 = Vector3.Transform(new Vector3(0, axisSize, 0), matrix);
1623+
1624+
vertices[i] = new(pos0, default, colY);
1625+
vertices[i + 1] = new(pos1, default, colY);
1626+
indices[i] = i;
1627+
indices[i + 1] = i + 1;
1628+
i += 2;
1629+
}
1630+
1631+
{
1632+
var pos0 = Vector3.Transform(new Vector3(0, 0, -axisSize), matrix);
1633+
var pos1 = Vector3.Transform(new Vector3(0, 0, axisSize), matrix);
1634+
vertices[i] = new(pos0, default, colZ);
1635+
vertices[i + 1] = new(pos1, default, colZ);
1636+
indices[i] = i;
1637+
indices[i + 1] = i + 1;
1638+
i += 2;
1639+
}
1640+
}
1641+
1642+
CurrentList.RecordCmd(DebugDrawPrimitiveTopology.LineList);
1643+
}
14791644
}
14801645
}

Hexa.NET.DebugDraw/DebugDrawCol.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Hexa.NET.DebugDraw
2+
{
3+
public enum DebugDrawCol
4+
{
5+
Grid,
6+
GridAxisX,
7+
GridAxisY,
8+
GridAxisZ,
9+
Count = 4,
10+
}
11+
}

Hexa.NET.DebugDraw/DebugDrawContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ internal DebugDrawContext()
2626

2727
public nint FontTextureId { get; set; }
2828

29+
public DebugDrawStyle Style { get; } = new();
30+
2931
public void SetCamera(Matrix4x4 camera)
3032
{
3133
drawData.Camera = camera;
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
namespace Hexa.NET.DebugDraw
2+
{
3+
using System.Numerics;
4+
5+
public class DebugDrawStyle
6+
{
7+
private readonly Vector4[] colors = new Vector4[(int)DebugDrawCol.Count];
8+
private readonly Stack<(DebugDrawCol col, Vector4 color)> colorStack = [];
9+
private readonly Stack<(DebugDrawStyleVar styleVar, StyleVarValue value)> styleVarStack = [];
10+
11+
public DebugDrawStyle()
12+
{
13+
colors[(int)DebugDrawCol.Grid] = new(1, 1, 1, 0.8f);
14+
colors[(int)DebugDrawCol.GridAxisX] = new(1, 0, 0, 0.8f);
15+
colors[(int)DebugDrawCol.GridAxisY] = new(0, 1, 0, 0.8f);
16+
colors[(int)DebugDrawCol.GridAxisZ] = new(0, 0, 1, 0.8f);
17+
}
18+
19+
public Vector4[] Colors => colors;
20+
21+
public float GridSize = 100;
22+
public float GridSpacing = 1;
23+
public float GridAxisSize = 1;
24+
25+
public float GetStyleVar(DebugDrawStyleVar var)
26+
{
27+
return var switch
28+
{
29+
DebugDrawStyleVar.GridSize => GridSize,
30+
DebugDrawStyleVar.GridSpacing => GridSpacing,
31+
DebugDrawStyleVar.GridAxisSize => GridAxisSize,
32+
_ => throw new ArgumentOutOfRangeException(nameof(var))
33+
};
34+
}
35+
36+
public uint GetColorU32(DebugDrawCol col)
37+
{
38+
return DebugDraw.ColorConvertFloat4ToU32(colors[(int)col]);
39+
}
40+
41+
public void PushStyleColor(DebugDrawCol col, Vector4 color)
42+
{
43+
colorStack.Push((col, colors[(int)col]));
44+
colors[(int)col] = color;
45+
}
46+
47+
public void PopStyleColor()
48+
{
49+
if (colorStack.Count > 0)
50+
{
51+
var (col, color) = colorStack.Pop();
52+
colors[(int)col] = color;
53+
}
54+
}
55+
56+
private void SetAndPush<T>(DebugDrawStyleVar var, ref T target, T value)
57+
{
58+
var previousValue = StyleVarValue.From(target);
59+
target = value;
60+
styleVarStack.Push((var, previousValue));
61+
}
62+
63+
public void PushStyleVar(DebugDrawStyleVar var, float value)
64+
{
65+
switch (var)
66+
{
67+
case DebugDrawStyleVar.GridSize:
68+
SetAndPush(var, ref GridSize, value);
69+
break;
70+
71+
case DebugDrawStyleVar.GridSpacing:
72+
SetAndPush(var, ref GridSpacing, value);
73+
break;
74+
75+
case DebugDrawStyleVar.GridAxisSize:
76+
SetAndPush(var, ref GridAxisSize, value);
77+
break;
78+
79+
// Handle other style variables here
80+
default:
81+
throw new ArgumentOutOfRangeException(nameof(var));
82+
}
83+
}
84+
85+
public void PopStyleVar()
86+
{
87+
if (styleVarStack.Count > 0)
88+
{
89+
var (var, previousValue) = styleVarStack.Pop();
90+
switch (var)
91+
{
92+
case DebugDrawStyleVar.GridSize:
93+
GridSize = previousValue.FloatValue;
94+
break;
95+
96+
case DebugDrawStyleVar.GridSpacing:
97+
GridSpacing = previousValue.FloatValue;
98+
break;
99+
100+
case DebugDrawStyleVar.GridAxisSize:
101+
GridAxisSize = previousValue.FloatValue;
102+
break;
103+
// Handle other style variables here
104+
default:
105+
throw new ArgumentOutOfRangeException(nameof(var));
106+
}
107+
}
108+
}
109+
}
110+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Hexa.NET.DebugDraw
2+
{
3+
public enum DebugDrawStyleVar
4+
{
5+
GridSize,
6+
GridSpacing,
7+
GridAxisSize,
8+
Count = 3,
9+
}
10+
}

Hexa.NET.DebugDraw/GridFlags.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Hexa.NET.DebugDraw
2+
{
3+
public enum GridFlags
4+
{
5+
None = 0,
6+
DrawAxis = 1,
7+
}
8+
}

Hexa.NET.DebugDraw/StyleVarType.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Hexa.NET.DebugDraw
2+
{
3+
public enum StyleVarType : ushort
4+
{
5+
Bool,
6+
Float,
7+
Vector2,
8+
Vector3,
9+
Vector4
10+
}
11+
}

0 commit comments

Comments
 (0)