OVRPhysicsRaycaster.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /************************************************************************************
  2. Copyright : Copyright 2017 Oculus VR, LLC. All Rights reserved.
  3. Licensed under the Oculus VR Rift SDK License Version 3.4.1 (the "License");
  4. you may not use the Oculus VR Rift SDK except in compliance with the License,
  5. which is provided at the time of installation or download, or which
  6. otherwise accompanies this software in either electronic or hard copy form.
  7. You may obtain a copy of the License at
  8. https://developer.oculus.com/licenses/sdk-3.4.1
  9. Unless required by applicable law or agreed to in writing, the Oculus VR SDK
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ************************************************************************************/
  15. using System.Collections.Generic;
  16. namespace UnityEngine.EventSystems
  17. {
  18. /// <summary>
  19. /// Simple event system using physics raycasts. Very closely based on UnityEngine.EventSystems.PhysicsRaycaster
  20. /// </summary>
  21. [RequireComponent(typeof(OVRCameraRig))]
  22. public class OVRPhysicsRaycaster : BaseRaycaster
  23. {
  24. /// <summary>
  25. /// Const to use for clarity when no event mask is set
  26. /// </summary>
  27. protected const int kNoEventMaskSet = -1;
  28. /// <summary>
  29. /// Layer mask used to filter events. Always combined with the camera's culling mask if a camera is used.
  30. /// </summary>
  31. [SerializeField]
  32. protected LayerMask m_EventMask = kNoEventMaskSet;
  33. protected OVRPhysicsRaycaster()
  34. { }
  35. public override Camera eventCamera
  36. {
  37. get
  38. {
  39. return GetComponent<OVRCameraRig>().leftEyeCamera;
  40. }
  41. }
  42. /// <summary>
  43. /// Depth used to determine the order of event processing.
  44. /// </summary>
  45. public virtual int depth
  46. {
  47. get { return (eventCamera != null) ? (int)eventCamera.depth : 0xFFFFFF; }
  48. }
  49. public int sortOrder = 20;
  50. public override int sortOrderPriority
  51. {
  52. get
  53. {
  54. return sortOrder;
  55. }
  56. }
  57. /// <summary>
  58. /// Event mask used to determine which objects will receive events.
  59. /// </summary>
  60. public int finalEventMask
  61. {
  62. get { return (eventCamera != null) ? eventCamera.cullingMask & m_EventMask : kNoEventMaskSet; }
  63. }
  64. /// <summary>
  65. /// Layer mask used to filter events. Always combined with the camera's culling mask if a camera is used.
  66. /// </summary>
  67. public LayerMask eventMask
  68. {
  69. get { return m_EventMask; }
  70. set { m_EventMask = value; }
  71. }
  72. /// <summary>
  73. /// Perform a raycast using the worldSpaceRay in eventData.
  74. /// </summary>
  75. /// <param name="eventData"></param>
  76. /// <param name="resultAppendList"></param>
  77. public override void Raycast(PointerEventData eventData, List<RaycastResult> resultAppendList)
  78. {
  79. // This function is closely based on PhysicsRaycaster.Raycast
  80. if (eventCamera == null)
  81. return;
  82. if (!eventData.IsVRPointer())
  83. return;
  84. var ray = eventData.GetRay();
  85. float dist = eventCamera.farClipPlane - eventCamera.nearClipPlane;
  86. var hits = Physics.RaycastAll(ray, dist, finalEventMask);
  87. if (hits.Length > 1)
  88. System.Array.Sort(hits, (r1, r2) => r1.distance.CompareTo(r2.distance));
  89. if (hits.Length != 0)
  90. {
  91. for (int b = 0, bmax = hits.Length; b < bmax; ++b)
  92. {
  93. var result = new RaycastResult
  94. {
  95. gameObject = hits[b].collider.gameObject,
  96. module = this,
  97. distance = hits[b].distance,
  98. index = resultAppendList.Count,
  99. worldPosition = hits[0].point,
  100. worldNormal = hits[0].normal,
  101. };
  102. resultAppendList.Add(result);
  103. }
  104. }
  105. }
  106. /// <summary>
  107. /// Perform a Spherecast using the worldSpaceRay in eventData.
  108. /// </summary>
  109. /// <param name="eventData"></param>
  110. /// <param name="resultAppendList"></param>
  111. /// <param name="radius">Radius of the sphere</param>
  112. public void Spherecast(PointerEventData eventData, List<RaycastResult> resultAppendList, float radius)
  113. {
  114. if (eventCamera == null)
  115. return;
  116. if (!eventData.IsVRPointer())
  117. return;
  118. var ray = eventData.GetRay();
  119. float dist = eventCamera.farClipPlane - eventCamera.nearClipPlane;
  120. var hits = Physics.SphereCastAll(ray, radius, dist, finalEventMask);
  121. if (hits.Length > 1)
  122. System.Array.Sort(hits, (r1, r2) => r1.distance.CompareTo(r2.distance));
  123. if (hits.Length != 0)
  124. {
  125. for (int b = 0, bmax = hits.Length; b < bmax; ++b)
  126. {
  127. var result = new RaycastResult
  128. {
  129. gameObject = hits[b].collider.gameObject,
  130. module = this,
  131. distance = hits[b].distance,
  132. index = resultAppendList.Count,
  133. worldPosition = hits[0].point,
  134. worldNormal = hits[0].normal,
  135. };
  136. resultAppendList.Add(result);
  137. }
  138. }
  139. }
  140. /// <summary>
  141. /// Get screen position of this world position as seen by the event camera of this OVRPhysicsRaycaster
  142. /// </summary>
  143. /// <param name="worldPosition"></param>
  144. /// <returns></returns>
  145. public Vector2 GetScreenPos(Vector3 worldPosition)
  146. {
  147. // In future versions of Uinty RaycastResult will contain screenPosition so this will not be necessary
  148. return eventCamera.WorldToScreenPoint(worldPosition);
  149. }
  150. }
  151. }