using System; using System.Collections.Generic; using kt.ik; using RootMotion.FinalIK; using UnityEngine; [Serializable] public class FreeFBIKCtrl : AIKCtrl { public FreeFBIKCtrl(FullBodyIKMgr ik_mgr, FullBodyIKMgr.IKEffectorType effector_type, bool is_upper, FullBodyIKMgr.IKBoneType bend_bone = FullBodyIKMgr.IKBoneType.None) : base(ik_mgr, effector_type) { base.isUpperBody = is_upper; this.Effector = new FreeFBIKEffector(ik_mgr.ik, base.bone, base.constraintTarget, base.isUpperBody); this.FABRIK = base.constraintTarget.parent.gameObject.AddComponent(); this.FABRIK.solver.target = base.constraintTarget; this.FABRIK.fixTransforms = false; this.FABRIK.solver.SetChain(this.ChainBones, this.ChainBones[0]); this.FABRIK.enabled = false; if (bend_bone == FullBodyIKMgr.IKBoneType.None) { return; } Transform[] chainBonesToInit = this.myIKMgr.GetChainBonesToInit(bend_bone); List list = new List(); for (int i = 0; i < chainBonesToInit.Length; i++) { list.Add(new FreeFBIKEffector.BendBone(chainBonesToInit[i], 1f - (float)i / (float)(chainBonesToInit.Length - 1))); } this.Effector.bendBones = list.ToArray(); this.Effector.CCDBones = chainBonesToInit; this.Effector.bodyClampWeight = 1f; this.Effector.boneClampWeight = 0f; this.Effector.CCDWeight = 0f; } public FreeFBIKEffector effector { get { return this.Effector; } } public override bool isNeedFullbodySolverUpdate { get { return base.isIKExec && this.IsPullOn; } } public override void SetIKSetting(IKAttachParam param) { this.Effector.bodyClampWeight = 1f; base.SetIKSetting(param); } public override void SetPullState(bool pull_on) { this.IsPullOn = pull_on; } public override void ApplyIKSetting() { this.BoneRotChache = base.bone.rotation; base.ApplyIKSetting(); } protected override void SetTargetTransform(AIKCtrl.IKSettingData data, Vector3 pos, Quaternion rot) { base.SetTargetTransform(data, pos, rot); if (!this.IsPullOn) { return; } if (!data.isPointAttach) { return; } if (this.myIKMgr.bodyEffector.positionWeight < base.positionWeight) { this.myIKMgr.bodyEffector.positionWeight = base.positionWeight; } if (this.myIKMgr.GetIKCtrl(FullBodyIKMgr.IKEffectorType.UpperArm_L).positionWeight < base.positionWeight) { this.myIKMgr.GetIKCtrl(FullBodyIKMgr.IKEffectorType.UpperArm_L).positionWeight = base.positionWeight; } if (this.myIKMgr.GetIKCtrl(FullBodyIKMgr.IKEffectorType.UpperArm_R).positionWeight < base.positionWeight) { this.myIKMgr.GetIKCtrl(FullBodyIKMgr.IKEffectorType.UpperArm_R).positionWeight = base.positionWeight; } if (this.myIKMgr.GetIKCtrl(FullBodyIKMgr.IKEffectorType.Thigh_L).positionWeight < base.positionWeight) { this.myIKMgr.GetIKCtrl(FullBodyIKMgr.IKEffectorType.Thigh_L).positionWeight = base.positionWeight; } if (this.myIKMgr.GetIKCtrl(FullBodyIKMgr.IKEffectorType.Thigh_R).positionWeight < base.positionWeight) { this.myIKMgr.GetIKCtrl(FullBodyIKMgr.IKEffectorType.Thigh_R).positionWeight = base.positionWeight; } 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) { this.Effector.bodyClampWeight = 0f; } this.effector.OnPreRead(); this.effector.Iterate(); } public override void OnPostFullBodySolverUpdate() { if (base.GetIKSettingData(AIKCtrl.IKAttachType.NewPoint).isIKExec && !base.GetIKSettingData(AIKCtrl.IKAttachType.NewPoint).isBlendNow) { this.FABRIK.solver.Update(); } if (base.GetIKSettingData(AIKCtrl.IKAttachType.Rotate).isIKExec) { base.bone.rotation = Quaternion.Lerp(this.BoneRotChache, base.constraintTarget.rotation, base.rotationWeight); } } public override void Detach() { this.Effector.bodyClampWeight = 1f; base.Detach(); } protected override void OnPostSetPositionWeight(float val) { IKSolver solver = this.FABRIK.solver; this.effector.positionWeight = val; solver.IKPositionWeight = val; } [SerializeField] [Header("最終的に位置を合わせるIKコンポーネント")] protected FABRIK FABRIK; [SerializeField] [Header("エフェクターデータ")] private FreeFBIKEffector Effector; private Quaternion BoneRotChache = Quaternion.identity; private bool IsPullOn = true; }