123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396 |
- using System;
- using RootMotion.FinalIK;
- using UnityEngine;
- public class LimbControl : MonoBehaviour
- {
- public void Initialize(IKManager mgr, LimbControl.Type type)
- {
- this.ik_mgr_ = mgr;
- this.type_ = type;
- Maid maid = this.ik_mgr_.maid;
- Transform root_trans = maid.body0.GetBone("Bip01").transform.root;
- IKManager.BoneType[] array = null;
- if (type == LimbControl.Type.Arm_R)
- {
- array = new IKManager.BoneType[]
- {
- IKManager.BoneType.UpperArm_R,
- IKManager.BoneType.Forearm_R,
- IKManager.BoneType.Hand_R
- };
- }
- else if (type == LimbControl.Type.Foot_R)
- {
- array = new IKManager.BoneType[]
- {
- IKManager.BoneType.Thigh_R,
- IKManager.BoneType.Calf_R,
- IKManager.BoneType.Foot_R
- };
- }
- else if (type == LimbControl.Type.Finger0_R)
- {
- array = new IKManager.BoneType[]
- {
- IKManager.BoneType.Finger0_Root_R,
- IKManager.BoneType.Finger0_0_R,
- IKManager.BoneType.Finger0_1_R
- };
- }
- else if (type == LimbControl.Type.Finger1_R)
- {
- array = new IKManager.BoneType[]
- {
- IKManager.BoneType.Finger1_Root_R,
- IKManager.BoneType.Finger1_0_R,
- IKManager.BoneType.Finger1_1_R
- };
- }
- else if (type == LimbControl.Type.Finger2_R)
- {
- array = new IKManager.BoneType[]
- {
- IKManager.BoneType.Finger2_Root_R,
- IKManager.BoneType.Finger2_0_R,
- IKManager.BoneType.Finger2_1_R
- };
- }
- else if (type == LimbControl.Type.Finger3_R)
- {
- array = new IKManager.BoneType[]
- {
- IKManager.BoneType.Finger3_Root_R,
- IKManager.BoneType.Finger3_0_R,
- IKManager.BoneType.Finger3_1_R
- };
- }
- else if (type == LimbControl.Type.Finger4_R)
- {
- array = new IKManager.BoneType[]
- {
- IKManager.BoneType.Finger4_Root_R,
- IKManager.BoneType.Finger4_0_R,
- IKManager.BoneType.Finger4_1_R
- };
- }
- else if (type == LimbControl.Type.Toe0_R)
- {
- array = new IKManager.BoneType[]
- {
- IKManager.BoneType.Toe0_Root_R,
- IKManager.BoneType.Root,
- IKManager.BoneType.Toe0_0_R
- };
- }
- else if (type == LimbControl.Type.Toe1_R)
- {
- array = new IKManager.BoneType[]
- {
- IKManager.BoneType.Toe1_Root_R,
- IKManager.BoneType.Root,
- IKManager.BoneType.Toe1_0_R
- };
- }
- else if (type == LimbControl.Type.Toe2_R)
- {
- array = new IKManager.BoneType[]
- {
- IKManager.BoneType.Toe2_Root_R,
- IKManager.BoneType.Root,
- IKManager.BoneType.Toe2_0_R
- };
- }
- else if (type == LimbControl.Type.Arm_L)
- {
- array = new IKManager.BoneType[]
- {
- IKManager.BoneType.UpperArm_L,
- IKManager.BoneType.Forearm_L,
- IKManager.BoneType.Hand_L
- };
- }
- else if (type == LimbControl.Type.Foot_L)
- {
- array = new IKManager.BoneType[]
- {
- IKManager.BoneType.Thigh_L,
- IKManager.BoneType.Calf_L,
- IKManager.BoneType.Foot_L
- };
- }
- else if (type == LimbControl.Type.Finger0_L)
- {
- array = new IKManager.BoneType[]
- {
- IKManager.BoneType.Finger0_Root_L,
- IKManager.BoneType.Finger0_0_L,
- IKManager.BoneType.Finger0_1_L
- };
- }
- else if (type == LimbControl.Type.Finger1_L)
- {
- array = new IKManager.BoneType[]
- {
- IKManager.BoneType.Finger1_Root_L,
- IKManager.BoneType.Finger1_0_L,
- IKManager.BoneType.Finger1_1_L
- };
- }
- else if (type == LimbControl.Type.Finger2_L)
- {
- array = new IKManager.BoneType[]
- {
- IKManager.BoneType.Finger2_Root_L,
- IKManager.BoneType.Finger2_0_L,
- IKManager.BoneType.Finger2_1_L
- };
- }
- else if (type == LimbControl.Type.Finger3_L)
- {
- array = new IKManager.BoneType[]
- {
- IKManager.BoneType.Finger3_Root_L,
- IKManager.BoneType.Finger3_0_L,
- IKManager.BoneType.Finger3_1_L
- };
- }
- else if (type == LimbControl.Type.Finger4_L)
- {
- array = new IKManager.BoneType[]
- {
- IKManager.BoneType.Finger4_Root_L,
- IKManager.BoneType.Finger4_0_L,
- IKManager.BoneType.Finger4_1_L
- };
- }
- else if (type == LimbControl.Type.Toe0_L)
- {
- array = new IKManager.BoneType[]
- {
- IKManager.BoneType.Toe0_Root_L,
- IKManager.BoneType.Root,
- IKManager.BoneType.Toe0_0_L
- };
- }
- else if (type == LimbControl.Type.Toe1_L)
- {
- array = new IKManager.BoneType[]
- {
- IKManager.BoneType.Toe1_Root_L,
- IKManager.BoneType.Root,
- IKManager.BoneType.Toe1_0_L
- };
- }
- else if (type == LimbControl.Type.Toe2_L)
- {
- array = new IKManager.BoneType[]
- {
- IKManager.BoneType.Toe2_Root_L,
- IKManager.BoneType.Root,
- IKManager.BoneType.Toe2_0_L
- };
- }
- this.bone_start_point_ = this.ik_mgr_.GetBone(array[0]).transform;
- this.bone_joint_point_ = ((array[1] == IKManager.BoneType.Root) ? null : this.ik_mgr_.GetBone(array[1]).transform);
- this.bone_end_point_ = this.ik_mgr_.GetBone(array[2]).transform;
- this.ik_fabrik_ = this.bone_start_point_.gameObject.AddComponent<FABRIK>();
- this.ik_fabrik_.fixTransforms = false;
- IKSolverFABRIK solver = this.ik_fabrik_.solver;
- solver.maxIterations = 1;
- solver.Initiate(root_trans);
- if (this.bone_joint_point_ != null)
- {
- this.joint_drag_point_ = IKManager.CreateIKDragPoint(this.bone_joint_point_.gameObject, maid);
- if (0 <= this.bone_joint_point_.gameObject.name.IndexOf("Finger"))
- {
- if (GameMain.Instance.VRMode)
- {
- this.joint_drag_point_.axis_obj.AllAxisObject.transform.localScale = new Vector3(0.01f, 0.01f, 0.01f);
- }
- else
- {
- this.joint_drag_point_.axis_obj.transform.localScale = new Vector3(0.03f, 0.03f, 0.03f);
- }
- }
- this.joint_drag_point_.drag_start_event = delegate
- {
- this.ik_fabrik_.solver.SetChain(new Transform[0], root_trans);
- this.ik_fabrik_.solver.SetChain(new Transform[]
- {
- this.bone_start_point_,
- this.bone_joint_point_
- }, root_trans);
- this.ik_fabrik_.solver.target = this.joint_drag_point_.transform;
- };
- this.joint_drag_point_.drag_end_event = delegate
- {
- this.ik_fabrik_.solver.SetChain(new Transform[0], root_trans);
- this.ik_mgr_.HistoryPush();
- };
- }
- this.tip_drag_point_ = IKManager.CreateIKDragPoint(this.bone_end_point_.gameObject, maid);
- if (0 <= this.bone_end_point_.gameObject.name.IndexOf("Finger") || 0 <= this.bone_end_point_.gameObject.name.IndexOf("Toe"))
- {
- if (GameMain.Instance.VRMode)
- {
- this.tip_drag_point_.axis_obj.AllAxisObject.transform.localScale = new Vector3(0.01f, 0.01f, 0.01f);
- }
- else
- {
- this.tip_drag_point_.axis_obj.transform.localScale = new Vector3(0.03f, 0.03f, 0.03f);
- }
- }
- this.tip_drag_point_.drag_start_event = delegate
- {
- if (this.bone_joint_point_ != null)
- {
- this.ik_fabrik_.solver.SetChain(new Transform[0], root_trans);
- if (this.joint_lock)
- {
- this.ik_fabrik_.solver.SetChain(new Transform[]
- {
- this.bone_joint_point_,
- this.bone_end_point_
- }, root_trans);
- }
- else
- {
- this.ik_fabrik_.solver.SetChain(new Transform[]
- {
- this.bone_start_point_,
- this.bone_joint_point_,
- this.bone_end_point_
- }, root_trans);
- }
- }
- else
- {
- this.ik_fabrik_.solver.SetChain(new Transform[0], root_trans);
- this.ik_fabrik_.solver.SetChain(new Transform[]
- {
- this.bone_start_point_,
- this.bone_end_point_
- }, root_trans);
- }
- this.ik_fabrik_.solver.target = this.tip_drag_point_.transform;
- };
- this.tip_drag_point_.drag_end_event = delegate
- {
- this.ik_fabrik_.solver.SetChain(new Transform[0], root_trans);
- this.ik_mgr_.HistoryPush();
- };
- }
- public void OnDestroy()
- {
- if (this.joint_drag_point_ != null)
- {
- UnityEngine.Object.Destroy(this.joint_drag_point_.gameObject);
- }
- if (this.tip_drag_point_ != null)
- {
- UnityEngine.Object.Destroy(this.tip_drag_point_.gameObject);
- }
- this.joint_drag_point_ = null;
- this.tip_drag_point_ = null;
- }
- private void Update()
- {
- this.joint_lock = (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl));
- }
- public bool visible
- {
- get
- {
- return this.visible_joint_point || this.visible_tip_point;
- }
- set
- {
- this.visible_tip_point = value;
- this.visible_joint_point = value;
- }
- }
- private bool visible_joint_point
- {
- get
- {
- return this.joint_drag_point_ != null && this.joint_drag_point_.visible;
- }
- set
- {
- if (this.joint_drag_point_ != null)
- {
- this.joint_drag_point_.visible = value;
- }
- }
- }
- private bool visible_tip_point
- {
- get
- {
- return this.tip_drag_point_ != null && this.tip_drag_point_.visible;
- }
- set
- {
- if (this.tip_drag_point_ != null)
- {
- this.tip_drag_point_.visible = value;
- }
- }
- }
- public LimbControl.Type type
- {
- get
- {
- return this.type_;
- }
- }
- public bool joint_lock;
- private IKManager ik_mgr_;
- private Transform bone_start_point_;
- private Transform bone_joint_point_;
- private Transform bone_end_point_;
- private IKDragPoint joint_drag_point_;
- private IKDragPoint tip_drag_point_;
- private LimbControl.Type type_;
- private FABRIK ik_fabrik_;
- public enum Type
- {
- Arm_R,
- Foot_R,
- Finger0_R,
- Finger1_R,
- Finger2_R,
- Finger3_R,
- Finger4_R,
- Toe0_R,
- Toe1_R,
- Toe2_R,
- Arm_L,
- Foot_L,
- Finger0_L,
- Finger1_L,
- Finger2_L,
- Finger3_L,
- Finger4_L,
- Toe0_L,
- Toe1_L,
- Toe2_L
- }
- }
|