DragPointChain.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using UnityEngine;
  2. namespace COM3D2.MeidoPhotoStudio.Plugin
  3. {
  4. using Input = InputManager;
  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. 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 = Input.Control;
  69. bool alt = Input.Alt;
  70. if (control && alt)
  71. {
  72. // mune
  73. CurrentDragType = DragType.RotY;
  74. }
  75. else if (control)
  76. {
  77. CurrentDragType = DragType.MoveXZ;
  78. }
  79. else if (alt)
  80. {
  81. CurrentDragType = Input.Shift ? DragType.RotLocalY : DragType.RotLocalXZ;
  82. }
  83. else
  84. {
  85. CurrentDragType = OtherDragType() ? DragType.Ignore : DragType.None;
  86. }
  87. }
  88. protected override void OnMouseDown()
  89. {
  90. base.OnMouseDown();
  91. if (isMune) meido.SetMune(true);
  92. InitializeRotation();
  93. InitializeIK(IK, ikChain[jointUpper], ikChain[jointMiddle], ikChain[jointLower]);
  94. }
  95. protected override void OnDoubleClick()
  96. {
  97. if (isMune && CurrentDragType == DragType.RotY) meido.SetMune();
  98. }
  99. protected override void Drag()
  100. {
  101. if (isPlaying) meido.Stop = true;
  102. bool altRotation = CurrentDragType == DragType.MoveXZ || CurrentDragType == DragType.RotY;
  103. if ((CurrentDragType == DragType.None) || altRotation)
  104. {
  105. int upperJoint = altRotation ? jointMiddle : jointUpper;
  106. Porc(IK, ikChain[upperJoint], ikChain[jointMiddle], ikChain[jointLower]);
  107. InitializeRotation();
  108. }
  109. Vector3 mouseDelta = MouseDelta();
  110. if (CurrentDragType == DragType.RotLocalY)
  111. {
  112. int joint = this.isMiddle ? jointUpper : jointLower;
  113. ikChain[joint].localRotation = jointRotation[joint];
  114. ikChain[joint].Rotate(Vector3.right * (-mouseDelta.x / 1.5f));
  115. }
  116. if (CurrentDragType == DragType.RotLocalXZ)
  117. {
  118. ikChain[jointLower].localRotation = jointRotation[jointLower];
  119. ikChain[jointLower].Rotate(Vector3.up * (foot * mouseDelta.x / 1.5f));
  120. ikChain[jointLower].Rotate(Vector3.forward * (foot * mouseDelta.y / 1.5f));
  121. }
  122. }
  123. }
  124. }