IKSolverFABRIKRootInspector.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4. namespace RootMotion.FinalIK {
  5. /*
  6. * Custom inspector and scene view tools for IKSolverFABRIKRoot
  7. * */
  8. public class IKSolverFABRIKRootInspector : IKSolverInspector {
  9. #region Public methods
  10. /*
  11. * Draws the custom inspector for IKSolverFABRIKRoot
  12. * */
  13. public static void AddInspector(SerializedProperty prop, bool editHierarchy) {
  14. AddClampedInt(prop.FindPropertyRelative("iterations"), new GUIContent("Iterations", "Solver iterations."), 0, int.MaxValue);
  15. EditorGUILayout.PropertyField(prop.FindPropertyRelative("IKPositionWeight"), new GUIContent("Weight", "Solver weight."));
  16. EditorGUILayout.PropertyField(prop.FindPropertyRelative("rootPin"), new GUIContent("Root Pin", "Weight of keeping all FABRIK Trees pinned to the root position."));
  17. EditorGUILayout.Space();
  18. EditorGUI.indentLevel = 0;
  19. EditorGUILayout.PropertyField(prop.FindPropertyRelative("chains"), new GUIContent("Chains", "FABRIK chains."), true);
  20. EditorGUILayout.Space();
  21. }
  22. /*
  23. * Draws the scene view helpers for IKSolverFABRIKRoot
  24. * */
  25. public static void AddScene(IKSolverFABRIKRoot solver, Color color, bool modifiable, ref FABRIKChain selected) {
  26. // Protect from null reference errors
  27. if (Application.isPlaying && !solver.initiated) return;
  28. if (!Application.isPlaying) {
  29. string message = string.Empty;
  30. if (!solver.IsValid(ref message)) return;
  31. }
  32. Handles.color = color;
  33. // Selecting solvers
  34. if (Application.isPlaying) {
  35. SelectChain(solver.chains, ref selected, color);
  36. }
  37. AddSceneChain(solver.chains, color, selected);
  38. // Root pin
  39. Handles.color = new Color(Mathf.Lerp(1f, color.r, solver.rootPin), Mathf.Lerp(1f, color.g, solver.rootPin), Mathf.Lerp(1f, color.b, solver.rootPin), Mathf.Lerp(0.5f, 1f, solver.rootPin));
  40. if (solver.GetRoot() != null) {
  41. Handles.DrawLine(solver.chains[0].ik.solver.bones[0].transform.position, solver.GetRoot().position);
  42. Inspector.CubeCap(0, solver.GetRoot().position, Quaternion.identity, GetHandleSize(solver.GetRoot().position));
  43. }
  44. }
  45. #endregion Public methods
  46. private static Color col, midColor, endColor;
  47. private static void SelectChain(FABRIKChain[] chain, ref FABRIKChain selected, Color color) {
  48. foreach (FABRIKChain c in chain) {
  49. if (c.ik.solver.IKPositionWeight > 0 && selected != c) {
  50. Handles.color = GetChainColor(c, color);
  51. if (Inspector.DotButton(c.ik.solver.GetIKPosition(), Quaternion.identity, GetHandleSize(c.ik.solver.GetIKPosition()), GetHandleSize(c.ik.solver.GetIKPosition()))) {
  52. selected = c;
  53. return;
  54. }
  55. }
  56. }
  57. }
  58. private static Color GetChainColor(FABRIKChain chain, Color color) {
  59. float midWeight = chain.pin;
  60. midColor = new Color(Mathf.Lerp(1f, color.r, midWeight), Mathf.Lerp(1f, color.g, midWeight), Mathf.Lerp(1f, color.b, midWeight), Mathf.Lerp(0.5f, 1f, midWeight));
  61. float endWeight = chain.pull;
  62. endColor = new Color(Mathf.Lerp(1f, color.r, endWeight), Mathf.Lerp(0f, color.g, endWeight), Mathf.Lerp(0f, color.b, endWeight), Mathf.Lerp(0.5f, 1f, endWeight));
  63. return chain.children.Length == 0? endColor: midColor;
  64. }
  65. private static void AddSceneChain(FABRIKChain[] chain, Color color, FABRIKChain selected) {
  66. foreach (FABRIKChain c in chain) {
  67. col = GetChainColor(c, color);
  68. IKSolverHeuristicInspector.AddScene(c.ik.solver as IKSolverHeuristic, col, selected == c);
  69. }
  70. }
  71. }
  72. }