OVRPhysicsRaycaster.cs 7.7 KB

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