DragJointFinger.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using UnityEngine;
  3. namespace COM3D2.MeidoPhotoStudio.Plugin
  4. {
  5. public class DragJointFinger : BaseDrag
  6. {
  7. private readonly TBody.IKCMO IK = new TBody.IKCMO();
  8. private readonly GameObject[] otherIK = new GameObject[3];
  9. private Transform[] ikChain;
  10. private Vector3[] jointRotation = new Vector3[2];
  11. private Vector3 off;
  12. private Vector3 off2;
  13. private bool baseFinger;
  14. public void Initialize(Transform[] ikChain, bool baseFinger, Maid maid, Func<Vector3> position, Func<Vector3> rotation)
  15. {
  16. base.Initialize(maid, position, rotation);
  17. this.ikChain = ikChain;
  18. this.baseFinger = baseFinger;
  19. InitializeIK();
  20. InitializeIK2();
  21. }
  22. public void InitializeIK()
  23. {
  24. IK.Init(ikChain[upperArm], ikChain[foreArm], ikChain[hand], maid.body0);
  25. }
  26. private void InitializeIK2()
  27. {
  28. for (int i = 0; i < otherIK.Length; i++)
  29. {
  30. otherIK[i] = new GameObject();
  31. otherIK[i].transform.position = this.ikChain[i].position;
  32. otherIK[i].transform.localRotation = this.ikChain[i].localRotation;
  33. }
  34. }
  35. protected override void GetDragType()
  36. {
  37. if (Utility.GetModKey(Utility.ModKey.Shift))
  38. {
  39. dragType = DragType.RotLocalY;
  40. }
  41. else
  42. {
  43. dragType = DragType.None;
  44. }
  45. }
  46. protected override void InitializeDrag()
  47. {
  48. base.InitializeDrag();
  49. IKCtrlData ikData = maid.body0.IKCtrl.GetIKData("左手");
  50. off = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, worldPoint.z));
  51. off2 = new Vector3(
  52. transform.position.x - ikChain[hand].position.x,
  53. transform.position.y - ikChain[hand].position.y,
  54. transform.position.z - ikChain[hand].position.z);
  55. jointRotation[upperArmRot] = ikChain[upperArm].localEulerAngles;
  56. jointRotation[handRot] = ikChain[hand].localEulerAngles;
  57. InitializeIK();
  58. }
  59. protected override void Drag()
  60. {
  61. if (isPlaying)
  62. {
  63. maid.GetAnimation().Stop();
  64. OnDragEvent();
  65. }
  66. IKCtrlData ikData = maid.body0.IKCtrl.GetIKData("左手");
  67. Vector3 pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, worldPoint.z)) + off - off2;
  68. if (dragType == DragType.None)
  69. {
  70. IK.Porc(ikChain[upperArm], ikChain[foreArm], ikChain[hand], pos, Vector3.zero, ikData);
  71. if (baseFinger)
  72. {
  73. jointRotation[handRot] = ikChain[hand].localEulerAngles;
  74. jointRotation[upperArmRot] = ikChain[upperArm].localEulerAngles;
  75. ikChain[upperArm].localEulerAngles = jointRotation[upperArm];
  76. ikChain[hand].localEulerAngles = jointRotation[handRot];
  77. }
  78. else
  79. {
  80. ikChain[hand].localEulerAngles = new Vector3(jointRotation[handRot].x, jointRotation[handRot].y, ikChain[hand].localEulerAngles.z);
  81. ikChain[upperArm].localEulerAngles = new Vector3(jointRotation[upperArmRot].x, jointRotation[upperArmRot].y, ikChain[upperArm].localEulerAngles.z);
  82. }
  83. }
  84. else
  85. {
  86. if (dragType == DragType.RotLocalY)
  87. {
  88. Vector3 vec31 = Input.mousePosition - mousePos;
  89. ikChain[upperArm].localEulerAngles = jointRotation[upperArmRot];
  90. ikChain[upperArm].localRotation = Quaternion.Euler(ikChain[upperArm].localEulerAngles)
  91. * Quaternion.AngleAxis((-vec31.x / 1.5f), Vector3.right);
  92. }
  93. }
  94. }
  95. }
  96. }