Docs / Transform gizmo

Transform gizmo: capabilities

The flags that choose the modes and frames each target offers, why scale has no world frame, the rules that turn flags into a mode and a frame, and a mode toolbar that follows them.

Color legend plain property method

One gizmo serves every kind of target, and each target says what the gizmo may do to it through Capabilities: which modes it offers and, for move and rotate, along whose axes. A crate takes everything, a point light only moves, a sphere trigger moves and grows. Names are colored by what they are: a property or a method; values and fields keep the plain color.

The flags

GizmoCapabilities is a set of flags. Each one offers a mode in one frame, or one kind of scale handle, and they combine with |:

Flag Offers
TranslateGlobal The move arms and plane squares along the world’s axes.
TranslateLocal The move arms and plane squares along the target’s own axes.
RotateGlobal The rings about the world’s axes.
RotateLocal The rings about the target’s own axes.
ScaleAxes The three scale arms, each scaling one axis, and the three plane squares, each scaling two.
ScaleUniform The center square, scaling all three axes together.

Translate, Rotate and Scale are the two flags of each mode together, and All is everything. None hides the gizmo for the target: nothing is drawn or picked, and every click goes to the scene.

Any combination is valid. Some that fit common objects:

Object Capabilities Because
A crate, a rock, any mesh All It can go anywhere, face any way and take any size.
A point light, a sound emitter Translate A point has no orientation and no size.
A spot light, a camera Translate | Rotate It aims, but has no size.
A sphere trigger Translate | ScaleUniform Its one size is its radius, the same along every axis.
A camera path waypoint TranslateGlobal It is placed in the world; the way the camera faces there is set elsewhere.

The gizmo reads the capabilities every frame while no drag runs, so they can follow the target’s state: return None for a locked object, or leave scale out while a physics body is simulating. A drag keeps the capabilities it began with until it ends.

GizmoCapabilities values

Value Description
None Nothing: the gizmo hides for the target.
TranslateGlobal The move arms and plane squares along the world’s axes.
TranslateLocal The move arms and plane squares along the target’s own axes.
RotateGlobal The rings about the world’s axes.
RotateLocal The rings about the target’s own axes.
ScaleAxes The three scale arms and the three plane squares.
ScaleUniform The center square.
Translate TranslateGlobal | TranslateLocal.
Rotate RotateGlobal | RotateLocal.
Scale ScaleAxes | ScaleUniform.
All Every flag.

Scale is always local

Move and rotate come in two frames; scale comes in one. Scaling a turned object along one of the world’s axes shears it: a box leans over into a slanted shape. A pose made of a scale, a rotation and a position has nowhere to keep a shear, so a scale handle along a world axis would have nothing true to write. Scale always works along the target’s own axes, and its two flags split it by handle instead: the arms and the plane squares, scaling one axis or two, and the center square, scaling all three, which is the same in any frame.

The plane squares come with the arms, not with the center square: like an arm, a square changes the target’s proportions. So a target that offers ScaleUniform alone, a sphere trigger say, keeps its shape.

The rules

GizmoCapabilitiesExtensions holds the rules that turn a target’s flags into what the gizmo does. The gizmo asks them itself, and they are public extension methods of GizmoCapabilities that need no graphics device, so a toolbar or an inspector can ask the same questions:

  • Allows(mode) says whether a mode is offered at all, and Allows(mode, space) whether it is offered in that frame. Scale is offered only in Local.
  • ResolveSpace settles the frame a mode works in: the preferred one when the mode is offered there, otherwise the other one. Scale is always Local, and a mode offered in neither frame gets the preferred one.
  • NextMode steps to the next offered mode in translate, rotate, scale order, wrapping round. A lone mode steps to itself, and with nothing offered the current mode comes back.
  • TryClampMode keeps a mode on one the target offers: the current mode when it is offered, else the first offered one in translate, rotate, scale order. It returns false, with the current mode unchanged, when nothing is offered.
var caps = GizmoCapabilities.Translate | GizmoCapabilities.RotateLocal;

caps.Allows(GizmoMode.Scale);                              // false
caps.Allows(GizmoMode.Rotate, GizmoSpace.Global);          // false: only about its own axes
caps.ResolveSpace(GizmoMode.Rotate, GizmoSpace.Global);    // Local, the one frame rotate has
caps.ResolveSpace(GizmoMode.Translate, GizmoSpace.Global); // Global, as preferred
caps.NextMode(GizmoMode.Rotate);                           // Translate: scale is skipped
caps.TryClampMode(GizmoMode.Scale, out var mode);          // true, and mode is Translate

The gizmo runs TryClampMode on Mode in every Update, TryBeginDrag and Draw while no drag runs, NextMode in CycleMode, and ResolveSpace wherever it needs a frame. The modes and frames page shows what that does to the handles.

GizmoCapabilitiesExtensions methods

Method Description
Allows(this GizmoCapabilities capabilities, GizmoMode mode) Whether the mode is offered at all, in either frame or with either scale handle.
Allows(this GizmoCapabilities capabilities, GizmoMode mode, GizmoSpace space) Whether the mode is offered in the frame. Scale is offered only in Local.
ResolveSpace(this GizmoCapabilities capabilities, GizmoMode mode, GizmoSpace preferred) The frame the mode works in: preferred when the mode is offered there, else the other frame when it is. Always Local for scale.
NextMode(this GizmoCapabilities capabilities, GizmoMode current) The next offered mode after current, in translate, rotate, scale order, wrapping round; current itself when nothing else is offered.
TryClampMode(this GizmoCapabilities capabilities, GizmoMode current, out GizmoMode mode) current when it is offered, else the first offered mode in translate, rotate, scale order. false, with current in mode, when nothing is offered.

A mode toolbar

An editor’s toolbar with a button for each mode and one for the frame, grayed out where the selection has nothing to offer. It follows the same rules as the gizmo, so a button never offers what the gizmo would refuse:

// once per frame, after the gizmo's update
var caps = selected?.Capabilities ?? GizmoCapabilities.None;
var mode = gizmo.Mode;

moveButton.Enabled = caps.Allows(GizmoMode.Translate);
rotateButton.Enabled = caps.Allows(GizmoMode.Rotate);
scaleButton.Enabled = caps.Allows(GizmoMode.Scale);

moveButton.Checked = mode == GizmoMode.Translate;
rotateButton.Checked = mode == GizmoMode.Rotate;
scaleButton.Checked = mode == GizmoMode.Scale;

// the frame in use, and whether there is another one to switch to
spaceButton.Text = caps.ResolveSpace(mode, gizmo.Space) == GizmoSpace.Local ? "Local" : "Global";
spaceButton.Enabled = caps.Allows(mode, GizmoSpace.Global) && caps.Allows(mode, GizmoSpace.Local);

with the buttons setting Mode and calling ToggleSpace when clicked. Read Mode after the gizmo’s update: the update is what moves it onto a mode the selection offers.

The frame button shows the frame in use rather than the preference: for a target that turns only about its own axes, rotate says Local whatever Space holds, and the button is grayed out, since there is no other frame to switch to.