NearInteractionTouchable.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. namespace SC.XR.Unity.Module_InputSystem
  8. {
  9. public class NearInteractionTouchable : BaseNearInteractionTouchable
  10. {
  11. //[SerializeField]
  12. BoxCollider boxCollider;
  13. public BoxCollider BoxCollider
  14. {
  15. get
  16. {
  17. if (boxCollider == null)
  18. {
  19. boxCollider = gameObject.GetComponent<BoxCollider>();
  20. if (boxCollider == null)
  21. {
  22. Debug.Log("添加BOX");
  23. boxCollider = gameObject.AddComponent<BoxCollider>();
  24. }
  25. }
  26. return boxCollider;
  27. }
  28. }
  29. public override float DistanceToTouchable(Vector3 samplePoint, out Vector3 normal)
  30. {
  31. normal = Normal;
  32. if (boxCollider == null)
  33. {
  34. boxCollider = GetComponent<BoxCollider>();
  35. if (boxCollider == null)
  36. {
  37. return float.PositiveInfinity;
  38. }
  39. }
  40. Vector3 worldForwardPlaneCenter;
  41. Vector3 localsamplePoint;
  42. float distance = 0;
  43. if(NormalType == NormalType.NZ) {
  44. worldForwardPlaneCenter = transform.position + transform.TransformVector(boxCollider.center + Vector3.forward * -boxCollider.size.z / 2f);
  45. } else if(NormalType == NormalType.Z) {
  46. worldForwardPlaneCenter = transform.position + transform.TransformVector(boxCollider.center + Vector3.forward * boxCollider.size.z / 2f);
  47. } else if(NormalType == NormalType.NY) {
  48. worldForwardPlaneCenter = transform.position + transform.TransformVector(boxCollider.center + Vector3.up * -boxCollider.size.y / 2f);
  49. } else if(NormalType == NormalType.Y) {
  50. worldForwardPlaneCenter = transform.position + transform.TransformVector(boxCollider.center + Vector3.up * boxCollider.size.y / 2f);
  51. } else if(NormalType == NormalType.NX) {
  52. worldForwardPlaneCenter = transform.position + transform.TransformVector(boxCollider.center + Vector3.right * -boxCollider.size.x / 2f);
  53. } else{
  54. worldForwardPlaneCenter = transform.position + transform.TransformVector(boxCollider.center + Vector3.right * boxCollider.size.x / 2f);
  55. }
  56. localsamplePoint = samplePoint - worldForwardPlaneCenter;
  57. distance = Vector3.Dot(localsamplePoint, Normal);
  58. // Get surface coordinates
  59. Vector3 planeSpacePoint = new Vector3(
  60. Vector3.Dot(localsamplePoint, transform.right),
  61. Vector3.Dot(localsamplePoint, transform.up),
  62. Vector3.Dot(localsamplePoint, transform.forward));
  63. if(NormalType == NormalType.NZ || NormalType == NormalType.Z) {
  64. if(planeSpacePoint.x < -(boxCollider.size.x * transform.lossyScale.x) / 2 ||
  65. planeSpacePoint.x > (boxCollider.size.x * transform.lossyScale.x) / 2 ||
  66. planeSpacePoint.y < -(boxCollider.size.y * transform.lossyScale.y) / 2 ||
  67. planeSpacePoint.y > (boxCollider.size.y * transform.lossyScale.y) / 2) {
  68. return float.PositiveInfinity;
  69. }
  70. } else if(NormalType == NormalType.NY || NormalType == NormalType.Y) {
  71. if(planeSpacePoint.z < -(boxCollider.size.z * transform.lossyScale.z) / 2 ||
  72. planeSpacePoint.z > (boxCollider.size.z * transform.lossyScale.z) / 2 ||
  73. planeSpacePoint.x < -(boxCollider.size.x * transform.lossyScale.x) / 2 ||
  74. planeSpacePoint.x > (boxCollider.size.x * transform.lossyScale.x) / 2) {
  75. return float.PositiveInfinity;
  76. }
  77. } else{
  78. if(planeSpacePoint.z < -(boxCollider.size.z * transform.lossyScale.z) / 2 ||
  79. planeSpacePoint.z > (boxCollider.size.z * transform.lossyScale.z) / 2 ||
  80. planeSpacePoint.y < -(boxCollider.size.y * transform.lossyScale.y) / 2 ||
  81. planeSpacePoint.y > (boxCollider.size.y * transform.lossyScale.y) / 2) {
  82. return float.PositiveInfinity;
  83. }
  84. }
  85. if (planeSpacePoint.x < -(boxCollider.size.x * transform.lossyScale.x) / 2 ||
  86. planeSpacePoint.x > (boxCollider.size.x * transform.lossyScale.x) / 2 ||
  87. planeSpacePoint.y < -(boxCollider.size.y * transform.lossyScale.y) / 2 ||
  88. planeSpacePoint.y > (boxCollider.size.y * transform.lossyScale.y) / 2)
  89. {
  90. return float.PositiveInfinity;
  91. }
  92. return Math.Abs(distance);
  93. }
  94. }
  95. }