DragPointManager.cs 36 KB

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