HandFootIKData.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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.NoPullBodyIK = base.IKTarget.parent.gameObject.AddComponent<LimbIK>();
  14. this.NoPullBodyIK.solver.SetChain(base.ChainBones[0], base.ChainBones[1], base.ChainBones[2], base.ChainBones[0]);
  15. this.NoPullBodyIK.solver.target = base.IKTarget;
  16. this.NoPullBodyIK.solver.bendModifier = IKSolverLimb.BendModifier.Goal;
  17. this.NoPullBodyIK.solver.maintainRotationWeight = effector.maintainRelativePositionWeight;
  18. this.NoPullBodyIK.enabled = false;
  19. if (this.TargetBone == this.MyIKCtrl.GetIKBone(FullBodyIKCtrl.IKBoneType.Hand_L))
  20. {
  21. this.NoPullBodyIK.solver.goal = AvatarIKGoal.LeftHand;
  22. }
  23. else if (this.TargetBone == this.MyIKCtrl.GetIKBone(FullBodyIKCtrl.IKBoneType.Hand_R))
  24. {
  25. this.NoPullBodyIK.solver.goal = AvatarIKGoal.RightHand;
  26. }
  27. else if (this.TargetBone == this.MyIKCtrl.GetIKBone(FullBodyIKCtrl.IKBoneType.Foot_L))
  28. {
  29. this.NoPullBodyIK.solver.goal = AvatarIKGoal.LeftFoot;
  30. }
  31. else if (this.TargetBone == this.MyIKCtrl.GetIKBone(FullBodyIKCtrl.IKBoneType.Foot_R))
  32. {
  33. this.NoPullBodyIK.solver.goal = AvatarIKGoal.RightFoot;
  34. }
  35. this.ToCorrectBone = this.TargetBone;
  36. }
  37. public ShoulderThighIKData ShoulderThighData
  38. {
  39. get
  40. {
  41. return this.m_ShoulderThighData;
  42. }
  43. }
  44. public ElbowKneeIKData ElbowKneeData
  45. {
  46. get
  47. {
  48. return this.m_ElbowKneeData;
  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. public override void TagetTransCpy()
  83. {
  84. base.TagetTransCpy();
  85. this.Effector.positionWeight = 0f;
  86. this.Effector.rotationWeight = 0f;
  87. this.NoPullBodyIK.solver.IKPositionWeight = 0f;
  88. this.NoPullBodyIK.solver.IKRotationWeight = 0f;
  89. this.NoPullBodyIK.solver.bendModifierWeight = 0f;
  90. }
  91. protected override void SetTargetTransform(IKCtrlData.IKSettingData data, Vector3 pos, Quaternion rot)
  92. {
  93. base.SetTargetTransform(data, pos, rot);
  94. if (!data.IsPointAttach)
  95. {
  96. return;
  97. }
  98. Transform iktarget = this.m_ShoulderThighData.IKTarget;
  99. Transform iktarget2 = this.m_ElbowKneeData.IKTarget;
  100. if (!this.ElbowKneeData.GetIKExecTruth(IKCtrlData.IKAttachType.NewPoint))
  101. {
  102. Vector3 position = this.TargetBone.InverseTransformPoint(this.m_ElbowKneeData.TargetBone.position);
  103. Vector3 vector = base.IKTarget.TransformPoint(position);
  104. Vector3 vector2 = base.IKTarget.position - vector;
  105. Vector3 vector3 = vector - iktarget.position;
  106. float f = Mathf.Clamp(Vector3.Dot(vector2.normalized, vector3.normalized), -1f, 1f);
  107. float num = Mathf.Abs(Mathf.Acos(f) * 57.29578f);
  108. float t = Mathf.Clamp01(num / this.m_BendFadeBorder);
  109. Vector3 vector4 = base.IKTarget.position - iktarget.position;
  110. float num2 = Vector3.Dot(vector - iktarget.position, vector4.normalized);
  111. if (float.IsNaN(num2))
  112. {
  113. num2 = 0f;
  114. }
  115. Vector3 vector5 = iktarget.position + vector4.normalized * num2;
  116. Vector3 a = iktarget2.rotation * Vector3.up * 0.01f;
  117. Vector3 b = (vector - vector5) * this.m_BendStlength;
  118. Vector3 b2 = Vector3.Slerp(a, b, t);
  119. iktarget2.position = vector5 + b2;
  120. this.SetElbowKneePositionWeight(base.PositionWeight);
  121. }
  122. this.ElbowKneeData.RotationWeight = base.PositionWeight;
  123. iktarget2.rotation = Quaternion.FromToRotation(this.TargetBone.position - this.ElbowKneeData.TargetBone.position, base.IKTarget.position - iktarget2.position) * iktarget2.rotation;
  124. }
  125. private void SetElbowKneePositionWeight(float val)
  126. {
  127. if (this.m_IsPullBody)
  128. {
  129. this.ElbowKneeData.PositionWeight = val;
  130. }
  131. else
  132. {
  133. this.NoPullBodyIK.solver.bendModifierWeight = val;
  134. }
  135. }
  136. public override void ApplyIKSetting()
  137. {
  138. if (!base.GetIKSettingData(IKCtrlData.IKAttachType.NewPoint).IsIKExec && !base.GetFlagData(IKCtrlData.IKAttachType.NewPoint).IsEnable && (this.FloorCorrect.Enable || this.WallCorrect.Enable))
  139. {
  140. base.ForceIK = true;
  141. }
  142. base.ApplyIKSetting();
  143. if (!base.IsIKExecTruth)
  144. {
  145. if (this.ElbowKneeData.RotationWeight > 0f)
  146. {
  147. base.PositionWeight = this.ElbowKneeData.RotationWeight;
  148. base.RotationWeight = this.ElbowKneeData.RotationWeight;
  149. base.IKTarget.position = this.ElbowKneeData.IKTarget.TransformPoint(this.TargetBone.localPosition);
  150. }
  151. else
  152. {
  153. base.PositionWeight = this.ElbowKneeData.PositionWeight;
  154. base.IKTarget.position += this.ElbowKneeData.IKTarget.position - this.ElbowKneeData.TargetBone.position;
  155. }
  156. }
  157. this.CheckBorder(this.WallCorrect);
  158. this.CheckBorder(this.FloorCorrect);
  159. }
  160. public override void Detach(IKCtrlData.IKAttachType attachType)
  161. {
  162. base.Detach(attachType);
  163. this.WallCorrect.Reset();
  164. this.FloorCorrect.Reset();
  165. this.ToCorrectBone = this.TargetBone;
  166. this.CorrectType = HandFootIKData.BorderCorrectType.Bone;
  167. }
  168. public void SetChainData(ElbowKneeIKData elbowknee_data, ShoulderThighIKData shoulderthigh_data)
  169. {
  170. this.m_ElbowKneeData = elbowknee_data;
  171. this.m_ShoulderThighData = shoulderthigh_data;
  172. this.NoPullBodyIK.solver.bendGoal = elbowknee_data.IKTarget;
  173. }
  174. protected override void OnPostSetPositionWeight(float val)
  175. {
  176. if (this.m_IsPullBody)
  177. {
  178. this.Effector.positionWeight = val;
  179. }
  180. else
  181. {
  182. this.NoPullBodyIK.solver.IKPositionWeight = val;
  183. }
  184. }
  185. protected override void OnPostSetRotationWeight(float val)
  186. {
  187. if (this.m_IsPullBody)
  188. {
  189. this.Effector.rotationWeight = val;
  190. }
  191. else
  192. {
  193. this.NoPullBodyIK.solver.IKRotationWeight = val;
  194. }
  195. }
  196. public override void Update()
  197. {
  198. if (this.m_IsPullBody)
  199. {
  200. return;
  201. }
  202. this.NoPullBodyIK.solver.Update();
  203. }
  204. [Header("壁・床補正情報")]
  205. public HandFootIKData.BorderCorrectData WallCorrect = new HandFootIKData.BorderCorrectData(Vector3.forward);
  206. public HandFootIKData.BorderCorrectData FloorCorrect = new HandFootIKData.BorderCorrectData(Vector3.up);
  207. public HandFootIKData.BorderCorrectType CorrectType;
  208. public Transform ToCorrectBone;
  209. [SerializeField]
  210. [Space]
  211. [Range(1f, 90f)]
  212. private float m_BendFadeBorder = 30f;
  213. [SerializeField]
  214. [Range(1f, 2f)]
  215. private float m_BendStlength = 1.01f;
  216. public readonly IKEffector Effector;
  217. private ShoulderThighIKData m_ShoulderThighData;
  218. private ElbowKneeIKData m_ElbowKneeData;
  219. private LimbIK NoPullBodyIK;
  220. public enum BorderCorrectType
  221. {
  222. Bone,
  223. HalfBody,
  224. Chara,
  225. All
  226. }
  227. [Serializable]
  228. public class BorderCorrectData
  229. {
  230. public BorderCorrectData(Vector3 axis)
  231. {
  232. this.Axis = axis;
  233. }
  234. public void Reset()
  235. {
  236. this.Border = 0f;
  237. this.Enable = false;
  238. }
  239. public float GetValue(Vector3 pos)
  240. {
  241. pos = KasaiUtility.Vec3Multiply(pos, this.Axis);
  242. return pos.magnitude * Vector3.Dot(this.Axis, pos.normalized);
  243. }
  244. public bool CheckBorder(Vector3 check_pos, ref float diff)
  245. {
  246. float value = this.GetValue(check_pos);
  247. diff = this.Border - value;
  248. return value < this.Border;
  249. }
  250. public float Border;
  251. public bool Enable;
  252. public Vector3 Axis;
  253. }
  254. }