ManipulationHandler.cs 8.6 KB

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