DragPointFinger.cs 3.1 KB

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