FarPointer.cs 11 KB

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