IKSolverLookAtInspector.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4. using System;
  5. namespace RootMotion.FinalIK {
  6. /*
  7. * Custom inspector and scene view tools for IKSolverLookAt
  8. * */
  9. public class IKSolverLookAtInspector: IKSolverInspector {
  10. #region Public methods
  11. /*
  12. * Draws the custom inspector for IKSolverLookAt
  13. * */
  14. public static void AddInspector(SerializedProperty prop, bool editHierarchy, bool showReferences) {
  15. EditorGUILayout.PropertyField(prop.FindPropertyRelative("target"), new GUIContent("Target", "The target Transform. Solver IKPosition will be automatically set to the position of the target."));
  16. EditorGUILayout.PropertyField(prop.FindPropertyRelative("IKPositionWeight"), new GUIContent("Weight", "Solver weight for smooth blending."));
  17. EditorGUILayout.PropertyField(prop.FindPropertyRelative("bodyWeight"), new GUIContent("Body Weight", "Weight of rotating spine to target."));
  18. EditorGUILayout.PropertyField(prop.FindPropertyRelative("headWeight"), new GUIContent("Head Weight", "Weight of rotating head to target."));
  19. EditorGUILayout.PropertyField(prop.FindPropertyRelative("eyesWeight"), new GUIContent("Eyes Weight", "Weight of rotating eyes to target."));
  20. EditorGUILayout.PropertyField(prop.FindPropertyRelative("clampWeight"), new GUIContent("Clamp Weight", "Clamping rotation of spine and head. 0 is free rotation, 1 is completely clamped to forward."));
  21. EditorGUILayout.PropertyField(prop.FindPropertyRelative("clampWeightHead"), new GUIContent("Clamp Weight Head", "Clamping rotation of the head. 0 is free rotation, 1 is completely clamped to forward."));
  22. EditorGUILayout.PropertyField(prop.FindPropertyRelative("clampWeightEyes"), new GUIContent("Clamp Weight Eyes", "Clamping rotation of the eyes. 0 is free rotation, 1 is completely clamped to forward."));
  23. EditorGUILayout.PropertyField(prop.FindPropertyRelative("clampSmoothing"), new GUIContent("Clamp Smoothing", "Number of sine smoothing iterations applied on clamping to make the clamping point smoother."));
  24. EditorGUILayout.PropertyField(prop.FindPropertyRelative("spineWeightCurve"), new GUIContent("Spine Weight Curve", "Weight distribution between spine bones (first bone is evaluated at time 0.0, last bone at 1.0)."));
  25. // References
  26. if (showReferences) {
  27. EditorGUILayout.Space();
  28. EditorGUILayout.PropertyField(prop.FindPropertyRelative("head.transform"), new GUIContent("Head", "The head bone."));
  29. EditorGUILayout.Space();
  30. AddArray(prop.FindPropertyRelative("spine"), new GUIContent("Spine", string.Empty), editHierarchy, false, null, null, DrawArrayElementLabelBone);
  31. EditorGUILayout.Space();
  32. AddArray(prop.FindPropertyRelative("eyes"), new GUIContent("Eyes", string.Empty), editHierarchy, false, null, null, DrawArrayElementLabelBone);
  33. }
  34. EditorGUILayout.Space();
  35. }
  36. /*
  37. * Draws the scene view helpers for IKSolverLookAt
  38. * */
  39. public static void AddScene(IKSolverLookAt solver, Color color, bool modifiable) {
  40. // Protect from null reference errors
  41. if (Application.isPlaying && !solver.initiated) return;
  42. if (!Application.isPlaying && !solver.IsValid()) return;
  43. // Display the Spine
  44. if (solver.spine.Length > 0) {
  45. Handles.color = color;
  46. GUI.color = color;
  47. for (int i = 0; i < solver.spine.Length; i++) {
  48. IKSolverLookAt.LookAtBone bone = solver.spine[i];
  49. if (i < solver.spine.Length - 1) Handles.DrawLine(bone.transform.position, solver.spine[i + 1].transform.position);
  50. Inspector.SphereCap(0, bone.transform.position, Quaternion.identity, GetHandleSize(bone.transform.position));
  51. }
  52. // Draw a transparent line from last bone to IKPosition
  53. if (Application.isPlaying) {
  54. Handles.color = new Color(color.r, color.g, color.b, color.a * solver.IKPositionWeight * solver.bodyWeight);
  55. Handles.DrawLine(solver.spine[solver.spine.Length - 1].transform.position, solver.IKPosition);
  56. }
  57. }
  58. // Display the eyes
  59. if (solver.eyes.Length > 0) {
  60. for (int i = 0; i < solver.eyes.Length; i++) {
  61. DrawLookAtBoneInScene(solver.eyes[i], solver.IKPosition, color, solver.IKPositionWeight * solver.eyesWeight);
  62. }
  63. }
  64. // Display the head
  65. if (solver.head.transform != null) {
  66. DrawLookAtBoneInScene(solver.head, solver.IKPosition, color, solver.IKPositionWeight * solver.headWeight);
  67. }
  68. Handles.color = color;
  69. GUI.color = color;
  70. // Selecting joint and manipulating IKPosition
  71. if (Application.isPlaying && solver.IKPositionWeight > 0) {
  72. if (modifiable) {
  73. Inspector.SphereCap(0, solver.IKPosition, Quaternion.identity, GetHandleSize(solver.IKPosition));
  74. // Manipulating position
  75. if (solver.target == null) solver.IKPosition = Handles.PositionHandle(solver.IKPosition, Quaternion.identity);
  76. }
  77. }
  78. Handles.color = Color.white;
  79. GUI.color = Color.white;
  80. }
  81. #endregion Public methods
  82. private static void DrawArrayElementLabelBone(SerializedProperty bone, bool editHierarchy) {
  83. AddObjectReference(bone.FindPropertyRelative("transform"), GUIContent.none, editHierarchy, 0, 300);
  84. }
  85. private static void DrawLookAtBoneInScene(IKSolverLookAt.LookAtBone bone, Vector3 IKPosition, Color color, float lineWeight) {
  86. Handles.color = color;
  87. GUI.color = color;
  88. Inspector.SphereCap(0, bone.transform.position, Quaternion.identity, GetHandleSize(bone.transform.position));
  89. // Draw a transparent line from last bone to IKPosition
  90. if (Application.isPlaying && lineWeight > 0) {
  91. Handles.color = new Color(color.r, color.g, color.b, color.a * lineWeight);
  92. Handles.DrawLine(bone.transform.position, IKPosition);
  93. }
  94. }
  95. }
  96. }