NearInteractionTouchable.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. boxCollider = gameObject.AddComponent<BoxCollider>();
  23. }
  24. }
  25. return boxCollider;
  26. }
  27. }
  28. public override float DistanceToTouchable(Vector3 samplePoint, out Vector3 normal)
  29. {
  30. normal = Normal;
  31. if (boxCollider == null)
  32. {
  33. boxCollider = GetComponent<BoxCollider>();
  34. if (boxCollider == null)
  35. {
  36. return float.PositiveInfinity;
  37. }
  38. }
  39. Vector3 worldForwardPlaneCenter;
  40. Vector3 localsamplePoint;
  41. float distance = 0;
  42. if(NormalType == NormalType.NZ) {
  43. worldForwardPlaneCenter = transform.position + transform.TransformVector(boxCollider.center + Vector3.forward * -boxCollider.size.z / 2f);
  44. } else if(NormalType == NormalType.Z) {
  45. worldForwardPlaneCenter = transform.position + transform.TransformVector(boxCollider.center + Vector3.forward * boxCollider.size.z / 2f);
  46. } else if(NormalType == NormalType.NY) {
  47. worldForwardPlaneCenter = transform.position + transform.TransformVector(boxCollider.center + Vector3.up * -boxCollider.size.y / 2f);
  48. } else if(NormalType == NormalType.Y) {
  49. worldForwardPlaneCenter = transform.position + transform.TransformVector(boxCollider.center + Vector3.up * boxCollider.size.y / 2f);
  50. } else if(NormalType == NormalType.NX) {
  51. worldForwardPlaneCenter = transform.position + transform.TransformVector(boxCollider.center + Vector3.right * -boxCollider.size.x / 2f);
  52. } else{
  53. worldForwardPlaneCenter = transform.position + transform.TransformVector(boxCollider.center + Vector3.right * boxCollider.size.x / 2f);
  54. }
  55. localsamplePoint = samplePoint - worldForwardPlaneCenter;
  56. distance = Vector3.Dot(localsamplePoint, Normal);
  57. // Get surface coordinates
  58. Vector3 planeSpacePoint = new Vector3(
  59. Vector3.Dot(localsamplePoint, transform.right),
  60. Vector3.Dot(localsamplePoint, transform.up),
  61. Vector3.Dot(localsamplePoint, transform.forward));
  62. if(NormalType == NormalType.NZ || NormalType == NormalType.Z) {
  63. if(planeSpacePoint.x < -(boxCollider.size.x * transform.lossyScale.x) / 2 ||
  64. planeSpacePoint.x > (boxCollider.size.x * transform.lossyScale.x) / 2 ||
  65. planeSpacePoint.y < -(boxCollider.size.y * transform.lossyScale.y) / 2 ||
  66. planeSpacePoint.y > (boxCollider.size.y * transform.lossyScale.y) / 2) {
  67. return float.PositiveInfinity;
  68. }
  69. } else if(NormalType == NormalType.NY || NormalType == NormalType.Y) {
  70. if(planeSpacePoint.z < -(boxCollider.size.z * transform.lossyScale.z) / 2 ||
  71. planeSpacePoint.z > (boxCollider.size.z * transform.lossyScale.z) / 2 ||
  72. planeSpacePoint.x < -(boxCollider.size.x * transform.lossyScale.x) / 2 ||
  73. planeSpacePoint.x > (boxCollider.size.x * transform.lossyScale.x) / 2) {
  74. return float.PositiveInfinity;
  75. }
  76. } else{
  77. if(planeSpacePoint.z < -(boxCollider.size.z * transform.lossyScale.z) / 2 ||
  78. planeSpacePoint.z > (boxCollider.size.z * transform.lossyScale.z) / 2 ||
  79. planeSpacePoint.y < -(boxCollider.size.y * transform.lossyScale.y) / 2 ||
  80. planeSpacePoint.y > (boxCollider.size.y * transform.lossyScale.y) / 2) {
  81. return float.PositiveInfinity;
  82. }
  83. }
  84. if (planeSpacePoint.x < -(boxCollider.size.x * transform.lossyScale.x) / 2 ||
  85. planeSpacePoint.x > (boxCollider.size.x * transform.lossyScale.x) / 2 ||
  86. planeSpacePoint.y < -(boxCollider.size.y * transform.lossyScale.y) / 2 ||
  87. planeSpacePoint.y > (boxCollider.size.y * transform.lossyScale.y) / 2)
  88. {
  89. return float.PositiveInfinity;
  90. }
  91. return Math.Abs(distance);
  92. }
  93. }
  94. }