DragPointFinger.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 readonly Quaternion[] jointRotation = new Quaternion[2];
  9. private Transform[] ikChain;
  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 myObject)
  13. {
  14. base.Set(myObject);
  15. string 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 == 7) || (parentName.Length == 4);
  18. ikChain = new Transform[2] {
  19. myObject.parent,
  20. myObject
  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. CurrentDragType = Input.GetKey(MpsKey.DragFinger)
  39. ? Input.Shift
  40. ? DragType.RotLocalY
  41. : DragType.MoveXZ
  42. : DragType.None;
  43. }
  44. protected override void OnMouseDown()
  45. {
  46. base.OnMouseDown();
  47. jointRotation[jointUpper] = ikChain[jointUpper].localRotation;
  48. jointRotation[jointMiddle] = ikChain[jointMiddle].localRotation;
  49. InitializeIK(IK, ikChain[jointUpper], ikChain[jointUpper], ikChain[jointMiddle]);
  50. }
  51. protected override void Drag()
  52. {
  53. if (isPlaying) meido.Stop = true;
  54. if (CurrentDragType == DragType.MoveXZ)
  55. {
  56. Porc(IK, ikChain[jointUpper], ikChain[jointUpper], ikChain[jointMiddle]);
  57. if (!baseFinger)
  58. {
  59. SetRotation(jointUpper);
  60. SetRotation(jointMiddle);
  61. }
  62. else
  63. {
  64. jointRotation[jointUpper] = ikChain[jointUpper].localRotation;
  65. jointRotation[jointMiddle] = ikChain[jointMiddle].localRotation;
  66. }
  67. }
  68. else if (CurrentDragType == DragType.RotLocalY)
  69. {
  70. Vector3 mouseDelta = MouseDelta();
  71. ikChain[jointUpper].localRotation = jointRotation[jointUpper];
  72. ikChain[jointUpper].Rotate(Vector3.right * (mouseDelta.x / 1.5f));
  73. }
  74. }
  75. }
  76. }