GrabPointer.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.1f; }
  16. private float GrabRadius { get => 0.035f; }
  17. public bool IsNearObject { get; set; }
  18. public override PointerType PointerType =>PointerType.Grab;
  19. private Collider[] queryBuffer=new Collider[30];
  20. RaycastResult grabSimlateResult;
  21. Vector3 OverlapSphereCenter {
  22. get {
  23. return (handDetector.inputDeviceHandPart.inputDeviceHandPartUI.modelHand.ActiveHandModel.GetJointTransform(FINGER.forefinger, JOINT.One).position +
  24. handDetector.inputDeviceHandPart.inputDeviceHandPartUI.modelHand.ActiveHandModel.GetJointTransform(FINGER.thumb, JOINT.One).transform.position) / 2.0f;
  25. }
  26. }
  27. float closestDistance;
  28. Vector3 closestNormal;
  29. NearInterationGrabbable newClosestGrabbable;
  30. Vector3 closestPoint;
  31. Vector3 start, end;
  32. public override void OnSCStart() {
  33. //base.OnSCStart();
  34. //cursorBase?.ModuleStart();
  35. }
  36. public override void OnSCDisable() {
  37. base.OnSCDisable();
  38. IsFocusLocked = false;
  39. }
  40. protected override void UpdateTransform() {
  41. if(newClosestGrabbable) {
  42. start = OverlapSphereCenter;
  43. end = closestPoint;
  44. if(end - start != Vector3.zero) {
  45. transform.rotation = Quaternion.LookRotation(end - start);
  46. } else {
  47. transform.rotation = Quaternion.identity;
  48. }
  49. transform.position = start;
  50. }
  51. }
  52. protected override void DoTargetDetect() {
  53. if(IsFocusLocked || newClosestGrabbable) {
  54. SCInputModule.Instance.ProcessCS(handDetector.inputDevicePartBase.inputDataBase.SCPointEventData, transform, LayerMask, MaxDetectDistance, true, grabSimlateResult);
  55. IsFocusLocked = handDetector.inputDevicePartBase.inputDataBase.SCPointEventData.DownPressGameObject != null;
  56. }
  57. }
  58. public bool FindClosestGrabbableForLayerMask() {
  59. newClosestGrabbable = null;
  60. closestDistance = float.PositiveInfinity;
  61. IsNearObject = false;
  62. int num = Physics.OverlapSphereNonAlloc(OverlapSphereCenter, GrabActiveRadius, queryBuffer, LayerMask);
  63. for(int i = 0; i < num; i++) {
  64. var collider = queryBuffer[i];
  65. var nearInteration = collider.GetComponent<NearInterationGrabbable>();
  66. IsNearObject = nearInteration != null;
  67. if(IsNearObject) {
  68. break;
  69. }
  70. }
  71. if(IsNearObject == false) {
  72. return false;
  73. }
  74. num = Physics.OverlapSphereNonAlloc(OverlapSphereCenter, GrabRadius, queryBuffer, LayerMask);
  75. Vector3 objectHitPoint = Vector3.zero;
  76. for(int i = 0; i < num; i++) {
  77. var collider = queryBuffer[i];
  78. var touchable = collider.GetComponent<NearInterationGrabbable>();
  79. if(touchable) {
  80. objectHitPoint = collider.ClosestPoint(OverlapSphereCenter);
  81. float distance = (objectHitPoint - OverlapSphereCenter).magnitude;
  82. if(distance < closestDistance) {
  83. newClosestGrabbable = touchable;
  84. closestDistance = distance;
  85. closestPoint = objectHitPoint;
  86. }
  87. }
  88. }
  89. if(newClosestGrabbable != null) {
  90. //Debug.Log("newClosestGarbbable:" + closest.name);
  91. grabSimlateResult.gameObject = newClosestGrabbable.gameObject;
  92. grabSimlateResult.worldPosition = closestPoint;
  93. }
  94. return IsNearObject;
  95. }
  96. public bool TryGetNearGraspPoint(out Vector3 position) {
  97. throw new NotImplementedException();
  98. }
  99. public bool TryGetNearGraspAxis(out Vector3 axis) {
  100. throw new NotImplementedException();
  101. }
  102. public bool TryGetDistanceToNearestSurface(out float distance) {
  103. throw new NotImplementedException();
  104. }
  105. public bool TryGetNormalToNearestSurface(out Vector3 normal) {
  106. throw new NotImplementedException();
  107. }
  108. void OnDrawGizmos() {
  109. if(Application.isPlaying) {
  110. Gizmos.color = Color.black * 0.3f;
  111. Gizmos.DrawSphere(transform.position, 0.01f);
  112. Gizmos.color = Color.black * 0.2f;
  113. Gizmos.DrawSphere(OverlapSphereCenter, GrabRadius);
  114. Gizmos.color = Color.black * 0.1f;
  115. Gizmos.DrawSphere(OverlapSphereCenter, GrabActiveRadius);
  116. if(newClosestGrabbable) {
  117. Gizmos.color = Color.blue * 0.3f;
  118. Gizmos.DrawSphere(start, 0.02f);
  119. Gizmos.color = Color.blue * 0.2f;
  120. Gizmos.DrawSphere(end, 0.01f);
  121. }
  122. }
  123. }
  124. }
  125. }