Scopes and Defaults
Scopes can be used to easily change the space a visual is drawn in, or to change certain properties of multiple visuals at the same time.
Similarly global defaults are available for certain properties which will be applied to all subsequent visuals.
All scopes are Disposable and work with C#'s using statement.
using (Draw.SCOPE(...)) {
// Draw visuals here.
// Nesting scopes are also supported.
using (Draw.SCOPE(...)) {
// Draw more visuals here.
}
}
Color
// Sets the default color to yellow.
// Any visual that isn't given a color will default to this value.
Draw.Color.Default = Color.yellow;
// Draws a line with the new default yellow color.
Draw.Line(new(-1, -1, 0), new(-1, 1, 0));
using (Draw.WithColor(Color.green)) {
// Draws a green line and arrow.
Draw.Line(new(-0.5f, -1, 0), new(-0.5f, 1, 0));
Draw.Arrow(new(0, -1, 0), new(0, 1, 0));
// This visual's color is explicitly set to red, overriding
// the scope and global default.
Draw.Arrow(new(0.5f, -1, 0), new(0.5f, 1, 0), Color.red);
}
Facing
By default any "2D" visuals, such as the Circle and Rectangle,
are oriented so that they face the Z axis, and lie along XY plane.
The facing can be changed with the following scope methods:
- OnPlane - Sets the facing to a custom normal vector.
- OnXY - Sets the facing on the
XYplane. - OnXZ - Sets the facing on the
XZplane. - OnYZ - Sets the facing on the
YZplane.
// Sets the default facing to a custom normal vector.
Draw.Facing.Default = math.normalize(new float3(1, 1, 0));
// Draws a circle lying on the plane with the normal `1, 1, 0`.
Draw.Circle(new(0, 0, 0), 1);
// Draws a circle facing right along the X axis.
using (Draw.OnPlane(math.right())) {
Draw.Circle(new(1, 0, 0), 1);
}
// Draws a circle facing up along the Y axis.
using (Draw.OnXZ()) {
Draw.Circle(new(2, 0, 0), 1);
// Both the scope and default facing are ignored.
Draw.Circle(new(3, 0, 0), 1).Facing(new float3(1, 0, 0));
// Use the FaceCamera method to make a visual orient itself
// so that it always faces the camera.
Draw.Circle(new(4, 0, 0), 1).FaceCamera();
}
Space
The transform, or space, visuals are drawn in can also be set globally or with scopes.
// All visuals will be translated 1 unit to the right and be twice as large.
Draw.Transform.Default = float4x4.TRS(new(1, 0, 0), quaternion.identity, new(2, 2, 2));
// Either a Matrix4x4/float4x4 or Transform can be used.
using (Draw.WithSpace(float4x4.identity)) {
Draw.Circle(new(-1, 0, 0), 1);
}
using (Draw.WithSpace(transform)) {
Draw.Circle(new(2, 0, 0), 1);
}