No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PhysicsRaycaster.cs 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using System.Collections.Generic;
  2. using UnityEngine.UI;
  3. namespace UnityEngine.EventSystems
  4. {
  5. /// <summary>
  6. /// Simple event system using physics raycasts.
  7. /// </summary>
  8. [AddComponentMenu("Event/Physics Raycaster")]
  9. [RequireComponent(typeof(Camera))]
  10. /// <summary>
  11. /// Raycaster for casting against 3D Physics components.
  12. /// </summary>
  13. public class PhysicsRaycaster : BaseRaycaster
  14. {
  15. /// <summary>
  16. /// Const to use for clarity when no event mask is set
  17. /// </summary>
  18. protected const int kNoEventMaskSet = -1;
  19. protected Camera m_EventCamera;
  20. /// <summary>
  21. /// Layer mask used to filter events. Always combined with the camera's culling mask if a camera is used.
  22. /// </summary>
  23. [SerializeField]
  24. protected LayerMask m_EventMask = kNoEventMaskSet;
  25. /// <summary>
  26. /// The max number of intersections allowed. 0 = allocating version anything else is non alloc.
  27. /// </summary>
  28. [SerializeField]
  29. protected int m_MaxRayIntersections = 0;
  30. protected int m_LastMaxRayIntersections = 0;
  31. #if PACKAGE_PHYSICS
  32. RaycastHit[] m_Hits;
  33. #endif
  34. protected PhysicsRaycaster()
  35. {}
  36. public override Camera eventCamera
  37. {
  38. get
  39. {
  40. if (m_EventCamera == null)
  41. m_EventCamera = GetComponent<Camera>();
  42. if (m_EventCamera == null)
  43. return Camera.main;
  44. return m_EventCamera ;
  45. }
  46. }
  47. /// <summary>
  48. /// Depth used to determine the order of event processing.
  49. /// </summary>
  50. public virtual int depth
  51. {
  52. get { return (eventCamera != null) ? (int)eventCamera.depth : 0xFFFFFF; }
  53. }
  54. /// <summary>
  55. /// Event mask used to determine which objects will receive events.
  56. /// </summary>
  57. public int finalEventMask
  58. {
  59. get { return (eventCamera != null) ? eventCamera.cullingMask & m_EventMask : kNoEventMaskSet; }
  60. }
  61. /// <summary>
  62. /// Layer mask used to filter events. Always combined with the camera's culling mask if a camera is used.
  63. /// </summary>
  64. public LayerMask eventMask
  65. {
  66. get { return m_EventMask; }
  67. set { m_EventMask = value; }
  68. }
  69. /// <summary>
  70. /// Max number of ray intersection allowed to be found.
  71. /// </summary>
  72. /// <remarks>
  73. /// A value of zero will represent using the allocating version of the raycast function where as any other value will use the non allocating version.
  74. /// </remarks>
  75. public int maxRayIntersections
  76. {
  77. get { return m_MaxRayIntersections; }
  78. set { m_MaxRayIntersections = value; }
  79. }
  80. /// <summary>
  81. /// Returns a ray going from camera through the event position and the distance between the near and far clipping planes along that ray.
  82. /// </summary>
  83. /// <param name="eventData">The pointer event for which we will cast a ray.</param>
  84. /// <param name="ray">The ray to use.</param>
  85. /// <param name="eventDisplayIndex">The display index used.</param>
  86. /// <param name="distanceToClipPlane">The distance between the near and far clipping planes along the ray.</param>
  87. /// <returns>True if the operation was successful. false if it was not possible to compute, such as the eventPosition being outside of the view.</returns>
  88. protected bool ComputeRayAndDistance(PointerEventData eventData, ref Ray ray, ref int eventDisplayIndex, ref float distanceToClipPlane)
  89. {
  90. if (eventCamera == null)
  91. return false;
  92. var eventPosition = MultipleDisplayUtilities.RelativeMouseAtScaled(eventData.position, eventData.displayIndex);
  93. if (eventPosition != Vector3.zero)
  94. {
  95. // We support multiple display and display identification based on event position.
  96. eventDisplayIndex = (int)eventPosition.z;
  97. // Discard events that are not part of this display so the user does not interact with multiple displays at once.
  98. if (eventDisplayIndex != eventCamera.targetDisplay)
  99. return false;
  100. }
  101. else
  102. {
  103. // The multiple display system is not supported on all platforms, when it is not supported the returned position
  104. // will be all zeros so when the returned index is 0 we will default to the event data to be safe.
  105. eventPosition = eventData.position;
  106. }
  107. // Cull ray casts that are outside of the view rect. (case 636595)
  108. if (!eventCamera.pixelRect.Contains(eventPosition))
  109. return false;
  110. ray = eventCamera.ScreenPointToRay(eventPosition);
  111. // compensate far plane distance - see MouseEvents.cs
  112. float projectionDirection = ray.direction.z;
  113. distanceToClipPlane = Mathf.Approximately(0.0f, projectionDirection)
  114. ? Mathf.Infinity
  115. : Mathf.Abs((eventCamera.farClipPlane - eventCamera.nearClipPlane) / projectionDirection);
  116. return true;
  117. }
  118. public override void Raycast(PointerEventData eventData, List<RaycastResult> resultAppendList)
  119. {
  120. #if PACKAGE_PHYSICS
  121. Ray ray = new Ray();
  122. int displayIndex = 0;
  123. float distanceToClipPlane = 0;
  124. if (!ComputeRayAndDistance(eventData, ref ray, ref displayIndex, ref distanceToClipPlane))
  125. return;
  126. int hitCount = 0;
  127. if (m_MaxRayIntersections == 0)
  128. {
  129. if (ReflectionMethodsCache.Singleton.raycast3DAll == null)
  130. return;
  131. m_Hits = ReflectionMethodsCache.Singleton.raycast3DAll(ray, distanceToClipPlane, finalEventMask);
  132. hitCount = m_Hits.Length;
  133. }
  134. else
  135. {
  136. if (ReflectionMethodsCache.Singleton.getRaycastNonAlloc == null)
  137. return;
  138. if (m_LastMaxRayIntersections != m_MaxRayIntersections)
  139. {
  140. m_Hits = new RaycastHit[m_MaxRayIntersections];
  141. m_LastMaxRayIntersections = m_MaxRayIntersections;
  142. }
  143. hitCount = ReflectionMethodsCache.Singleton.getRaycastNonAlloc(ray, m_Hits, distanceToClipPlane, finalEventMask);
  144. }
  145. if (hitCount != 0)
  146. {
  147. if (hitCount > 1)
  148. System.Array.Sort(m_Hits, 0, hitCount, RaycastHitComparer.instance);
  149. for (int b = 0, bmax = hitCount; b < bmax; ++b)
  150. {
  151. var result = new RaycastResult
  152. {
  153. gameObject = m_Hits[b].collider.gameObject,
  154. module = this,
  155. distance = m_Hits[b].distance,
  156. worldPosition = m_Hits[b].point,
  157. worldNormal = m_Hits[b].normal,
  158. screenPosition = eventData.position,
  159. displayIndex = displayIndex,
  160. index = resultAppendList.Count,
  161. sortingLayer = 0,
  162. sortingOrder = 0
  163. };
  164. resultAppendList.Add(result);
  165. }
  166. }
  167. #endif
  168. }
  169. #if PACKAGE_PHYSICS
  170. private class RaycastHitComparer : IComparer<RaycastHit>
  171. {
  172. public static RaycastHitComparer instance = new RaycastHitComparer();
  173. public int Compare(RaycastHit x, RaycastHit y)
  174. {
  175. return x.distance.CompareTo(y.distance);
  176. }
  177. }
  178. #endif
  179. }
  180. }