IKSolverTrigonometricInspector.cs 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4. namespace RootMotion.FinalIK {
  5. /*
  6. * Custom inspector and scene view tools for IKSolverTrigonometric
  7. * */
  8. public class IKSolverTrigonometricInspector: IKSolverInspector {
  9. #region Public methods
  10. /*
  11. * Draws the custom inspector for IKSolverTrigonometric
  12. * */
  13. public static void AddInspector(SerializedProperty prop, bool editHierarchy, bool showReferences) {
  14. EditorGUI.indentLevel = 0;
  15. // Bone references
  16. if (showReferences) {
  17. EditorGUILayout.Space();
  18. AddObjectReference(prop.FindPropertyRelative("bone1.transform"), new GUIContent("Bone 1", "The first bone in the hierarchy (thigh or upper arm)."), editHierarchy, 100);
  19. AddObjectReference(prop.FindPropertyRelative("bone2.transform"), new GUIContent("Bone 2", "The second bone in the hierarchy (calf or forearm)."), editHierarchy, 100);
  20. AddObjectReference(prop.FindPropertyRelative("bone3.transform"), new GUIContent("Bone 3", "The last bone in the hierarchy (foot or hand)."), editHierarchy, 100);
  21. EditorGUILayout.Space();
  22. }
  23. EditorGUILayout.PropertyField(prop.FindPropertyRelative("target"), new GUIContent("Target", "The target Transform. Solver IKPosition will be automatically set to the position of the target."));
  24. EditorGUILayout.PropertyField(prop.FindPropertyRelative("IKPositionWeight"), new GUIContent("Position Weight", "Solver weight for smooth blending."));
  25. EditorGUILayout.PropertyField(prop.FindPropertyRelative("IKRotationWeight"), new GUIContent("Rotation Weight", "Weight of last bone's rotation to IKRotation."));
  26. if (showReferences) {
  27. EditorGUILayout.PropertyField(prop.FindPropertyRelative("bendNormal"), new GUIContent("Bend Normal", "Bend plane normal."));
  28. }
  29. }
  30. public static void AddTestInspector(SerializedObject obj) {
  31. SerializedProperty solver = obj.FindProperty("solver");
  32. EditorGUILayout.PropertyField(solver.FindPropertyRelative("bendNormal"));
  33. }
  34. /*
  35. * Draws the scene view helpers for IKSolverTrigonometric
  36. * */
  37. public static void AddScene(IKSolverTrigonometric solver, Color color, bool modifiable) {
  38. if (Application.isPlaying && !solver.initiated) return;
  39. if (!Application.isPlaying && !solver.IsValid()) return;
  40. //float length = Vector3.Distance(solver.bone1.transform.position, solver.bone2.transform.position) + Vector3.Distance(solver.bone2.transform.position, solver.bone3.transform.position);
  41. //float size = length * 0.05f;
  42. Handles.color = color;
  43. GUI.color = color;
  44. Vector3 bendPosition = solver.bone2.transform.position;
  45. Vector3 endPosition = solver.bone3.transform.position;
  46. // Chain lines
  47. Handles.DrawLine(solver.bone1.transform.position, bendPosition);
  48. Handles.DrawLine(bendPosition, endPosition);
  49. // Joints
  50. Inspector.SphereCap(0, solver.bone1.transform.position, Quaternion.identity, GetHandleSize(solver.bone1.transform.position));
  51. Inspector.SphereCap(0, bendPosition, Quaternion.identity, GetHandleSize(bendPosition));
  52. Inspector.SphereCap(0, endPosition, Quaternion.identity, GetHandleSize(endPosition));
  53. if (Application.isPlaying && (solver.IKPositionWeight > 0 || solver.IKRotationWeight > 0)) {
  54. if (modifiable) {
  55. Inspector.CubeCap(0, solver.IKPosition, solver.IKRotation, GetHandleSize(solver.IKPosition));
  56. // Manipulating position and rotation
  57. switch(Tools.current) {
  58. case Tool.Move:
  59. if (solver.target == null) solver.IKPosition = Handles.PositionHandle(solver.IKPosition, Quaternion.identity);
  60. break;
  61. case Tool.Rotate:
  62. if (solver.target == null) solver.IKRotation = Handles.RotationHandle(solver.IKRotation, solver.IKPosition);
  63. break;
  64. }
  65. }
  66. // Target
  67. Handles.color = new Color(color.r, color.g, color.b, color.a * Mathf.Max(solver.IKPositionWeight, solver.IKRotationWeight));
  68. Handles.DrawLine(endPosition, solver.IKPosition);
  69. }
  70. Handles.color = Color.white;
  71. GUI.color = Color.white;
  72. }
  73. #endregion Public methods
  74. }
  75. }