using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace UnityEngine.XR.Interaction.Toolkit
{
///
/// Utility functions related to .
///
public static class GizmoHelpers
{
static readonly Color s_XAxisColor = new Color(219f / 255, 62f / 255, 29f / 255, .93f);
static readonly Color s_YAxisColor = new Color(154f / 255, 243f / 255, 72f / 255, .93f);
static readonly Color s_ZAxisColor = new Color(58f / 255, 122f / 255, 248f / 255, .93f);
// Helper dictionary for easier axis lookup, adapted from lookup mechanism in Editor/Mono/EditorHandles/BoundsHandle/CapsuleBoundsHandle.cs
static readonly Dictionary s_AxisMapping = new Dictionary()
{
{Vector3.up, (Vector3.forward, Vector3.right)},
{Vector3.forward, (Vector3.right, Vector3.up)},
{Vector3.right, (Vector3.up, Vector3.forward)},
};
///
/// Draws oriented wire plane.
///
/// Position of the plane.
/// Rotation of the plane.
/// Size of the plane.
public static void DrawWirePlaneOriented(Vector3 position, Quaternion rotation, float size)
{
var halfSize = size / 2f;
var tl = new Vector3(halfSize, 0f, -halfSize);
var tr = new Vector3(halfSize, 0f, halfSize);
var bl = new Vector3(-halfSize, 0f, -halfSize);
var br = new Vector3(-halfSize, 0f, halfSize);
Gizmos.DrawLine((rotation * tl) + position,
(rotation * tr) + position);
Gizmos.DrawLine((rotation * tr) + position,
(rotation * br) + position);
Gizmos.DrawLine((rotation * br) + position,
(rotation * bl) + position);
Gizmos.DrawLine((rotation * bl) + position,
(rotation * tl) + position);
}
///
/// Draws oriented wire cube.
///
/// Position of the cube.
/// Rotation of the cube.
/// Size of the cube.
public static void DrawWireCubeOriented(Vector3 position, Quaternion rotation, float size)
{
var halfSize = size / 2f;
var tl = new Vector3(halfSize, 0f, -halfSize);
var tr = new Vector3(halfSize, 0f, halfSize);
var bl = new Vector3(-halfSize, 0f, -halfSize);
var br = new Vector3(-halfSize, 0f, halfSize);
var tlt = new Vector3(halfSize, size, -halfSize);
var trt = new Vector3(halfSize, size, halfSize);
var blt = new Vector3(-halfSize, size, -halfSize);
var brt = new Vector3(-halfSize, size, halfSize);
Gizmos.DrawLine((rotation * tl) + position, (rotation * tr) + position);
Gizmos.DrawLine((rotation * tr) + position, (rotation * br) + position);
Gizmos.DrawLine((rotation * br) + position, (rotation * bl) + position);
Gizmos.DrawLine((rotation * bl) + position, (rotation * tl) + position);
Gizmos.DrawLine((rotation * tlt) + position, (rotation * trt) + position);
Gizmos.DrawLine((rotation * trt) + position, (rotation * brt) + position);
Gizmos.DrawLine((rotation * brt) + position, (rotation * blt) + position);
Gizmos.DrawLine((rotation * blt) + position, (rotation * tlt) + position);
Gizmos.DrawLine((rotation * tlt) + position, (rotation * tl) + position);
Gizmos.DrawLine((rotation * trt) + position, (rotation * tr) + position);
Gizmos.DrawLine((rotation * brt) + position, (rotation * br) + position);
Gizmos.DrawLine((rotation * blt) + position, (rotation * bl) + position);
}
///
/// Draws world space standard basis vectors at .
///
/// The to represent.
/// Length of each ray.
public static void DrawAxisArrows(Transform transform, float size)
{
var position = transform.position;
Gizmos.color = s_ZAxisColor;
Gizmos.DrawRay(position, transform.forward * size);
Gizmos.color = s_YAxisColor;
Gizmos.DrawRay(position, transform.up * size);
Gizmos.color = s_XAxisColor;
Gizmos.DrawRay(position, transform.right * size);
}
///
/// Draws a capsule gizmo in the Scene view at the center location defined.
/// Only functional in the Unity Editor.
///
/// Center of the capsule.
/// Height of the capsule to be drawn, not including the radius.
/// Radius of the arcs of the capsule being drawn.
/// Direction the capsule will be drawn.
/// Color of the capsule.
///
/// must be set to one of the 3 following: , , or
///
internal static void DrawCapsule(Vector3 center, float height, float radius, Vector3 axis, Color color)
{
#if UNITY_EDITOR
if (!s_AxisMapping.TryGetValue(axis, out var mapping))
{
axis = Vector3.up;
mapping = s_AxisMapping[axis];
}
Vector3 heightAxis = axis;
Vector3 forwardAxis = mapping.Item1;
Vector3 rightAxis = mapping.Item2;
float halfHeight = height * 0.5f;
Vector3 top = center + heightAxis * (halfHeight - radius);
Vector3 bottom = center - heightAxis * (halfHeight - radius);
Handles.color = color;
Handles.DrawWireArc(top, forwardAxis, rightAxis, 180f, radius);
Handles.DrawWireArc(bottom, forwardAxis, rightAxis, -180f, radius);
Handles.DrawLine(top + rightAxis * radius, bottom + rightAxis * radius);
Handles.DrawLine(top - rightAxis * radius, bottom - rightAxis * radius);
Handles.DrawWireArc(top, rightAxis, forwardAxis, -180f, radius);
Handles.DrawWireArc(bottom, rightAxis, forwardAxis, 180f, radius);
Handles.DrawLine(top + forwardAxis * radius, bottom + forwardAxis * radius);
Handles.DrawLine(top - forwardAxis * radius, bottom - forwardAxis * radius);
Handles.DrawWireArc(top, heightAxis, forwardAxis, 360f, radius);
Handles.DrawWireArc(bottom, heightAxis, forwardAxis, -360f, radius);
#endif
}
}
}