IKBodyOffsetCtrl.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using System;
  2. using kt.ik;
  3. using kt.Utility;
  4. using UnityEngine;
  5. [Serializable]
  6. public class IKBodyOffsetCtrl : AIKCtrl
  7. {
  8. public IKBodyOffsetCtrl(FullBodyIKMgr ik_mgr) : base(ik_mgr, FullBodyIKMgr.IKEffectorType.Body)
  9. {
  10. this.posBaseBone = base.bone;
  11. this.ChainBones = new Transform[]
  12. {
  13. base.bone
  14. };
  15. this.IsIgnoreLateUpdateEnd = true;
  16. }
  17. public bool isCharaPosSetEnd { get; private set; }
  18. public override void SetIKSetting(IKAttachParam param)
  19. {
  20. BodyOffsetIKParam bodyOffsetIKParam = param as BodyOffsetIKParam;
  21. bodyOffsetIKParam.attachType = AIKCtrl.IKAttachType.NewPoint;
  22. bodyOffsetIKParam.posOffsetType = AIKCtrl.PosOffsetType.OffsetTarget;
  23. this.heightFit = bodyOffsetIKParam.heightFitType;
  24. this.posBaseBone = bodyOffsetIKParam.posBaseBone;
  25. this.AddOffset = bodyOffsetIKParam.addOffset;
  26. this.BlendPosOffset = this.lastPosOffset;
  27. base.SetIKSetting(param);
  28. }
  29. public void CheckBlendType()
  30. {
  31. if (!base.pointIKData.isIKExec)
  32. {
  33. return;
  34. }
  35. if (!base.pointIKData.isSetFirstFramePosRot)
  36. {
  37. if (base.pointIKData.blendType == AIKCtrl.IKBlendType.Not_Blend)
  38. {
  39. base.pointIKData.SetBlendType(AIKCtrl.IKBlendType.IK_To_IK);
  40. }
  41. }
  42. else
  43. {
  44. base.pointIKData.SetBlendType(AIKCtrl.IKBlendType.Not_Blend);
  45. }
  46. }
  47. public override void ApplyIKSetting()
  48. {
  49. this.PosOffset = Vector3.zero;
  50. if (base.isIKExec)
  51. {
  52. Maid targetChara = base.pointIKData.curTargetData.targetChara;
  53. if (targetChara && !targetChara.isOffsetUpdateEnd)
  54. {
  55. targetChara.OffsetUpdate();
  56. }
  57. }
  58. base.ApplyIKSetting();
  59. }
  60. private bool CheckDoIK()
  61. {
  62. if (!base.isIKExec)
  63. {
  64. return false;
  65. }
  66. if (this.heightFit == IKBodyOffsetCtrl.HeightFitType.None)
  67. {
  68. return true;
  69. }
  70. int num = this.myIKMgr.chara.GetProp(MPN.sintyou).value + this.myIKMgr.chara.GetProp(MPN.DouPer).value;
  71. Maid targetChara = base.pointIKData.curTargetData.targetChara;
  72. if (!targetChara)
  73. {
  74. return true;
  75. }
  76. int num2 = targetChara.GetProp(MPN.sintyou).value + targetChara.GetProp(MPN.DouPer).value;
  77. if (this.heightFit == IKBodyOffsetCtrl.HeightFitType.Higher)
  78. {
  79. return num > num2;
  80. }
  81. return num < num2;
  82. }
  83. protected override void SetTargetTransform(AIKCtrl.IKSettingData data, Vector3 pos, Quaternion rot)
  84. {
  85. if (!data.isPointAttach || !this.CheckDoIK())
  86. {
  87. return;
  88. }
  89. Vector3 vector = data.curTargetData.tgtOffset + this.AddOffset;
  90. Transform transform;
  91. if (data.curTargetData.targetChara)
  92. {
  93. transform = data.curTargetData.targetChara.transform;
  94. }
  95. else if (data.curTargetData.lastTgtChara)
  96. {
  97. transform = data.curTargetData.lastTgtChara.transform;
  98. }
  99. else if (data.curTargetData.lastTarget)
  100. {
  101. transform = data.curTargetData.lastTarget;
  102. }
  103. else
  104. {
  105. transform = data.curTargetData.target;
  106. }
  107. Transform transform2 = this.myIKMgr.chara.transform;
  108. if (data.isFirstFrame && !data.isSetFirstFramePosRot)
  109. {
  110. this.FirstBaseBonePos = transform2.InverseTransformPoint(this.posBaseBone.position);
  111. this.FirstLocalTgtPos = transform.InverseTransformPoint(this.FirstTgtPosRot.pos);
  112. }
  113. Vector3 vector2 = (!data.isBlendNow) ? transform2.TransformPoint(this.FirstBaseBonePos) : this.posBaseBone.position;
  114. pos = ((!data.isBlendNow) ? transform.TransformPoint(this.FirstLocalTgtPos) : pos);
  115. DebugUtility.DrawAxis(pos, rot, 0.0625f);
  116. vector = transform.TransformDirection(vector);
  117. base.constraintTarget.position = pos + vector;
  118. Vector3 vector3 = data.offsetEnable.GetEnable(base.constraintTarget.position - vector2, false);
  119. DebugUtility.DrawAxis(vector2, rot, 0.0125f);
  120. Debug.DrawLine(vector2, base.constraintTarget.position, Color.black);
  121. Debug.DrawRay(pos, vector, Color.gray);
  122. vector3 = this.myIKMgr.chara.transform.InverseTransformDirection(vector3);
  123. if (data.blendType == AIKCtrl.IKBlendType.IK_To_IK)
  124. {
  125. this.PosOffset = Vector3.Lerp(this.BlendPosOffset, vector3, data.blendWeight);
  126. }
  127. else if (data.blendType == AIKCtrl.IKBlendType.IK_To_Detach)
  128. {
  129. this.PosOffset = Vector3.Lerp(Vector3.zero, this.BlendPosOffset, base.positionWeight);
  130. }
  131. else
  132. {
  133. this.PosOffset = Vector3.Lerp(Vector3.zero, vector3, base.positionWeight);
  134. }
  135. }
  136. public override void OnPostFullBodySolverUpdate()
  137. {
  138. if (!this.CheckDoIK())
  139. {
  140. return;
  141. }
  142. this.AddPosOffset(this.PosOffset);
  143. }
  144. public void AddPosOffset(Vector3 offset)
  145. {
  146. this.isCharaPosSetEnd = true;
  147. this.myIKMgr.chara.AddPosOffset(offset);
  148. }
  149. protected override void SaveLastTargetPosRot()
  150. {
  151. base.SaveLastTargetPosRot();
  152. this.LastIKPosRot.Copy((!(this.posBaseBone != null)) ? base.bone : this.posBaseBone);
  153. this.lastPosOffset = this.PosOffset;
  154. }
  155. public void CharaPosReset()
  156. {
  157. this.isCharaPosSetEnd = false;
  158. }
  159. public override void Detach()
  160. {
  161. this.BlendPosOffset = this.lastPosOffset;
  162. base.Detach();
  163. this.heightFit = IKBodyOffsetCtrl.HeightFitType.None;
  164. }
  165. [SerializeField]
  166. private Vector3 PosOffset = Vector3.zero;
  167. private Vector3 AddOffset = Vector3.zero;
  168. private Vector3 FirstBaseBonePos;
  169. private Vector3 FirstLocalTgtPos;
  170. private Vector3 lastPosOffset = Vector3.zero;
  171. private Vector3 BlendPosOffset = Vector3.zero;
  172. public Transform posBaseBone;
  173. public IKBodyOffsetCtrl.HeightFitType heightFit = IKBodyOffsetCtrl.HeightFitType.None;
  174. public enum HeightFitType
  175. {
  176. Higher,
  177. Lower,
  178. None
  179. }
  180. }