DragPointChain.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. // TODO: All the dragpoints
  39. DragType current = CurrentDragType;
  40. bool isBone = IsBone;
  41. if (CurrentDragType == DragType.Ignore) ApplyProperties();
  42. else if (current == DragType.RotLocalXZ)
  43. {
  44. if (isLower) ApplyProperties(!isBone, false, isBone);
  45. else ApplyProperties();
  46. }
  47. else if (current == DragType.RotLocalY)
  48. {
  49. if (isLower || isMiddle) ApplyProperties(!isBone, false, false);
  50. else if (isUpper) ApplyProperties(false, false, isBone);
  51. else ApplyProperties();
  52. }
  53. else if (current == DragType.RotY)
  54. {
  55. if (isMune) ApplyProperties(true, false, false);
  56. else if (isMiddle) ApplyProperties(false, false, isBone);
  57. else ApplyProperties();
  58. }
  59. else if (current == DragType.MoveXZ)
  60. {
  61. if (isLower) ApplyProperties(true, isBone, false);
  62. else ApplyProperties();
  63. }
  64. else ApplyProperties(!isMune, (isBone && !isMune), false);
  65. }
  66. protected override void UpdateDragType()
  67. {
  68. bool control = Utility.GetModKey(Utility.ModKey.Control);
  69. bool alt = Utility.GetModKey(Utility.ModKey.Alt);
  70. if (Input.GetKey(KeyCode.Space) || OtherDragType())
  71. {
  72. CurrentDragType = DragType.Ignore;
  73. }
  74. else if (control && alt)
  75. {
  76. // mune
  77. CurrentDragType = DragType.RotY;
  78. }
  79. else if (control)
  80. {
  81. CurrentDragType = DragType.MoveXZ;
  82. }
  83. else if (alt)
  84. {
  85. bool shift = Utility.GetModKey(Utility.ModKey.Shift);
  86. CurrentDragType = shift ? DragType.RotLocalY : DragType.RotLocalXZ;
  87. }
  88. else
  89. {
  90. CurrentDragType = DragType.None;
  91. }
  92. }
  93. protected override void OnMouseDown()
  94. {
  95. base.OnMouseDown();
  96. if (isMune) meido.SetMune(true);
  97. InitializeRotation();
  98. InitializeIK(IK, ikChain[jointUpper], ikChain[jointMiddle], ikChain[jointLower]);
  99. }
  100. protected override void OnDoubleClick()
  101. {
  102. if (isMune && CurrentDragType == DragType.RotY) meido.SetMune();
  103. }
  104. protected override void Drag()
  105. {
  106. if (isPlaying) meido.Stop = true;
  107. bool altRotation = CurrentDragType == DragType.MoveXZ || CurrentDragType == DragType.RotY;
  108. if ((CurrentDragType == DragType.None) || altRotation)
  109. {
  110. int upperJoint = altRotation ? jointMiddle : jointUpper;
  111. Porc(IK, ikChain[upperJoint], ikChain[jointMiddle], ikChain[jointLower]);
  112. InitializeRotation();
  113. }
  114. Vector3 mouseDelta = MouseDelta();
  115. if (CurrentDragType == DragType.RotLocalY)
  116. {
  117. int joint = this.isMiddle ? jointUpper : jointLower;
  118. ikChain[joint].localRotation = jointRotation[joint];
  119. ikChain[joint].Rotate(Vector3.right * (-mouseDelta.x / 1.5f));
  120. }
  121. if (CurrentDragType == DragType.RotLocalXZ)
  122. {
  123. ikChain[jointLower].localRotation = jointRotation[jointLower];
  124. ikChain[jointLower].Rotate(Vector3.up * (foot * mouseDelta.x / 1.5f));
  125. ikChain[jointLower].Rotate(Vector3.forward * (foot * mouseDelta.y / 1.5f));
  126. }
  127. }
  128. }
  129. }