HandFootIKData.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using System;
  2. using RootMotion.FinalIK;
  3. using UnityEngine;
  4. [Serializable]
  5. public class HandFootIKData : LimbIKData
  6. {
  7. public HandFootIKData(IKEffector effector, FBIKChain chain, IKMappingLimb ik_mapping, FullBodyIKCtrl ik_ctrl, Transform tgt_bone, bool use_old = false) : base(ik_ctrl, chain, ik_mapping, tgt_bone, use_old)
  8. {
  9. this.Effector = effector;
  10. this.Effector.target = base.IKTarget;
  11. this.m_IsUpperBody = (this.TargetBone == this.MyIKCtrl.GetIKBone(FullBodyIKCtrl.IKBoneType.Hand_L) || this.TargetBone == this.MyIKCtrl.GetIKBone(FullBodyIKCtrl.IKBoneType.Hand_R));
  12. this.m_ForceIKEnable = true;
  13. this.ToCorrectBone = this.TargetBone;
  14. }
  15. public ShoulderThighIKData ShoulderThighData
  16. {
  17. get
  18. {
  19. return this.m_ShoulderThighData;
  20. }
  21. }
  22. public ElbowKneeIKData ElbowKneeData
  23. {
  24. get
  25. {
  26. return this.m_ElbowKneeData;
  27. }
  28. }
  29. public override float PositionWeight
  30. {
  31. get
  32. {
  33. return this.Effector.positionWeight;
  34. }
  35. set
  36. {
  37. this.Effector.positionWeight = value;
  38. }
  39. }
  40. public override float RotationWeight
  41. {
  42. get
  43. {
  44. return this.Effector.rotationWeight;
  45. }
  46. set
  47. {
  48. this.Effector.rotationWeight = value;
  49. }
  50. }
  51. private void CheckBorder(HandFootIKData.BorderCorrectData correctData)
  52. {
  53. if (!correctData.Enable)
  54. {
  55. return;
  56. }
  57. float d = 0f;
  58. Vector3 position = this.ToCorrectBone.position;
  59. if (correctData.CheckBorder(position, ref d))
  60. {
  61. if (!base.GetIKSettingData(IKCtrlData.IKAttachType.NewPoint).IsIKExec)
  62. {
  63. base.GetIKSettingData(IKCtrlData.IKAttachType.NewPoint).IsIKExec = true;
  64. }
  65. switch (this.CorrectType)
  66. {
  67. case HandFootIKData.BorderCorrectType.Bone:
  68. base.IKTarget.position += correctData.Axis * d;
  69. this.m_ElbowKneeData.IKTarget.position += correctData.Axis * d;
  70. break;
  71. case HandFootIKData.BorderCorrectType.HalfBody:
  72. base.IKTarget.position += correctData.Axis * d;
  73. this.m_ElbowKneeData.IKTarget.position += correctData.Axis * d;
  74. this.m_ShoulderThighData.IKTarget.position += correctData.Axis * d;
  75. break;
  76. case HandFootIKData.BorderCorrectType.Chara:
  77. this.MyIKCtrl.BodyCtrlData.SetPosOffset(correctData.Axis * d);
  78. break;
  79. }
  80. }
  81. }
  82. protected override void SetTargetTransform(IKCtrlData.IKSettingData data, Vector3 pos, Quaternion rot)
  83. {
  84. base.SetTargetTransform(data, pos, rot);
  85. if (!data.IsPointAttach)
  86. {
  87. return;
  88. }
  89. Transform iktarget = this.m_ShoulderThighData.IKTarget;
  90. Transform iktarget2 = this.m_ElbowKneeData.IKTarget;
  91. if (!this.ElbowKneeData.GetIKExecTruth(IKCtrlData.IKAttachType.NewPoint))
  92. {
  93. Vector3 position = this.TargetBone.InverseTransformPoint(this.m_ElbowKneeData.TargetBone.position);
  94. Vector3 vector = base.IKTarget.TransformPoint(position);
  95. Vector3 vector2 = base.IKTarget.position - vector;
  96. Vector3 vector3 = vector - iktarget.position;
  97. float f = Mathf.Clamp(Vector3.Dot(vector2.normalized, vector3.normalized), -1f, 1f);
  98. float num = Mathf.Abs(Mathf.Acos(f) * 57.29578f);
  99. float t = Mathf.Clamp01(num / this.m_BendFadeBorder);
  100. Vector3 vector4 = base.IKTarget.position - iktarget.position;
  101. float d = Vector3.Dot(vector - iktarget.position, vector4.normalized);
  102. Vector3 vector5 = iktarget.position + vector4.normalized * d;
  103. Vector3 a = iktarget2.rotation * Vector3.up * 0.01f;
  104. Vector3 b = (vector - vector5) * this.m_BendStlength;
  105. Vector3 b2 = Vector3.Slerp(a, b, t);
  106. iktarget2.position = vector5 + b2;
  107. this.ElbowKneeData.PositionWeight = this.PositionWeight;
  108. }
  109. this.ElbowKneeData.RotationWeight = this.PositionWeight;
  110. iktarget2.rotation = Quaternion.FromToRotation(this.TargetBone.position - this.ElbowKneeData.TargetBone.position, base.IKTarget.position - iktarget2.position) * iktarget2.rotation;
  111. }
  112. public override void ApplyIKSetting()
  113. {
  114. if (!base.GetIKSettingData(IKCtrlData.IKAttachType.NewPoint).IsIKExec && !base.GetFlagData(IKCtrlData.IKAttachType.NewPoint).IsEnable && (this.FloorCorrect.Enable || this.WallCorrect.Enable))
  115. {
  116. base.ForceIK = true;
  117. }
  118. base.ApplyIKSetting();
  119. if (!base.IsIKExecTruth)
  120. {
  121. if (this.ElbowKneeData.RotationWeight > 0f)
  122. {
  123. this.PositionWeight = this.ElbowKneeData.RotationWeight;
  124. this.RotationWeight = this.ElbowKneeData.RotationWeight;
  125. base.IKTarget.position = this.ElbowKneeData.IKTarget.TransformPoint(this.TargetBone.localPosition);
  126. }
  127. else
  128. {
  129. this.PositionWeight = this.ElbowKneeData.PositionWeight;
  130. base.IKTarget.position += this.ElbowKneeData.IKTarget.position - this.ElbowKneeData.TargetBone.position;
  131. }
  132. }
  133. this.CheckBorder(this.WallCorrect);
  134. this.CheckBorder(this.FloorCorrect);
  135. }
  136. public override void Detach(IKCtrlData.IKAttachType attachType)
  137. {
  138. base.Detach(attachType);
  139. this.WallCorrect.Reset();
  140. this.FloorCorrect.Reset();
  141. this.ToCorrectBone = this.TargetBone;
  142. this.CorrectType = HandFootIKData.BorderCorrectType.Bone;
  143. }
  144. public void SetChainData(ElbowKneeIKData elbowknee_data, ShoulderThighIKData shoulderthigh_data)
  145. {
  146. this.m_ElbowKneeData = elbowknee_data;
  147. this.m_ShoulderThighData = shoulderthigh_data;
  148. }
  149. [Header("壁・床補正情報")]
  150. public HandFootIKData.BorderCorrectData WallCorrect = new HandFootIKData.BorderCorrectData(Vector3.forward);
  151. public HandFootIKData.BorderCorrectData FloorCorrect = new HandFootIKData.BorderCorrectData(Vector3.up);
  152. public HandFootIKData.BorderCorrectType CorrectType;
  153. public Transform ToCorrectBone;
  154. [SerializeField]
  155. [Space]
  156. [Range(1f, 90f)]
  157. private float m_BendFadeBorder = 30f;
  158. [SerializeField]
  159. [Range(1f, 2f)]
  160. private float m_BendStlength = 1.01f;
  161. public readonly IKEffector Effector;
  162. private ShoulderThighIKData m_ShoulderThighData;
  163. private ElbowKneeIKData m_ElbowKneeData;
  164. public enum BorderCorrectType
  165. {
  166. Bone,
  167. HalfBody,
  168. Chara,
  169. All
  170. }
  171. [Serializable]
  172. public class BorderCorrectData
  173. {
  174. public BorderCorrectData(Vector3 axis)
  175. {
  176. this.Axis = axis;
  177. }
  178. public void Reset()
  179. {
  180. this.Border = 0f;
  181. this.Enable = false;
  182. }
  183. public float GetValue(Vector3 pos)
  184. {
  185. pos = KasaiUtility.Vec3Multiply(pos, this.Axis);
  186. return pos.magnitude * Vector3.Dot(this.Axis, pos.normalized);
  187. }
  188. public bool CheckBorder(Vector3 check_pos, ref float diff)
  189. {
  190. float value = this.GetValue(check_pos);
  191. diff = this.Border - value;
  192. return value < this.Border;
  193. }
  194. public float Border;
  195. public bool Enable;
  196. public Vector3 Axis;
  197. }
  198. }