DragPointChain.cs 5.0 KB

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