IKSolverAimInspector.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 IKSolverAim
  8. * */
  9. public class IKSolverAimInspector: IKSolverInspector {
  10. #region Public methods
  11. /// <summary>
  12. /// Draws the custom inspector for IKSolverAim
  13. /// </summary>
  14. public static void AddInspector(SerializedProperty prop, bool editHierarchy) {
  15. IKSolverHeuristicInspector.AddTarget(prop);
  16. if (!prop.FindPropertyRelative("XY").boolValue) EditorGUILayout.PropertyField(prop.FindPropertyRelative("poleTarget"), new GUIContent("Pole Target", "If assigned, will automatically set polePosition to the position of this Transform."));
  17. EditorGUILayout.PropertyField(prop.FindPropertyRelative("transform"), new GUIContent("Aim Transform", "The transform that you want to be aimed at the target. Needs to be a lineal descendant of the bone hierarchy. For example, if you wish to aim a gun, it should be the gun, one of it's children or the hand bone."));
  18. EditorGUILayout.PropertyField(prop.FindPropertyRelative("axis"), new GUIContent("Axis", "The local axis of the Transform that you want to be aimed at IKPosition."));
  19. if (!prop.FindPropertyRelative("XY").boolValue) EditorGUILayout.PropertyField(prop.FindPropertyRelative("poleAxis"), new GUIContent("Pole Axis", "Keeps that axis of the Aim Transform directed at the polePosition."));
  20. EditorGUILayout.Space();
  21. IKSolverHeuristicInspector.AddIKPositionWeight(prop);
  22. if (!prop.FindPropertyRelative("XY").boolValue) EditorGUILayout.PropertyField(prop.FindPropertyRelative("poleWeight"), new GUIContent("Pole Weight", "The weight of the Pole."));
  23. IKSolverHeuristicInspector.AddProps(prop);
  24. EditorGUILayout.PropertyField(prop.FindPropertyRelative("clampWeight"), new GUIContent("Clamp Weight", "Clamping rotation of the solver. 0 is free rotation, 1 is completely clamped to transform axis."));
  25. EditorGUILayout.PropertyField(prop.FindPropertyRelative("clampSmoothing"), new GUIContent("Clamp Smoothing", "Number of sine smoothing iterations applied on clamping to make the clamping point smoother."));
  26. IKSolverHeuristicInspector.AddBones(prop, editHierarchy, true);
  27. }
  28. /// <summary>
  29. /// Draws the scene view helpers for IKSolverAim
  30. /// </summary>
  31. public static void AddScene(IKSolverAim solver, Color color, bool modifiable) {
  32. // Protect from null reference errors
  33. if (solver.transform == null) return;
  34. if (Application.isPlaying && !solver.initiated) return;
  35. if (!Application.isPlaying) {
  36. string message = string.Empty;
  37. if (!solver.IsValid(ref message)) return;
  38. }
  39. Handles.color = color;
  40. GUI.color = color;
  41. // Display the bones
  42. for (int i = 0; i < solver.bones.Length; i++) {
  43. IKSolver.Bone bone = solver.bones[i];
  44. if (i < solver.bones.Length - 1) Handles.DrawLine(bone.transform.position, solver.bones[i + 1].transform.position);
  45. Inspector.SphereCap(0, solver.bones[i].transform.position, Quaternion.identity, GetHandleSize(solver.bones[i].transform.position));
  46. }
  47. if (solver.axis != Vector3.zero) Inspector.ConeCap(0, solver.transform.position, Quaternion.LookRotation(solver.transform.rotation * solver.axis), GetHandleSize(solver.transform.position) * 2f);
  48. // Selecting joint and manipulating IKPosition
  49. if (Application.isPlaying && solver.IKPositionWeight > 0) {
  50. if (modifiable) {
  51. Inspector.SphereCap(0, solver.IKPosition, Quaternion.identity, GetHandleSize(solver.IKPosition));
  52. // Manipulating position
  53. solver.IKPosition = Handles.PositionHandle(solver.IKPosition, Quaternion.identity);
  54. }
  55. // Draw a transparent line from transform to IKPosition
  56. Handles.color = new Color(color.r, color.g, color.b, color.a * solver.IKPositionWeight);
  57. Handles.DrawLine(solver.bones[solver.bones.Length - 1].transform.position, solver.transform.position);
  58. Handles.DrawLine(solver.transform.position, solver.IKPosition);
  59. }
  60. Handles.color = color;
  61. // Pole
  62. if (Application.isPlaying && solver.poleWeight > 0f) {
  63. if (modifiable) {
  64. Inspector.SphereCap(0, solver.polePosition, Quaternion.identity, GetHandleSize(solver.IKPosition) * 0.5f);
  65. // Manipulating position
  66. solver.polePosition = Handles.PositionHandle(solver.polePosition, Quaternion.identity);
  67. }
  68. // Draw a transparent line from transform to polePosition
  69. Handles.color = new Color(color.r, color.g, color.b, color.a * solver.poleWeight);
  70. Handles.DrawLine(solver.transform.position, solver.polePosition);
  71. }
  72. Handles.color = Color.white;
  73. GUI.color = Color.white;
  74. }
  75. #endregion Public methods
  76. }
  77. }