DragPointChain.cs 4.7 KB

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