TwoBoneIKJob.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright Unity Technologies 2019 // https://github.com/Unity-Technologies/animation-jobs-samples //
  2. // This file has not been modified other to put it in the Animancer.Examples.Jobs namespace and add this message and the comment on the struct.
  3. using UnityEngine;
  4. using UnityEngine.Animations;
  5. namespace Animancer.Examples.Jobs
  6. {
  7. /// <example><see href="https://kybernetik.com.au/animancer/docs/examples/jobs/two-bone-ik">Two Bone IK</see></example>
  8. /// https://kybernetik.com.au/animancer/api/Animancer.Examples.Jobs/TwoBoneIKJob
  9. ///
  10. public struct TwoBoneIKJob : IAnimationJob
  11. {
  12. public TransformSceneHandle effector;
  13. public TransformStreamHandle top;
  14. public TransformStreamHandle mid;
  15. public TransformStreamHandle low;
  16. public void Setup(Animator animator, Transform topX, Transform midX, Transform lowX, Transform effectorX)
  17. {
  18. top = animator.BindStreamTransform(topX);
  19. mid = animator.BindStreamTransform(midX);
  20. low = animator.BindStreamTransform(lowX);
  21. effector = animator.BindSceneTransform(effectorX);
  22. }
  23. public void ProcessRootMotion(AnimationStream stream)
  24. {
  25. }
  26. public void ProcessAnimation(AnimationStream stream)
  27. {
  28. Solve(stream, top, mid, low, effector);
  29. }
  30. /// <summary>
  31. /// Returns the angle needed between v1 and v2 so that their extremities are
  32. /// spaced with a specific length.
  33. /// </summary>
  34. /// <returns>The angle between v1 and v2.</returns>
  35. /// <param name="aLen">The desired length between the extremities of v1 and v2.</param>
  36. /// <param name="v1">First triangle edge.</param>
  37. /// <param name="v2">Second triangle edge.</param>
  38. private static float TriangleAngle(float aLen, Vector3 v1, Vector3 v2)
  39. {
  40. float aLen1 = v1.magnitude;
  41. float aLen2 = v2.magnitude;
  42. float c = Mathf.Clamp((aLen1 * aLen1 + aLen2 * aLen2 - aLen * aLen) / (aLen1 * aLen2) / 2.0f, -1.0f, 1.0f);
  43. return Mathf.Acos(c);
  44. }
  45. private static void Solve(AnimationStream stream, TransformStreamHandle topHandle, TransformStreamHandle midHandle, TransformStreamHandle lowHandle, TransformSceneHandle effectorHandle)
  46. {
  47. Quaternion aRotation = topHandle.GetRotation(stream);
  48. Quaternion bRotation = midHandle.GetRotation(stream);
  49. Quaternion eRotation = effectorHandle.GetRotation(stream);
  50. Vector3 aPosition = topHandle.GetPosition(stream);
  51. Vector3 bPosition = midHandle.GetPosition(stream);
  52. Vector3 cPosition = lowHandle.GetPosition(stream);
  53. Vector3 ePosition = effectorHandle.GetPosition(stream);
  54. Vector3 ab = bPosition - aPosition;
  55. Vector3 bc = cPosition - bPosition;
  56. Vector3 ac = cPosition - aPosition;
  57. Vector3 ae = ePosition - aPosition;
  58. float abcAngle = TriangleAngle(ac.magnitude, ab, bc);
  59. float abeAngle = TriangleAngle(ae.magnitude, ab, bc);
  60. float angle = (abcAngle - abeAngle) * Mathf.Rad2Deg;
  61. Vector3 axis = Vector3.Cross(ab, bc).normalized;
  62. Quaternion fromToRotation = Quaternion.AngleAxis(angle, axis);
  63. Quaternion worldQ = fromToRotation * bRotation;
  64. midHandle.SetRotation(stream, worldQ);
  65. cPosition = lowHandle.GetPosition(stream);
  66. ac = cPosition - aPosition;
  67. Quaternion fromTo = Quaternion.FromToRotation(ac, ae);
  68. topHandle.SetRotation(stream, fromTo * aRotation);
  69. lowHandle.SetRotation(stream, eRotation);
  70. }
  71. }
  72. }