ManipulationHandler.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using SC.XR.Unity.Module_InputSystem.InputDeviceGC;
  2. using SC.XR.Unity.Module_InputSystem.InputDeviceGC.BT3Dof;
  3. using SC.XR.Unity.Module_InputSystem.InputDeviceHand;
  4. using SC.XR.Unity.Module_InputSystem.InputDeviceHead;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using UnityEngine;
  9. using UnityEngine.EventSystems;
  10. namespace SC.XR.Unity.Module_InputSystem
  11. {
  12. public struct SCPose
  13. {
  14. public Vector3 position;
  15. public Quaternion rotation;
  16. public SCPose(Vector3 position, Quaternion rotation)
  17. {
  18. this.position = position;
  19. this.rotation = rotation;
  20. }
  21. }
  22. // [RequireComponent(typeof(NearInterationGrabbable))]
  23. // [RequireComponent(typeof(BoxCollider))]
  24. public class ManipulationHandler : PointerHandler
  25. {
  26. public Transform Target;
  27. [SerializeField]
  28. protected SCAudiosConfig.AudioType StartAudio = SCAudiosConfig.AudioType.Move_Start;
  29. [SerializeField]
  30. protected SCAudiosConfig.AudioType EndAudio = SCAudiosConfig.AudioType.Move_End;
  31. protected Vector3 TargetRelativeTo;
  32. protected SCPointEventData sCPointEventData;
  33. protected Matrix4x4 matrix;
  34. private Rigidbody rigidBody;
  35. private bool wasKinematic = false;
  36. private IDevicePartManipulation devicePartManipulation;
  37. private IDevicePartCountManipulation devicePartCountManipulation;
  38. private Dictionary<InputDevicePartType, SCPointEventData> eventDataDic;
  39. private ScaleLogic scaleLogic;
  40. private RotateLogic rotateLogic;
  41. private MoveLogic moveLogic;
  42. private Vector3 targetStartScale;
  43. public bool headRotate = false;
  44. public bool oneGameControllerRotate = false;
  45. public bool canOneHandRotate = false;
  46. public bool canTwoHandRotate = false;
  47. public bool canTwoHandScale = false;
  48. public float minScaleRatio = 0.8f;
  49. public float maxScaleRatio = 3f;
  50. public bool isParentDrag;
  51. public virtual void Start()
  52. {
  53. targetStartScale = Target == null ? transform.localScale : Target.localScale;
  54. }
  55. public override void OnPointerDown(PointerEventData eventData)
  56. {
  57. base.OnPointerDown(eventData);
  58. if (eventDataDic == null)
  59. {
  60. eventDataDic = new Dictionary<InputDevicePartType, SCPointEventData>();
  61. }
  62. if (scaleLogic == null)
  63. {
  64. scaleLogic = new ScaleLogic();
  65. }
  66. if (rotateLogic == null)
  67. {
  68. rotateLogic = new RotateLogic();
  69. }
  70. if (moveLogic == null)
  71. {
  72. moveLogic = new MoveLogic();
  73. }
  74. if (eventData is SCPointEventData)
  75. {
  76. SCPointEventData scPointEventData = eventData as SCPointEventData;
  77. eventDataDic[scPointEventData.inputDevicePartBase.PartType] = scPointEventData;
  78. if (scPointEventData.inputDevicePartBase is InputDeviceHandPart)
  79. {
  80. devicePartManipulation = new HandDevicePartManipulation();
  81. }
  82. else if (scPointEventData.inputDevicePartBase is InputDeviceGCPart)
  83. {
  84. devicePartManipulation = new GameControllerDevicePartManipulation();
  85. }
  86. else if (scPointEventData.inputDevicePartBase is InputDeviceHeadPart)
  87. {
  88. devicePartManipulation = new HeadDevicePartManipulation();
  89. }
  90. }
  91. //Two DevicePart
  92. if (eventDataDic.Count > 1)
  93. {
  94. devicePartCountManipulation = new TwoDevicePartCountManipulation();
  95. devicePartCountManipulation.Init(this, devicePartManipulation, eventDataDic, Target == null ? this.transform : Target.transform, moveLogic, rotateLogic, scaleLogic);
  96. }
  97. //One DevicePart
  98. else if (eventDataDic.Count == 1)
  99. {
  100. devicePartCountManipulation = new OneDevicePartCountManipulation();
  101. devicePartCountManipulation.Init(this, devicePartManipulation, eventDataDic, Target == null ? this.transform : Target.transform, moveLogic, rotateLogic, scaleLogic);
  102. }
  103. rigidBody = Target == null ? this.GetComponent<Rigidbody>() : Target.GetComponent<Rigidbody>();
  104. if (rigidBody != null)
  105. {
  106. wasKinematic = rigidBody.isKinematic;
  107. rigidBody.isKinematic = true;
  108. }
  109. AudioSystem.getInstance.PlayAudioOneShot(gameObject, StartAudio);
  110. }
  111. public override void OnPointerUp(PointerEventData eventData)
  112. {
  113. base.OnPointerUp(eventData);
  114. if (eventData is SCPointEventData)
  115. {
  116. SCPointEventData scPointEventData = eventData as SCPointEventData;
  117. if (eventDataDic.ContainsKey(scPointEventData.inputDevicePartBase.PartType))
  118. {
  119. eventDataDic.Remove(scPointEventData.inputDevicePartBase.PartType);
  120. }
  121. //from two hand to one hand
  122. if (eventDataDic.Count == 1)
  123. {
  124. devicePartCountManipulation = new OneDevicePartCountManipulation();
  125. devicePartCountManipulation.Init(this, devicePartManipulation, eventDataDic, Target == null ? this.transform : Target.transform, moveLogic, rotateLogic, scaleLogic);
  126. }
  127. }
  128. if (rigidBody != null)
  129. {
  130. rigidBody.isKinematic = wasKinematic;
  131. rigidBody = null;
  132. }
  133. AudioSystem.getInstance.PlayAudioOneShot(gameObject, EndAudio);
  134. }
  135. public override void OnDrag(PointerEventData eventData)
  136. {
  137. base.OnDrag(eventData);
  138. sCPointEventData = eventData as SCPointEventData;
  139. if (sCPointEventData == null)
  140. {
  141. return;
  142. }
  143. Tuple<Vector3, Quaternion, Vector3> result = devicePartCountManipulation.Update(CaculateScaleConstraint);
  144. Vector3 position = result.Item1;
  145. Quaternion rotation = result.Item2;
  146. Vector3 scale = result.Item3;
  147. if (Target)
  148. {
  149. Target.position = position;
  150. Target.rotation = rotation;
  151. Target.localScale = scale;
  152. }
  153. else
  154. {
  155. transform.position = position;
  156. transform.rotation = rotation;
  157. transform.localScale = scale;
  158. }
  159. }
  160. public override void OnPointerExit(PointerEventData eventData)
  161. {
  162. base.OnPointerExit(eventData);
  163. }
  164. public override void OnPointerEnter(PointerEventData eventData)
  165. {
  166. base.OnPointerEnter(eventData);
  167. }
  168. public override void OnPointerClick(PointerEventData eventData)
  169. {
  170. base.OnPointerClick(eventData);
  171. }
  172. private Vector3 CaculateScaleConstraint(Vector3 toScale)
  173. {
  174. Vector3 minimumScale = targetStartScale * minScaleRatio;
  175. Vector3 maximumScale = targetStartScale * maxScaleRatio;
  176. if (Vector3.Min(maximumScale, toScale) != toScale)
  177. {
  178. float maxRatio = 0.0f;
  179. int maxIdx = -1;
  180. for (int i = 0; i < 3; i++)
  181. {
  182. if (maximumScale[i] > 0)
  183. {
  184. float ratio = toScale[i] / maximumScale[i];
  185. if (ratio > maxRatio)
  186. {
  187. maxRatio = ratio;
  188. maxIdx = i;
  189. }
  190. }
  191. }
  192. if (maxIdx != -1)
  193. {
  194. toScale /= maxRatio;
  195. }
  196. }
  197. if (Vector3.Max(minimumScale, toScale) != toScale)
  198. {
  199. float minRatio = 1.0f;
  200. int minIdx = -1;
  201. // Find out the component with the minimum ratio to its minimum allowed value
  202. for (int i = 0; i < 3; ++i)
  203. {
  204. if (minimumScale[i] > 0)
  205. {
  206. float ratio = toScale[i] / minimumScale[i];
  207. if (ratio < minRatio)
  208. {
  209. minRatio = ratio;
  210. minIdx = i;
  211. }
  212. }
  213. }
  214. if (minIdx != -1)
  215. {
  216. toScale /= minRatio;
  217. }
  218. }
  219. return toScale;
  220. }
  221. }
  222. }