BipedIKCtrlData.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 void SetPullState(bool pull_on)
  238. {
  239. if (this.m_OrijinPull < 0f)
  240. {
  241. this.m_OrijinPull = this.Chain.pull;
  242. }
  243. if (pull_on)
  244. {
  245. this.Chain.pull = this.m_OrijinPull;
  246. }
  247. else
  248. {
  249. this.Chain.pull = 0.1f;
  250. }
  251. }
  252. 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)
  253. {
  254. base.SetIKSetting(attachType, is_next, tgt_maid, slot_no, attach_name, axis_bone, target, f_vecOffset, do_animation);
  255. if (this.IsRootIK)
  256. {
  257. base.BlendPosRot.Copy(this.m_lastRootPosRot);
  258. }
  259. else if (this.IsBendIK)
  260. {
  261. base.BlendPosRot.Copy(this.m_lastBendPosRot);
  262. }
  263. }
  264. public override void Detach(IKCtrlData.IKAttachType attachType)
  265. {
  266. base.Detach(attachType);
  267. this.IsRootIK = (this.IsBendIK = false);
  268. this.RootEffector.positionWeight = 0f;
  269. this.RootEffector.rotationWeight = 0f;
  270. this.m_BoneBendGoal.PosOffset = Vector3.zero;
  271. this.m_RootBoneTgtPair.PosOffset = Vector3.zero;
  272. this.WallCorrect.Reset();
  273. this.FloorCorrect.Reset();
  274. this.ToCorrectBone = this.TargetBone;
  275. this.CorrectType = BipedIKCtrlData.BorderCorrectType.Bone;
  276. this.m_BendweitCtrl.Reset();
  277. }
  278. public override void SetTargetOffset(Vector3 offset, bool inverse = false)
  279. {
  280. if (base.IsIKExec)
  281. {
  282. return;
  283. }
  284. base.SetTargetOffset(offset, inverse);
  285. this.m_BoneBendGoal.PosOffset = this.m_OffsetEnable.GetEnable(offset, inverse);
  286. this.m_RootBoneTgtPair.PosOffset = this.m_OffsetEnable.GetEnable(offset, inverse);
  287. }
  288. public override void LateUpdate()
  289. {
  290. base.LateUpdate();
  291. this.m_lastBendPosRot.Copy(this.m_BoneBendGoal.Bone);
  292. this.m_lastRootPosRot.Copy(this.m_RootBoneTgtPair.Bone);
  293. }
  294. private const float PULL_MIN_VALUE = 0.1f;
  295. private IKCtrlData.BoneTgtPair m_BoneBendGoal;
  296. private IKCtrlData.PosRotPair m_lastBendPosRot = new IKCtrlData.PosRotPair();
  297. private IKCtrlData.ChangeFlagData m_BendweitCtrl = new IKCtrlData.ChangeFlagData();
  298. private IKCtrlData.BoneTgtPair m_RootBoneTgtPair;
  299. private IKCtrlData.PosRotPair m_lastRootPosRot = new IKCtrlData.PosRotPair();
  300. private float m_OrijinPull = -1f;
  301. [Header("壁・床補正情報")]
  302. public BipedIKCtrlData.BorderCorrectData WallCorrect = new BipedIKCtrlData.BorderCorrectData(Vector3.forward);
  303. public BipedIKCtrlData.BorderCorrectData FloorCorrect = new BipedIKCtrlData.BorderCorrectData(Vector3.up);
  304. public BipedIKCtrlData.BorderCorrectType CorrectType;
  305. public Transform ToCorrectBone;
  306. [Header("肘・膝をターゲットに合わせるフラグ")]
  307. public bool IsBendIK;
  308. [Header("肩・太ももをターゲットに合わせるフラグ")]
  309. public bool IsRootIK;
  310. [SerializeField]
  311. [Space]
  312. [Range(1f, 90f)]
  313. private float m_BendFadeBorder = 30f;
  314. public readonly IKEffector Effector;
  315. public readonly FBIKChain Chain;
  316. public readonly IKMappingLimb IKMapping;
  317. public readonly IKEffector RootEffector;
  318. public enum BorderCorrectType
  319. {
  320. Bone,
  321. HalfBody,
  322. Chara,
  323. All
  324. }
  325. [Serializable]
  326. public class BorderCorrectData
  327. {
  328. public BorderCorrectData(Vector3 axis)
  329. {
  330. this.Axis = axis;
  331. }
  332. public void Reset()
  333. {
  334. this.Border = 0f;
  335. this.Enable = false;
  336. }
  337. public float GetValue(Vector3 pos)
  338. {
  339. pos = KasaiUtility.Vec3Multiply(pos, this.Axis);
  340. return pos.magnitude * Vector3.Dot(this.Axis, pos.normalized);
  341. }
  342. public bool CheckBorder(Vector3 check_pos, ref float diff)
  343. {
  344. float value = this.GetValue(check_pos);
  345. diff = this.Border - value;
  346. return value < this.Border;
  347. }
  348. public float Border;
  349. public bool Enable;
  350. public Vector3 Axis;
  351. }
  352. }