GrabPointer.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 GrabPointer : PointerBase,INearPointer {
  10. public HandDetector handDetector {
  11. get {
  12. return detectorBase as HandDetector;
  13. }
  14. }
  15. public float GrabActiveRadius { get => 0.07f; }
  16. private float GrabRadius { get => 0.035f; }
  17. public bool IsNearObject { get; set; }
  18. public override PointerType PointerType =>PointerType.Grab;
  19. private Collider[] queryBuffer;
  20. private Collider[] smallerqueryBuffer = new Collider[60];
  21. private Collider[] bigqueryBuffer = new Collider[180];
  22. RaycastResult grabSimlateResult;
  23. Vector3 OverlapSphereCenter {
  24. get {
  25. return handDetector.inputDeviceHandPart.inputDataHand.handInfo.MainGrabPosition;
  26. }
  27. }
  28. float closestDistance;
  29. Vector3 closestNormal;
  30. NearInterationGrabbable newClosestGrabbable;
  31. Vector3 closestPoint;
  32. Vector3 start, end;
  33. public override void OnSCAwake() {
  34. base.OnSCAwake();
  35. queryBuffer = smallerqueryBuffer;
  36. }
  37. public override void OnSCStart() {
  38. base.OnSCStart();
  39. queryBuffer = smallerqueryBuffer;
  40. lineBase?.ModuleStop();
  41. }
  42. public override void OnSCLateUpdate() {
  43. base.OnSCLateUpdate();
  44. if (newClosestGrabbable) {
  45. if (cursorBase && !cursorBase.IsModuleStarted) {
  46. cursorBase.ModuleStart();
  47. }
  48. } else {
  49. if (cursorBase && cursorBase.IsModuleStarted) {
  50. cursorBase.ModuleStop();
  51. }
  52. }
  53. }
  54. public override void OnSCDisable() {
  55. base.OnSCDisable();
  56. IsFocusLocked = false;
  57. }
  58. protected override void UpdateTransform() {
  59. start = OverlapSphereCenter;
  60. transform.position = start;
  61. transform.rotation = Quaternion.LookRotation(start - Camera.main.transform.position, Camera.main.transform.up);
  62. //if (newClosestGrabbable) {
  63. // end = closestPoint;
  64. // if (end - start != Vector3.zero) {
  65. // transform.rotation = Quaternion.LookRotation(end - start);
  66. // } else {
  67. // transform.rotation = Quaternion.identity;
  68. // }
  69. // transform.rotation = newClosestGrabbable.transform.rotation;
  70. //} else {
  71. // transform.rotation = handDetector.inputDeviceHandPart.inputDataHand.handInfo.MainGrabRotation;
  72. //}
  73. }
  74. protected override void DoTargetDetect()
  75. {
  76. Debug.Log("DoTargetDetect2222===");
  77. if (IsFocusLocked || newClosestGrabbable) {
  78. SCInputModule.Instance.ProcessCS(handDetector.inputDevicePartBase.inputDataBase.SCPointEventData, transform, LayerMask, MaxDetectDistance, true, grabSimlateResult);
  79. IsFocusLocked = handDetector.inputDevicePartBase.inputDataBase.SCPointEventData.DownPressGameObject != null;
  80. }
  81. }
  82. public bool FindClosestGrabbableForLayerMask() {
  83. newClosestGrabbable = null;
  84. closestDistance = float.PositiveInfinity;
  85. IsNearObject = false;
  86. Array.Clear(queryBuffer, 0, queryBuffer.Length);
  87. int numColliders = Physics.OverlapSphereNonAlloc(OverlapSphereCenter, GrabActiveRadius, queryBuffer, LayerMask);
  88. if (numColliders == queryBuffer.Length) {
  89. queryBuffer = bigqueryBuffer;
  90. Debug.LogWarning($"Maximum number of {numColliders} colliders found in GrabPointer overlap query. Consider increasing the query buffer size in the input system settings.");
  91. numColliders = Physics.OverlapSphereNonAlloc(OverlapSphereCenter, GrabActiveRadius, queryBuffer, LayerMask);
  92. }
  93. for (int i = 0; i < numColliders; i++) {
  94. var collider = queryBuffer[i];
  95. var nearInteration = collider.GetComponent<NearInterationGrabbable>();
  96. IsNearObject = nearInteration != null;
  97. if(IsNearObject) {
  98. break;
  99. }
  100. }
  101. if(IsNearObject == false) {
  102. return false;
  103. }
  104. numColliders = Physics.OverlapSphereNonAlloc(OverlapSphereCenter, GrabRadius, queryBuffer, LayerMask);
  105. Vector3 objectHitPoint = Vector3.zero;
  106. for(int i = 0; i < numColliders; i++) {
  107. var collider = queryBuffer[i];
  108. var touchable = collider.GetComponent<NearInterationGrabbable>();
  109. if(touchable) {
  110. objectHitPoint = collider.ClosestPoint(OverlapSphereCenter);
  111. float distance = (objectHitPoint - OverlapSphereCenter).magnitude;
  112. if(distance < closestDistance) {
  113. newClosestGrabbable = touchable;
  114. closestDistance = distance;
  115. closestPoint = objectHitPoint;
  116. }
  117. }
  118. }
  119. if(newClosestGrabbable != null) {
  120. //Debug.Log("newClosestGarbbable:" + closest.name);
  121. grabSimlateResult.gameObject = newClosestGrabbable.gameObject;
  122. grabSimlateResult.worldPosition = closestPoint;
  123. }
  124. return IsNearObject;
  125. }
  126. public bool TryGetNearGraspPoint(out Vector3 position) {
  127. throw new NotImplementedException();
  128. }
  129. public bool TryGetNearGraspAxis(out Vector3 axis) {
  130. throw new NotImplementedException();
  131. }
  132. public bool TryGetDistanceToNearestSurface(out float distance) {
  133. throw new NotImplementedException();
  134. }
  135. public bool TryGetNormalToNearestSurface(out Vector3 normal) {
  136. throw new NotImplementedException();
  137. }
  138. void OnDrawGizmos() {
  139. if(Application.isPlaying) {
  140. Gizmos.color = Color.black * 0.3f;
  141. Gizmos.DrawSphere(transform.position, 0.01f);
  142. Gizmos.color = Color.black * 0.2f;
  143. Gizmos.DrawSphere(OverlapSphereCenter, GrabRadius);
  144. Gizmos.color = Color.black * 0.1f;
  145. Gizmos.DrawSphere(OverlapSphereCenter, GrabActiveRadius);
  146. if(newClosestGrabbable) {
  147. Gizmos.color = Color.blue * 0.3f;
  148. Gizmos.DrawSphere(start, 0.02f);
  149. Gizmos.color = Color.blue * 0.2f;
  150. Gizmos.DrawSphere(end, 0.01f);
  151. }
  152. }
  153. }
  154. }
  155. }