IKMappingBone.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace RootMotion.FinalIK {
  4. /// <summary>
  5. /// Maps a single bone to a node in %IK Solver
  6. /// </summary>
  7. [System.Serializable]
  8. public class IKMappingBone: IKMapping {
  9. #region Main Interface
  10. /// <summary>
  11. /// The bone transform.
  12. /// </summary>
  13. public Transform bone;
  14. /// <summary>
  15. /// The weight of maintaining the bone's rotation after solver has finished.
  16. /// </summary>
  17. [Range(0f, 1f)]
  18. public float maintainRotationWeight = 1f;
  19. /// <summary>
  20. /// Determines whether this IKMappingBone is valid.
  21. /// </summary>
  22. public override bool IsValid(IKSolver solver, ref string message) {
  23. if (!base.IsValid(solver, ref message)) return false;
  24. if (bone == null) {
  25. message = "IKMappingBone's bone is null.";
  26. return false;
  27. }
  28. return true;
  29. }
  30. #endregion Main Interface
  31. private BoneMap boneMap = new BoneMap();
  32. public IKMappingBone() {}
  33. public IKMappingBone(Transform bone) {
  34. this.bone = bone;
  35. }
  36. public void StoreDefaultLocalState() {
  37. boneMap.StoreDefaultLocalState();
  38. }
  39. public void FixTransforms() {
  40. boneMap.FixTransform(false);
  41. }
  42. /*
  43. * Initiating and setting defaults
  44. * */
  45. public override void Initiate(IKSolverFullBody solver) {
  46. if (boneMap == null) boneMap = new BoneMap();
  47. boneMap.Initiate(bone, solver);
  48. }
  49. /*
  50. * Pre-solving
  51. * */
  52. public void ReadPose() {
  53. boneMap.MaintainRotation();
  54. }
  55. public void WritePose(float solverWeight) {
  56. // Rotating back to the last maintained rotation
  57. boneMap.RotateToMaintain(solverWeight * maintainRotationWeight);
  58. }
  59. }
  60. }