FreeFBIKCtrl.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections.Generic;
  3. using kt.ik;
  4. using RootMotion.FinalIK;
  5. using UnityEngine;
  6. [Serializable]
  7. public class FreeFBIKCtrl : AIKCtrl
  8. {
  9. public FreeFBIKCtrl(FullBodyIKMgr ik_mgr, FullBodyIKMgr.IKEffectorType effector_type, bool is_upper, FullBodyIKMgr.IKBoneType bend_bone = FullBodyIKMgr.IKBoneType.None) : base(ik_mgr, effector_type)
  10. {
  11. base.isUpperBody = is_upper;
  12. this.Effector = new FreeFBIKEffector(ik_mgr.ik, base.bone, base.constraintTarget, base.isUpperBody);
  13. this.FABRIK = base.constraintTarget.parent.gameObject.AddComponent<FABRIK>();
  14. this.FABRIK.solver.target = base.constraintTarget;
  15. this.FABRIK.fixTransforms = false;
  16. this.FABRIK.solver.SetChain(this.ChainBones, this.ChainBones[0]);
  17. this.FABRIK.enabled = false;
  18. if (bend_bone == FullBodyIKMgr.IKBoneType.None)
  19. {
  20. return;
  21. }
  22. Transform[] chainBonesToInit = this.myIKMgr.GetChainBonesToInit(bend_bone);
  23. List<FreeFBIKEffector.BendBone> list = new List<FreeFBIKEffector.BendBone>();
  24. for (int i = 0; i < chainBonesToInit.Length; i++)
  25. {
  26. list.Add(new FreeFBIKEffector.BendBone(chainBonesToInit[i], 1f - (float)i / (float)(chainBonesToInit.Length - 1)));
  27. }
  28. this.Effector.bendBones = list.ToArray();
  29. this.Effector.CCDBones = chainBonesToInit;
  30. this.Effector.bodyClampWeight = 1f;
  31. this.Effector.boneClampWeight = 0f;
  32. this.Effector.CCDWeight = 0f;
  33. }
  34. public FreeFBIKEffector effector
  35. {
  36. get
  37. {
  38. return this.Effector;
  39. }
  40. }
  41. public override bool isNeedFullbodySolverUpdate
  42. {
  43. get
  44. {
  45. return base.isIKExec && this.IsPullOn;
  46. }
  47. }
  48. public override void SetIKSetting(IKAttachParam param)
  49. {
  50. this.Effector.bodyClampWeight = 1f;
  51. base.SetIKSetting(param);
  52. }
  53. public override void SetPullState(bool pull_on)
  54. {
  55. this.IsPullOn = pull_on;
  56. }
  57. public override void ApplyIKSetting()
  58. {
  59. this.BoneRotChache = base.bone.rotation;
  60. base.ApplyIKSetting();
  61. }
  62. protected override void SetTargetTransform(AIKCtrl.IKSettingData data, Vector3 pos, Quaternion rot)
  63. {
  64. base.SetTargetTransform(data, pos, rot);
  65. if (!this.IsPullOn)
  66. {
  67. return;
  68. }
  69. if (!data.isPointAttach)
  70. {
  71. return;
  72. }
  73. if (this.myIKMgr.bodyEffector.positionWeight < base.positionWeight)
  74. {
  75. this.myIKMgr.bodyEffector.positionWeight = base.positionWeight;
  76. }
  77. if (this.myIKMgr.GetIKCtrl(FullBodyIKMgr.IKEffectorType.UpperArm_L).positionWeight < base.positionWeight)
  78. {
  79. this.myIKMgr.GetIKCtrl(FullBodyIKMgr.IKEffectorType.UpperArm_L).positionWeight = base.positionWeight;
  80. }
  81. if (this.myIKMgr.GetIKCtrl(FullBodyIKMgr.IKEffectorType.UpperArm_R).positionWeight < base.positionWeight)
  82. {
  83. this.myIKMgr.GetIKCtrl(FullBodyIKMgr.IKEffectorType.UpperArm_R).positionWeight = base.positionWeight;
  84. }
  85. if (this.myIKMgr.GetIKCtrl(FullBodyIKMgr.IKEffectorType.Thigh_L).positionWeight < base.positionWeight)
  86. {
  87. this.myIKMgr.GetIKCtrl(FullBodyIKMgr.IKEffectorType.Thigh_L).positionWeight = base.positionWeight;
  88. }
  89. if (this.myIKMgr.GetIKCtrl(FullBodyIKMgr.IKEffectorType.Thigh_R).positionWeight < base.positionWeight)
  90. {
  91. this.myIKMgr.GetIKCtrl(FullBodyIKMgr.IKEffectorType.Thigh_R).positionWeight = base.positionWeight;
  92. }
  93. if (this.myIKMgr.GetIKCtrl(FullBodyIKMgr.IKEffectorType.Hand_L).isIKExec || this.myIKMgr.GetIKCtrl(FullBodyIKMgr.IKEffectorType.Hand_R).isIKExec || this.myIKMgr.GetIKCtrl(FullBodyIKMgr.IKEffectorType.Foot_L).isIKExec || this.myIKMgr.GetIKCtrl(FullBodyIKMgr.IKEffectorType.Foot_R).isIKExec)
  94. {
  95. this.Effector.bodyClampWeight = 0f;
  96. }
  97. this.effector.OnPreRead();
  98. this.effector.Iterate();
  99. }
  100. public override void OnPostFullBodySolverUpdate()
  101. {
  102. if (base.GetIKSettingData(AIKCtrl.IKAttachType.NewPoint).isIKExec && !base.GetIKSettingData(AIKCtrl.IKAttachType.NewPoint).isBlendNow)
  103. {
  104. this.FABRIK.solver.Update();
  105. }
  106. if (base.GetIKSettingData(AIKCtrl.IKAttachType.Rotate).isIKExec)
  107. {
  108. base.bone.rotation = Quaternion.Lerp(this.BoneRotChache, base.constraintTarget.rotation, base.rotationWeight);
  109. }
  110. }
  111. public override void Detach()
  112. {
  113. this.Effector.bodyClampWeight = 1f;
  114. base.Detach();
  115. }
  116. protected override void OnPostSetPositionWeight(float val)
  117. {
  118. IKSolver solver = this.FABRIK.solver;
  119. this.effector.positionWeight = val;
  120. solver.IKPositionWeight = val;
  121. }
  122. [SerializeField]
  123. [Header("最終的に位置を合わせるIKコンポーネント")]
  124. protected FABRIK FABRIK;
  125. [SerializeField]
  126. [Header("エフェクターデータ")]
  127. private FreeFBIKEffector Effector;
  128. private Quaternion BoneRotChache = Quaternion.identity;
  129. private bool IsPullOn = true;
  130. }