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