Assignment for RMIT Mixed Reality in 2020
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.

179 lines
6.3 KiB

  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. /// <summary>
  23. /// Const to use for clarity when no event mask is set
  24. /// </summary>
  25. protected const int kNoEventMaskSet = -1;
  26. /// <summary>
  27. /// Layer mask used to filter events. Always combined with the camera's culling mask if a camera is used.
  28. /// </summary>
  29. [SerializeField]
  30. protected LayerMask m_EventMask = kNoEventMaskSet;
  31. protected OVRPhysicsRaycaster()
  32. { }
  33. public override Camera eventCamera
  34. {
  35. get
  36. {
  37. return GetComponent<OVRCameraRig>().leftEyeCamera;
  38. }
  39. }
  40. /// <summary>
  41. /// Depth used to determine the order of event processing.
  42. /// </summary>
  43. public virtual int depth
  44. {
  45. get { return (eventCamera != null) ? (int)eventCamera.depth : 0xFFFFFF; }
  46. }
  47. public int sortOrder = 0;
  48. public override int sortOrderPriority
  49. {
  50. get
  51. {
  52. return sortOrder;
  53. }
  54. }
  55. /// <summary>
  56. /// Event mask used to determine which objects will receive events.
  57. /// </summary>
  58. public int finalEventMask
  59. {
  60. get { return (eventCamera != null) ? eventCamera.cullingMask & m_EventMask : kNoEventMaskSet; }
  61. }
  62. /// <summary>
  63. /// Layer mask used to filter events. Always combined with the camera's culling mask if a camera is used.
  64. /// </summary>
  65. public LayerMask eventMask
  66. {
  67. get { return m_EventMask; }
  68. set { m_EventMask = value; }
  69. }
  70. /// <summary>
  71. /// Perform a raycast using the worldSpaceRay in eventData.
  72. /// </summary>
  73. /// <param name="eventData"></param>
  74. /// <param name="resultAppendList"></param>
  75. public override void Raycast(PointerEventData eventData, List<RaycastResult> resultAppendList)
  76. {
  77. // This function is closely based on PhysicsRaycaster.Raycast
  78. if (eventCamera == null)
  79. return;
  80. if (!eventData.IsVRPointer())
  81. return;
  82. var ray = eventData.GetRay();
  83. float dist = eventCamera.farClipPlane - eventCamera.nearClipPlane;
  84. var hits = Physics.RaycastAll(ray, dist, finalEventMask);
  85. if (hits.Length > 1)
  86. System.Array.Sort(hits, (r1, r2) => r1.distance.CompareTo(r2.distance));
  87. if (hits.Length != 0)
  88. {
  89. for (int b = 0, bmax = hits.Length; b < bmax; ++b)
  90. {
  91. var result = new RaycastResult
  92. {
  93. gameObject = hits[b].collider.gameObject,
  94. module = this,
  95. distance = hits[b].distance,
  96. index = resultAppendList.Count,
  97. worldPosition = hits[0].point,
  98. worldNormal = hits[0].normal,
  99. };
  100. resultAppendList.Add(result);
  101. }
  102. }
  103. }
  104. /// <summary>
  105. /// Perform a Spherecast using the worldSpaceRay in eventData.
  106. /// </summary>
  107. /// <param name="eventData"></param>
  108. /// <param name="resultAppendList"></param>
  109. /// <param name="radius">Radius of the sphere</param>
  110. public void Spherecast(PointerEventData eventData, List<RaycastResult> resultAppendList, float radius)
  111. {
  112. if (eventCamera == null)
  113. return;
  114. if (!eventData.IsVRPointer())
  115. return;
  116. var ray = eventData.GetRay();
  117. float dist = eventCamera.farClipPlane - eventCamera.nearClipPlane;
  118. var hits = Physics.SphereCastAll(ray, radius, dist, finalEventMask);
  119. if (hits.Length > 1)
  120. System.Array.Sort(hits, (r1, r2) => r1.distance.CompareTo(r2.distance));
  121. if (hits.Length != 0)
  122. {
  123. for (int b = 0, bmax = hits.Length; b < bmax; ++b)
  124. {
  125. var result = new RaycastResult
  126. {
  127. gameObject = hits[b].collider.gameObject,
  128. module = this,
  129. distance = hits[b].distance,
  130. index = resultAppendList.Count,
  131. worldPosition = hits[0].point,
  132. worldNormal = hits[0].normal,
  133. };
  134. resultAppendList.Add(result);
  135. }
  136. }
  137. }
  138. /// <summary>
  139. /// Get screen position of this world position as seen by the event camera of this OVRPhysicsRaycaster
  140. /// </summary>
  141. /// <param name="worldPosition"></param>
  142. /// <returns></returns>
  143. public Vector2 GetScreenPos(Vector3 worldPosition)
  144. {
  145. // In future versions of Uinty RaycastResult will contain screenPosition so this will not be necessary
  146. return eventCamera.WorldToScreenPoint(worldPosition);
  147. }
  148. }
  149. }