DragPointChain.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using UnityEngine;
  2. namespace COM3D2.MeidoPhotoStudio.Plugin
  3. {
  4. using Input = InputManager;
  5. internal class DragPointChain : DragPointMeido
  6. {
  7. private readonly TBody.IKCMO IK = new TBody.IKCMO();
  8. private readonly Quaternion[] jointRotation = new Quaternion[3];
  9. private IKCtrlData ikCtrlData;
  10. private Transform[] ikChain;
  11. private int foot = 1;
  12. private bool isLower;
  13. private bool isMiddle;
  14. private bool isUpper;
  15. private bool isMune;
  16. private bool isMuneL;
  17. public override void Set(Transform myObject)
  18. {
  19. base.Set(myObject);
  20. string name = myObject.name;
  21. isMune = name.StartsWith("Mune");
  22. isMuneL = isMune && name[5] == 'L'; // Mune_L_Sub
  23. foot = name.EndsWith("Foot") ? -1 : 1;
  24. isLower = name.EndsWith("Hand") || foot == -1;
  25. isMiddle = name.EndsWith("Calf") || name.EndsWith("Forearm");
  26. isUpper = !(isMiddle || isLower) && !isMune;
  27. ikChain = new Transform[] {
  28. myObject.parent,
  29. myObject.parent,
  30. myObject
  31. };
  32. if (isLower) ikChain[0] = ikChain[0].parent;
  33. ikCtrlData = IkCtrlData;
  34. }
  35. private void InitializeRotation()
  36. {
  37. for (int i = 0; i < jointRotation.Length; i++) jointRotation[i] = ikChain[i].localRotation;
  38. }
  39. protected override void ApplyDragType()
  40. {
  41. DragType current = CurrentDragType;
  42. bool isBone = IsBone;
  43. if (CurrentDragType == DragType.Ignore) ApplyProperties();
  44. else if (current == DragType.RotLocalXZ)
  45. {
  46. if (isLower) ApplyProperties(!isBone, false, isBone);
  47. else ApplyProperties();
  48. }
  49. else if (current == DragType.RotLocalY)
  50. {
  51. if (isLower || isMiddle) ApplyProperties(!isBone, false, false);
  52. else if (isUpper) ApplyProperties(false, false, isBone);
  53. else ApplyProperties();
  54. }
  55. else if (current == DragType.RotY)
  56. {
  57. if (isMune) ApplyProperties(true, false, false);
  58. else if (isMiddle) ApplyProperties(false, false, isBone);
  59. else ApplyProperties();
  60. }
  61. else if (current == DragType.MoveXZ)
  62. {
  63. if (isLower) ApplyProperties(true, isBone, false);
  64. else ApplyProperties();
  65. }
  66. else ApplyProperties(!isMune, isBone && !isMune, false);
  67. }
  68. protected override void UpdateDragType()
  69. {
  70. bool control = Input.Control;
  71. bool alt = Input.Alt;
  72. if (control && alt)
  73. {
  74. // mune
  75. CurrentDragType = DragType.RotY;
  76. }
  77. else if (control)
  78. {
  79. CurrentDragType = DragType.MoveXZ;
  80. }
  81. else if (alt)
  82. {
  83. CurrentDragType = Input.Shift ? DragType.RotLocalY : DragType.RotLocalXZ;
  84. }
  85. else
  86. {
  87. CurrentDragType = OtherDragType() ? DragType.Ignore : DragType.None;
  88. }
  89. }
  90. protected override void OnMouseDown()
  91. {
  92. base.OnMouseDown();
  93. if (isMune) meido.SetMune(false, isMuneL);
  94. InitializeRotation();
  95. InitializeIK(IK, ikChain[jointUpper], ikChain[jointMiddle], ikChain[jointLower]);
  96. }
  97. protected override void OnDoubleClick()
  98. {
  99. if (isMune && CurrentDragType == DragType.RotY) meido.SetMune(true, isMuneL);
  100. }
  101. protected override void Drag()
  102. {
  103. if (isPlaying) meido.Stop = true;
  104. bool altRotation = CurrentDragType == DragType.MoveXZ || CurrentDragType == DragType.RotY;
  105. if ((CurrentDragType == DragType.None) || altRotation)
  106. {
  107. int upperJoint = altRotation ? jointMiddle : jointUpper;
  108. Porc(IK, ikCtrlData, ikChain[upperJoint], ikChain[jointMiddle], ikChain[jointLower]);
  109. InitializeRotation();
  110. }
  111. Vector3 mouseDelta = MouseDelta();
  112. if (CurrentDragType == DragType.RotLocalY)
  113. {
  114. int joint = isMiddle ? jointUpper : jointLower;
  115. ikChain[joint].localRotation = jointRotation[joint];
  116. ikChain[joint].Rotate(Vector3.right * (-mouseDelta.x / 1.5f));
  117. }
  118. if (CurrentDragType == DragType.RotLocalXZ)
  119. {
  120. ikChain[jointLower].localRotation = jointRotation[jointLower];
  121. ikChain[jointLower].Rotate(Vector3.up * (foot * mouseDelta.x / 1.5f));
  122. ikChain[jointLower].Rotate(Vector3.forward * (foot * mouseDelta.y / 1.5f));
  123. }
  124. }
  125. }
  126. }