DragPointFinger.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using UnityEngine;
  2. namespace COM3D2.MeidoPhotoStudio.Plugin
  3. {
  4. using Input = InputManager;
  5. public class DragPointFinger : DragPointMeido
  6. {
  7. private static readonly Color dragpointColour = new Color(0.1f, 0.4f, 0.95f, defaultAlpha);
  8. private readonly TBody.IKCMO IK = new TBody.IKCMO();
  9. private readonly Quaternion[] jointRotation = new Quaternion[2];
  10. private IKCtrlData ikCtrlData;
  11. private Transform[] ikChain;
  12. private bool baseFinger;
  13. public override void Set(Transform myObject)
  14. {
  15. base.Set(myObject);
  16. string parentName = myObject.parent.name.Split(' ')[2];
  17. // Base finger names have the form 'FingerN' or 'ToeN' where N is a natural number
  18. baseFinger = (parentName.Length == 7) || (parentName.Length == 4);
  19. ikChain = new Transform[2] {
  20. myObject.parent,
  21. myObject
  22. };
  23. ikCtrlData = IkCtrlData;
  24. }
  25. private void SetRotation(int joint)
  26. {
  27. Vector3 rotation = jointRotation[joint].eulerAngles;
  28. rotation.z = ikChain[joint].localEulerAngles.z;
  29. ikChain[joint].localRotation = Quaternion.Euler(rotation);
  30. }
  31. protected override void ApplyDragType()
  32. {
  33. if (baseFinger && CurrentDragType == DragType.RotLocalY) ApplyProperties(true, true, false);
  34. else if (CurrentDragType == DragType.MoveXZ) ApplyProperties(true, true, false);
  35. else ApplyProperties(false, false, false);
  36. ApplyColour(dragpointColour);
  37. }
  38. protected override void UpdateDragType()
  39. {
  40. CurrentDragType = Input.GetKey(MpsKey.DragFinger)
  41. ? Input.Shift
  42. ? DragType.RotLocalY
  43. : DragType.MoveXZ
  44. : 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, ikCtrlData, 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. }