MeidoDragPointManager.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. namespace COM3D2.MeidoPhotoStudio.Plugin
  7. {
  8. internal enum AttachPoint
  9. {
  10. None, Head, Neck, UpperArmL, UpperArmR, ForearmL, ForearmR, MuneL, MuneR, HandL, HandR,
  11. Pelvis, ThighL, ThighR, CalfL, CalfR, FootL, FootR
  12. }
  13. internal class MeidoDragPointManager
  14. {
  15. private 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<AttachPoint, Bone> PointToBone = new Dictionary<AttachPoint, Bone>()
  46. {
  47. [AttachPoint.Head] = Bone.Head,
  48. [AttachPoint.Neck] = Bone.HeadNub,
  49. [AttachPoint.UpperArmL] = Bone.UpperArmL,
  50. [AttachPoint.UpperArmR] = Bone.UpperArmR,
  51. [AttachPoint.ForearmL] = Bone.ForearmL,
  52. [AttachPoint.ForearmR] = Bone.ForearmR,
  53. [AttachPoint.MuneL] = Bone.MuneL,
  54. [AttachPoint.MuneR] = Bone.MuneR,
  55. [AttachPoint.HandL] = Bone.HandL,
  56. [AttachPoint.HandR] = Bone.HandR,
  57. [AttachPoint.Pelvis] = Bone.Pelvis,
  58. [AttachPoint.ThighL] = Bone.ThighL,
  59. [AttachPoint.ThighR] = Bone.ThighR,
  60. [AttachPoint.CalfL] = Bone.CalfL,
  61. [AttachPoint.CalfR] = Bone.CalfR,
  62. [AttachPoint.FootL] = Bone.FootL,
  63. [AttachPoint.FootR] = Bone.FootR,
  64. };
  65. private static bool cubeActive;
  66. public static bool CubeActive
  67. {
  68. get => cubeActive;
  69. set
  70. {
  71. if (value != cubeActive)
  72. {
  73. cubeActive = value;
  74. CubeActiveChange?.Invoke(null, EventArgs.Empty);
  75. }
  76. }
  77. }
  78. private static bool cubeSmall = false;
  79. public static bool CubeSmall
  80. {
  81. get => cubeSmall;
  82. set
  83. {
  84. if (value != cubeSmall)
  85. {
  86. cubeSmall = value;
  87. CubeSmallChange?.Invoke(null, EventArgs.Empty);
  88. }
  89. }
  90. }
  91. private static EventHandler CubeActiveChange;
  92. private static EventHandler CubeSmallChange;
  93. private bool initialized = false;
  94. private Meido meido;
  95. private Dictionary<Bone, Transform> BoneTransform = new Dictionary<Bone, Transform>();
  96. private Dictionary<Bone, DragPointMeido> DragPoints = new Dictionary<Bone, DragPointMeido>();
  97. private DragPointBody dragBody;
  98. private DragPointBody dragCube;
  99. public event EventHandler<MeidoUpdateEventArgs> SelectMaid;
  100. private bool isBone = false;
  101. public bool IsBone
  102. {
  103. get => isBone;
  104. set
  105. {
  106. if (!initialized) return;
  107. if (isBone != value)
  108. {
  109. isBone = value;
  110. foreach (DragPointMeido dragPoint in DragPoints.Values)
  111. {
  112. dragPoint.IsBone = isBone;
  113. }
  114. }
  115. }
  116. }
  117. private bool active = true;
  118. public bool Active
  119. {
  120. get => active;
  121. set
  122. {
  123. if (!initialized) return;
  124. if (active != value)
  125. {
  126. active = value;
  127. foreach (DragPointMeido dragPoint in DragPoints.Values)
  128. {
  129. dragPoint.gameObject.SetActive(active);
  130. }
  131. DragPointHead head = (DragPointHead)DragPoints[Bone.Head];
  132. head.gameObject.SetActive(true);
  133. head.IsIK = !active;
  134. }
  135. }
  136. }
  137. public MeidoDragPointManager(Meido meido) => this.meido = meido;
  138. public void Deserialize(BinaryReader binaryReader)
  139. {
  140. Bone[] bones = {
  141. Bone.Hip, Bone.Pelvis, Bone.Spine, Bone.Spine0a, Bone.Spine1, Bone.Spine1a, Bone.Neck,
  142. Bone.ClavicleL, Bone.ClavicleR, Bone.UpperArmL, Bone.UpperArmR, Bone.ForearmL, Bone.ForearmR,
  143. Bone.ThighL, Bone.ThighR, Bone.CalfL, Bone.CalfR, Bone.MuneL, Bone.MuneR, Bone.MuneSubL, Bone.MuneSubR,
  144. Bone.HandL, Bone.HandR, Bone.FootL, Bone.FootR
  145. };
  146. int localRotationIndex = Array.IndexOf(bones, Bone.CalfR);
  147. for (Bone bone = Bone.Finger0L; bone <= Bone.Toe2NubR; ++bone)
  148. {
  149. BoneTransform[bone].localRotation = binaryReader.ReadQuaternion();
  150. }
  151. for (int i = 0; i < bones.Length; i++)
  152. {
  153. Bone bone = bones[i];
  154. Quaternion rotation = binaryReader.ReadQuaternion();
  155. if (i > localRotationIndex) BoneTransform[bone].localRotation = rotation;
  156. else BoneTransform[bone].rotation = rotation;
  157. }
  158. // WHY????
  159. GameMain.Instance.StartCoroutine(ApplyHipPosition(binaryReader.ReadVector3()));
  160. }
  161. /*
  162. Somebody smarter than me please help me find a way to do this better T_T
  163. inb4 for loop.
  164. */
  165. private System.Collections.IEnumerator ApplyHipPosition(Vector3 hipPosition)
  166. {
  167. BoneTransform[Bone.Hip].position = hipPosition;
  168. yield return new WaitForEndOfFrame();
  169. BoneTransform[Bone.Hip].position = hipPosition;
  170. yield return new WaitForEndOfFrame();
  171. BoneTransform[Bone.Hip].position = hipPosition;
  172. }
  173. public Transform GetAttachPointTransform(AttachPoint point)
  174. {
  175. if (point == AttachPoint.None) return null;
  176. return BoneTransform[PointToBone[point]];
  177. }
  178. public void Flip()
  179. {
  180. meido.Stop = true;
  181. Bone[] single = new[] { Bone.Pelvis, Bone.Spine, Bone.Spine0a, Bone.Spine1, Bone.Spine1a, Bone.Neck };
  182. Bone[] pair = new[] {
  183. Bone.ClavicleL, Bone.ClavicleR, Bone.UpperArmL, Bone.UpperArmR, Bone.ForearmL, Bone.ForearmR,
  184. Bone.ThighL, Bone.ThighR, Bone.CalfL, Bone.CalfR, Bone.HandL, Bone.HandR, Bone.FootL, Bone.FootR
  185. };
  186. List<Vector3> singleRotations = single.Select(bone => BoneTransform[bone].eulerAngles).ToList();
  187. List<Vector3> pairRotations = pair.Select(bone => BoneTransform[bone].eulerAngles).ToList();
  188. Transform hip = BoneTransform[Bone.Hip];
  189. Vector3 vecHip = hip.eulerAngles;
  190. Transform hipL = meido.Maid.body0.GetBone("Hip_L");
  191. Vector3 vecHipL = hipL.eulerAngles;
  192. Transform hipR = meido.Maid.body0.GetBone("Hip_R");
  193. Vector3 vecHipR = hipR.eulerAngles;
  194. hip.rotation = Quaternion.Euler(
  195. 360f - (vecHip.x + 270f) - 270f, 360f - (vecHip.y + 90f) - 90f, 360f - vecHip.z
  196. );
  197. hipL.rotation = FlipRotation(vecHipR);
  198. hipR.rotation = FlipRotation(vecHipL);
  199. for (int i = 0; i < single.Length; i++)
  200. {
  201. Bone bone = single[i];
  202. BoneTransform[bone].rotation = FlipRotation(singleRotations[i]);
  203. }
  204. for (int i = 0; i < pair.Length; i += 2)
  205. {
  206. Bone boneA = pair[i];
  207. Bone boneB = pair[i + 1];
  208. BoneTransform[boneA].rotation = FlipRotation(pairRotations[i + 1]);
  209. BoneTransform[boneB].rotation = FlipRotation(pairRotations[i]);
  210. }
  211. byte[] leftHand = SerializeHand(right: false);
  212. byte[] rightHand = SerializeHand(right: true);
  213. DeserializeHand(leftHand, right: true, true);
  214. DeserializeHand(rightHand, right: false, true);
  215. leftHand = SerializeFoot(right: false);
  216. rightHand = SerializeFoot(right: true);
  217. DeserializeFoot(leftHand, right: true, true);
  218. DeserializeFoot(rightHand, right: false, true);
  219. }
  220. private Quaternion FlipRotation(Vector3 rotation)
  221. {
  222. return Quaternion.Euler(360f - rotation.x, 360f - (rotation.y + 90f) - 90f, rotation.z);
  223. }
  224. public byte[] SerializeHand(bool right)
  225. {
  226. Bone start = right ? Bone.Finger0R : Bone.Finger0L;
  227. Bone end = right ? Bone.Finger4R : Bone.Finger4L;
  228. return SerializeFinger(start, end, right);
  229. }
  230. public void DeserializeHand(byte[] handBinary, bool right, bool mirroring = false)
  231. {
  232. Bone start = right ? Bone.Finger0R : Bone.Finger0L;
  233. Bone end = right ? Bone.Finger4R : Bone.Finger4L;
  234. DeserializeFinger(start, end, handBinary, right, mirroring);
  235. }
  236. public byte[] SerializeFoot(bool right)
  237. {
  238. Bone start = right ? Bone.Toe0R : Bone.Toe0L;
  239. Bone end = right ? Bone.Toe2R : Bone.Toe2L;
  240. return SerializeFinger(start, end, right);
  241. }
  242. public void DeserializeFoot(byte[] footBinary, bool right, bool mirroring = false)
  243. {
  244. Bone start = right ? Bone.Toe0R : Bone.Toe0L;
  245. Bone end = right ? Bone.Toe2R : Bone.Toe2L;
  246. DeserializeFinger(start, end, footBinary, right, mirroring);
  247. }
  248. private byte[] SerializeFinger(Bone start, Bone end, bool right)
  249. {
  250. int joints = BoneTransform[start].name.Split(' ')[2].StartsWith("Finger") ? 4 : 3;
  251. byte[] buf;
  252. using (MemoryStream memoryStream = new MemoryStream())
  253. using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream))
  254. {
  255. for (Bone bone = start; bone <= end; bone += joints)
  256. {
  257. for (int i = 0; i < joints - 1; i++)
  258. {
  259. binaryWriter.WriteQuaternion(BoneTransform[bone + i].localRotation);
  260. }
  261. }
  262. buf = memoryStream.ToArray();
  263. }
  264. return buf;
  265. }
  266. private void DeserializeFinger(Bone start, Bone end, byte[] fingerBinary, bool right, bool mirroring = false)
  267. {
  268. int joints = BoneTransform[start].name.Split(' ')[2].StartsWith("Finger") ? 4 : 3;
  269. int mirror = mirroring ? -1 : 1;
  270. using (MemoryStream memoryStream = new MemoryStream(fingerBinary))
  271. using (BinaryReader binaryReader = new BinaryReader(memoryStream))
  272. {
  273. for (Bone bone = start; bone <= end; bone += joints)
  274. {
  275. for (int i = 0; i < joints - 1; i++)
  276. {
  277. BoneTransform[bone + i].localRotation = new Quaternion
  278. (
  279. binaryReader.ReadSingle() * mirror,
  280. binaryReader.ReadSingle() * mirror,
  281. binaryReader.ReadSingle(),
  282. binaryReader.ReadSingle()
  283. );
  284. }
  285. }
  286. }
  287. }
  288. public void Destroy()
  289. {
  290. foreach (DragPointMeido dragPoint in DragPoints.Values)
  291. {
  292. if (dragPoint != null)
  293. {
  294. GameObject.Destroy(dragPoint.gameObject);
  295. }
  296. }
  297. if (dragCube != null) GameObject.Destroy(dragCube.gameObject);
  298. if (dragBody != null) GameObject.Destroy(dragBody.gameObject);
  299. BoneTransform.Clear();
  300. DragPoints.Clear();
  301. CubeActiveChange -= OnCubeActive;
  302. CubeSmallChange -= OnCubeSmall;
  303. initialized = false;
  304. }
  305. public void Initialize()
  306. {
  307. if (initialized) return;
  308. initialized = true;
  309. CubeActiveChange += OnCubeActive;
  310. CubeSmallChange += OnCubeSmall;
  311. InitializeBones();
  312. InitializeDragPoints();
  313. }
  314. private void InitializeDragPoints()
  315. {
  316. dragCube = DragPoint.Make<DragPointBody>(
  317. PrimitiveType.Cube, Vector3.one * 0.12f, DragPoint.Blue
  318. );
  319. dragCube.Initialize(() => meido.Maid.transform.position, () => Vector3.zero);
  320. dragCube.Set(meido.Maid.transform);
  321. dragCube.IsCube = true;
  322. dragCube.ConstantScale = true;
  323. dragCube.Select += OnSelectBody;
  324. dragCube.EndScale += OnSetDragPointScale;
  325. dragCube.gameObject.SetActive(CubeActive);
  326. dragBody = DragPoint.Make<DragPointBody>(
  327. PrimitiveType.Capsule, new Vector3(0.2f, 0.3f, 0.24f), DragPoint.LightBlue
  328. );
  329. dragBody.Initialize(
  330. () => new Vector3(
  331. (BoneTransform[Bone.Hip].position.x + BoneTransform[Bone.Spine0a].position.x) / 2f,
  332. (BoneTransform[Bone.Spine1].position.y + BoneTransform[Bone.Spine0a].position.y) / 2f,
  333. (BoneTransform[Bone.Spine0a].position.z + BoneTransform[Bone.Hip].position.z) / 2f
  334. ),
  335. () => new Vector3(
  336. BoneTransform[Bone.Spine0a].eulerAngles.x,
  337. BoneTransform[Bone.Spine0a].eulerAngles.y,
  338. BoneTransform[Bone.Spine0a].eulerAngles.z + 90f
  339. )
  340. );
  341. dragBody.Set(meido.Maid.transform);
  342. dragBody.Select += OnSelectBody;
  343. dragBody.EndScale += OnSetDragPointScale;
  344. // Neck Dragpoint
  345. DragPointHead dragNeck = DragPoint.Make<DragPointHead>(
  346. PrimitiveType.Sphere, new Vector3(0.2f, 0.24f, 0.2f), DragPoint.LightBlue
  347. );
  348. dragNeck.Initialize(meido,
  349. () => new Vector3(
  350. BoneTransform[Bone.Head].position.x,
  351. (BoneTransform[Bone.Head].position.y * 1.2f + BoneTransform[Bone.HeadNub].position.y * 0.8f) / 2f,
  352. BoneTransform[Bone.Head].position.z
  353. ),
  354. () => new Vector3(
  355. BoneTransform[Bone.Head].eulerAngles.x,
  356. BoneTransform[Bone.Head].eulerAngles.y,
  357. BoneTransform[Bone.Head].eulerAngles.z + 90f
  358. )
  359. );
  360. dragNeck.Set(BoneTransform[Bone.Neck]);
  361. dragNeck.Select += OnSelectFace;
  362. DragPoints[Bone.Head] = dragNeck;
  363. // Head Dragpoint
  364. DragPointSpine dragHead = DragPoint.Make<DragPointSpine>(
  365. PrimitiveType.Sphere, Vector3.one * 0.045f, DragPoint.LightBlue
  366. );
  367. dragHead.Initialize(meido, () => BoneTransform[Bone.Head].position, () => Vector3.zero);
  368. dragHead.Set(BoneTransform[Bone.Head]);
  369. dragHead.AddGizmo();
  370. DragPoints[Bone.HeadNub] = dragHead;
  371. // Torso Dragpoint
  372. Transform spineTrans1 = BoneTransform[Bone.Spine1];
  373. Transform spineTrans2 = BoneTransform[Bone.Spine1a];
  374. DragPointTorso dragTorso = DragPoint.Make<DragPointTorso>(
  375. PrimitiveType.Capsule, new Vector3(0.2f, 0.19f, 0.24f), DragPoint.LightBlue
  376. );
  377. dragTorso.Initialize(meido,
  378. () => new Vector3(
  379. spineTrans1.position.x,
  380. spineTrans2.position.y,
  381. spineTrans1.position.z - 0.05f
  382. ),
  383. () => new Vector3(
  384. spineTrans1.eulerAngles.x,
  385. spineTrans1.eulerAngles.y,
  386. spineTrans1.eulerAngles.z + 90f
  387. )
  388. );
  389. dragTorso.Set(BoneTransform[Bone.Spine1a]);
  390. DragPoints[Bone.Torso] = dragTorso;
  391. // Pelvis Dragpoint
  392. Transform pelvisTrans = BoneTransform[Bone.Pelvis];
  393. Transform spineTrans = BoneTransform[Bone.Spine];
  394. DragPointPelvis dragPelvis = DragPoint.Make<DragPointPelvis>(
  395. PrimitiveType.Capsule, new Vector3(0.2f, 0.15f, 0.24f), DragPoint.LightBlue
  396. );
  397. dragPelvis.Initialize(meido,
  398. () => new Vector3(
  399. pelvisTrans.position.x,
  400. (pelvisTrans.position.y + spineTrans.position.y) / 2f,
  401. pelvisTrans.position.z
  402. ),
  403. () => new Vector3(
  404. pelvisTrans.eulerAngles.x + 90f,
  405. pelvisTrans.eulerAngles.y + 90f,
  406. pelvisTrans.eulerAngles.z
  407. )
  408. );
  409. dragPelvis.Set(BoneTransform[Bone.Pelvis]);
  410. DragPoints[Bone.Pelvis] = dragPelvis;
  411. InitializeMuneDragPoint(left: true);
  412. InitializeMuneDragPoint(left: false);
  413. DragPointChain[] armDragPointL = MakeIKChain(BoneTransform[Bone.HandL]);
  414. DragPoints[Bone.UpperArmL] = armDragPointL[0];
  415. DragPoints[Bone.ForearmL] = armDragPointL[1];
  416. DragPoints[Bone.HandL] = armDragPointL[2];
  417. DragPointChain[] armDragPointR = MakeIKChain(BoneTransform[Bone.HandR]);
  418. DragPoints[Bone.UpperArmR] = armDragPointR[0];
  419. DragPoints[Bone.ForearmR] = armDragPointR[1];
  420. DragPoints[Bone.HandR] = armDragPointR[2];
  421. DragPointChain[] legDragPointL = MakeIKChain(BoneTransform[Bone.FootL]);
  422. DragPoints[Bone.CalfL] = legDragPointL[0];
  423. DragPoints[Bone.FootL] = legDragPointL[1];
  424. DragPointChain[] legDragPointR = MakeIKChain(BoneTransform[Bone.FootR]);
  425. DragPoints[Bone.CalfR] = legDragPointR[0];
  426. DragPoints[Bone.FootR] = legDragPointR[1];
  427. InitializeSpineDragPoint(
  428. Bone.Neck, Bone.Spine, Bone.Spine0a, Bone.Spine1, Bone.Spine1a, Bone.Hip, Bone.ThighL, Bone.ThighR
  429. );
  430. InitializeFingerDragPoint(Bone.Finger0L, Bone.Finger4R);
  431. InitializeFingerDragPoint(Bone.Toe0L, Bone.Toe2R);
  432. }
  433. private void InitializeMuneDragPoint(bool left)
  434. {
  435. Bone mune = left ? Bone.MuneL : Bone.MuneR;
  436. Bone sub = left ? Bone.MuneSubL : Bone.MuneSubR;
  437. DragPointChain muneDragPoint = DragPoint.Make<DragPointChain>(
  438. PrimitiveType.Sphere, Vector3.one * 0.12f, DragPoint.LightBlue
  439. );
  440. muneDragPoint.Initialize(meido,
  441. () => (BoneTransform[mune].position + BoneTransform[sub].position) / 2f,
  442. () => Vector3.zero
  443. );
  444. muneDragPoint.Set(BoneTransform[sub]);
  445. DragPoints[mune] = muneDragPoint;
  446. }
  447. private DragPointChain[] MakeIKChain(Transform lower)
  448. {
  449. Vector3 limbDragPointSize = Vector3.one * 0.12f;
  450. // Ignore Thigh transform when making a leg IK chain
  451. bool isLeg = lower.name.EndsWith("Foot");
  452. DragPointChain[] dragPoints = new DragPointChain[isLeg ? 2 : 3];
  453. for (int i = dragPoints.Length - 1; i >= 0; i--)
  454. {
  455. Transform joint = lower;
  456. dragPoints[i] = DragPoint.Make<DragPointChain>(
  457. PrimitiveType.Sphere, limbDragPointSize, DragPoint.LightBlue
  458. );
  459. dragPoints[i].Initialize(meido, () => joint.position, () => Vector3.zero);
  460. dragPoints[i].Set(joint);
  461. dragPoints[i].AddGizmo();
  462. lower = lower.parent;
  463. }
  464. return dragPoints;
  465. }
  466. private void InitializeFingerDragPoint(Bone start, Bone end)
  467. {
  468. Vector3 fingerDragPointSize = Vector3.one * 0.015f;
  469. int joints = BoneTransform[start].name.Split(' ')[2].StartsWith("Finger") ? 4 : 3;
  470. for (Bone bone = start; bone <= end; bone += joints)
  471. {
  472. for (int i = 1; i < joints; i++)
  473. {
  474. Transform trans = BoneTransform[bone + i];
  475. DragPointFinger chain = DragPoint.Make<DragPointFinger>(
  476. PrimitiveType.Sphere, fingerDragPointSize, DragPoint.Blue
  477. );
  478. chain.Initialize(meido, () => trans.position, () => Vector3.zero);
  479. chain.Set(trans);
  480. DragPoints[bone + i] = chain;
  481. }
  482. }
  483. }
  484. private void InitializeSpineDragPoint(params Bone[] bones)
  485. {
  486. Vector3 spineDragPointSize = Vector3.one * 0.045f;
  487. foreach (Bone bone in bones)
  488. {
  489. Transform spine = BoneTransform[bone];
  490. PrimitiveType primitive = bone == Bone.Hip ? PrimitiveType.Cube : PrimitiveType.Sphere;
  491. DragPointSpine dragPoint = DragPoint.Make<DragPointSpine>(
  492. primitive, spineDragPointSize, DragPoint.LightBlue
  493. );
  494. dragPoint.Initialize(meido,
  495. () => spine.position,
  496. () => Vector3.zero
  497. );
  498. dragPoint.Set(spine);
  499. dragPoint.AddGizmo();
  500. DragPoints[bone] = dragPoint;
  501. }
  502. }
  503. private void OnCubeActive(object sender, EventArgs args)
  504. {
  505. dragCube.gameObject.SetActive(CubeActive);
  506. }
  507. private void OnCubeSmall(object sender, EventArgs args)
  508. {
  509. dragCube.DragPointScale = CubeSmall ? DragPointGeneral.smallCube : 1f;
  510. }
  511. private void OnSetDragPointScale(object sender, EventArgs args)
  512. {
  513. this.SetDragPointScale(meido.Maid.transform.localScale.x);
  514. }
  515. private void OnSelectBody(object sender, EventArgs args)
  516. {
  517. SelectMaid?.Invoke(this, new MeidoUpdateEventArgs(meido.Slot, fromMaid: true, isBody: true));
  518. }
  519. private void OnSelectFace(object sender, EventArgs args)
  520. {
  521. SelectMaid?.Invoke(this, new MeidoUpdateEventArgs(meido.Slot, fromMaid: true, isBody: false));
  522. }
  523. private void SetDragPointScale(float scale)
  524. {
  525. foreach (DragPointMeido dragPoint in DragPoints.Values)
  526. {
  527. dragPoint.DragPointScale = scale;
  528. }
  529. dragBody.DragPointScale = scale;
  530. }
  531. private void InitializeBones()
  532. {
  533. // TODO: Move to external file somehow
  534. Transform transform = meido.Body.m_Bones.transform;
  535. BoneTransform = new Dictionary<Bone, Transform>()
  536. {
  537. [Bone.Head] = CMT.SearchObjName(transform, "Bip01 Head"),
  538. [Bone.Neck] = CMT.SearchObjName(transform, "Bip01 Neck"),
  539. [Bone.HeadNub] = CMT.SearchObjName(transform, "Bip01 HeadNub"),
  540. /*[Bone.IKHandL] = CMT.SearchObjName(transform, "_IK_handL"),
  541. [Bone.IKHandR] = CMT.SearchObjName(transform, "_IK_handR"),*/
  542. [Bone.MuneL] = CMT.SearchObjName(transform, "Mune_L"),
  543. [Bone.MuneSubL] = CMT.SearchObjName(transform, "Mune_L_sub"),
  544. [Bone.MuneR] = CMT.SearchObjName(transform, "Mune_R"),
  545. [Bone.MuneSubR] = CMT.SearchObjName(transform, "Mune_R_sub"),
  546. [Bone.Pelvis] = CMT.SearchObjName(transform, "Bip01 Pelvis"),
  547. [Bone.Hip] = CMT.SearchObjName(transform, "Bip01"),
  548. [Bone.Spine] = CMT.SearchObjName(transform, "Bip01 Spine"),
  549. [Bone.Spine0a] = CMT.SearchObjName(transform, "Bip01 Spine0a"),
  550. [Bone.Spine1] = CMT.SearchObjName(transform, "Bip01 Spine1"),
  551. [Bone.Spine1a] = CMT.SearchObjName(transform, "Bip01 Spine1a"),
  552. [Bone.ClavicleL] = CMT.SearchObjName(transform, "Bip01 L Clavicle"),
  553. [Bone.ClavicleR] = CMT.SearchObjName(transform, "Bip01 R Clavicle"),
  554. [Bone.UpperArmL] = CMT.SearchObjName(transform, "Bip01 L UpperArm"),
  555. [Bone.ForearmL] = CMT.SearchObjName(transform, "Bip01 L Forearm"),
  556. [Bone.HandL] = CMT.SearchObjName(transform, "Bip01 L Hand"),
  557. [Bone.UpperArmR] = CMT.SearchObjName(transform, "Bip01 R UpperArm"),
  558. [Bone.ForearmR] = CMT.SearchObjName(transform, "Bip01 R Forearm"),
  559. [Bone.HandR] = CMT.SearchObjName(transform, "Bip01 R Hand"),
  560. [Bone.ThighL] = CMT.SearchObjName(transform, "Bip01 L Thigh"),
  561. [Bone.CalfL] = CMT.SearchObjName(transform, "Bip01 L Calf"),
  562. [Bone.FootL] = CMT.SearchObjName(transform, "Bip01 L Foot"),
  563. [Bone.ThighR] = CMT.SearchObjName(transform, "Bip01 R Thigh"),
  564. [Bone.CalfR] = CMT.SearchObjName(transform, "Bip01 R Calf"),
  565. [Bone.FootR] = CMT.SearchObjName(transform, "Bip01 R Foot"),
  566. // fingers
  567. [Bone.Finger0L] = CMT.SearchObjName(transform, "Bip01 L Finger0"),
  568. [Bone.Finger01L] = CMT.SearchObjName(transform, "Bip01 L Finger01"),
  569. [Bone.Finger02L] = CMT.SearchObjName(transform, "Bip01 L Finger02"),
  570. [Bone.Finger0NubL] = CMT.SearchObjName(transform, "Bip01 L Finger0Nub"),
  571. [Bone.Finger1L] = CMT.SearchObjName(transform, "Bip01 L Finger1"),
  572. [Bone.Finger11L] = CMT.SearchObjName(transform, "Bip01 L Finger11"),
  573. [Bone.Finger12L] = CMT.SearchObjName(transform, "Bip01 L Finger12"),
  574. [Bone.Finger1NubL] = CMT.SearchObjName(transform, "Bip01 L Finger1Nub"),
  575. [Bone.Finger2L] = CMT.SearchObjName(transform, "Bip01 L Finger2"),
  576. [Bone.Finger21L] = CMT.SearchObjName(transform, "Bip01 L Finger21"),
  577. [Bone.Finger22L] = CMT.SearchObjName(transform, "Bip01 L Finger22"),
  578. [Bone.Finger2NubL] = CMT.SearchObjName(transform, "Bip01 L Finger2Nub"),
  579. [Bone.Finger3L] = CMT.SearchObjName(transform, "Bip01 L Finger3"),
  580. [Bone.Finger31L] = CMT.SearchObjName(transform, "Bip01 L Finger31"),
  581. [Bone.Finger32L] = CMT.SearchObjName(transform, "Bip01 L Finger32"),
  582. [Bone.Finger3NubL] = CMT.SearchObjName(transform, "Bip01 L Finger3Nub"),
  583. [Bone.Finger4L] = CMT.SearchObjName(transform, "Bip01 L Finger4"),
  584. [Bone.Finger41L] = CMT.SearchObjName(transform, "Bip01 L Finger41"),
  585. [Bone.Finger42L] = CMT.SearchObjName(transform, "Bip01 L Finger42"),
  586. [Bone.Finger4NubL] = CMT.SearchObjName(transform, "Bip01 L Finger4Nub"),
  587. [Bone.Finger0R] = CMT.SearchObjName(transform, "Bip01 R Finger0"),
  588. [Bone.Finger01R] = CMT.SearchObjName(transform, "Bip01 R Finger01"),
  589. [Bone.Finger02R] = CMT.SearchObjName(transform, "Bip01 R Finger02"),
  590. [Bone.Finger0NubR] = CMT.SearchObjName(transform, "Bip01 R Finger0Nub"),
  591. [Bone.Finger1R] = CMT.SearchObjName(transform, "Bip01 R Finger1"),
  592. [Bone.Finger11R] = CMT.SearchObjName(transform, "Bip01 R Finger11"),
  593. [Bone.Finger12R] = CMT.SearchObjName(transform, "Bip01 R Finger12"),
  594. [Bone.Finger1NubR] = CMT.SearchObjName(transform, "Bip01 R Finger1Nub"),
  595. [Bone.Finger2R] = CMT.SearchObjName(transform, "Bip01 R Finger2"),
  596. [Bone.Finger21R] = CMT.SearchObjName(transform, "Bip01 R Finger21"),
  597. [Bone.Finger22R] = CMT.SearchObjName(transform, "Bip01 R Finger22"),
  598. [Bone.Finger2NubR] = CMT.SearchObjName(transform, "Bip01 R Finger2Nub"),
  599. [Bone.Finger3R] = CMT.SearchObjName(transform, "Bip01 R Finger3"),
  600. [Bone.Finger31R] = CMT.SearchObjName(transform, "Bip01 R Finger31"),
  601. [Bone.Finger32R] = CMT.SearchObjName(transform, "Bip01 R Finger32"),
  602. [Bone.Finger3NubR] = CMT.SearchObjName(transform, "Bip01 R Finger3Nub"),
  603. [Bone.Finger4R] = CMT.SearchObjName(transform, "Bip01 R Finger4"),
  604. [Bone.Finger41R] = CMT.SearchObjName(transform, "Bip01 R Finger41"),
  605. [Bone.Finger42R] = CMT.SearchObjName(transform, "Bip01 R Finger42"),
  606. [Bone.Finger4NubR] = CMT.SearchObjName(transform, "Bip01 R Finger4Nub"),
  607. // Toes
  608. [Bone.Toe0L] = CMT.SearchObjName(transform, "Bip01 L Toe0"),
  609. [Bone.Toe01L] = CMT.SearchObjName(transform, "Bip01 L Toe01"),
  610. [Bone.Toe0NubL] = CMT.SearchObjName(transform, "Bip01 L Toe0Nub"),
  611. [Bone.Toe1L] = CMT.SearchObjName(transform, "Bip01 L Toe1"),
  612. [Bone.Toe11L] = CMT.SearchObjName(transform, "Bip01 L Toe11"),
  613. [Bone.Toe1NubL] = CMT.SearchObjName(transform, "Bip01 L Toe1Nub"),
  614. [Bone.Toe2L] = CMT.SearchObjName(transform, "Bip01 L Toe2"),
  615. [Bone.Toe21L] = CMT.SearchObjName(transform, "Bip01 L Toe21"),
  616. [Bone.Toe2NubL] = CMT.SearchObjName(transform, "Bip01 L Toe2Nub"),
  617. [Bone.Toe0R] = CMT.SearchObjName(transform, "Bip01 R Toe0"),
  618. [Bone.Toe01R] = CMT.SearchObjName(transform, "Bip01 R Toe01"),
  619. [Bone.Toe0NubR] = CMT.SearchObjName(transform, "Bip01 R Toe0Nub"),
  620. [Bone.Toe1R] = CMT.SearchObjName(transform, "Bip01 R Toe1"),
  621. [Bone.Toe11R] = CMT.SearchObjName(transform, "Bip01 R Toe11"),
  622. [Bone.Toe1NubR] = CMT.SearchObjName(transform, "Bip01 R Toe1Nub"),
  623. [Bone.Toe2R] = CMT.SearchObjName(transform, "Bip01 R Toe2"),
  624. [Bone.Toe21R] = CMT.SearchObjName(transform, "Bip01 R Toe21"),
  625. [Bone.Toe2NubR] = CMT.SearchObjName(transform, "Bip01 R Toe2Nub")
  626. };
  627. }
  628. }
  629. internal struct AttachPointInfo
  630. {
  631. public AttachPoint AttachPoint { get; }
  632. public string MaidGuid { get; }
  633. public int MaidIndex { get; }
  634. public static AttachPointInfo Empty => new AttachPointInfo(AttachPoint.None, String.Empty, -1);
  635. public AttachPointInfo(AttachPoint attachPoint, string maidGuid, int maidIndex)
  636. {
  637. this.AttachPoint = attachPoint;
  638. this.MaidGuid = maidGuid;
  639. this.MaidIndex = maidIndex;
  640. }
  641. public void Serialize(BinaryWriter binaryWriter)
  642. {
  643. binaryWriter.Write((int)AttachPoint);
  644. binaryWriter.Write(MaidIndex);
  645. }
  646. public static AttachPointInfo Deserialize(BinaryReader binaryReader)
  647. {
  648. return new AttachPointInfo(
  649. (AttachPoint)binaryReader.ReadInt32(),
  650. String.Empty,
  651. binaryReader.ReadInt32()
  652. );
  653. }
  654. }
  655. }