BipedIKCtrlData.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 = this.TargetBone;
  23. this.m_BendweitCtrl.Axis = Vector3.up;
  24. }
  25. public Transform BendBone
  26. {
  27. get
  28. {
  29. return this.m_BoneBendGoal.Bone;
  30. }
  31. }
  32. public Transform RootBone
  33. {
  34. get
  35. {
  36. return this.m_RootBoneTgtPair.Bone;
  37. }
  38. }
  39. public IKCtrlData.ChangeFlagData BendweightCtrl
  40. {
  41. get
  42. {
  43. return this.m_BendweitCtrl;
  44. }
  45. }
  46. public override Transform TargetBone
  47. {
  48. get
  49. {
  50. if (this.IsRootIK)
  51. {
  52. return this.RootBone;
  53. }
  54. if (this.IsBendIK)
  55. {
  56. return this.BendBone;
  57. }
  58. return base.TargetBone;
  59. }
  60. }
  61. public override Transform[] ChainBones
  62. {
  63. get
  64. {
  65. return (from node in this.Chain.nodes
  66. select node.transform).ToArray<Transform>();
  67. }
  68. }
  69. public override float PositionWeight
  70. {
  71. get
  72. {
  73. return this.Effector.positionWeight;
  74. }
  75. set
  76. {
  77. this.Effector.positionWeight = value;
  78. }
  79. }
  80. public override float RotationWeight
  81. {
  82. get
  83. {
  84. return this.Effector.rotationWeight;
  85. }
  86. set
  87. {
  88. this.Effector.rotationWeight = value;
  89. }
  90. }
  91. private Transform CreateSubTarget(string name)
  92. {
  93. Transform transform = base.IKTarget.parent.Find(name);
  94. if (!transform)
  95. {
  96. transform = new GameObject(name).transform;
  97. transform.SetParent(base.IKTarget.parent, false);
  98. }
  99. return transform;
  100. }
  101. private void CheckBorder(BipedIKCtrlData.BorderCorrectData correctData)
  102. {
  103. if (!correctData.Enable)
  104. {
  105. return;
  106. }
  107. float num = 0f;
  108. Vector3 check_pos = this.ToCorrectBone.position + this.MyIKCtrl.BodyCtrlData.BodyOffset;
  109. if (correctData.CheckBorder(check_pos, ref num))
  110. {
  111. switch (this.CorrectType)
  112. {
  113. case BipedIKCtrlData.BorderCorrectType.Bone:
  114. base.IKTarget.position += correctData.Axis * num;
  115. this.m_BoneBendGoal.Target.position += correctData.Axis * num;
  116. if (this.PositionWeight == 0f)
  117. {
  118. this.PositionWeight = 1f;
  119. base.PointIK.IsIKExec = true;
  120. }
  121. break;
  122. case BipedIKCtrlData.BorderCorrectType.HalfBody:
  123. base.IKTarget.position += correctData.Axis * num;
  124. this.m_BoneBendGoal.Target.position += correctData.Axis * num;
  125. this.m_RootBoneTgtPair.Target.position += correctData.Axis * num;
  126. if (this.PositionWeight == 0f)
  127. {
  128. this.PositionWeight = 1f;
  129. base.PointIK.IsIKExec = true;
  130. }
  131. break;
  132. case BipedIKCtrlData.BorderCorrectType.Chara:
  133. if (correctData.GetValue(this.MyIKCtrl.BodyCtrlData.AddOffset) < num)
  134. {
  135. this.MyIKCtrl.BodyCtrlData.SetAddOffset(correctData.Axis * num);
  136. }
  137. break;
  138. case BipedIKCtrlData.BorderCorrectType.All:
  139. if (correctData.GetValue(this.MyIKCtrl.BodyCtrlData.AllOffset) < num)
  140. {
  141. this.MyIKCtrl.BodyCtrlData.SetAllOffset(correctData.Axis * num);
  142. }
  143. break;
  144. }
  145. }
  146. }
  147. protected override void SetTargetTransform(IKCtrlData.IKParam data, Vector3 pos, Quaternion rot)
  148. {
  149. base.SetTargetTransform(data, pos, rot);
  150. Transform target = this.m_BoneBendGoal.Target;
  151. Transform target2 = this.m_RootBoneTgtPair.Target;
  152. if (this.IsRootIK || this.IsBendIK)
  153. {
  154. Vector3 position = target2.InverseTransformPoint(target.position);
  155. Vector3 position2 = this.m_BoneBendGoal.Bone.InverseTransformPoint(base.TargetBone.position);
  156. if (this.IsRootIK)
  157. {
  158. if (data.IsPointAttach)
  159. {
  160. this.RootEffector.positionWeight = this.PositionWeight;
  161. target2.position = base.IKTarget.position;
  162. }
  163. else
  164. {
  165. this.RootEffector.rotationWeight = this.RotationWeight;
  166. target2.rotation = base.IKTarget.rotation;
  167. target.rotation = target2.rotation * this.m_BoneBendGoal.Bone.localRotation;
  168. base.IKTarget.rotation = target.rotation * base.TargetBone.localRotation;
  169. }
  170. target.position = target2.TransformPoint(position);
  171. base.IKTarget.position = target.TransformPoint(position2);
  172. }
  173. else
  174. {
  175. if (data.IsPointAttach)
  176. {
  177. this.Chain.bendConstraint.weight = this.PositionWeight;
  178. this.RootEffector.positionWeight = (this.RootEffector.rotationWeight = this.PositionWeight);
  179. target2.rotation = Quaternion.FromToRotation(target.position - target2.position, base.IKTarget.position - target2.position) * target2.rotation;
  180. Vector3 b = base.IKTarget.position - target2.TransformPoint(position);
  181. target2.position += b;
  182. target.position = base.IKTarget.position;
  183. }
  184. else
  185. {
  186. target.rotation = base.IKTarget.rotation;
  187. base.IKTarget.rotation = target.rotation * base.TargetBone.localRotation;
  188. }
  189. base.IKTarget.position = this.m_BoneBendGoal.Target.TransformPoint(position2);
  190. }
  191. }
  192. else if (data.IsPointAttach)
  193. {
  194. Vector3 vector = this.m_BoneBendGoal.Bone.position - this.TargetBone.position;
  195. Vector3 vector2 = this.m_RootBoneTgtPair.Bone.position - this.m_BoneBendGoal.Bone.position;
  196. float f = Vector3.Dot(vector.normalized, vector2.normalized);
  197. float num = Mathf.Acos(f) * 57.29578f;
  198. float num2 = Mathf.Clamp01(num / this.m_BendFadeBorder);
  199. this.Chain.bendConstraint.weight = num2 * this.PositionWeight;
  200. Vector3 position3 = this.TargetBone.InverseTransformPoint(this.m_RootBoneTgtPair.Bone.position);
  201. target2.position = base.IKTarget.TransformPoint(position3);
  202. Vector3 position4 = this.TargetBone.InverseTransformPoint(this.m_BoneBendGoal.Bone.position);
  203. Vector3 position5 = base.IKTarget.TransformPoint(position4);
  204. target.position = position5;
  205. }
  206. }
  207. public void SetBendweightCtrl(string name)
  208. {
  209. if (this.MyIKCtrl.TgtMaid.boMAN)
  210. {
  211. this.BendweightCtrl.Target = this.MyIKCtrl.TgtBody.GetBone(name);
  212. }
  213. else
  214. {
  215. this.BendweightCtrl.Target = this.MyIKCtrl.GetSTFlagObj(name);
  216. }
  217. }
  218. public override void ApplyIKSetting()
  219. {
  220. this.m_BoneBendGoal.Cppy();
  221. this.m_RootBoneTgtPair.Cppy();
  222. this.Chain.bendConstraint.weight = 0f;
  223. this.RootEffector.positionWeight = 0f;
  224. this.RootEffector.rotationWeight = 0f;
  225. if (!base.PointIK.IsIKExec && !base.PointFlagData.IsEnable && (this.FloorCorrect.Enable || this.WallCorrect.Enable))
  226. {
  227. base.ForceIK = true;
  228. }
  229. base.ApplyIKSetting();
  230. this.CheckBorder(this.WallCorrect);
  231. this.CheckBorder(this.FloorCorrect);
  232. if (!this.IsBendIK && !this.IsRootIK && this.m_BendweitCtrl.IsEnable)
  233. {
  234. this.Chain.bendConstraint.weight = 1f - this.m_BendweitCtrl.Value01();
  235. }
  236. }
  237. public override void SetIKSetting(IKCtrlData.IKAttachType attachType, bool is_next, Maid tgt_maid, int slot_no, string attach_name, Transform axis_bone, Transform target, Vector3 f_vecOffset, bool do_animation = false)
  238. {
  239. base.SetIKSetting(attachType, is_next, tgt_maid, slot_no, attach_name, axis_bone, target, f_vecOffset, do_animation);
  240. if (this.IsRootIK)
  241. {
  242. base.BlendPosRot.Copy(this.m_lastRootPosRot);
  243. }
  244. else if (this.IsBendIK)
  245. {
  246. base.BlendPosRot.Copy(this.m_lastBendPosRot);
  247. }
  248. }
  249. public override void Detach(IKCtrlData.IKAttachType attachType)
  250. {
  251. base.Detach(attachType);
  252. this.IsRootIK = (this.IsBendIK = false);
  253. this.RootEffector.positionWeight = 0f;
  254. this.RootEffector.rotationWeight = 0f;
  255. this.m_BoneBendGoal.PosOffset = Vector3.zero;
  256. this.m_RootBoneTgtPair.PosOffset = Vector3.zero;
  257. this.WallCorrect.Reset();
  258. this.FloorCorrect.Reset();
  259. this.ToCorrectBone = this.TargetBone;
  260. this.CorrectType = BipedIKCtrlData.BorderCorrectType.Bone;
  261. this.m_BendweitCtrl.Reset();
  262. }
  263. public override void SetTargetOffset(Vector3 offset, bool inverse = false)
  264. {
  265. if (base.IsIKExec)
  266. {
  267. return;
  268. }
  269. base.SetTargetOffset(offset, inverse);
  270. this.m_BoneBendGoal.PosOffset = this.m_OffsetEnable.GetEnable(offset, inverse);
  271. this.m_RootBoneTgtPair.PosOffset = this.m_OffsetEnable.GetEnable(offset, inverse);
  272. }
  273. public override void LateUpdate()
  274. {
  275. base.LateUpdate();
  276. this.m_lastBendPosRot.Copy(this.m_BoneBendGoal.Bone);
  277. this.m_lastRootPosRot.Copy(this.m_RootBoneTgtPair.Bone);
  278. }
  279. private IKCtrlData.BoneTgtPair m_BoneBendGoal;
  280. private IKCtrlData.PosRotPair m_lastBendPosRot = new IKCtrlData.PosRotPair();
  281. private IKCtrlData.ChangeFlagData m_BendweitCtrl = new IKCtrlData.ChangeFlagData();
  282. private IKCtrlData.BoneTgtPair m_RootBoneTgtPair;
  283. private IKCtrlData.PosRotPair m_lastRootPosRot = new IKCtrlData.PosRotPair();
  284. [Header("壁・床補正情報")]
  285. public BipedIKCtrlData.BorderCorrectData WallCorrect = new BipedIKCtrlData.BorderCorrectData(Vector3.forward);
  286. public BipedIKCtrlData.BorderCorrectData FloorCorrect = new BipedIKCtrlData.BorderCorrectData(Vector3.up);
  287. public BipedIKCtrlData.BorderCorrectType CorrectType;
  288. public Transform ToCorrectBone;
  289. [Header("肘・膝をターゲットに合わせるフラグ")]
  290. public bool IsBendIK;
  291. [Header("肩・太ももをターゲットに合わせるフラグ")]
  292. public bool IsRootIK;
  293. [SerializeField]
  294. [Space]
  295. [Range(1f, 90f)]
  296. private float m_BendFadeBorder = 30f;
  297. public readonly IKEffector Effector;
  298. public readonly FBIKChain Chain;
  299. public readonly IKMappingLimb IKMapping;
  300. public readonly IKEffector RootEffector;
  301. public enum BorderCorrectType
  302. {
  303. Bone,
  304. HalfBody,
  305. Chara,
  306. All
  307. }
  308. [Serializable]
  309. public class BorderCorrectData
  310. {
  311. public BorderCorrectData(Vector3 axis)
  312. {
  313. this.Axis = axis;
  314. }
  315. public void Reset()
  316. {
  317. this.Border = 0f;
  318. this.Enable = false;
  319. }
  320. public float GetValue(Vector3 pos)
  321. {
  322. pos = KasaiUtility.Vec3Multiply(pos, this.Axis);
  323. return pos.magnitude * Vector3.Dot(this.Axis, pos.normalized);
  324. }
  325. public bool CheckBorder(Vector3 check_pos, ref float diff)
  326. {
  327. float value = this.GetValue(check_pos);
  328. diff = this.Border - value;
  329. return value < this.Border;
  330. }
  331. public float Border;
  332. public bool Enable;
  333. public Vector3 Axis;
  334. }
  335. }