DragPointFinger.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using UnityEngine;
  2. using Input = MeidoPhotoStudio.Plugin.InputManager;
  3. namespace MeidoPhotoStudio.Plugin;
  4. public class DragPointFinger : DragPointMeido
  5. {
  6. private static readonly Color DragpointColour = new(0.1f, 0.4f, 0.95f, DefaultAlpha);
  7. private readonly TBody.IKCMO ik = new();
  8. private readonly Quaternion[] jointRotation = new Quaternion[2];
  9. private IKCtrlData ikCtrlData;
  10. private Transform[] ikChain;
  11. private bool baseFinger;
  12. public override void Set(Transform myObject)
  13. {
  14. base.Set(myObject);
  15. var parentName = myObject.parent.name.Split(' ')[2];
  16. // Base finger names have the form 'FingerN' or 'ToeN' where N is a natural number
  17. baseFinger = parentName.Length is 7 or 4;
  18. ikChain = new Transform[2] { myObject.parent, myObject };
  19. ikCtrlData = IkCtrlData;
  20. }
  21. protected override void ApplyDragType()
  22. {
  23. if (baseFinger && CurrentDragType is DragType.RotLocalY)
  24. ApplyProperties(true, true, false);
  25. else if (CurrentDragType is DragType.MoveXZ)
  26. ApplyProperties(true, true, false);
  27. else
  28. ApplyProperties(false, false, false);
  29. ApplyColour(DragpointColour);
  30. }
  31. protected override void UpdateDragType() =>
  32. CurrentDragType = !Input.GetKey(MpsKey.DragFinger)
  33. ? DragType.None
  34. : Input.Shift
  35. ? DragType.RotLocalY
  36. : DragType.MoveXZ;
  37. protected override void OnMouseDown()
  38. {
  39. base.OnMouseDown();
  40. jointRotation[JointUpper] = ikChain[JointUpper].localRotation;
  41. jointRotation[JointMiddle] = ikChain[JointMiddle].localRotation;
  42. InitializeIK(ik, ikChain[JointUpper], ikChain[JointUpper], ikChain[JointMiddle]);
  43. }
  44. protected override void Drag()
  45. {
  46. if (isPlaying)
  47. meido.Stop = true;
  48. if (CurrentDragType is DragType.MoveXZ)
  49. {
  50. Porc(ik, ikCtrlData, ikChain[JointUpper], ikChain[JointUpper], ikChain[JointMiddle]);
  51. if (!baseFinger)
  52. {
  53. SetRotation(JointUpper);
  54. SetRotation(JointMiddle);
  55. }
  56. else
  57. {
  58. jointRotation[JointUpper] = ikChain[JointUpper].localRotation;
  59. jointRotation[JointMiddle] = ikChain[JointMiddle].localRotation;
  60. }
  61. }
  62. else if (CurrentDragType is DragType.RotLocalY)
  63. {
  64. var mouseDelta = MouseDelta();
  65. ikChain[JointUpper].localRotation = jointRotation[JointUpper];
  66. ikChain[JointUpper].Rotate(Vector3.right * (mouseDelta.x / 1.5f));
  67. }
  68. }
  69. private void SetRotation(int joint)
  70. {
  71. var rotation = jointRotation[joint].eulerAngles;
  72. rotation.z = ikChain[joint].localEulerAngles.z;
  73. ikChain[joint].localRotation = Quaternion.Euler(rotation);
  74. }
  75. }