ElbowKneeIKData.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using RootMotion.FinalIK;
  3. using UnityEngine;
  4. [Serializable]
  5. public class ElbowKneeIKData : LimbIKData
  6. {
  7. public ElbowKneeIKData(FBIKChain chain, IKMappingLimb ik_mapping, FullBodyIKCtrl ik_ctrl, Transform tgt_bone) : base(ik_ctrl, chain, ik_mapping, tgt_bone, false)
  8. {
  9. this.Chain.bendConstraint.bendGoal = base.IKTarget;
  10. this.PositionWeight = 0f;
  11. this.m_IsUpperBody = (this.TargetBone == this.MyIKCtrl.GetIKBone(FullBodyIKCtrl.IKBoneType.Forearm_L) || this.TargetBone == this.MyIKCtrl.GetIKBone(FullBodyIKCtrl.IKBoneType.Forearm_R));
  12. this.m_ForceIKEnable = true;
  13. this.m_FABRIK = ik_ctrl.IKTgtRoot.gameObject.AddComponent<FABRIK>();
  14. this.m_FABRIK.solver.SetChain(new Transform[]
  15. {
  16. base.ChainBones[0],
  17. base.ChainBones[1]
  18. }, base.ChainBones[0]);
  19. this.m_FABRIK.solver.target = base.IKTarget;
  20. this.m_FABRIK.enabled = false;
  21. }
  22. public override float PositionWeight
  23. {
  24. get
  25. {
  26. return this.Chain.bendConstraint.weight;
  27. }
  28. set
  29. {
  30. this.Chain.bendConstraint.weight = value;
  31. }
  32. }
  33. public override float RotationWeight { get; set; }
  34. public ShoulderThighIKData ShoulderThighData
  35. {
  36. get
  37. {
  38. return this.m_ShoulderThighData;
  39. }
  40. }
  41. public HandFootIKData HandFootData
  42. {
  43. get
  44. {
  45. return this.m_HandFootData;
  46. }
  47. }
  48. public override void ApplyIKSetting()
  49. {
  50. this.m_TargetBoneLocalRot = this.TargetBone.localRotation;
  51. this.m_HandFootLocalRot = this.HandFootData.TargetBone.localRotation;
  52. base.ApplyIKSetting();
  53. }
  54. protected override void SetTargetTransform(IKCtrlData.IKSettingData data, Vector3 pos, Quaternion rot)
  55. {
  56. base.SetTargetTransform(data, pos, rot);
  57. Transform iktarget = this.m_ShoulderThighData.IKTarget;
  58. if (data.IsPointAttach)
  59. {
  60. iktarget.rotation = Quaternion.FromToRotation(this.TargetBone.position - iktarget.position, base.IKTarget.position - iktarget.position) * iktarget.rotation;
  61. }
  62. else
  63. {
  64. Vector3 axis = base.IKTarget.position - iktarget.position;
  65. Quaternion rotation = base.IKTarget.rotation;
  66. Vector3 lhs = rotation * Vector3.forward;
  67. Vector3 rhs = this.TargetBone.rotation * Vector3.forward;
  68. float f = Vector3.Dot(lhs, rhs);
  69. float num = Mathf.Acos(f) * 57.29578f;
  70. if (float.IsNaN(num))
  71. {
  72. num = 0f;
  73. }
  74. iktarget.rotation = Quaternion.AngleAxis(num, axis) * iktarget.rotation;
  75. }
  76. }
  77. public void SetChainData(HandFootIKData handfoot_data, ShoulderThighIKData shoulderthigh_data)
  78. {
  79. this.m_HandFootData = handfoot_data;
  80. this.m_ShoulderThighData = shoulderthigh_data;
  81. }
  82. public override void Update()
  83. {
  84. if (this.HandFootData.IsIKExecTruth)
  85. {
  86. return;
  87. }
  88. if (base.RotateIK.IsIKExec)
  89. {
  90. this.ShoulderThighData.TargetBone.rotation = Quaternion.Slerp(this.ShoulderThighData.TargetBone.rotation, this.ShoulderThighData.IKTarget.rotation, this.RotationWeight);
  91. this.TargetBone.localRotation = this.m_TargetBoneLocalRot;
  92. this.HandFootData.TargetBone.localRotation = this.m_HandFootLocalRot;
  93. }
  94. Quaternion rotation = this.TargetBone.rotation;
  95. if (base.PointIK.IsIKExec)
  96. {
  97. this.m_FABRIK.solver.IKPositionWeight = this.PositionWeight;
  98. this.m_FABRIK.solver.Update();
  99. this.TargetBone.rotation = rotation;
  100. }
  101. }
  102. private ShoulderThighIKData m_ShoulderThighData;
  103. private HandFootIKData m_HandFootData;
  104. private FABRIK m_FABRIK;
  105. private Quaternion m_HandFootLocalRot = Quaternion.identity;
  106. private Quaternion m_TargetBoneLocalRot = Quaternion.identity;
  107. }