ManipulationHandler.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 virtual void Start()
  51. {
  52. targetStartScale = Target == null ? transform.localScale : Target.localScale;
  53. }
  54. public override void OnPointerDown(PointerEventData eventData)
  55. {
  56. xx = 0;
  57. yy = 0;
  58. base.OnPointerDown(eventData);
  59. if (eventDataDic == null)
  60. {
  61. eventDataDic = new Dictionary<InputDevicePartType, SCPointEventData>();
  62. }
  63. if (scaleLogic == null)
  64. {
  65. scaleLogic = new ScaleLogic();
  66. }
  67. if (rotateLogic == null)
  68. {
  69. rotateLogic = new RotateLogic();
  70. }
  71. if (moveLogic == null)
  72. {
  73. moveLogic = new MoveLogic();
  74. }
  75. if (eventData is SCPointEventData)
  76. {
  77. SCPointEventData scPointEventData = eventData as SCPointEventData;
  78. eventDataDic[scPointEventData.inputDevicePartBase.PartType] = scPointEventData;
  79. if (scPointEventData.inputDevicePartBase is InputDeviceHandPart)
  80. {
  81. devicePartManipulation = new HandDevicePartManipulation();
  82. }
  83. else if (scPointEventData.inputDevicePartBase is InputDeviceGCPart)
  84. {
  85. devicePartManipulation = new GameControllerDevicePartManipulation();
  86. }
  87. else if (scPointEventData.inputDevicePartBase is InputDeviceHeadPart)
  88. {
  89. devicePartManipulation = new HeadDevicePartManipulation();
  90. }
  91. }
  92. //Two DevicePart
  93. if (eventDataDic.Count > 1)
  94. {
  95. devicePartCountManipulation = new TwoDevicePartCountManipulation();
  96. devicePartCountManipulation.Init(this, devicePartManipulation, eventDataDic, Target == null ? this.transform : Target.transform, moveLogic, rotateLogic, scaleLogic);
  97. }
  98. //One DevicePart
  99. else if (eventDataDic.Count == 1)
  100. {
  101. devicePartCountManipulation = new OneDevicePartCountManipulation();
  102. devicePartCountManipulation.Init(this, devicePartManipulation, eventDataDic, Target == null ? this.transform : Target.transform, moveLogic, rotateLogic, scaleLogic);
  103. }
  104. rigidBody = Target == null ? this.GetComponent<Rigidbody>() : Target.GetComponent<Rigidbody>();
  105. if (rigidBody != null)
  106. {
  107. wasKinematic = rigidBody.isKinematic;
  108. rigidBody.isKinematic = true;
  109. }
  110. AudioSystem.getInstance.PlayAudioOneShot(gameObject, StartAudio);
  111. }
  112. public override void OnPointerUp(PointerEventData eventData)
  113. {
  114. xx = 0;
  115. yy = 0;
  116. base.OnPointerUp(eventData);
  117. if (eventData is SCPointEventData)
  118. {
  119. SCPointEventData scPointEventData = eventData as SCPointEventData;
  120. if (eventDataDic.ContainsKey(scPointEventData.inputDevicePartBase.PartType))
  121. {
  122. eventDataDic.Remove(scPointEventData.inputDevicePartBase.PartType);
  123. }
  124. //from two hand to one hand
  125. if (eventDataDic.Count == 1)
  126. {
  127. devicePartCountManipulation = new OneDevicePartCountManipulation();
  128. devicePartCountManipulation.Init(this, devicePartManipulation, eventDataDic, Target == null ? this.transform : Target.transform, moveLogic, rotateLogic, scaleLogic);
  129. }
  130. }
  131. if (rigidBody != null)
  132. {
  133. rigidBody.isKinematic = wasKinematic;
  134. rigidBody = null;
  135. }
  136. AudioSystem.getInstance.PlayAudioOneShot(gameObject, EndAudio);
  137. }
  138. float xx;
  139. float yy;
  140. public override void OnDrag(PointerEventData eventData)
  141. {
  142. base.OnDrag(eventData);
  143. sCPointEventData = eventData as SCPointEventData;
  144. if (sCPointEventData == null)
  145. {
  146. return;
  147. }
  148. Tuple<Vector3, Quaternion, Vector3> result = devicePartCountManipulation.Update(CaculateScaleConstraint);
  149. Vector3 v3Pos = result.Item1;
  150. Vector3 position = v3Pos;
  151. Quaternion rotation = result.Item2;
  152. Vector3 scale = result.Item3;
  153. if (Target)
  154. {
  155. Target.position = position;
  156. Target.rotation = rotation;
  157. Target.localScale = scale;
  158. }
  159. else
  160. {
  161. transform.position = position;
  162. transform.rotation = rotation;
  163. transform.localScale = scale;
  164. }
  165. }
  166. private Vector3 GetBetweenPoint(Vector3 start, Vector3 end, float percent = 0.5f)
  167. {
  168. Vector3 normal = (end - start).normalized;
  169. float distance = Vector3.Distance(start, end);
  170. return normal * (distance * percent) + start;
  171. }
  172. public override void OnPointerExit(PointerEventData eventData)
  173. {
  174. base.OnPointerExit(eventData);
  175. }
  176. public override void OnPointerEnter(PointerEventData eventData)
  177. {
  178. base.OnPointerEnter(eventData);
  179. }
  180. public override void OnPointerClick(PointerEventData eventData)
  181. {
  182. base.OnPointerClick(eventData);
  183. }
  184. private Vector3 CaculateScaleConstraint(Vector3 toScale)
  185. {
  186. Vector3 minimumScale = targetStartScale * minScaleRatio;
  187. Vector3 maximumScale = targetStartScale * maxScaleRatio;
  188. if (Vector3.Min(maximumScale, toScale) != toScale)
  189. {
  190. float maxRatio = 0.0f;
  191. int maxIdx = -1;
  192. for (int i = 0; i < 3; i++)
  193. {
  194. if (maximumScale[i] > 0)
  195. {
  196. float ratio = toScale[i] / maximumScale[i];
  197. if (ratio > maxRatio)
  198. {
  199. maxRatio = ratio;
  200. maxIdx = i;
  201. }
  202. }
  203. }
  204. if (maxIdx != -1)
  205. {
  206. toScale /= maxRatio;
  207. }
  208. }
  209. if (Vector3.Max(minimumScale, toScale) != toScale)
  210. {
  211. float minRatio = 1.0f;
  212. int minIdx = -1;
  213. // Find out the component with the minimum ratio to its minimum allowed value
  214. for (int i = 0; i < 3; ++i)
  215. {
  216. if (minimumScale[i] > 0)
  217. {
  218. float ratio = toScale[i] / minimumScale[i];
  219. if (ratio < minRatio)
  220. {
  221. minRatio = ratio;
  222. minIdx = i;
  223. }
  224. }
  225. }
  226. if (minIdx != -1)
  227. {
  228. toScale /= minRatio;
  229. }
  230. }
  231. return toScale;
  232. }
  233. }
  234. }