FarPointer.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using Rokid.UXR.Interaction;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using UnityEngine;
  8. using UnityEngine.EventSystems;
  9. namespace SC.XR.Unity.Module_InputSystem.InputDeviceHand {
  10. public class FarPointer : PointerBase {
  11. public HandDetector handDetector {
  12. get {
  13. return detectorBase as HandDetector;
  14. }
  15. }
  16. HandCursor _handCursor;
  17. public HandCursor handCursor {
  18. get {
  19. if (_handCursor == null) {
  20. _handCursor = GetComponentInChildren<HandCursor>(true);
  21. }
  22. return _handCursor;
  23. }
  24. }
  25. public override PointerType PointerType { get => PointerType.Far; }
  26. public Action<bool> TargetDetectModelChange;
  27. private Vector3 RotationOffset;
  28. private float slowness = 0;
  29. public bool ShowHandCursor = false;
  30. public bool ShowHandLine = true;
  31. public bool OutCameraShowHandLine = false;
  32. private Vector3 LeftOffset = new Vector3(0.015f,0.05f, 0f);
  33. private Vector3 RightOffset = new Vector3(-0.035f, 0.05f, 0f);
  34. public override void OnSCAwake() {
  35. base.OnSCAwake();
  36. AddModule(handCursor);
  37. RotationOffset = Vector3.zero;
  38. if (API_Module_SDKConfiguration.HasKey("Module_InputSystem", "FarPointerRotationOffsetX")) {
  39. RotationOffset.x = API_Module_SDKConfiguration.GetFloat("Module_InputSystem", "FarPointerRotationOffsetX", 0);
  40. }
  41. if (API_Module_SDKConfiguration.HasKey("Module_InputSystem", "FarPointerRotationOffsetY")) {
  42. RotationOffset.y = API_Module_SDKConfiguration.GetFloat("Module_InputSystem", "FarPointerRotationOffsetY", 0);
  43. }
  44. if (API_Module_SDKConfiguration.HasKey("Module_InputSystem", "FarPointerRotationOffsetZ")) {
  45. RotationOffset.z = API_Module_SDKConfiguration.GetFloat("Module_InputSystem", "FarPointerRotationOffsetZ", 0);
  46. }
  47. DebugMy.Log("FarPointerRotationOffset:" + RotationOffset, this, true);
  48. if (API_Module_Device.IsGSXRAndroidDevice == false) {
  49. RotationOffset.x += 10;
  50. } else {
  51. //if (handDetector.inputDeviceHandPart.PartType == InputDevicePartType.HandLeft) {
  52. // RotationOffset.x = -20;
  53. //} else if (handDetector.inputDeviceHandPart.PartType == InputDevicePartType.HandRight) {
  54. // RotationOffset.x = -10;
  55. //}
  56. }
  57. }
  58. public override void OnSCStart() {
  59. base.OnSCStart();
  60. //lineBase?.ModuleStop();
  61. InputDeviceHandPartEventBase.eventDelegate += CatchEvent;
  62. //if (handDetector.inputDeviceHandPart.PartType == InputDevicePartType.HandRight) {
  63. // //lineBase?.ModuleStop();
  64. //}
  65. if (API_Module_SDKConfiguration.HasKey("Module_InputSystem", "ShowHandLine")) {
  66. ShowHandLine = API_Module_SDKConfiguration.GetBool("Module_InputSystem", "ShowHandLine", 0);
  67. DebugMy.Log("ShowHandLine:" + ShowHandLine, this, true);
  68. }
  69. if (ShowHandLine==false) {
  70. lineBase?.ModuleStop();
  71. }
  72. if (API_Module_SDKConfiguration.HasKey("Module_InputSystem", "ShowHandCursor")) {
  73. ShowHandCursor = API_Module_SDKConfiguration.GetBool("Module_InputSystem", "ShowHandCursor", 0);
  74. DebugMy.Log("ShowHandCursor:" + ShowHandCursor, this, true);
  75. }
  76. if (API_Module_SDKConfiguration.HasKey("Module_InputSystem", "OutCameraShowHandLine")) {
  77. OutCameraShowHandLine = API_Module_SDKConfiguration.GetBool("Module_InputSystem", "OutCameraShowHandLine", 0);
  78. DebugMy.Log("OutCameraShowHandLine:" + OutCameraShowHandLine, this, true);
  79. }
  80. if (ShowHandCursor) {
  81. if (handCursor != null && !handCursor.IsModuleStarted)
  82. handCursor.ModuleStart();
  83. }
  84. }
  85. public GameObject leftHand;
  86. public GameObject rightHand;
  87. Vector3 up, forward = Vector3.forward;
  88. protected override void UpdateTransform() {
  89. //if (HandRayOffset) {
  90. // transform.position = handDetector.inputDeviceHandPart.inputDataHand.handInfo.MainGrabPosition;
  91. //} else {
  92. // transform.position = handDetector.inputDeviceHandPart.inputDeviceHandPartUI.modelHand.ActiveHandModel.GetJointTransform(FINGER.forefinger, JOINT.Four).position;
  93. //}
  94. transform.position = handDetector.inputDeviceHandPart.inputDeviceHandPartUI.modelHand.ActiveHandModel.GetJointTransform(FINGER.forefinger, JOINT.Four).position;
  95. Quaternion temprotation = Quaternion.identity;
  96. if (handDetector.inputDeviceHandPart.PartType == InputDevicePartType.HandLeft) {
  97. temprotation = Quaternion.LookRotation(transform.position - leftHand.transform.position, leftHand.transform.up);
  98. } else if (handDetector.inputDeviceHandPart.PartType == InputDevicePartType.HandRight) {
  99. temprotation = Quaternion.LookRotation(transform.position - rightHand.transform.position, rightHand.transform.up);
  100. }
  101. //if (handDetector.inputDevicePartBase.inputDataBase.SCPointEventData.pointerCurrentRaycast.gameObject != null && slowness > 0) {
  102. // forward = Vector3.Lerp(forward, handDetector.inputDevicePartBase.inputDataBase.SCPointEventData.pointerCurrentRaycast.worldPosition - transform.position,0.5f);
  103. //} else {
  104. //if (handDetector.inputDeviceHandPart.PartType == InputDevicePartType.HandLeft) {
  105. // forward = transform.position - API_GSXR_Slam.SlamManager.shoulder.Left.position;
  106. // up = Vector3.Cross(handDetector.inputDeviceHandPart.inputDataHand.handInfo.right, forward);
  107. //} else if (handDetector.inputDeviceHandPart.PartType == InputDevicePartType.HandRight) {
  108. // forward = transform.position - API_GSXR_Slam.SlamManager.shoulder.Right.position;
  109. // up = Vector3.Cross(forward, handDetector.inputDeviceHandPart.inputDataHand.handInfo.right);
  110. //}
  111. //}
  112. //if (handDetector.inputDeviceHandPart.PartType == InputDevicePartType.HandLeft) {
  113. // forward = transform.position - API_GSXR_Slam.SlamManager.shoulder.Left.position;
  114. // up = Vector3.Cross(handDetector.inputDeviceHandPart.inputDataHand.handInfo.right, forward);
  115. //} else if (handDetector.inputDeviceHandPart.PartType == InputDevicePartType.HandRight) {
  116. // forward = transform.position - API_GSXR_Slam.SlamManager.shoulder.Right.position;
  117. // up = Vector3.Cross(forward, handDetector.inputDeviceHandPart.inputDataHand.handInfo.right);
  118. //}
  119. //temprotation = Quaternion.LookRotation(forward, up);
  120. transform.rotation = temprotation;
  121. transform.Rotate(RotationOffset);
  122. if (handDetector.inputDeviceHandPart.PartType == InputDevicePartType.HandLeft) {
  123. // transform.position = transform.position + transform.rotation * LeftOffset;
  124. if(XRInputManager.isHand)
  125. {
  126. transform.rotation = lR.transform.rotation;
  127. transform.position = lR.transform.position;
  128. }else
  129. {
  130. }
  131. } else if (handDetector.inputDeviceHandPart.PartType == InputDevicePartType.HandRight) {
  132. // transform.position = transform.position + transform.rotation * RightOffset;
  133. if(XRInputManager.isHand)
  134. {
  135. transform.rotation = RR.transform.rotation;
  136. transform.position = RR.transform.position;
  137. }else
  138. {
  139. Transform t = ThreeDofEventInput.Instance.transform.GetChild(0);
  140. if (t)
  141. {
  142. transform.eulerAngles = t.eulerAngles;
  143. // rootLeftHand.transform.localPosition = new Vector3(rootLeftHand.transform.localPosition.x-20, rootLeftHand.transform.localPosition.y, rootLeftHand.transform.localPosition.z);
  144. transform.position = t.position;
  145. }
  146. }
  147. }
  148. }
  149. public GameObject lR;
  150. public GameObject RR;
  151. protected override void DoTargetDetect()
  152. {
  153. // Debug.Log("DoTargetDetect4444===");
  154. SCInputModule.Instance.ProcessCS(handDetector.inputDevicePartBase.inputDataBase.SCPointEventData, transform, LayerMask, MaxDetectDistance);
  155. IsFocusLocked = handDetector.inputDevicePartBase.inputDataBase.SCPointEventData.DownPressGameObject != null;
  156. if (handDetector.inputDevicePartBase.inputDataBase.SCPointEventData.DownPressGameObject || handDetector.inputDevicePartBase.inputDataBase.SCPointEventData.pointerCurrentRaycast.gameObject != null) {
  157. if (cursorBase && !cursorBase.IsModuleStarted) {
  158. cursorBase?.ModuleStart();
  159. }
  160. } else {
  161. if (cursorBase && cursorBase.IsModuleStarted) {
  162. cursorBase?.ModuleStop();
  163. }
  164. }
  165. }
  166. public override void OnSCDisable() {
  167. base.OnSCDisable();
  168. IsFocusLocked = false;
  169. InputDeviceHandPartEventBase.eventDelegate -= CatchEvent;
  170. }
  171. protected override void UpdateSubVisual() {
  172. //base.UpdateSubVisual();
  173. if (OutCameraShowHandLine && lineBase) {
  174. if (API_GSXR_Slam.SlamManager != null && API_GSXR_Slam.SlamManager.IsRunning) {
  175. if (!IsInCamera(Camera.main, cursorBase.transform.position) &&
  176. !IsInCamera(API_GSXR_Slam.SlamManager.rightCamera, cursorBase.transform.position)) {
  177. if (lineBase != null && !lineBase.IsModuleStarted) {
  178. lineBase.ModuleStart();
  179. }
  180. } else {
  181. if (lineBase != null && lineBase.IsModuleStarted) {
  182. lineBase.ModuleStop();
  183. }
  184. }
  185. }
  186. }
  187. }
  188. Vector3 viewPos;
  189. bool IsInCamera(Camera camera,Vector3 worldPosition) {
  190. viewPos = camera.WorldToViewportPoint(worldPosition);
  191. if (viewPos.z < 0)
  192. return false;
  193. if (viewPos.z > camera.farClipPlane)
  194. return false;
  195. if (viewPos.x < 0 || viewPos.y < 0 || viewPos.x > 1 || viewPos.y > 1)
  196. return false;
  197. return true;
  198. }
  199. void CatchEvent(InputDeviceHandPart inputDeviceHandPart, HandEventType eventType, float eventPercent) {
  200. //if (handDetector && handDetector.inputDeviceHandPart == inputDeviceHandPart) {
  201. // if (eventType == HandEventType.CatchReady) {
  202. // slowness = 0f;
  203. // if (eventPercent > 0.2f) {
  204. // slowness = 1f;
  205. // }
  206. // } else if (eventType == HandEventType.CatchDrag || eventType == HandEventType.CatchDown || eventType == HandEventType.CatchUp ) {
  207. // slowness = 0f;
  208. // }
  209. //}
  210. }
  211. }
  212. }