Skip to content

Instantly share code, notes, and snippets.

@ScruffyRules
Created September 3, 2020 16:31
Show Gist options
  • Save ScruffyRules/e4dfeeb0e52e5bdddcbd8b3959ae8ffa to your computer and use it in GitHub Desktop.
Save ScruffyRules/e4dfeeb0e52e5bdddcbd8b3959ae8ffa to your computer and use it in GitHub Desktop.
A utility to show the axes of humanoid bones as gizmos
/*
MIT License
Copyright (c) 2020 Scott Scheiner
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using UnityEngine;
using System.Collections.Generic;
[ExecuteInEditMode]
[RequireComponent(typeof(Animator))]
[AddComponentMenu("Scripts/ArmatureGizmo")]
public class ArmatureGizmo : MonoBehaviour
{
private Animator ani;
[SerializeField]
[Header("Should not be Generic")]
[Header("Disable 3D Icons under Gizmos to see it easier")]
private bool IsGeneric = false;
[SerializeField]
private bool RequiresSelected = false;
[SerializeField]
private bool ShowLines = true;
[SerializeField]
private bool ShowAxis = true;
[SerializeField]
private bool IgnoreFingers = true;
[SerializeField]
private bool ShowSingleBone = false;
[SerializeField]
private HumanBodyBones SingleBone = HumanBodyBones.Hips;
[SerializeField]
[Range(0.01f, 0.25f)]
private float AxisSize = 0.1f;
[SerializeField]
[Range(0.0f, 1.0f)]
private float AxisAlpha = 1.0f;
[SerializeField]
private Color LineColor = new Color(1, 1, 0, 0.5f);
private bool OldIgnoreFingers = true;
private Color red = new Color(1, 0, 0, 1);
private Color green = new Color(0, 1, 0, 1);
private Color blue = new Color(0, 0, 1, 1);
private List<Transform> humanBodyBones = null;
private List<HumanBodyBones> fingers = new List<HumanBodyBones> {
HumanBodyBones.LeftThumbProximal,
HumanBodyBones.LeftThumbIntermediate,
HumanBodyBones.LeftThumbDistal,
HumanBodyBones.LeftIndexProximal,
HumanBodyBones.LeftIndexIntermediate,
HumanBodyBones.LeftIndexDistal,
HumanBodyBones.LeftMiddleProximal,
HumanBodyBones.LeftMiddleIntermediate,
HumanBodyBones.LeftMiddleDistal,
HumanBodyBones.LeftRingProximal,
HumanBodyBones.LeftRingIntermediate,
HumanBodyBones.LeftRingDistal,
HumanBodyBones.LeftLittleProximal,
HumanBodyBones.LeftLittleIntermediate,
HumanBodyBones.LeftLittleDistal,
HumanBodyBones.RightThumbProximal,
HumanBodyBones.RightThumbIntermediate,
HumanBodyBones.RightThumbDistal,
HumanBodyBones.RightIndexProximal,
HumanBodyBones.RightIndexIntermediate,
HumanBodyBones.RightIndexDistal,
HumanBodyBones.RightMiddleProximal,
HumanBodyBones.RightMiddleIntermediate,
HumanBodyBones.RightMiddleDistal,
HumanBodyBones.RightRingProximal,
HumanBodyBones.RightRingIntermediate,
HumanBodyBones.RightRingDistal,
HumanBodyBones.RightLittleProximal,
HumanBodyBones.RightLittleIntermediate,
HumanBodyBones.RightLittleDistal,
};
void Setup()
{
ani = GetComponent<Animator>();
if (ani == null)
{
enabled = false;
return;
}
if (!ani.isHuman)
{
IsGeneric = true;
enabled = false;
return;
}
humanBodyBones = new List<Transform>();
foreach (HumanBodyBones hbb in System.Enum.GetValues(typeof(HumanBodyBones)))
{
if (hbb == HumanBodyBones.LastBone) continue;
if (IgnoreFingers && fingers.Contains(hbb)) continue;
humanBodyBones.Add(ani.GetBoneTransform(hbb));
}
if (humanBodyBones.Count == 0) enabled = false;
}
void Start() { Setup(); }
void OnEnable() { Setup(); }
void OnDrawGizmos()
{
if (RequiresSelected) return;
DrawGizmos();
}
void OnDrawGizmosSelected()
{
if (!RequiresSelected) return;
DrawGizmos();
}
void OnValidate()
{
red = new Color(1, 0, 0, AxisAlpha);
green = new Color(0, 1, 0, AxisAlpha);
blue = new Color(0, 0, 1, AxisAlpha);
if (OldIgnoreFingers != IgnoreFingers) {
Setup();
OldIgnoreFingers = IgnoreFingers;
}
}
void DrawGizmos()
{
if (ani == null) return;
if (IsGeneric) return;
if (humanBodyBones == null || humanBodyBones.Count == 0)
{
Setup();
return;
}
if (!ShowAxis && !ShowLines) return;
if (ShowSingleBone)
{
if (SingleBone == HumanBodyBones.LastBone) SingleBone = HumanBodyBones.Hips;
DrawAxis(ani.GetBoneTransform(SingleBone));
return;
}
foreach (Transform boneTransform in humanBodyBones)
{
if (ShowAxis) DrawAxis(boneTransform);
if (ShowLines) DrawLine(boneTransform);
}
}
void DrawAxis(Transform boneTransform)
{
if (boneTransform == null) return;
Gizmos.color = red;
Gizmos.DrawLine(boneTransform.position, boneTransform.position + (boneTransform.right * AxisSize));
Gizmos.color = green;
Gizmos.DrawLine(boneTransform.position, boneTransform.position + (boneTransform.up * AxisSize));
Gizmos.color = blue;
Gizmos.DrawLine(boneTransform.position, boneTransform.position + (boneTransform.forward * AxisSize));
}
void DrawLine(Transform boneTransform)
{
if (boneTransform == null) return;
if (boneTransform == ani.GetBoneTransform(HumanBodyBones.Hips)) return;
Gizmos.color = LineColor;
Gizmos.DrawLine(boneTransform.position, boneTransform.parent.position);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment