BipedIKCtrlData.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. break;
  117. case BipedIKCtrlData.BorderCorrectType.HalfBody:
  118. base.IKTarget.position += correctData.Axis * num;
  119. this.m_BoneBendGoal.Target.position += correctData.Axis * num;
  120. this.m_RootBoneTgtPair.Target.position += correctData.Axis * num;
  121. break;
  122. case BipedIKCtrlData.BorderCorrectType.Chara:
  123. if (correctData.GetValue(this.MyIKCtrl.BodyCtrlData.AddOffset) < num)
  124. {
  125. this.MyIKCtrl.BodyCtrlData.SetAddOffset(correctData.Axis * num);
  126. }
  127. break;
  128. case BipedIKCtrlData.BorderCorrectType.All:
  129. if (correctData.GetValue(this.MyIKCtrl.BodyCtrlData.AllOffset) < num)
  130. {
  131. this.MyIKCtrl.BodyCtrlData.SetAllOffset(correctData.Axis * num);
  132. }
  133. break;
  134. }
  135. }
  136. }
  137. protected override void SetTargetTransform(IKCtrlData.IKParam data, Vector3 pos, Quaternion rot)
  138. {
  139. base.SetTargetTransform(data, pos, rot);
  140. Transform target = this.m_BoneBendGoal.Target;
  141. Transform target2 = this.m_RootBoneTgtPair.Target;
  142. Vector3 position = target2.InverseTransformPoint(target.position);
  143. Vector3 position2 = this.m_BoneBendGoal.Bone.InverseTransformPoint(base.TargetBone.position);
  144. if (this.IsRootIK)
  145. {
  146. if (data.IsPointAttach)
  147. {
  148. this.RootEffector.positionWeight = this.PositionWeight;
  149. target2.position = base.IKTarget.position;
  150. }
  151. else
  152. {
  153. this.RootEffector.rotationWeight = this.RotationWeight;
  154. target2.rotation = base.IKTarget.rotation;
  155. base.IKTarget.rotation = base.TargetBone.rotation;
  156. }
  157. target.position = target2.TransformPoint(position);
  158. base.IKTarget.position = target.TransformPoint(position2);
  159. }
  160. else if (this.IsBendIK)
  161. {
  162. if (data.IsPointAttach)
  163. {
  164. this.Chain.bendConstraint.weight = this.PositionWeight;
  165. this.RootEffector.positionWeight = (this.RootEffector.rotationWeight = this.PositionWeight);
  166. target2.rotation = Quaternion.FromToRotation(target.position - target2.position, base.IKTarget.position - target2.position) * target2.rotation;
  167. Vector3 b = base.IKTarget.position - target2.TransformPoint(position);
  168. target2.position += b;
  169. target.position = base.IKTarget.position;
  170. }
  171. else
  172. {
  173. this.m_BoneBendGoal.Target.rotation = base.IKTarget.rotation;
  174. base.IKTarget.rotation = base.TargetBone.rotation;
  175. }
  176. base.IKTarget.position = this.m_BoneBendGoal.Target.TransformPoint(position2);
  177. }
  178. }
  179. public void SetBendweightCtrl(string name)
  180. {
  181. if (this.MyIKCtrl.TgtMaid.boMAN)
  182. {
  183. this.BendweightCtrl.Target = this.MyIKCtrl.TgtBody.GetBone(name);
  184. }
  185. else
  186. {
  187. this.BendweightCtrl.Target = this.MyIKCtrl.GetSTFlagObj(name);
  188. }
  189. }
  190. public override void ApplyIKSetting()
  191. {
  192. this.m_BoneBendGoal.Cppy();
  193. this.m_RootBoneTgtPair.Cppy();
  194. if (!this.IsBendIK && !this.IsRootIK && this.m_BendweitCtrl.IsEnable)
  195. {
  196. this.Chain.bendConstraint.weight = 1f - this.m_BendweitCtrl.Value01();
  197. }
  198. base.ApplyIKSetting();
  199. if (base.ForceIK)
  200. {
  201. this.CheckBorder(this.WallCorrect);
  202. this.CheckBorder(this.FloorCorrect);
  203. }
  204. }
  205. 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, float blend_time = 0f)
  206. {
  207. base.SetIKSetting(attachType, is_next, tgt_maid, slot_no, attach_name, axis_bone, target, f_vecOffset, do_animation, blend_time);
  208. if (this.IsRootIK)
  209. {
  210. base.BlendPosRot.Copy(this.m_lastRootPosRot);
  211. }
  212. else if (this.IsBendIK)
  213. {
  214. base.BlendPosRot.Copy(this.m_lastBendPosRot);
  215. }
  216. }
  217. public override void Detach(IKCtrlData.IKAttachType attachType, float blend_time = 0f)
  218. {
  219. base.Detach(attachType, blend_time);
  220. this.IsRootIK = (this.IsBendIK = false);
  221. this.Chain.bendConstraint.weight = 1f;
  222. this.RootEffector.positionWeight = 0f;
  223. this.RootEffector.rotationWeight = 0f;
  224. this.m_BoneBendGoal.PosOffset = Vector3.zero;
  225. this.m_RootBoneTgtPair.PosOffset = Vector3.zero;
  226. this.WallCorrect.Reset();
  227. this.FloorCorrect.Reset();
  228. this.ToCorrectBone = this.TargetBone;
  229. this.CorrectType = BipedIKCtrlData.BorderCorrectType.Bone;
  230. this.m_BendweitCtrl.Reset();
  231. }
  232. public override void SetTargetOffset(Vector3 offset, bool inverse = false)
  233. {
  234. if (base.IsIKExec)
  235. {
  236. return;
  237. }
  238. base.SetTargetOffset(offset, inverse);
  239. this.m_BoneBendGoal.PosOffset = this.m_OffsetEnable.GetEnable(offset, inverse);
  240. this.m_RootBoneTgtPair.PosOffset = this.m_OffsetEnable.GetEnable(offset, inverse);
  241. }
  242. public override void LateUpdate()
  243. {
  244. base.LateUpdate();
  245. this.m_lastBendPosRot.Copy(this.m_BoneBendGoal.Bone);
  246. this.m_lastRootPosRot.Copy(this.m_RootBoneTgtPair.Bone);
  247. }
  248. private IKCtrlData.BoneTgtPair m_BoneBendGoal;
  249. private IKCtrlData.PosRotPair m_lastBendPosRot = new IKCtrlData.PosRotPair();
  250. private IKCtrlData.ChangeFlagData m_BendweitCtrl = new IKCtrlData.ChangeFlagData();
  251. private IKCtrlData.BoneTgtPair m_RootBoneTgtPair;
  252. private IKCtrlData.PosRotPair m_lastRootPosRot = new IKCtrlData.PosRotPair();
  253. [Header("壁・床補正情報")]
  254. public BipedIKCtrlData.BorderCorrectData WallCorrect = new BipedIKCtrlData.BorderCorrectData(Vector3.forward);
  255. public BipedIKCtrlData.BorderCorrectData FloorCorrect = new BipedIKCtrlData.BorderCorrectData(Vector3.up);
  256. public BipedIKCtrlData.BorderCorrectType CorrectType;
  257. public Transform ToCorrectBone;
  258. [Header("肘・膝をターゲットに合わせるフラグ")]
  259. public bool IsBendIK;
  260. [Header("肘・膝をターゲットに合わせるフラグ")]
  261. public bool IsRootIK;
  262. public readonly IKEffector Effector;
  263. public readonly FBIKChain Chain;
  264. public readonly IKMappingLimb IKMapping;
  265. public readonly IKEffector RootEffector;
  266. public enum BorderCorrectType
  267. {
  268. Bone,
  269. HalfBody,
  270. Chara,
  271. All
  272. }
  273. [Serializable]
  274. public class BorderCorrectData
  275. {
  276. public BorderCorrectData(Vector3 axis)
  277. {
  278. this.Axis = axis;
  279. }
  280. public void Reset()
  281. {
  282. this.Border = 0f;
  283. this.Enable = false;
  284. }
  285. public float GetValue(Vector3 pos)
  286. {
  287. pos = KasaiUtility.Vec3Multiply(pos, this.Axis);
  288. return pos.magnitude * Vector3.Dot(this.Axis, pos.normalized);
  289. }
  290. public bool CheckBorder(Vector3 check_pos, ref float diff)
  291. {
  292. float value = this.GetValue(check_pos);
  293. diff = this.Border - value;
  294. return value < this.Border;
  295. }
  296. public float Border;
  297. public bool Enable;
  298. public Vector3 Axis;
  299. }
  300. }