DragPointFinger.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. private static readonly Color dragpointColour = new Color(0.1f, 0.4f, 0.95f, defaultAlpha);
  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) ApplyProperties(true, true, false);
  31. else if (CurrentDragType == DragType.MoveXZ) ApplyProperties(true, true, false);
  32. else ApplyProperties(false, false, false);
  33. ApplyColour(dragpointColour);
  34. }
  35. protected override void UpdateDragType()
  36. {
  37. if (Input.GetKey(KeyCode.Space))
  38. {
  39. CurrentDragType = Utility.GetModKey(Utility.ModKey.Shift)
  40. ? DragType.RotLocalY
  41. : DragType.MoveXZ;
  42. }
  43. else CurrentDragType = DragType.None;
  44. }
  45. protected override void OnMouseDown()
  46. {
  47. base.OnMouseDown();
  48. jointRotation[jointUpper] = ikChain[jointUpper].localRotation;
  49. jointRotation[jointMiddle] = ikChain[jointMiddle].localRotation;
  50. InitializeIK(IK, ikChain[jointUpper], ikChain[jointUpper], ikChain[jointMiddle]);
  51. }
  52. protected override void Drag()
  53. {
  54. if (isPlaying) meido.Stop = true;
  55. if (CurrentDragType == DragType.MoveXZ)
  56. {
  57. Porc(IK, ikChain[jointUpper], ikChain[jointUpper], ikChain[jointMiddle]);
  58. if (!baseFinger)
  59. {
  60. SetRotation(jointUpper);
  61. SetRotation(jointMiddle);
  62. }
  63. else
  64. {
  65. jointRotation[jointUpper] = ikChain[jointUpper].localRotation;
  66. jointRotation[jointMiddle] = ikChain[jointMiddle].localRotation;
  67. }
  68. }
  69. else if (CurrentDragType == DragType.RotLocalY)
  70. {
  71. Vector3 mouseDelta = MouseDelta();
  72. ikChain[jointUpper].localRotation = jointRotation[jointUpper];
  73. ikChain[jointUpper].Rotate(Vector3.right * (mouseDelta.x / 1.5f));
  74. }
  75. }
  76. }
  77. }