BipedIKCtrlData.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System;
  2. using System.Linq;
  3. using RootMotion.FinalIK;
  4. using UnityEngine;
  5. [Serializable]
  6. public class BipedIKCtrlData : IKCtrlData
  7. {
  8. public BipedIKCtrlData(IKEffector effector, FBIKChain chain, IKMappingLimb ik_mapping, FullBodyIKCtrl ik_ctrl, Transform tgt_bone, IKEffector sub_effector, bool use_old = false) : base(ik_ctrl, tgt_bone, use_old, false)
  9. {
  10. this.Effector = effector;
  11. this.Effector.target = base.IKTarget;
  12. this.Chain = chain;
  13. this.IKMapping = ik_mapping;
  14. this.RootEffector = sub_effector;
  15. Transform transform = this.CreateSubTarget("BendBone");
  16. this.m_BoneBendGoal = new IKCtrlData.BoneTgtPair(this.Chain.bendConstraint.bone2, transform);
  17. this.Chain.bendConstraint.bendGoal = transform;
  18. this.Chain.bendConstraint.weight = 1f;
  19. this.RootEffector.target = this.CreateSubTarget("ChainRootBone");
  20. this.m_RootBoneTgtPair = new IKCtrlData.BoneTgtPair(this.RootEffector.bone, this.RootEffector.target);
  21. this.m_ForceIKEnable = true;
  22. this.ToCorrectBone = base.TargetBone;
  23. }
  24. public Transform BendBone
  25. {
  26. get
  27. {
  28. return this.m_BoneBendGoal.Bone;
  29. }
  30. }
  31. public override Transform[] ChainBones
  32. {
  33. get
  34. {
  35. return (from node in this.Chain.nodes
  36. select node.transform).ToArray<Transform>();
  37. }
  38. }
  39. public override float PositionWeight
  40. {
  41. get
  42. {
  43. return this.Effector.positionWeight;
  44. }
  45. set
  46. {
  47. this.Effector.positionWeight = value;
  48. }
  49. }
  50. public override float RotationWeight
  51. {
  52. get
  53. {
  54. return this.Effector.rotationWeight;
  55. }
  56. set
  57. {
  58. this.Effector.rotationWeight = value;
  59. }
  60. }
  61. private Transform CreateSubTarget(string name)
  62. {
  63. Transform transform = base.IKTarget.parent.Find(name);
  64. if (!transform)
  65. {
  66. transform = new GameObject(name).transform;
  67. transform.SetParent(base.IKTarget.parent, false);
  68. }
  69. return transform;
  70. }
  71. private void CheckBorder(BipedIKCtrlData.BorderCorrectData correctData)
  72. {
  73. if (!correctData.Enable)
  74. {
  75. return;
  76. }
  77. float num = 0f;
  78. Vector3 check_pos = this.ToCorrectBone.position + this.MyIKCtrl.BodyCtrlData.BodyOffset;
  79. if (correctData.CheckBorder(check_pos, ref num))
  80. {
  81. switch (this.CorrectType)
  82. {
  83. case BipedIKCtrlData.BorderCorrectType.Bone:
  84. base.IKTarget.position += correctData.Axis * num;
  85. this.m_BoneBendGoal.Target.position += correctData.Axis * num;
  86. break;
  87. case BipedIKCtrlData.BorderCorrectType.HalfBody:
  88. base.IKTarget.position += correctData.Axis * num;
  89. this.m_BoneBendGoal.Target.position += correctData.Axis * num;
  90. this.m_RootBoneTgtPair.Target.position += correctData.Axis * num;
  91. break;
  92. case BipedIKCtrlData.BorderCorrectType.Chara:
  93. if (correctData.GetValue(this.MyIKCtrl.BodyCtrlData.AddOffset) < num)
  94. {
  95. this.MyIKCtrl.BodyCtrlData.SetAddOffset(correctData.Axis * num);
  96. }
  97. break;
  98. case BipedIKCtrlData.BorderCorrectType.All:
  99. if (correctData.GetValue(this.MyIKCtrl.BodyCtrlData.AllOffset) < num)
  100. {
  101. this.MyIKCtrl.BodyCtrlData.SetAllOffset(correctData.Axis * num);
  102. }
  103. break;
  104. }
  105. }
  106. }
  107. public override void ApplyIKSetting()
  108. {
  109. this.m_BoneBendGoal.Cppy();
  110. this.m_RootBoneTgtPair.Cppy();
  111. base.ApplyIKSetting();
  112. if (base.ForceIK)
  113. {
  114. this.CheckBorder(this.WallCorrect);
  115. this.CheckBorder(this.FloorCorrect);
  116. }
  117. }
  118. public override void Detach(IKCtrlData.IKAttachType attachType, float blend_time = 0f)
  119. {
  120. base.Detach(attachType, blend_time);
  121. this.Chain.bendConstraint.weight = 1f;
  122. this.RootEffector.positionWeight = 0f;
  123. this.RootEffector.rotationWeight = 0f;
  124. this.m_BoneBendGoal.PosOffset = Vector3.zero;
  125. this.m_RootBoneTgtPair.PosOffset = Vector3.zero;
  126. this.WallCorrect.Reset();
  127. this.FloorCorrect.Reset();
  128. this.ToCorrectBone = base.TargetBone;
  129. this.CorrectType = BipedIKCtrlData.BorderCorrectType.Bone;
  130. }
  131. public override void SetTargetOffset(Vector3 offset, bool inverse = false)
  132. {
  133. if (base.IsIKExec)
  134. {
  135. return;
  136. }
  137. base.SetTargetOffset(offset, inverse);
  138. this.m_BoneBendGoal.PosOffset = this.m_OffsetEnable.GetEnablePos(offset, inverse);
  139. this.m_RootBoneTgtPair.PosOffset = this.m_OffsetEnable.GetEnablePos(offset, inverse);
  140. }
  141. private IKCtrlData.BoneTgtPair m_BoneBendGoal;
  142. private IKCtrlData.BoneTgtPair m_RootBoneTgtPair;
  143. [Header("壁・床補正情報")]
  144. public BipedIKCtrlData.BorderCorrectData WallCorrect = new BipedIKCtrlData.BorderCorrectData(Vector3.forward);
  145. public BipedIKCtrlData.BorderCorrectData FloorCorrect = new BipedIKCtrlData.BorderCorrectData(Vector3.up);
  146. public BipedIKCtrlData.BorderCorrectType CorrectType;
  147. public Transform ToCorrectBone;
  148. public readonly IKEffector Effector;
  149. public readonly FBIKChain Chain;
  150. public readonly IKMappingLimb IKMapping;
  151. public readonly IKEffector RootEffector;
  152. public enum BorderCorrectType
  153. {
  154. Bone,
  155. HalfBody,
  156. Chara,
  157. All
  158. }
  159. [Serializable]
  160. public class BorderCorrectData
  161. {
  162. public BorderCorrectData(Vector3 axis)
  163. {
  164. this.Axis = axis;
  165. }
  166. public void Reset()
  167. {
  168. this.Border = 0f;
  169. this.Enable = false;
  170. }
  171. public float GetValue(Vector3 pos)
  172. {
  173. pos = KasaiUtility.Vec3Multiply(pos, this.Axis);
  174. return pos.magnitude * Vector3.Dot(this.Axis, pos.normalized);
  175. }
  176. public bool CheckBorder(Vector3 check_pos, ref float diff)
  177. {
  178. float value = this.GetValue(check_pos);
  179. diff = this.Border - value;
  180. return value < this.Border;
  181. }
  182. public float Border;
  183. public bool Enable;
  184. public Vector3 Axis;
  185. }
  186. }