DragJointHand.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 void Initialize(Transform[] ikChain, bool foot, Maid maid, Func<Vector3> position, Func<Vector3> rotation)
  16. {
  17. base.Initialize(maid, 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. }
  29. public void InitializeIK()
  30. {
  31. IK.Init(ikChain[upperArm], ikChain[foreArm], ikChain[hand], maid.body0);
  32. }
  33. private void InitializeIK2()
  34. {
  35. for (int i = 0; i < otherIK.Length; i++)
  36. {
  37. otherIK[i] = new GameObject();
  38. otherIK[i].transform.position = this.ikChain[i].position;
  39. otherIK[i].transform.localRotation = this.ikChain[i].localRotation;
  40. }
  41. }
  42. protected override void GetDragType()
  43. {
  44. if (Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.LeftAlt))
  45. {
  46. dragType = DragType.RotLocalY;
  47. }
  48. else if (Input.GetKey(KeyCode.LeftAlt))
  49. {
  50. dragType = DragType.RotLocalXZ;
  51. }
  52. else if (Input.GetKey(KeyCode.LeftControl))
  53. {
  54. dragType = DragType.MoveXZ;
  55. }
  56. else
  57. {
  58. dragType = DragType.None;
  59. }
  60. }
  61. protected override void InitializeDrag()
  62. {
  63. base.InitializeDrag();
  64. Transform[] ikChain = dragType == DragType.MoveXZ ? this.ikChainLock : this.ikChain;
  65. IKCtrlData ikData = maid.body0.IKCtrl.GetIKData("左手");
  66. off = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, worldPoint.z));
  67. off2 = new Vector3(
  68. transform.position.x - ikChain[hand].position.x,
  69. transform.position.y - ikChain[hand].position.y,
  70. transform.position.z - ikChain[hand].position.z);
  71. jointRotation[upperArmRot] = ikChain[upperArm].localEulerAngles;
  72. jointRotation[handRot] = ikChain[hand].localEulerAngles;
  73. InitializeIK();
  74. }
  75. protected override void Drag()
  76. {
  77. if (isPlaying)
  78. {
  79. maid.GetAnimation().Stop();
  80. }
  81. IKCtrlData ikData = maid.body0.IKCtrl.GetIKData("左手");
  82. Vector3 pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, worldPoint.z)) + off - off2;
  83. if (dragType == DragType.None || dragType == DragType.MoveXZ)
  84. {
  85. Transform[] ikChain = dragType == DragType.MoveXZ ? this.ikChainLock : this.ikChain;
  86. IK.Porc(ikChain[upperArm], ikChain[foreArm], ikChain[hand], pos, Vector3.zero, ikData);
  87. jointRotation[handRot] = ikChain[hand].localEulerAngles;
  88. jointRotation[upperArmRot] = ikChain[upperArm].localEulerAngles;
  89. ikChain[upperArm].localEulerAngles = jointRotation[upperArm];
  90. ikChain[hand].localEulerAngles = jointRotation[handRot];
  91. }
  92. else
  93. {
  94. Vector3 vec31 = Input.mousePosition - mousePos;
  95. if (dragType == DragType.RotLocalY)
  96. {
  97. ikChain[hand].localEulerAngles = jointRotation[handRot];
  98. ikChain[hand].localRotation = Quaternion.Euler(ikChain[hand].localEulerAngles)
  99. * Quaternion.AngleAxis(vec31.x / 1.5f, Vector3.right);
  100. }
  101. if (dragType == DragType.RotLocalXZ)
  102. {
  103. ikChain[hand].localEulerAngles = jointRotation[handRot];
  104. ikChain[hand].localRotation = Quaternion.Euler(ikChain[hand].localEulerAngles)
  105. * Quaternion.AngleAxis(foot * vec31.x / 1.5f, Vector3.up);
  106. ikChain[hand].localRotation = Quaternion.Euler(ikChain[hand].localEulerAngles)
  107. * Quaternion.AngleAxis(foot * vec31.y / 1.5f, Vector3.forward);
  108. }
  109. }
  110. }
  111. }
  112. }