IKSolverFullBodyInspector.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 IKSolverFullBody
  8. * */
  9. public class IKSolverFullBodyInspector : IKSolverInspector {
  10. #region Public methods
  11. /*
  12. * Draws the custom inspector for IKSolverFullBody
  13. * */
  14. public static void AddInspector(SerializedProperty prop, bool editWeights) {
  15. EditorGUILayout.PropertyField(prop.FindPropertyRelative("IKPositionWeight"), new GUIContent("Weight", "Solver weight for smooth blending (ik.solver.IKPositionWeight)."));
  16. EditorGUILayout.PropertyField(prop.FindPropertyRelative("iterations"), new GUIContent("Iterations", "Solver iterations per frame."));
  17. }
  18. /*
  19. * Draws the scene view helpers for IKSolverFullBody
  20. * */
  21. public static void AddScene(UnityEngine.Object target, IKSolverFullBody solver, Color color, bool modifiable, ref int selectedEffector, float size) {
  22. if (!modifiable) return;
  23. if (!solver.initiated) return;
  24. if (!Application.isPlaying && !solver.IsValid()) return;
  25. // Effectors
  26. for (int i = 0; i < solver.effectors.Length; i++) {
  27. bool rotate = solver.effectors[i].isEndEffector;
  28. float weight = rotate? Mathf.Max(solver.effectors[i].positionWeight, solver.effectors[i].rotationWeight): solver.effectors[i].positionWeight;
  29. if (weight > 0 && selectedEffector != i) {
  30. Handles.color = color;
  31. if (rotate) {
  32. if (Inspector.DotButton(solver.effectors[i].position, solver.effectors[i].rotation, size * 0.5f, size * 0.5f)) {
  33. selectedEffector = i;
  34. return;
  35. }
  36. } else {
  37. if (Inspector.SphereButton(solver.effectors[i].position, solver.effectors[i].rotation, size, size)) {
  38. selectedEffector = i;
  39. return;
  40. }
  41. }
  42. }
  43. }
  44. for (int i = 0; i < solver.effectors.Length; i++) IKEffectorInspector.AddScene(solver.effectors[i], color, modifiable && i == selectedEffector, size);
  45. if (GUI.changed) EditorUtility.SetDirty(target);
  46. }
  47. #endregion Public methods
  48. public static void AddChain(FBIKChain[] chain, int index, Color color, float size) {
  49. Handles.color = color;
  50. for (int i = 0; i < chain[index].nodes.Length - 1; i++) {
  51. Handles.DrawLine(GetNodePosition(chain[index].nodes[i]), GetNodePosition(chain[index].nodes[i + 1]));
  52. Inspector.SphereCap(0, GetNodePosition(chain[index].nodes[i]), Quaternion.identity, size);
  53. }
  54. Inspector.SphereCap(0, GetNodePosition(chain[index].nodes[chain[index].nodes.Length - 1]), Quaternion.identity, size);
  55. for (int i = 0; i < chain[index].children.Length; i++) {
  56. Handles.DrawLine(GetNodePosition(chain[index].nodes[chain[index].nodes.Length - 1]), GetNodePosition(chain[chain[index].children[i]].nodes[0]));
  57. }
  58. }
  59. private static Vector3 GetNodePosition(IKSolver.Node node) {
  60. if (Application.isPlaying) return node.solverPosition;
  61. return node.transform.position;
  62. }
  63. }
  64. }