Drawing Scopes

A powerful abstraction used in this package is the notion of scopes. Scopes can be used to set the color, matrix, line width or duration of multiple drawing commands at once without having to individually specify them or having to do matrix multiplications manually. This is both faster and leads to more readable code.

// Draw three red cubes
using (Draw.WithColor(Color.red)) {
Draw.WireBox(transform.position, Vector3.one);
Draw.WireBox(transform.position + Vector3.right, Vector3.one);
Draw.WireBox(transform.position - Vector3.right, Vector3.one);
}
Using matrix scopes is very useful if you want to, for example, draw in local space relative to some object. In the example below, a cylinder is drawn in an object's local space. This means it is moved, rotated and scaled with the object automatically.

using (Draw.InLocalSpace(transform)) {
// Draw a box at (0,0,0) relative to the current object
// This means it will show up at the object's position
Draw.WireBox(Vector3.zero, Vector3.one);
}

// Equivalent code using the lower level WithMatrix scope
using (Draw.WithMatrix(transform.localToWorldMatrix)) {
Draw.WireBox(Vector3.zero, Vector3.one);
}

In the example below the color and duration scopes are shown. // This box will be drawn for 2 seconds
using (Draw.WithDuration(2)) {
Draw.WireBox(Vector3.zero, Vector3.one);
}

// Scopes can be nested
using (Draw.WithColor(Color.red)) {
using (Draw.WithDuration(2)) {
Draw.WireBox(Vector3.zero, Vector3.one);
}
}