DragJointHand.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using UnityEngine;
  3. namespace COM3D2.MeidoPhotoStudio.Plugin
  4. {
  5. public class DragJointHand : 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 Transform[] ikChainLock;
  11. private Vector3[] jointRotation = new Vector3[2];
  12. private Vector3 off;
  13. private Vector3 off2;
  14. private int foot = 1;
  15. public DragJointHand Initialize(Transform[] ikChain, bool foot, Meido meido, Func<Vector3> position, Func<Vector3> rotation)
  16. {
  17. base.Initialize(meido, position, rotation);
  18. this.ikChain = ikChain;
  19. this.foot = foot ? -1 : 1;
  20. this.ikChainLock = new Transform[3] {
  21. ikChain[foreArm],
  22. ikChain[foreArm],
  23. ikChain[hand]
  24. };
  25. InitializeIK();
  26. InitializeIK2();
  27. InitializeGizmo(this.ikChain[hand]);
  28. return this;
  29. }
  30. public void InitializeIK()
  31. {
  32. IK.Init(ikChain[upperArm], ikChain[foreArm], ikChain[hand], maid.body0);
  33. }
  34. private void InitializeIK2()
  35. {
  36. for (int i = 0; i < otherIK.Length; i++)
  37. {
  38. otherIK[i] = new GameObject();
  39. otherIK[i].transform.position = this.ikChain[i].position;
  40. otherIK[i].transform.localRotation = this.ikChain[i].localRotation;
  41. }
  42. }
  43. protected override void GetDragType()
  44. {
  45. if (Utility.GetModKey(Utility.ModKey.Shift) && Utility.GetModKey(Utility.ModKey.Alt))
  46. {
  47. dragType = DragType.RotLocalY;
  48. }
  49. else if (Utility.GetModKey(Utility.ModKey.Alt))
  50. {
  51. dragType = DragType.RotLocalXZ;
  52. }
  53. else if (Utility.GetModKey(Utility.ModKey.Control))
  54. {
  55. dragType = DragType.MoveXZ;
  56. }
  57. else
  58. {
  59. dragType = DragType.None;
  60. }
  61. }
  62. protected override void InitializeDrag()
  63. {
  64. base.InitializeDrag();
  65. Transform[] ikChain = dragType == DragType.MoveXZ ? this.ikChainLock : this.ikChain;
  66. IKCtrlData ikData = maid.body0.IKCtrl.GetIKData("左手");
  67. off = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, worldPoint.z));
  68. off2 = new Vector3(
  69. transform.position.x - ikChain[hand].position.x,
  70. transform.position.y - ikChain[hand].position.y,
  71. transform.position.z - ikChain[hand].position.z);
  72. jointRotation[upperArmRot] = ikChain[upperArm].localEulerAngles;
  73. jointRotation[handRot] = ikChain[hand].localEulerAngles;
  74. InitializeIK();
  75. }
  76. protected override void Drag()
  77. {
  78. if (isPlaying) meido.IsStop = true;
  79. IKCtrlData ikData = maid.body0.IKCtrl.GetIKData("左手");
  80. Vector3 pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, worldPoint.z)) + off - off2;
  81. if (dragType == DragType.None || dragType == DragType.MoveXZ)
  82. {
  83. Transform[] ikChain = dragType == DragType.MoveXZ ? this.ikChainLock : this.ikChain;
  84. IK.Porc(ikChain[upperArm], ikChain[foreArm], ikChain[hand], pos, Vector3.zero, ikData);
  85. jointRotation[handRot] = ikChain[hand].localEulerAngles;
  86. jointRotation[upperArmRot] = ikChain[upperArm].localEulerAngles;
  87. ikChain[upperArm].localEulerAngles = jointRotation[upperArm];
  88. ikChain[hand].localEulerAngles = jointRotation[handRot];
  89. }
  90. else
  91. {
  92. Vector3 vec31 = Input.mousePosition - mousePos;
  93. if (dragType == DragType.RotLocalY)
  94. {
  95. ikChain[hand].localEulerAngles = jointRotation[handRot];
  96. ikChain[hand].localRotation = Quaternion.Euler(ikChain[hand].localEulerAngles)
  97. * Quaternion.AngleAxis(vec31.x / 1.5f, Vector3.right);
  98. }
  99. if (dragType == DragType.RotLocalXZ)
  100. {
  101. ikChain[hand].localEulerAngles = jointRotation[handRot];
  102. ikChain[hand].localRotation = Quaternion.Euler(ikChain[hand].localEulerAngles)
  103. * Quaternion.AngleAxis(foot * vec31.x / 1.5f, Vector3.up);
  104. ikChain[hand].localRotation = Quaternion.Euler(ikChain[hand].localEulerAngles)
  105. * Quaternion.AngleAxis(foot * vec31.y / 1.5f, Vector3.forward);
  106. }
  107. }
  108. }
  109. }
  110. }