DragJointForearm.cs 3.5 KB

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