RotationLimitSpline.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace RootMotion.FinalIK {
  4. /// <summary>
  5. /// Using a spline to limit the range of rotation on universal and ball-and-socket joints.
  6. /// Reachable area is defined by an AnimationCurve orthogonally mapped onto a sphere.
  7. /// </summary>
  8. [HelpURL("http://www.root-motion.com/finalikdox/html/page14.html")]
  9. [AddComponentMenu("Scripts/RootMotion.FinalIK/Rotation Limits/Rotation Limit Spline")]
  10. public class RotationLimitSpline : RotationLimit {
  11. // Open the User Manual URL
  12. [ContextMenu("User Manual")]
  13. private void OpenUserManual() {
  14. Application.OpenURL("http://www.root-motion.com/finalikdox/html/page14.html");
  15. }
  16. // Open the Script Reference URL
  17. [ContextMenu("Scrpt Reference")]
  18. private void OpenScriptReference() {
  19. Application.OpenURL("http://www.root-motion.com/finalikdox/html/class_root_motion_1_1_final_i_k_1_1_rotation_limit_spline.html");
  20. }
  21. // Link to the Final IK Google Group
  22. [ContextMenu("Support Group")]
  23. void SupportGroup() {
  24. Application.OpenURL("https://groups.google.com/forum/#!forum/final-ik");
  25. }
  26. // Link to the Final IK Asset Store thread in the Unity Community
  27. [ContextMenu("Asset Store Thread")]
  28. void ASThread() {
  29. Application.OpenURL("http://forum.unity3d.com/threads/final-ik-full-body-ik-aim-look-at-fabrik-ccd-ik-1-0-released.222685/");
  30. }
  31. #region Main Interface
  32. /// <summary>
  33. /// Limit of twist rotation around the main axis.
  34. /// </summary>
  35. [Range(0f, 180f)] public float twistLimit = 180;
  36. /// <summary>
  37. /// Set the spline keyframes.
  38. /// </summary>
  39. /// <param name='keyframes'>
  40. /// Keyframes.
  41. /// </param>
  42. public void SetSpline(Keyframe[] keyframes) {
  43. spline.keys = keyframes;
  44. }
  45. /*
  46. * The AnimationCurve orthogonally mapped onto a sphere that defines the swing limits
  47. * */
  48. [HideInInspector] public AnimationCurve spline;
  49. #endregion Main Interface
  50. /*
  51. * Limits the rotation in the local space of this instance's Transform.
  52. * */
  53. protected override Quaternion LimitRotation(Quaternion rotation) {
  54. // Subtracting off-limits swing
  55. Quaternion swing = LimitSwing(rotation);
  56. // Apply twist limits
  57. return LimitTwist(swing, axis, secondaryAxis, twistLimit);
  58. }
  59. /*
  60. * Apply the swing rotation limits
  61. * */
  62. public Quaternion LimitSwing(Quaternion rotation) {
  63. if (axis == Vector3.zero) return rotation; // Ignore with zero axes
  64. if (rotation == Quaternion.identity) return rotation; // Assuming initial rotation is in the reachable area
  65. // Get the rotation angle orthogonal to Axis
  66. Vector3 swingAxis = rotation * axis;
  67. float angle = GetOrthogonalAngle(swingAxis, secondaryAxis, axis);
  68. // Convert angle from 180 to 360 degrees representation
  69. float dot = Vector3.Dot(swingAxis, crossAxis);
  70. if (dot < 0) angle = 180 + (180 - angle);
  71. // Evaluate the limit for this angle
  72. float limit = spline.Evaluate(angle);
  73. // Get the limited swing axis
  74. Quaternion swingRotation = Quaternion.FromToRotation(axis, swingAxis);
  75. Quaternion limitedSwingRotation = Quaternion.RotateTowards(Quaternion.identity, swingRotation, limit);
  76. // Rotation from current(illegal) swing rotation to the limited(legal) swing rotation
  77. Quaternion toLimits = Quaternion.FromToRotation(swingAxis, limitedSwingRotation * axis);
  78. // Subtract the illegal rotation
  79. return toLimits * rotation;
  80. }
  81. }
  82. }