LimbControl.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. using System;
  2. using RootMotion.FinalIK;
  3. using UnityEngine;
  4. public class LimbControl : MonoBehaviour
  5. {
  6. public void Initialize(IKManager mgr, LimbControl.Type type)
  7. {
  8. this.ik_mgr_ = mgr;
  9. this.type_ = type;
  10. Maid maid = this.ik_mgr_.maid;
  11. Transform root_trans = maid.body0.GetBone("Bip01").transform.root;
  12. IKManager.BoneType[] array = null;
  13. if (type == LimbControl.Type.Arm_R)
  14. {
  15. array = new IKManager.BoneType[]
  16. {
  17. IKManager.BoneType.UpperArm_R,
  18. IKManager.BoneType.Forearm_R,
  19. IKManager.BoneType.Hand_R
  20. };
  21. }
  22. else if (type == LimbControl.Type.Foot_R)
  23. {
  24. array = new IKManager.BoneType[]
  25. {
  26. IKManager.BoneType.Thigh_R,
  27. IKManager.BoneType.Calf_R,
  28. IKManager.BoneType.Foot_R
  29. };
  30. }
  31. else if (type == LimbControl.Type.Finger0_R)
  32. {
  33. array = new IKManager.BoneType[]
  34. {
  35. IKManager.BoneType.Finger0_Root_R,
  36. IKManager.BoneType.Finger0_0_R,
  37. IKManager.BoneType.Finger0_1_R
  38. };
  39. }
  40. else if (type == LimbControl.Type.Finger1_R)
  41. {
  42. array = new IKManager.BoneType[]
  43. {
  44. IKManager.BoneType.Finger1_Root_R,
  45. IKManager.BoneType.Finger1_0_R,
  46. IKManager.BoneType.Finger1_1_R
  47. };
  48. }
  49. else if (type == LimbControl.Type.Finger2_R)
  50. {
  51. array = new IKManager.BoneType[]
  52. {
  53. IKManager.BoneType.Finger2_Root_R,
  54. IKManager.BoneType.Finger2_0_R,
  55. IKManager.BoneType.Finger2_1_R
  56. };
  57. }
  58. else if (type == LimbControl.Type.Finger3_R)
  59. {
  60. array = new IKManager.BoneType[]
  61. {
  62. IKManager.BoneType.Finger3_Root_R,
  63. IKManager.BoneType.Finger3_0_R,
  64. IKManager.BoneType.Finger3_1_R
  65. };
  66. }
  67. else if (type == LimbControl.Type.Finger4_R)
  68. {
  69. array = new IKManager.BoneType[]
  70. {
  71. IKManager.BoneType.Finger4_Root_R,
  72. IKManager.BoneType.Finger4_0_R,
  73. IKManager.BoneType.Finger4_1_R
  74. };
  75. }
  76. else if (type == LimbControl.Type.Toe0_R)
  77. {
  78. array = new IKManager.BoneType[]
  79. {
  80. IKManager.BoneType.Toe0_Root_R,
  81. IKManager.BoneType.Root,
  82. IKManager.BoneType.Toe0_0_R
  83. };
  84. }
  85. else if (type == LimbControl.Type.Toe1_R)
  86. {
  87. array = new IKManager.BoneType[]
  88. {
  89. IKManager.BoneType.Toe1_Root_R,
  90. IKManager.BoneType.Root,
  91. IKManager.BoneType.Toe1_0_R
  92. };
  93. }
  94. else if (type == LimbControl.Type.Toe2_R)
  95. {
  96. array = new IKManager.BoneType[]
  97. {
  98. IKManager.BoneType.Toe2_Root_R,
  99. IKManager.BoneType.Root,
  100. IKManager.BoneType.Toe2_0_R
  101. };
  102. }
  103. else if (type == LimbControl.Type.Arm_L)
  104. {
  105. array = new IKManager.BoneType[]
  106. {
  107. IKManager.BoneType.UpperArm_L,
  108. IKManager.BoneType.Forearm_L,
  109. IKManager.BoneType.Hand_L
  110. };
  111. }
  112. else if (type == LimbControl.Type.Foot_L)
  113. {
  114. array = new IKManager.BoneType[]
  115. {
  116. IKManager.BoneType.Thigh_L,
  117. IKManager.BoneType.Calf_L,
  118. IKManager.BoneType.Foot_L
  119. };
  120. }
  121. else if (type == LimbControl.Type.Finger0_L)
  122. {
  123. array = new IKManager.BoneType[]
  124. {
  125. IKManager.BoneType.Finger0_Root_L,
  126. IKManager.BoneType.Finger0_0_L,
  127. IKManager.BoneType.Finger0_1_L
  128. };
  129. }
  130. else if (type == LimbControl.Type.Finger1_L)
  131. {
  132. array = new IKManager.BoneType[]
  133. {
  134. IKManager.BoneType.Finger1_Root_L,
  135. IKManager.BoneType.Finger1_0_L,
  136. IKManager.BoneType.Finger1_1_L
  137. };
  138. }
  139. else if (type == LimbControl.Type.Finger2_L)
  140. {
  141. array = new IKManager.BoneType[]
  142. {
  143. IKManager.BoneType.Finger2_Root_L,
  144. IKManager.BoneType.Finger2_0_L,
  145. IKManager.BoneType.Finger2_1_L
  146. };
  147. }
  148. else if (type == LimbControl.Type.Finger3_L)
  149. {
  150. array = new IKManager.BoneType[]
  151. {
  152. IKManager.BoneType.Finger3_Root_L,
  153. IKManager.BoneType.Finger3_0_L,
  154. IKManager.BoneType.Finger3_1_L
  155. };
  156. }
  157. else if (type == LimbControl.Type.Finger4_L)
  158. {
  159. array = new IKManager.BoneType[]
  160. {
  161. IKManager.BoneType.Finger4_Root_L,
  162. IKManager.BoneType.Finger4_0_L,
  163. IKManager.BoneType.Finger4_1_L
  164. };
  165. }
  166. else if (type == LimbControl.Type.Toe0_L)
  167. {
  168. array = new IKManager.BoneType[]
  169. {
  170. IKManager.BoneType.Toe0_Root_L,
  171. IKManager.BoneType.Root,
  172. IKManager.BoneType.Toe0_0_L
  173. };
  174. }
  175. else if (type == LimbControl.Type.Toe1_L)
  176. {
  177. array = new IKManager.BoneType[]
  178. {
  179. IKManager.BoneType.Toe1_Root_L,
  180. IKManager.BoneType.Root,
  181. IKManager.BoneType.Toe1_0_L
  182. };
  183. }
  184. else if (type == LimbControl.Type.Toe2_L)
  185. {
  186. array = new IKManager.BoneType[]
  187. {
  188. IKManager.BoneType.Toe2_Root_L,
  189. IKManager.BoneType.Root,
  190. IKManager.BoneType.Toe2_0_L
  191. };
  192. }
  193. this.bone_start_point_ = this.ik_mgr_.GetBone(array[0]).transform;
  194. this.bone_joint_point_ = ((array[1] == IKManager.BoneType.Root) ? null : this.ik_mgr_.GetBone(array[1]).transform);
  195. this.bone_end_point_ = this.ik_mgr_.GetBone(array[2]).transform;
  196. this.ik_fabrik_ = this.bone_start_point_.gameObject.AddComponent<FABRIK>();
  197. this.ik_fabrik_.fixTransforms = false;
  198. IKSolverFABRIK solver = this.ik_fabrik_.solver;
  199. solver.maxIterations = 1;
  200. solver.Initiate(root_trans);
  201. if (this.bone_joint_point_ != null)
  202. {
  203. this.joint_drag_point_ = IKManager.CreateIKDragPoint(this.bone_joint_point_.gameObject, maid);
  204. if (0 <= this.bone_joint_point_.gameObject.name.IndexOf("Finger"))
  205. {
  206. if (GameMain.Instance.VRMode)
  207. {
  208. this.joint_drag_point_.axis_obj.AllAxisObject.transform.localScale = new Vector3(0.01f, 0.01f, 0.01f);
  209. }
  210. else
  211. {
  212. this.joint_drag_point_.axis_obj.transform.localScale = new Vector3(0.03f, 0.03f, 0.03f);
  213. }
  214. }
  215. this.joint_drag_point_.drag_start_event = delegate
  216. {
  217. this.ik_fabrik_.solver.SetChain(new Transform[0], root_trans);
  218. this.ik_fabrik_.solver.SetChain(new Transform[]
  219. {
  220. this.bone_start_point_,
  221. this.bone_joint_point_
  222. }, root_trans);
  223. this.ik_fabrik_.solver.target = this.joint_drag_point_.transform;
  224. };
  225. this.joint_drag_point_.drag_end_event = delegate
  226. {
  227. this.ik_fabrik_.solver.SetChain(new Transform[0], root_trans);
  228. this.ik_mgr_.HistoryPush();
  229. };
  230. }
  231. this.tip_drag_point_ = IKManager.CreateIKDragPoint(this.bone_end_point_.gameObject, maid);
  232. if (0 <= this.bone_end_point_.gameObject.name.IndexOf("Finger") || 0 <= this.bone_end_point_.gameObject.name.IndexOf("Toe"))
  233. {
  234. if (GameMain.Instance.VRMode)
  235. {
  236. this.tip_drag_point_.axis_obj.AllAxisObject.transform.localScale = new Vector3(0.01f, 0.01f, 0.01f);
  237. }
  238. else
  239. {
  240. this.tip_drag_point_.axis_obj.transform.localScale = new Vector3(0.03f, 0.03f, 0.03f);
  241. }
  242. }
  243. this.tip_drag_point_.drag_start_event = delegate
  244. {
  245. if (this.bone_joint_point_ != null)
  246. {
  247. this.ik_fabrik_.solver.SetChain(new Transform[0], root_trans);
  248. if (this.joint_lock)
  249. {
  250. this.ik_fabrik_.solver.SetChain(new Transform[]
  251. {
  252. this.bone_joint_point_,
  253. this.bone_end_point_
  254. }, root_trans);
  255. }
  256. else
  257. {
  258. this.ik_fabrik_.solver.SetChain(new Transform[]
  259. {
  260. this.bone_start_point_,
  261. this.bone_joint_point_,
  262. this.bone_end_point_
  263. }, root_trans);
  264. }
  265. }
  266. else
  267. {
  268. this.ik_fabrik_.solver.SetChain(new Transform[0], root_trans);
  269. this.ik_fabrik_.solver.SetChain(new Transform[]
  270. {
  271. this.bone_start_point_,
  272. this.bone_end_point_
  273. }, root_trans);
  274. }
  275. this.ik_fabrik_.solver.target = this.tip_drag_point_.transform;
  276. };
  277. this.tip_drag_point_.drag_end_event = delegate
  278. {
  279. this.ik_fabrik_.solver.SetChain(new Transform[0], root_trans);
  280. this.ik_mgr_.HistoryPush();
  281. };
  282. }
  283. public void OnDestroy()
  284. {
  285. if (this.joint_drag_point_ != null)
  286. {
  287. UnityEngine.Object.Destroy(this.joint_drag_point_.gameObject);
  288. }
  289. if (this.tip_drag_point_ != null)
  290. {
  291. UnityEngine.Object.Destroy(this.tip_drag_point_.gameObject);
  292. }
  293. this.joint_drag_point_ = null;
  294. this.tip_drag_point_ = null;
  295. }
  296. private void Update()
  297. {
  298. this.joint_lock = (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl));
  299. }
  300. public bool visible
  301. {
  302. get
  303. {
  304. return this.visible_joint_point || this.visible_tip_point;
  305. }
  306. set
  307. {
  308. this.visible_tip_point = value;
  309. this.visible_joint_point = value;
  310. }
  311. }
  312. private bool visible_joint_point
  313. {
  314. get
  315. {
  316. return this.joint_drag_point_ != null && this.joint_drag_point_.visible;
  317. }
  318. set
  319. {
  320. if (this.joint_drag_point_ != null)
  321. {
  322. this.joint_drag_point_.visible = value;
  323. }
  324. }
  325. }
  326. private bool visible_tip_point
  327. {
  328. get
  329. {
  330. return this.tip_drag_point_ != null && this.tip_drag_point_.visible;
  331. }
  332. set
  333. {
  334. if (this.tip_drag_point_ != null)
  335. {
  336. this.tip_drag_point_.visible = value;
  337. }
  338. }
  339. }
  340. public LimbControl.Type type
  341. {
  342. get
  343. {
  344. return this.type_;
  345. }
  346. }
  347. public bool joint_lock;
  348. private IKManager ik_mgr_;
  349. private Transform bone_start_point_;
  350. private Transform bone_joint_point_;
  351. private Transform bone_end_point_;
  352. private IKDragPoint joint_drag_point_;
  353. private IKDragPoint tip_drag_point_;
  354. private LimbControl.Type type_;
  355. private FABRIK ik_fabrik_;
  356. public enum Type
  357. {
  358. Arm_R,
  359. Foot_R,
  360. Finger0_R,
  361. Finger1_R,
  362. Finger2_R,
  363. Finger3_R,
  364. Finger4_R,
  365. Toe0_R,
  366. Toe1_R,
  367. Toe2_R,
  368. Arm_L,
  369. Foot_L,
  370. Finger0_L,
  371. Finger1_L,
  372. Finger2_L,
  373. Finger3_L,
  374. Finger4_L,
  375. Toe0_L,
  376. Toe1_L,
  377. Toe2_L
  378. }
  379. }