Drawing in 2D

How to draw in the XY or XZ 2D planes.

Sometimes all you have are some 2D coordinates. Sure, you can convert a float2 to a float3 using e.g.new float3(point.x, point.y, 0)
, but that's hardly ergonomic. To help you draw using 2D coordinates more easily you can access Draw.xy or Draw.xz.

var p1 = new Vector2(0, 1);
var p2 = new Vector2(5, 7);

// Draw it in the XY plane
Draw.xy.Line(p1, p2);

// Draw it in the XZ plane
Draw.xz.Line(p1, p2);
When you use Draw.xy or Draw.xz, the code will ensure reasonable defaults are used for those planes. For example, when you draw a circle using Draw.xy, the circle will actually lie in the XY plane. This applies to many things, for example the orientation of arrows, circles, grids, etc.

Draw.xy.Circle(new Vector2(2, 2), 0.9f, Palette.Red);
Draw.xy.Label2D(new Vector2(2, 2), "I'm drawn in the XY plane", 20, LabelAlignment.Center, Color.black);

Draw.xz.Circle(new Vector2(2, 2), 0.9f, Palette.Blue);
Draw.xz.Label2D(new Vector2(2, 2), "and I'm in the XZ plane", 20, LabelAlignment.Center, Color.black);

Combining 2D rendering with in-game rendering

You can combine the XY and XZ plane rendering with ingame/editor rendering, and you can also cache the command builder in a variable to not have to type everything at the same time (this is also slightly faster).

Draw.ingame.xy.Cross(new float2(1, 2));
Draw.editor.xz.Cross(new float2(1, 2));

var draw = Draw.ingame.xy;
draw.Cross(new float2(1, 2));
Keep in mind that while the editor/ingame command builders refer to different underlaying command builders, the XY and XZ command builders refer to the same command builder. This means you can freely mix scopes between them:

// Draw and Draw.xy refer to the same command builder, so this works.
using (Draw.WithLineWidth(2)) {
Draw.xy.Circle(float2.zero, 1.0f);
}

// But Draw and Draw.ingame do not. So this does *not* work.
using (Draw.WithLineWidth(2)) {
Draw.ingame.xy.Circle(float2.zero, 1.0f);
}

Exclusive 2D primitives

There are some primitives which are only available in 2D.

WireRectangle (rect)

Draws a rectangle outline.

SolidRectangle (rect)

Draws a solid rectangle.

WirePill (..., radius, [color])

Draws a wire pill in 2D.

See

CommandBuilder2D for a full list of 2D commands.

Different depths

Even when primarily drawing in a single plane, it can be useful to draw using 3D coordinates, but take advantage of the default orientations of either the XY or XZ plane. The CommandBuilder2D has overloads for taking 3D coordinates as well as 2D coordinates for this purpose. It will behave identically to using the methods from the Draw or CommandBuilder classes, but with different default orientations for things. For exampleDraw.xy.Circle(new float3(1, 2, 3), 1f);
will draw a circle oriented with its normal in the Z direction, in contrast to when using Draw.xz.Circle which would make the normal be in the Y direction.

You can also accomplish a similar effect using the Draw.WithMatrix scope, and continuing to use purely 2D coordinates.

for (int i = 0; i < 5; i++) {
// You can use a matrix to offset the drawing
using (Draw.WithMatrix(Matrix4x4.Translate(new Vector3(0, 0, i*0.2f)))) {
Draw.xy.Cross(new Vector2(3, 2), 0.5f, Palette.Red);
Draw.xy.Circle(new Vector2(3, 2), 0.5f, Palette.Red);
}

// Or pass 3D coordinates directly
Draw.xy.Cross(new Vector3(1, 2, i*0.2f), 0.5f, Palette.Blue);
Draw.xy.Circle(new Vector3(1, 2, i*0.2f), 0.5f, Palette.Blue);
}