DragPointManager.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace COM3D2.MeidoPhotoStudio.Plugin
  6. {
  7. using ModKey = Utility.ModKey;
  8. public class DragPointManager
  9. {
  10. enum IKMode
  11. {
  12. None, UpperLock, Mune, RotLocal, BodyTransform, FingerRotLocalY, FingerRotLocalXZ, BodySelect,
  13. UpperRot
  14. }
  15. enum Bone
  16. {
  17. Head, HeadNub, ClavicleL, ClavicleR,
  18. UpperArmL, UpperArmR, ForearmL, ForearmR,
  19. HandL, HandR, IKHandL, IKHandR,
  20. MuneL, MuneSubL, MuneR, MuneSubR,
  21. Neck, Spine, Spine0a, Spine1, Spine1a, ThighL, ThighR,
  22. Pelvis, Hip,
  23. CalfL, CalfR, FootL, FootR,
  24. // Dragpoint specific
  25. Cube, Body, Torso,
  26. // Fingers
  27. Finger0L, Finger01L, Finger02L, Finger0NubL,
  28. Finger1L, Finger11L, Finger12L, Finger1NubL,
  29. Finger2L, Finger21L, Finger22L, Finger2NubL,
  30. Finger3L, Finger31L, Finger32L, Finger3NubL,
  31. Finger4L, Finger41L, Finger42L, Finger4NubL,
  32. Finger0R, Finger01R, Finger02R, Finger0NubR,
  33. Finger1R, Finger11R, Finger12R, Finger1NubR,
  34. Finger2R, Finger21R, Finger22R, Finger2NubR,
  35. Finger3R, Finger31R, Finger32R, Finger3NubR,
  36. Finger4R, Finger41R, Finger42R, Finger4NubR,
  37. // Toes
  38. Toe0L, Toe01L, Toe0NubL,
  39. Toe1L, Toe11L, Toe1NubL,
  40. Toe2L, Toe21L, Toe2NubL,
  41. Toe0R, Toe01R, Toe0NubR,
  42. Toe1R, Toe11R, Toe1NubR,
  43. Toe2R, Toe21R, Toe2NubR
  44. }
  45. private static readonly Dictionary<IKMode, Bone[]> IKGroup = new Dictionary<IKMode, Bone[]>()
  46. {
  47. [IKMode.None] = new[]
  48. {
  49. Bone.UpperArmL, Bone.ForearmL, Bone.HandL, Bone.UpperArmR,
  50. Bone.ForearmR, Bone.HandR, Bone.CalfL, Bone.FootL, Bone.CalfR, Bone.FootR
  51. },
  52. [IKMode.UpperLock] = new[] { Bone.HandL, Bone.HandR, Bone.FootL, Bone.FootR },
  53. [IKMode.Mune] = new[] { Bone.Head, Bone.MuneL, Bone.MuneR },
  54. [IKMode.RotLocal] = new[]
  55. {
  56. Bone.Head, Bone.CalfL, Bone.CalfR, Bone.Torso,
  57. Bone.Pelvis, Bone.HandL, Bone.HandR, Bone.FootL, Bone.FootR
  58. },
  59. [IKMode.BodyTransform] = new[] { Bone.Body, Bone.Cube },
  60. [IKMode.BodySelect] = new[] { Bone.Head, Bone.Body },
  61. [IKMode.FingerRotLocalXZ] = new[]
  62. {
  63. Bone.Finger01L, Bone.Finger02L, Bone.Finger0NubL,
  64. Bone.Finger11L, Bone.Finger12L, Bone.Finger1NubL,
  65. Bone.Finger21L, Bone.Finger22L, Bone.Finger2NubL,
  66. Bone.Finger31L, Bone.Finger32L, Bone.Finger3NubL,
  67. Bone.Finger41L, Bone.Finger42L, Bone.Finger4NubL,
  68. Bone.Finger01R, Bone.Finger02R, Bone.Finger0NubR,
  69. Bone.Finger11R, Bone.Finger12R, Bone.Finger1NubR,
  70. Bone.Finger21R, Bone.Finger22R, Bone.Finger2NubR,
  71. Bone.Finger31R, Bone.Finger32R, Bone.Finger3NubR,
  72. Bone.Finger41R, Bone.Finger42R, Bone.Finger4NubR,
  73. Bone.Toe01L, Bone.Toe0NubL, Bone.Toe11L, Bone.Toe1NubL,
  74. Bone.Toe21L, Bone.Toe2NubL, Bone.Toe01R, Bone.Toe0NubR,
  75. Bone.Toe11R, Bone.Toe1NubR, Bone.Toe21R, Bone.Toe2NubR
  76. },
  77. [IKMode.FingerRotLocalY] = new[] {
  78. Bone.Finger01L, Bone.Finger11L, Bone.Finger21L, Bone.Finger31L, Bone.Finger41L,
  79. Bone.Finger01R, Bone.Finger11R, Bone.Finger21R, Bone.Finger31R, Bone.Finger41R,
  80. Bone.Toe01L, Bone.Toe11L, Bone.Toe21L, Bone.Toe01R, Bone.Toe11R, Bone.Toe21R
  81. }
  82. };
  83. private static readonly Dictionary<IKMode, DragInfo[]> IKGroupBone = new Dictionary<IKMode, DragInfo[]>()
  84. {
  85. [IKMode.None] = new[] {
  86. DragInfo.DragBone(Bone.UpperArmL), DragInfo.DragBone(Bone.ForearmL), DragInfo.DragBone(Bone.HandL),
  87. DragInfo.DragBone(Bone.UpperArmR), DragInfo.DragBone(Bone.ForearmR), DragInfo.DragBone(Bone.HandR),
  88. DragInfo.DragBone(Bone.CalfL), DragInfo.DragBone(Bone.FootL), DragInfo.DragBone(Bone.CalfR),
  89. DragInfo.DragBone(Bone.FootR), DragInfo.DragBone(Bone.Neck), DragInfo.DragBone(Bone.Spine1a),
  90. DragInfo.DragBone(Bone.Spine1), DragInfo.DragBone(Bone.Spine0a), DragInfo.DragBone(Bone.Spine),
  91. DragInfo.DragBone(Bone.Hip)
  92. },
  93. [IKMode.UpperLock] = new[] {
  94. DragInfo.Gizmo(Bone.Neck), DragInfo.Gizmo(Bone.Spine1a), DragInfo.Gizmo(Bone.Spine1),
  95. DragInfo.Gizmo(Bone.Spine0a), DragInfo.Gizmo(Bone.Spine), DragInfo.DragBone(Bone.Hip),
  96. DragInfo.DragBone(Bone.HandR), DragInfo.DragBone(Bone.HandL), DragInfo.DragBone(Bone.FootL),
  97. DragInfo.DragBone(Bone.FootR)
  98. },
  99. [IKMode.RotLocal] = new[] {
  100. DragInfo.Gizmo(Bone.HandL), DragInfo.Gizmo(Bone.HandR), DragInfo.Gizmo(Bone.FootL),
  101. DragInfo.Gizmo(Bone.FootR), DragInfo.Gizmo(Bone.Hip)
  102. },
  103. [IKMode.Mune] = new[] {
  104. DragInfo.Gizmo(Bone.ForearmL), DragInfo.Gizmo(Bone.ForearmR), DragInfo.Gizmo(Bone.CalfL),
  105. DragInfo.Gizmo(Bone.CalfR), DragInfo.Drag(Bone.MuneL), DragInfo.Drag(Bone.MuneR),
  106. DragInfo.Drag(Bone.Head)
  107. },
  108. [IKMode.UpperRot] = new[] {
  109. DragInfo.Gizmo(Bone.UpperArmL), DragInfo.Gizmo(Bone.UpperArmR), DragInfo.Gizmo(Bone.ThighL),
  110. DragInfo.Gizmo(Bone.ThighR)
  111. },
  112. [IKMode.BodyTransform] = new[] { DragInfo.Drag(Bone.Body), DragInfo.Drag(Bone.Cube) },
  113. [IKMode.BodySelect] = new[] { DragInfo.Drag(Bone.Head), DragInfo.Drag(Bone.Body) },
  114. [IKMode.FingerRotLocalXZ] = IKGroup[IKMode.FingerRotLocalXZ]
  115. .Select(bone => DragInfo.DragBone(bone)).ToArray(),
  116. [IKMode.FingerRotLocalY] = IKGroup[IKMode.FingerRotLocalY]
  117. .Select(bone => DragInfo.DragBone(bone)).ToArray()
  118. };
  119. private Meido meido;
  120. private Maid maid;
  121. private Dictionary<Bone, BaseDrag> DragPoint;
  122. private Dictionary<Bone, Transform> BoneTransform;
  123. private IKMode ikMode;
  124. private IKMode ikModeOld = IKMode.None;
  125. public event EventHandler<MeidoChangeEventArgs> SelectMaid;
  126. public bool Initialized { get; private set; }
  127. public bool Active { get; set; }
  128. public bool IsBone { get; set; }
  129. private static bool cubeActive = false;
  130. public DragPointManager(Meido meido)
  131. {
  132. this.meido = meido;
  133. this.maid = meido.Maid;
  134. this.meido.BodyLoad += Initialize;
  135. }
  136. public void Initialize(object sender, EventArgs args)
  137. {
  138. if (Initialized) return;
  139. Initialized = true;
  140. InitializeBones();
  141. InitializeDragPoints();
  142. SetActive(true);
  143. meido.BodyLoad -= Initialize;
  144. }
  145. public void Deactivate()
  146. {
  147. foreach (KeyValuePair<Bone, BaseDrag> dragPoint in DragPoint)
  148. {
  149. GameObject.Destroy(dragPoint.Value.gameObject);
  150. }
  151. DragPoint.Clear();
  152. BoneTransform.Clear();
  153. Initialized = false;
  154. this.Active = false;
  155. }
  156. public void SetActive(bool active)
  157. {
  158. this.Active = active;
  159. if (this.Active)
  160. {
  161. ikMode = ikModeOld = IKMode.None;
  162. UpdateIK();
  163. }
  164. else
  165. {
  166. foreach (KeyValuePair<Bone, BaseDrag> dragPoint in DragPoint)
  167. {
  168. dragPoint.Value.gameObject.SetActive(false);
  169. }
  170. DragPoint[Bone.Head].SetDragProp(false, true, false);
  171. DragPoint[Bone.Body].SetDragProp(false, true, false);
  172. DragPoint[Bone.Cube].SetDragProp(false, true, true);
  173. }
  174. }
  175. public void Update()
  176. {
  177. if (Input.GetKey(KeyCode.Z) || Input.GetKey(KeyCode.X) || Input.GetKey(KeyCode.C))
  178. {
  179. ikMode = IKMode.BodyTransform;
  180. }
  181. else if (Input.GetKey(KeyCode.A))
  182. {
  183. ikMode = IKMode.BodySelect;
  184. }
  185. else if (Utility.GetModKey(ModKey.Control) && Utility.GetModKey(ModKey.Alt))
  186. {
  187. ikMode = IKMode.Mune;
  188. }
  189. else if (Utility.GetModKey(ModKey.Shift) && Input.GetKey(KeyCode.Space))
  190. {
  191. ikMode = IKMode.FingerRotLocalY;
  192. }
  193. else if (Utility.GetModKey(ModKey.Alt))
  194. {
  195. bool shift = IsBone && Utility.GetModKey(ModKey.Shift);
  196. ikMode = shift ? IKMode.UpperRot : IKMode.RotLocal;
  197. }
  198. else if (Input.GetKey(KeyCode.Space))
  199. {
  200. ikMode = IKMode.FingerRotLocalXZ;
  201. }
  202. else if (Utility.GetModKey(ModKey.Control))
  203. {
  204. ikMode = IKMode.UpperLock;
  205. }
  206. else
  207. {
  208. ikMode = IKMode.None;
  209. }
  210. if (ikMode != ikModeOld) UpdateIK();
  211. ikModeOld = ikMode;
  212. }
  213. private void UpdateIK()
  214. {
  215. if (Active)
  216. {
  217. if (this.IsBone) UpdateBoneIK();
  218. else
  219. {
  220. foreach (KeyValuePair<Bone, BaseDrag> dragPoint in DragPoint)
  221. {
  222. dragPoint.Value.gameObject.SetActive(false);
  223. }
  224. foreach (Bone bone in IKGroup[ikMode])
  225. {
  226. DragPoint[bone].gameObject.SetActive(true);
  227. }
  228. if (ikMode == IKMode.BodyTransform)
  229. {
  230. DragPoint[Bone.Cube].gameObject.SetActive(cubeActive);
  231. }
  232. }
  233. }
  234. else
  235. {
  236. if (ikMode == IKMode.BodySelect)
  237. {
  238. DragPoint[Bone.Body].gameObject.SetActive(true);
  239. DragPoint[Bone.Head].gameObject.SetActive(true);
  240. }
  241. else if (ikMode == IKMode.BodyTransform)
  242. {
  243. DragPoint[Bone.Body].gameObject.SetActive(true);
  244. DragPoint[Bone.Cube].gameObject.SetActive(cubeActive);
  245. }
  246. else if (ikMode == IKMode.UpperRot || ikMode == IKMode.RotLocal)
  247. {
  248. DragPoint[Bone.Head].gameObject.SetActive(true);
  249. }
  250. else
  251. {
  252. DragPoint[Bone.Body].gameObject.SetActive(false);
  253. DragPoint[Bone.Head].gameObject.SetActive(false);
  254. DragPoint[Bone.Cube].gameObject.SetActive(false);
  255. }
  256. }
  257. }
  258. private void UpdateBoneIK()
  259. {
  260. foreach (KeyValuePair<Bone, BaseDrag> dragPoint in DragPoint)
  261. {
  262. dragPoint.Value.gameObject.SetActive(false);
  263. dragPoint.Value.SetDragProp(false, false, dragPoint.Key >= Bone.Finger0L);
  264. }
  265. foreach (DragInfo info in IKGroupBone[ikMode])
  266. {
  267. BaseDrag drag = DragPoint[info.Bone];
  268. drag.gameObject.SetActive(true);
  269. drag.SetDragProp(info.GizmoActive, info.DragPointActive, info.DragPointVisible);
  270. }
  271. }
  272. private void SetDragPointScale(float scale)
  273. {
  274. foreach (KeyValuePair<Bone, BaseDrag> kvp in DragPoint)
  275. {
  276. BaseDrag dragPoint = kvp.Value;
  277. dragPoint.DragPointScale = dragPoint.BaseScale * scale;
  278. }
  279. }
  280. public void BoneModeActive(bool active)
  281. {
  282. this.IsBone = active;
  283. foreach (KeyValuePair<Bone, BaseDrag> dragPoint in DragPoint)
  284. {
  285. dragPoint.Value.IsBone = this.IsBone;
  286. if (!this.IsBone)
  287. {
  288. dragPoint.Value.SetDragProp(false, true, dragPoint.Key >= Bone.Finger0L);
  289. }
  290. }
  291. SetActive(true);
  292. }
  293. // TODO: Rework this a little to reduce number of needed BaseDrag derived components
  294. private void InitializeDragPoints()
  295. {
  296. DragPoint = new Dictionary<Bone, BaseDrag>();
  297. Vector3 limbDragPointSize = Vector3.one * 0.12f;
  298. Vector3 limbDragPointSizeBone = Vector3.one * 0.07f;
  299. Vector3 fingerDragPointSize = Vector3.one * 0.015f;
  300. Func<Transform[], Transform[], Transform[], bool, BaseDrag[]> MakeIKChainDragPoint =
  301. (upper, middle, lower, leg) =>
  302. {
  303. GameObject[] dragPoints = new GameObject[3];
  304. for (int i = 0; i < dragPoints.Length; i++)
  305. {
  306. dragPoints[i] =
  307. BaseDrag.MakeDragPoint(PrimitiveType.Sphere, limbDragPointSize, BaseDrag.LightBlue);
  308. }
  309. return new BaseDrag[3] {
  310. dragPoints[0].AddComponent<DragJointForearm>()
  311. .Initialize(upper, false, meido, () => upper[2].position, () => Vector3.zero),
  312. dragPoints[1].AddComponent<DragJointForearm>()
  313. .Initialize(middle, leg, meido, () => middle[2].position, () => Vector3.zero),
  314. dragPoints[2].AddComponent<DragJointHand>()
  315. .Initialize(lower, leg, meido, () => lower[2].position, () => Vector3.zero)
  316. };
  317. };
  318. // TODO: Modify dragpoint sizes for each joint
  319. Action<Bone, Bone, int> MakeFingerDragPoint = (start, end, joints) =>
  320. {
  321. for (Bone it = start; it <= end; it += joints)
  322. {
  323. for (int i = 0; i < joints - 1; i++)
  324. {
  325. Bone bone = it + 1 + i;
  326. DragPoint[bone] = BaseDrag.MakeDragPoint(PrimitiveType.Sphere, fingerDragPointSize, BaseDrag.Blue)
  327. .AddComponent<DragJointFinger>()
  328. .Initialize(new Transform[3] {
  329. BoneTransform[bone - 1],
  330. BoneTransform[bone - 1],
  331. BoneTransform[bone]
  332. }, i == 0, meido, () => BoneTransform[bone].position, () => Vector3.zero
  333. );
  334. DragPoint[bone].gameObject.layer = 0;
  335. DragPoint[bone].DragPointVisible = true;
  336. }
  337. }
  338. };
  339. // Cube Dragpoint
  340. DragPoint[Bone.Cube] =
  341. BaseDrag.MakeDragPoint(PrimitiveType.Cube, new Vector3(0.12f, 0.12f, 0.12f), BaseDrag.Blue)
  342. .AddComponent<DragBody>()
  343. .Initialize(meido,
  344. () => maid.transform.position,
  345. () => maid.transform.eulerAngles
  346. );
  347. DragBody dragCube = DragPoint[Bone.Cube] as DragBody;
  348. dragCube.Scale += (s, a) => SetDragPointScale(maid.transform.localScale.x);
  349. dragCube.DragPointVisible = true;
  350. // Body Dragpoint
  351. DragPoint[Bone.Body] =
  352. BaseDrag.MakeDragPoint(PrimitiveType.Capsule, new Vector3(0.2f, 0.3f, 0.24f), BaseDrag.LightBlue)
  353. .AddComponent<DragBody>()
  354. .Initialize(meido,
  355. () => new Vector3(
  356. (BoneTransform[Bone.Hip].position.x + BoneTransform[Bone.Spine0a].position.x) / 2f,
  357. (BoneTransform[Bone.Spine1].position.y + BoneTransform[Bone.Spine0a].position.y) / 2f,
  358. (BoneTransform[Bone.Spine0a].position.z + BoneTransform[Bone.Hip].position.z) / 2f
  359. ),
  360. () => new Vector3(
  361. BoneTransform[Bone.Spine0a].transform.eulerAngles.x,
  362. BoneTransform[Bone.Spine0a].transform.eulerAngles.y,
  363. BoneTransform[Bone.Spine0a].transform.eulerAngles.z + 90f
  364. )
  365. );
  366. DragBody dragBody = DragPoint[Bone.Body] as DragBody;
  367. dragBody.Select += (s, e) => OnMeidoSelect(new MeidoChangeEventArgs(meido.ActiveSlot, true));
  368. dragBody.Scale += (s, a) => SetDragPointScale(maid.transform.localScale.x);
  369. // Head Dragpoint
  370. DragPoint[Bone.Head] =
  371. BaseDrag.MakeDragPoint(PrimitiveType.Sphere, new Vector3(0.2f, 0.24f, 0.2f), BaseDrag.LightBlue)
  372. .AddComponent<DragHead>()
  373. .Initialize(BoneTransform[Bone.Neck], meido,
  374. () => new Vector3(
  375. BoneTransform[Bone.Head].position.x,
  376. (BoneTransform[Bone.Head].position.y * 1.2f + BoneTransform[Bone.HeadNub].position.y * 0.8f) / 2f,
  377. BoneTransform[Bone.Head].position.z
  378. ),
  379. () => new Vector3(
  380. BoneTransform[Bone.Head].eulerAngles.x,
  381. BoneTransform[Bone.Head].eulerAngles.y,
  382. BoneTransform[Bone.Head].eulerAngles.z + 90f
  383. )
  384. );
  385. DragHead dragHead = DragPoint[Bone.Head] as DragHead;
  386. dragHead.Select += (s, a) => OnMeidoSelect(new MeidoChangeEventArgs(meido.ActiveSlot, true, false));
  387. // Torso Dragpoint
  388. DragPoint[Bone.Torso] =
  389. BaseDrag.MakeDragPoint(PrimitiveType.Capsule, new Vector3(0.2f, 0.19f, 0.24f), BaseDrag.LightBlue)
  390. .AddComponent<DragTorso>();
  391. Transform spineTrans1 = BoneTransform[Bone.Spine1];
  392. Transform spineTrans2 = BoneTransform[Bone.Spine1a];
  393. Transform[] spineParts = new Transform[4] {
  394. BoneTransform[Bone.Spine1a],
  395. BoneTransform[Bone.Spine1],
  396. BoneTransform[Bone.Spine0a],
  397. BoneTransform[Bone.Spine]
  398. };
  399. DragTorso dragTorso = DragPoint[Bone.Torso] as DragTorso;
  400. dragTorso.Initialize(spineParts, meido,
  401. () => new Vector3(
  402. spineTrans1.position.x,
  403. (spineTrans2.position.y * 2f) / 2f,
  404. spineTrans1.position.z
  405. ),
  406. () => new Vector3(
  407. spineTrans1.eulerAngles.x,
  408. spineTrans1.eulerAngles.y,
  409. spineTrans1.eulerAngles.z + 90f
  410. )
  411. );
  412. // Pelvis Dragpoint
  413. DragPoint[Bone.Pelvis] =
  414. BaseDrag.MakeDragPoint(PrimitiveType.Capsule, new Vector3(0.2f, 0.15f, 0.24f), BaseDrag.LightBlue)
  415. .AddComponent<DragPelvis>();
  416. Transform pelvisTrans = BoneTransform[Bone.Pelvis];
  417. Transform spineTrans = BoneTransform[Bone.Spine];
  418. DragPelvis dragPelvis = DragPoint[Bone.Pelvis] as DragPelvis;
  419. dragPelvis.Initialize(BoneTransform[Bone.Pelvis], meido,
  420. () => new Vector3(
  421. pelvisTrans.position.x,
  422. (pelvisTrans.position.y + spineTrans.position.y) / 2f,
  423. pelvisTrans.position.z
  424. ),
  425. () => new Vector3(
  426. pelvisTrans.eulerAngles.x + 90f,
  427. pelvisTrans.eulerAngles.y + 90f,
  428. pelvisTrans.eulerAngles.z
  429. )
  430. );
  431. // Left Mune Dragpoint
  432. DragPoint[Bone.MuneL] =
  433. BaseDrag.MakeDragPoint(PrimitiveType.Sphere, new Vector3(0.12f, 0.12f, 0.12f), BaseDrag.LightBlue)
  434. .AddComponent<DragMune>();
  435. Transform[] muneIKChainL = new Transform[3] {
  436. BoneTransform[Bone.MuneL],
  437. BoneTransform[Bone.MuneL],
  438. BoneTransform[Bone.MuneSubL]
  439. };
  440. DragMune dragMuneL = DragPoint[Bone.MuneL] as DragMune;
  441. dragMuneL.Initialize(muneIKChainL, meido,
  442. () => (BoneTransform[Bone.MuneL].position + BoneTransform[Bone.MuneSubL].position) / 2f,
  443. () => Vector3.zero
  444. );
  445. // Right Mune Dragpoint
  446. DragPoint[Bone.MuneR] =
  447. BaseDrag.MakeDragPoint(PrimitiveType.Sphere, new Vector3(0.12f, 0.12f, 0.12f), BaseDrag.LightBlue)
  448. .AddComponent<DragMune>();
  449. Transform[] muneIKChainR = new Transform[3] {
  450. BoneTransform[Bone.MuneR],
  451. BoneTransform[Bone.MuneR],
  452. BoneTransform[Bone.MuneSubR]
  453. };
  454. DragMune dragMuneR = DragPoint[Bone.MuneR] as DragMune;
  455. dragMuneR.Initialize(muneIKChainR, meido,
  456. () => (BoneTransform[Bone.MuneR].position + BoneTransform[Bone.MuneSubR].position) / 2f,
  457. () => Vector3.zero
  458. );
  459. // Left Arm Dragpoint
  460. BaseDrag[] ikChainArmL = MakeIKChainDragPoint(
  461. new Transform[3] {
  462. BoneTransform[Bone.ClavicleL],
  463. BoneTransform[Bone.ClavicleL],
  464. BoneTransform[Bone.UpperArmL]
  465. },
  466. new Transform[3] {
  467. BoneTransform[Bone.UpperArmL],
  468. BoneTransform[Bone.UpperArmL],
  469. BoneTransform[Bone.ForearmL]
  470. },
  471. new Transform[3] {
  472. BoneTransform[Bone.UpperArmL],
  473. BoneTransform[Bone.ForearmL],
  474. BoneTransform[Bone.HandL]
  475. },
  476. false
  477. );
  478. DragPoint[Bone.UpperArmL] = ikChainArmL[0];
  479. DragPoint[Bone.ForearmL] = ikChainArmL[1];
  480. DragPoint[Bone.HandL] = ikChainArmL[2];
  481. // Right Arm Dragpoint
  482. BaseDrag[] ikChainArmR = MakeIKChainDragPoint(
  483. new Transform[3] {
  484. BoneTransform[Bone.ClavicleR],
  485. BoneTransform[Bone.ClavicleR],
  486. BoneTransform[Bone.UpperArmR]
  487. },
  488. new Transform[3] {
  489. BoneTransform[Bone.UpperArmR],
  490. BoneTransform[Bone.UpperArmR],
  491. BoneTransform[Bone.ForearmR]
  492. },
  493. new Transform[3] {
  494. BoneTransform[Bone.UpperArmR],
  495. BoneTransform[Bone.ForearmR],
  496. BoneTransform[Bone.HandR]
  497. },
  498. false
  499. );
  500. DragPoint[Bone.UpperArmR] = ikChainArmR[0];
  501. DragPoint[Bone.ForearmR] = ikChainArmR[1];
  502. DragPoint[Bone.HandR] = ikChainArmR[2];
  503. // Left Leg Dragpoint
  504. BaseDrag[] ikChainLegL = MakeIKChainDragPoint(
  505. new Transform[3] {
  506. BoneTransform[Bone.ThighL],
  507. BoneTransform[Bone.CalfL],
  508. BoneTransform[Bone.FootL]
  509. },
  510. new Transform[3] {
  511. BoneTransform[Bone.ThighL],
  512. BoneTransform[Bone.ThighL],
  513. BoneTransform[Bone.CalfL]
  514. },
  515. new Transform[3] {
  516. BoneTransform[Bone.ThighL],
  517. BoneTransform[Bone.CalfL],
  518. BoneTransform[Bone.FootL]
  519. },
  520. true
  521. );
  522. DragPoint[Bone.CalfL] = ikChainLegL[1];
  523. DragPoint[Bone.FootL] = ikChainLegL[2];
  524. // Right Arm Dragpoint
  525. BaseDrag[] ikChainLegR = MakeIKChainDragPoint(
  526. new Transform[3] {
  527. BoneTransform[Bone.ThighR],
  528. BoneTransform[Bone.CalfR],
  529. BoneTransform[Bone.FootR]
  530. },
  531. new Transform[3] {
  532. BoneTransform[Bone.ThighR],
  533. BoneTransform[Bone.ThighR],
  534. BoneTransform[Bone.CalfR]
  535. },
  536. new Transform[3] {
  537. BoneTransform[Bone.ThighR],
  538. BoneTransform[Bone.CalfR],
  539. BoneTransform[Bone.FootR]
  540. },
  541. true
  542. );
  543. DragPoint[Bone.CalfR] = ikChainLegR[1];
  544. DragPoint[Bone.FootR] = ikChainLegR[2];
  545. // destroy unused thigh dragpoints
  546. GameObject.Destroy(ikChainLegL[0].gameObject);
  547. GameObject.Destroy(ikChainLegR[0].gameObject);
  548. // Spine Dragpoints
  549. for (Bone bone = Bone.Neck; bone <= Bone.ThighR; ++bone)
  550. {
  551. Transform pos = BoneTransform[bone];
  552. DragPoint[bone] = BaseDrag.MakeDragPoint(PrimitiveType.Sphere, Vector3.one * 0.04f, BaseDrag.LightBlue)
  553. .AddComponent<DragSpine>()
  554. .Initialize(BoneTransform[bone], false, meido,
  555. () => pos.position,
  556. () => Vector3.zero
  557. );
  558. }
  559. // Hip DragPoint
  560. DragPoint[Bone.Hip] = BaseDrag.MakeDragPoint(PrimitiveType.Cube, Vector3.one * 0.045f, BaseDrag.LightBlue)
  561. .AddComponent<DragSpine>()
  562. .Initialize(BoneTransform[Bone.Hip], true, meido,
  563. () => BoneTransform[Bone.Hip].position,
  564. () => Vector3.zero
  565. );
  566. MakeFingerDragPoint(Bone.Finger0L, Bone.Finger4R, 4);
  567. MakeFingerDragPoint(Bone.Toe0L, Bone.Toe2R, 3);
  568. }
  569. private void InitializeBones()
  570. {
  571. BoneTransform = new Dictionary<Bone, Transform>()
  572. {
  573. [Bone.Head] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 Head", true),
  574. [Bone.Neck] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 Neck", true),
  575. [Bone.HeadNub] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 HeadNub", true),
  576. [Bone.IKHandL] = CMT.SearchObjName(maid.body0.m_Bones.transform, "_IK_handL", true),
  577. [Bone.IKHandR] = CMT.SearchObjName(maid.body0.m_Bones.transform, "_IK_handR", true),
  578. [Bone.MuneL] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Mune_L", true),
  579. [Bone.MuneSubL] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Mune_L_sub", true),
  580. [Bone.MuneR] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Mune_R", true),
  581. [Bone.MuneSubR] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Mune_R_sub", true),
  582. [Bone.Pelvis] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 Pelvis", true),
  583. [Bone.Hip] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01", true),
  584. [Bone.Spine] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 Spine", true),
  585. [Bone.Spine0a] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 Spine0a", true),
  586. [Bone.Spine1] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 Spine1", true),
  587. [Bone.Spine1a] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 Spine1a", true),
  588. [Bone.ClavicleL] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Clavicle", true),
  589. [Bone.ClavicleR] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Clavicle", true),
  590. [Bone.UpperArmL] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L UpperArm", true),
  591. [Bone.ForearmL] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Forearm", true),
  592. [Bone.HandL] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Hand", true),
  593. [Bone.UpperArmR] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R UpperArm", true),
  594. [Bone.ForearmR] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Forearm", true),
  595. [Bone.HandR] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Hand", true),
  596. [Bone.ThighL] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Thigh", true),
  597. [Bone.CalfL] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Calf", true),
  598. [Bone.FootL] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Foot", true),
  599. [Bone.ThighR] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Thigh", true),
  600. [Bone.CalfR] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Calf", true),
  601. [Bone.FootR] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Foot", true),
  602. // fingers
  603. [Bone.Finger0L] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Finger0", true),
  604. [Bone.Finger01L] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Finger01", true),
  605. [Bone.Finger02L] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Finger02", true),
  606. [Bone.Finger0NubL] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Finger0Nub", true),
  607. [Bone.Finger1L] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Finger1", true),
  608. [Bone.Finger11L] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Finger11", true),
  609. [Bone.Finger12L] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Finger12", true),
  610. [Bone.Finger1NubL] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Finger1Nub", true),
  611. [Bone.Finger2L] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Finger2", true),
  612. [Bone.Finger21L] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Finger21", true),
  613. [Bone.Finger22L] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Finger22", true),
  614. [Bone.Finger2NubL] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Finger2Nub", true),
  615. [Bone.Finger3L] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Finger3", true),
  616. [Bone.Finger31L] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Finger31", true),
  617. [Bone.Finger32L] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Finger32", true),
  618. [Bone.Finger3NubL] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Finger3Nub", true),
  619. [Bone.Finger4L] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Finger4", true),
  620. [Bone.Finger41L] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Finger41", true),
  621. [Bone.Finger42L] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Finger42", true),
  622. [Bone.Finger4NubL] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Finger4Nub", true),
  623. [Bone.Finger0R] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Finger0", true),
  624. [Bone.Finger01R] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Finger01", true),
  625. [Bone.Finger02R] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Finger02", true),
  626. [Bone.Finger0NubR] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Finger0Nub", true),
  627. [Bone.Finger1R] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Finger1", true),
  628. [Bone.Finger11R] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Finger11", true),
  629. [Bone.Finger12R] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Finger12", true),
  630. [Bone.Finger1NubR] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Finger1Nub", true),
  631. [Bone.Finger2R] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Finger2", true),
  632. [Bone.Finger21R] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Finger21", true),
  633. [Bone.Finger22R] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Finger22", true),
  634. [Bone.Finger2NubR] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Finger2Nub", true),
  635. [Bone.Finger3R] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Finger3", true),
  636. [Bone.Finger31R] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Finger31", true),
  637. [Bone.Finger32R] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Finger32", true),
  638. [Bone.Finger3NubR] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Finger3Nub", true),
  639. [Bone.Finger4R] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Finger4", true),
  640. [Bone.Finger41R] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Finger41", true),
  641. [Bone.Finger42R] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Finger42", true),
  642. [Bone.Finger4NubR] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Finger4Nub", true),
  643. // Toes
  644. [Bone.Toe0L] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Toe0", true),
  645. [Bone.Toe01L] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Toe01", true),
  646. [Bone.Toe0NubL] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Toe0Nub", true),
  647. [Bone.Toe1L] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Toe1", true),
  648. [Bone.Toe11L] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Toe11", true),
  649. [Bone.Toe1NubL] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Toe1Nub", true),
  650. [Bone.Toe2L] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Toe2", true),
  651. [Bone.Toe21L] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Toe21", true),
  652. [Bone.Toe2NubL] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 L Toe2Nub", true),
  653. [Bone.Toe0R] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Toe0", true),
  654. [Bone.Toe01R] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Toe01", true),
  655. [Bone.Toe0NubR] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Toe0Nub", true),
  656. [Bone.Toe1R] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Toe1", true),
  657. [Bone.Toe11R] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Toe11", true),
  658. [Bone.Toe1NubR] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Toe1Nub", true),
  659. [Bone.Toe2R] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Toe2", true),
  660. [Bone.Toe21R] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Toe21", true),
  661. [Bone.Toe2NubR] = CMT.SearchObjName(maid.body0.m_Bones.transform, "Bip01 R Toe2Nub", true)
  662. };
  663. }
  664. private void OnMeidoSelect(MeidoChangeEventArgs args)
  665. {
  666. SelectMaid?.Invoke(this, args);
  667. }
  668. private void OnDragEvent(object sender, EventArgs args)
  669. {
  670. this.meido.IsStop = true;
  671. }
  672. private struct DragInfo
  673. {
  674. public Bone Bone { get; private set; }
  675. public bool GizmoActive { get; private set; }
  676. public bool DragPointActive { get; private set; }
  677. public bool DragPointVisible { get; private set; }
  678. public DragInfo(Bone bone, bool gizmoActive, bool dragPointActive, bool dragPointVisible)
  679. {
  680. this.Bone = bone;
  681. this.GizmoActive = gizmoActive;
  682. this.DragPointActive = dragPointActive;
  683. this.DragPointVisible = dragPointVisible;
  684. }
  685. public static DragInfo Gizmo(Bone bone)
  686. {
  687. return new DragInfo(bone, true, false, false);
  688. }
  689. public static DragInfo Drag(Bone bone)
  690. {
  691. return new DragInfo(bone, false, true, false);
  692. }
  693. public static DragInfo DragBone(Bone bone)
  694. {
  695. return new DragInfo(bone, false, true, true);
  696. }
  697. }
  698. }
  699. }