DragPointChain.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using UnityEngine;
  2. namespace COM3D2.MeidoPhotoStudio.Plugin
  3. {
  4. internal class DragPointChain : DragPointMeido
  5. {
  6. private readonly TBody.IKCMO IK = new TBody.IKCMO();
  7. private Transform[] ikChain;
  8. private Quaternion[] jointRotation = new Quaternion[3];
  9. private int foot = 1;
  10. private bool isLower = false;
  11. private bool isMiddle = false;
  12. private bool isUpper = false;
  13. private bool isMune = false;
  14. public override void Set(Transform lower)
  15. {
  16. base.Set(lower);
  17. this.isMune = lower.name.StartsWith("Mune");
  18. this.foot = lower.name.EndsWith("Foot") ? -1 : 1;
  19. this.isLower = lower.name.EndsWith("Hand") || foot == -1;
  20. this.isMiddle = lower.name.EndsWith("Calf") || lower.name.EndsWith("Forearm");
  21. this.isUpper = !(isMiddle || isLower) && !isMune;
  22. this.ikChain = new Transform[] {
  23. lower.parent,
  24. lower.parent,
  25. lower
  26. };
  27. if (this.isLower) ikChain[0] = ikChain[0].parent;
  28. }
  29. private void InitializeRotation()
  30. {
  31. for (int i = 0; i < jointRotation.Length; i++)
  32. {
  33. jointRotation[i] = ikChain[i].localRotation;
  34. }
  35. }
  36. protected override void ApplyDragType()
  37. {
  38. DragType current = CurrentDragType;
  39. bool isBone = IsBone;
  40. if (CurrentDragType == DragType.Ignore) ApplyProperties();
  41. else if (current == DragType.RotLocalXZ)
  42. {
  43. if (isLower) ApplyProperties(!isBone, false, isBone);
  44. else ApplyProperties();
  45. }
  46. else if (current == DragType.RotLocalY)
  47. {
  48. if (isLower || isMiddle) ApplyProperties(!isBone, false, false);
  49. else if (isUpper) ApplyProperties(false, false, isBone);
  50. else ApplyProperties();
  51. }
  52. else if (current == DragType.RotY)
  53. {
  54. if (isMune) ApplyProperties(true, false, false);
  55. else if (isMiddle) ApplyProperties(false, false, isBone);
  56. else ApplyProperties();
  57. }
  58. else if (current == DragType.MoveXZ)
  59. {
  60. if (isLower) ApplyProperties(true, isBone, false);
  61. else ApplyProperties();
  62. }
  63. else ApplyProperties(!isMune, (isBone && !isMune), false);
  64. }
  65. protected override void UpdateDragType()
  66. {
  67. bool control = Utility.GetModKey(Utility.ModKey.Control);
  68. bool alt = Utility.GetModKey(Utility.ModKey.Alt);
  69. if (Input.GetKey(KeyCode.Space) || OtherDragType())
  70. {
  71. CurrentDragType = DragType.Ignore;
  72. }
  73. else if (control && alt)
  74. {
  75. // mune
  76. CurrentDragType = DragType.RotY;
  77. }
  78. else if (control)
  79. {
  80. CurrentDragType = DragType.MoveXZ;
  81. }
  82. else if (alt)
  83. {
  84. bool shift = Utility.GetModKey(Utility.ModKey.Shift);
  85. CurrentDragType = shift ? DragType.RotLocalY : DragType.RotLocalXZ;
  86. }
  87. else
  88. {
  89. CurrentDragType = DragType.None;
  90. }
  91. }
  92. protected override void OnMouseDown()
  93. {
  94. base.OnMouseDown();
  95. if (isMune) meido.SetMune(true);
  96. InitializeRotation();
  97. InitializeIK(IK, ikChain[jointUpper], ikChain[jointMiddle], ikChain[jointLower]);
  98. }
  99. protected override void OnDoubleClick()
  100. {
  101. if (isMune && CurrentDragType == DragType.RotY) meido.SetMune();
  102. }
  103. protected override void Drag()
  104. {
  105. if (isPlaying) meido.Stop = true;
  106. bool altRotation = CurrentDragType == DragType.MoveXZ || CurrentDragType == DragType.RotY;
  107. if ((CurrentDragType == DragType.None) || altRotation)
  108. {
  109. int upperJoint = altRotation ? jointMiddle : jointUpper;
  110. Porc(IK, ikChain[upperJoint], ikChain[jointMiddle], ikChain[jointLower]);
  111. InitializeRotation();
  112. }
  113. Vector3 mouseDelta = MouseDelta();
  114. if (CurrentDragType == DragType.RotLocalY)
  115. {
  116. int joint = this.isMiddle ? jointUpper : jointLower;
  117. ikChain[joint].localRotation = jointRotation[joint];
  118. ikChain[joint].Rotate(Vector3.right * (-mouseDelta.x / 1.5f));
  119. }
  120. if (CurrentDragType == DragType.RotLocalXZ)
  121. {
  122. ikChain[jointLower].localRotation = jointRotation[jointLower];
  123. ikChain[jointLower].Rotate(Vector3.up * (foot * mouseDelta.x / 1.5f));
  124. ikChain[jointLower].Rotate(Vector3.forward * (foot * mouseDelta.y / 1.5f));
  125. }
  126. }
  127. }
  128. }