FarPointer.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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.01f,0.09f);
  32. private Vector3 RightOffset = new Vector3(-0.035f, -0.01f, 0.08f);
  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. Vector3 up, forward = Vector3.forward;
  85. protected override void UpdateTransform() {
  86. //if (HandRayOffset) {
  87. // transform.position = handDetector.inputDeviceHandPart.inputDataHand.handInfo.MainGrabPosition;
  88. //} else {
  89. // transform.position = handDetector.inputDeviceHandPart.inputDeviceHandPartUI.modelHand.ActiveHandModel.GetJointTransform(FINGER.forefinger, JOINT.Four).position;
  90. //}
  91. transform.position = handDetector.inputDeviceHandPart.inputDeviceHandPartUI.modelHand.ActiveHandModel.GetJointTransform(FINGER.forefinger, JOINT.Four).position;
  92. Quaternion temprotation = Quaternion.identity;
  93. if (handDetector.inputDeviceHandPart.PartType == InputDevicePartType.HandLeft) {
  94. temprotation = Quaternion.LookRotation(transform.position - API_GSXR_Slam.SlamManager.shoulder.Left.position, API_GSXR_Slam.SlamManager.shoulder.Left.up);
  95. } else if (handDetector.inputDeviceHandPart.PartType == InputDevicePartType.HandRight) {
  96. temprotation = Quaternion.LookRotation(transform.position - API_GSXR_Slam.SlamManager.shoulder.Right.position, API_GSXR_Slam.SlamManager.shoulder.Right.up);
  97. }
  98. //if (handDetector.inputDevicePartBase.inputDataBase.SCPointEventData.pointerCurrentRaycast.gameObject != null && slowness > 0) {
  99. // forward = Vector3.Lerp(forward, handDetector.inputDevicePartBase.inputDataBase.SCPointEventData.pointerCurrentRaycast.worldPosition - transform.position,0.5f);
  100. //} else {
  101. //if (handDetector.inputDeviceHandPart.PartType == InputDevicePartType.HandLeft) {
  102. // forward = transform.position - API_GSXR_Slam.SlamManager.shoulder.Left.position;
  103. // up = Vector3.Cross(handDetector.inputDeviceHandPart.inputDataHand.handInfo.right, forward);
  104. //} else if (handDetector.inputDeviceHandPart.PartType == InputDevicePartType.HandRight) {
  105. // forward = transform.position - API_GSXR_Slam.SlamManager.shoulder.Right.position;
  106. // up = Vector3.Cross(forward, handDetector.inputDeviceHandPart.inputDataHand.handInfo.right);
  107. //}
  108. //}
  109. //if (handDetector.inputDeviceHandPart.PartType == InputDevicePartType.HandLeft) {
  110. // forward = transform.position - API_GSXR_Slam.SlamManager.shoulder.Left.position;
  111. // up = Vector3.Cross(handDetector.inputDeviceHandPart.inputDataHand.handInfo.right, forward);
  112. //} else if (handDetector.inputDeviceHandPart.PartType == InputDevicePartType.HandRight) {
  113. // forward = transform.position - API_GSXR_Slam.SlamManager.shoulder.Right.position;
  114. // up = Vector3.Cross(forward, handDetector.inputDeviceHandPart.inputDataHand.handInfo.right);
  115. //}
  116. //temprotation = Quaternion.LookRotation(forward, up);
  117. transform.rotation = temprotation;
  118. transform.Rotate(RotationOffset);
  119. if (handDetector.inputDeviceHandPart.PartType == InputDevicePartType.HandLeft) {
  120. transform.position = transform.position + transform.rotation * LeftOffset;
  121. } else if (handDetector.inputDeviceHandPart.PartType == InputDevicePartType.HandRight) {
  122. transform.position = transform.position + transform.rotation * RightOffset;
  123. }
  124. }
  125. protected override void DoTargetDetect()
  126. {
  127. Debug.Log("DoTargetDetect4444===");
  128. SCInputModule.Instance.ProcessCS(handDetector.inputDevicePartBase.inputDataBase.SCPointEventData, transform, LayerMask, MaxDetectDistance);
  129. IsFocusLocked = handDetector.inputDevicePartBase.inputDataBase.SCPointEventData.DownPressGameObject != null;
  130. if (handDetector.inputDevicePartBase.inputDataBase.SCPointEventData.DownPressGameObject || handDetector.inputDevicePartBase.inputDataBase.SCPointEventData.pointerCurrentRaycast.gameObject != null) {
  131. if (cursorBase && !cursorBase.IsModuleStarted) {
  132. cursorBase?.ModuleStart();
  133. }
  134. } else {
  135. if (cursorBase && cursorBase.IsModuleStarted) {
  136. cursorBase?.ModuleStop();
  137. }
  138. }
  139. }
  140. public override void OnSCDisable() {
  141. base.OnSCDisable();
  142. IsFocusLocked = false;
  143. InputDeviceHandPartEventBase.eventDelegate -= CatchEvent;
  144. }
  145. protected override void UpdateSubVisual() {
  146. //base.UpdateSubVisual();
  147. if (OutCameraShowHandLine && lineBase) {
  148. if (API_GSXR_Slam.SlamManager != null && API_GSXR_Slam.SlamManager.IsRunning) {
  149. if (!IsInCamera(Camera.main, cursorBase.transform.position) &&
  150. !IsInCamera(API_GSXR_Slam.SlamManager.rightCamera, cursorBase.transform.position)) {
  151. if (lineBase != null && !lineBase.IsModuleStarted) {
  152. lineBase.ModuleStart();
  153. }
  154. } else {
  155. if (lineBase != null && lineBase.IsModuleStarted) {
  156. lineBase.ModuleStop();
  157. }
  158. }
  159. }
  160. }
  161. }
  162. Vector3 viewPos;
  163. bool IsInCamera(Camera camera,Vector3 worldPosition) {
  164. viewPos = camera.WorldToViewportPoint(worldPosition);
  165. if (viewPos.z < 0)
  166. return false;
  167. if (viewPos.z > camera.farClipPlane)
  168. return false;
  169. if (viewPos.x < 0 || viewPos.y < 0 || viewPos.x > 1 || viewPos.y > 1)
  170. return false;
  171. return true;
  172. }
  173. void CatchEvent(InputDeviceHandPart inputDeviceHandPart, HandEventType eventType, float eventPercent) {
  174. //if (handDetector && handDetector.inputDeviceHandPart == inputDeviceHandPart) {
  175. // if (eventType == HandEventType.CatchReady) {
  176. // slowness = 0f;
  177. // if (eventPercent > 0.2f) {
  178. // slowness = 1f;
  179. // }
  180. // } else if (eventType == HandEventType.CatchDrag || eventType == HandEventType.CatchDown || eventType == HandEventType.CatchUp ) {
  181. // slowness = 0f;
  182. // }
  183. //}
  184. }
  185. }
  186. }