MeidoDragPointManager.cs 31 KB

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