Нема описа
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System.Collections.Generic;
  2. using UnityEngine.Scripting.APIUpdating;
  3. namespace UnityEngine.U2D.IK
  4. {
  5. /// <summary>
  6. /// Component responsible for 2D Limb IK.
  7. /// </summary>
  8. [MovedFrom("UnityEngine.Experimental.U2D.IK")]
  9. [Solver2DMenuAttribute("Limb")]
  10. [IconAttribute(IconUtility.IconPath + "Animation.IKLimb.png")]
  11. public sealed class LimbSolver2D : Solver2D
  12. {
  13. [SerializeField]
  14. IKChain2D m_Chain = new IKChain2D();
  15. [SerializeField]
  16. bool m_Flip;
  17. Vector3[] m_Positions = new Vector3[3];
  18. float[] m_Lengths = new float[2];
  19. float[] m_Angles = new float[2];
  20. /// <summary>
  21. /// Get and set the flip property.
  22. /// </summary>
  23. public bool flip
  24. {
  25. get => m_Flip;
  26. set => m_Flip = value;
  27. }
  28. /// <summary>
  29. /// Initializes the solver.
  30. /// </summary>
  31. protected override void DoInitialize()
  32. {
  33. m_Chain.transformCount = m_Chain.effector == null || IKUtility.GetAncestorCount(m_Chain.effector) < 2 ? 0 : 3;
  34. base.DoInitialize();
  35. }
  36. /// <summary>
  37. /// Returns the number of chains in the solver.
  38. /// </summary>
  39. /// <returns>Returns 1, because Limb Solver has only one chain.</returns>
  40. protected override int GetChainCount() => 1;
  41. /// <summary>
  42. /// Gets the chain in the solver at index.
  43. /// </summary>
  44. /// <param name="index">Index to query. Not used in this override.</param>
  45. /// <returns>Returns IKChain2D for the Solver.</returns>
  46. public override IKChain2D GetChain(int index) => m_Chain;
  47. /// <summary>
  48. /// Prepares the data required for updating the solver.
  49. /// </summary>
  50. protected override void DoPrepare()
  51. {
  52. var lengths = m_Chain.lengths;
  53. m_Positions[0] = m_Chain.transforms[0].position;
  54. m_Positions[1] = m_Chain.transforms[1].position;
  55. m_Positions[2] = m_Chain.transforms[2].position;
  56. m_Lengths[0] = lengths[0];
  57. m_Lengths[1] = lengths[1];
  58. }
  59. /// <summary>
  60. /// Updates the IK and sets the chain's transform positions.
  61. /// </summary>
  62. /// <param name="targetPositions">List of target positions.</param>
  63. protected override void DoUpdateIK(List<Vector3> targetPositions)
  64. {
  65. var targetPosition = targetPositions[0];
  66. var upperLimb = m_Chain.transforms[0];
  67. var lowerLimb = m_Chain.transforms[1];
  68. var effector = m_Chain.effector;
  69. var targetLocalPosition2D = (Vector2)upperLimb.InverseTransformPoint(targetPosition);
  70. targetPosition = upperLimb.TransformPoint(targetLocalPosition2D);
  71. if (targetLocalPosition2D.sqrMagnitude > 0f && Limb.Solve(targetPosition, m_Lengths, m_Positions, ref m_Angles))
  72. {
  73. var upperLimbRotationAngle = Vector2.SignedAngle(Vector2.right, targetLocalPosition2D) + Vector2.SignedAngle(lowerLimb.localPosition, Vector2.right) + (flip ? -1f : 1f) * m_Angles[0];
  74. upperLimb.localRotation *= Quaternion.AngleAxis(upperLimbRotationAngle, Vector3.forward);
  75. var lowerLimbRotation = Vector2.SignedAngle(Vector2.right, lowerLimb.InverseTransformPoint(targetPosition)) + Vector2.SignedAngle(effector.localPosition, Vector2.right);
  76. m_Chain.transforms[1].localRotation *= Quaternion.AngleAxis(lowerLimbRotation, Vector3.forward);
  77. }
  78. }
  79. }
  80. }