HandFootIKData.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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, tgt_bone, use_old)
  8. {
  9. this.Effector = effector;
  10. this.Effector.target = base.IKTarget;
  11. this.IKMapping = ik_mapping;
  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. Vector3 vector = this.m_ElbowKneeData.TargetBone.position - this.TargetBone.position;
  92. Vector3 vector2 = this.m_ShoulderThighData.TargetBone.position - this.m_ElbowKneeData.TargetBone.position;
  93. if (this.ElbowKneeData.GetIKSettingData(IKCtrlData.IKAttachType.NewPoint).IsIKExec)
  94. {
  95. return;
  96. }
  97. float f = Vector3.Dot(vector.normalized, vector2.normalized);
  98. float num = Mathf.Acos(f) * 57.29578f;
  99. float num2 = Mathf.Clamp01(num / this.m_BendFadeBorder);
  100. this.ElbowKneeData.PositionWeight = num2 * this.PositionWeight;
  101. Vector3 position = this.TargetBone.InverseTransformPoint(this.m_ElbowKneeData.TargetBone.position);
  102. Vector3 position2 = base.IKTarget.TransformPoint(position);
  103. iktarget2.position = position2;
  104. if (this.ElbowKneeData.GetIKSettingData(IKCtrlData.IKAttachType.Rotate).IsIKExec)
  105. {
  106. this.ElbowKneeData.RotationWeight = this.PositionWeight;
  107. iktarget2.rotation = Quaternion.FromToRotation(this.TargetBone.position - this.ElbowKneeData.TargetBone.position, base.IKTarget.position - iktarget2.position) * iktarget2.rotation;
  108. }
  109. }
  110. public override void ApplyIKSetting()
  111. {
  112. if (!base.GetIKSettingData(IKCtrlData.IKAttachType.NewPoint).IsIKExec && !base.GetFlagData(IKCtrlData.IKAttachType.NewPoint).IsEnable && (this.FloorCorrect.Enable || this.WallCorrect.Enable))
  113. {
  114. base.ForceIK = true;
  115. }
  116. base.ApplyIKSetting();
  117. this.CheckBorder(this.WallCorrect);
  118. this.CheckBorder(this.FloorCorrect);
  119. }
  120. public override void Detach(IKCtrlData.IKAttachType attachType)
  121. {
  122. base.Detach(attachType);
  123. this.WallCorrect.Reset();
  124. this.FloorCorrect.Reset();
  125. this.ToCorrectBone = this.TargetBone;
  126. this.CorrectType = HandFootIKData.BorderCorrectType.Bone;
  127. }
  128. public void SetChainData(ElbowKneeIKData elbowknee_data, ShoulderThighIKData shoulderthigh_data)
  129. {
  130. this.m_ElbowKneeData = elbowknee_data;
  131. this.m_ShoulderThighData = shoulderthigh_data;
  132. }
  133. [Header("壁・床補正情報")]
  134. public HandFootIKData.BorderCorrectData WallCorrect = new HandFootIKData.BorderCorrectData(Vector3.forward);
  135. public HandFootIKData.BorderCorrectData FloorCorrect = new HandFootIKData.BorderCorrectData(Vector3.up);
  136. public HandFootIKData.BorderCorrectType CorrectType;
  137. public Transform ToCorrectBone;
  138. [SerializeField]
  139. [Space]
  140. [Range(1f, 90f)]
  141. private float m_BendFadeBorder = 30f;
  142. public readonly IKEffector Effector;
  143. public readonly IKMappingLimb IKMapping;
  144. private ShoulderThighIKData m_ShoulderThighData;
  145. private ElbowKneeIKData m_ElbowKneeData;
  146. public enum BorderCorrectType
  147. {
  148. Bone,
  149. HalfBody,
  150. Chara,
  151. All
  152. }
  153. [Serializable]
  154. public class BorderCorrectData
  155. {
  156. public BorderCorrectData(Vector3 axis)
  157. {
  158. this.Axis = axis;
  159. }
  160. public void Reset()
  161. {
  162. this.Border = 0f;
  163. this.Enable = false;
  164. }
  165. public float GetValue(Vector3 pos)
  166. {
  167. pos = KasaiUtility.Vec3Multiply(pos, this.Axis);
  168. return pos.magnitude * Vector3.Dot(this.Axis, pos.normalized);
  169. }
  170. public bool CheckBorder(Vector3 check_pos, ref float diff)
  171. {
  172. float value = this.GetValue(check_pos);
  173. diff = this.Border - value;
  174. return value < this.Border;
  175. }
  176. public float Border;
  177. public bool Enable;
  178. public Vector3 Axis;
  179. }
  180. }