DragPointFinger.cs 3.1 KB

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