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