DragPointFinger.cs 3.1 KB

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