HandPoser.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace RootMotion.FinalIK {
  4. /// <summary>
  5. /// Posing the children of a Transform to match the children of another Transform
  6. /// </summary>
  7. public class HandPoser : Poser {
  8. public override void AutoMapping() {
  9. if (poseRoot == null) poseChildren = new Transform[0];
  10. else poseChildren = (Transform[])poseRoot.GetComponentsInChildren<Transform>();
  11. _poseRoot = poseRoot;
  12. }
  13. protected override void InitiatePoser() {
  14. // Find the children
  15. children = (Transform[])GetComponentsInChildren<Transform>();
  16. StoreDefaultState();
  17. }
  18. protected override void FixPoserTransforms() {
  19. for (int i = 0; i < children.Length; i++) {
  20. children[i].localPosition = defaultLocalPositions[i];
  21. children[i].localRotation = defaultLocalRotations[i];
  22. }
  23. }
  24. protected override void UpdatePoser() {
  25. if (weight <= 0f) return;
  26. if (localPositionWeight <= 0f && localRotationWeight <= 0f) return;
  27. // Get the children, if we don't have them already
  28. if (_poseRoot != poseRoot) AutoMapping();
  29. if (poseRoot == null) return;
  30. // Something went wrong
  31. if (children.Length != poseChildren.Length) {
  32. Warning.Log("Number of children does not match with the pose", transform);
  33. return;
  34. }
  35. // Calculate weights
  36. float rW = localRotationWeight * weight;
  37. float pW = localPositionWeight * weight;
  38. // Lerping the localRotation and the localPosition
  39. for (int i = 0; i < children.Length; i++) {
  40. if (children[i] != transform) {
  41. children[i].localRotation = Quaternion.Lerp(children[i].localRotation, poseChildren[i].localRotation, rW);
  42. children[i].localPosition = Vector3.Lerp(children[i].localPosition, poseChildren[i].localPosition, pW);
  43. }
  44. }
  45. }
  46. protected Transform[] children;
  47. private Transform _poseRoot;
  48. private Transform[] poseChildren;
  49. private Vector3[] defaultLocalPositions;
  50. private Quaternion[] defaultLocalRotations;
  51. protected void StoreDefaultState() {
  52. defaultLocalPositions = new Vector3[children.Length];
  53. defaultLocalRotations = new Quaternion[children.Length];
  54. for (int i = 0; i < children.Length; i++) {
  55. defaultLocalPositions[i] = children[i].localPosition;
  56. defaultLocalRotations[i] = children[i].localRotation;
  57. }
  58. }
  59. }
  60. }