DragPointManager.cs 36 KB

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