OVRBoundary.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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;
  14. using System.Collections.Generic;
  15. using UnityEngine;
  16. using VR = UnityEngine.VR;
  17. using System.Runtime.InteropServices;
  18. #if UNITY_2017_2_OR_NEWER
  19. using Boundary = UnityEngine.Experimental.XR.Boundary;
  20. #elif UNITY_2017_1_OR_NEWER
  21. using Boundary = UnityEngine.Experimental.VR.Boundary;
  22. #endif
  23. /// <summary>
  24. /// Provides access to the Oculus boundary system.
  25. /// </summary>
  26. public class OVRBoundary
  27. {
  28. /// <summary>
  29. /// Specifies a tracked node that can be queried through the boundary system.
  30. /// </summary>
  31. public enum Node
  32. {
  33. HandLeft = OVRPlugin.Node.HandLeft, ///< Tracks the left hand node.
  34. HandRight = OVRPlugin.Node.HandRight, ///< Tracks the right hand node.
  35. Head = OVRPlugin.Node.Head, ///< Tracks the head node.
  36. }
  37. /// <summary>
  38. /// Specifies a boundary type surface.
  39. /// </summary>
  40. public enum BoundaryType
  41. {
  42. OuterBoundary = OVRPlugin.BoundaryType.OuterBoundary, ///< Outer boundary that closely matches the user's configured walls.
  43. PlayArea = OVRPlugin.BoundaryType.PlayArea, ///< Smaller convex area inset within the outer boundary.
  44. }
  45. /// <summary>
  46. /// Provides test results of boundary system queries.
  47. /// </summary>
  48. public struct BoundaryTestResult
  49. {
  50. public bool IsTriggering; ///< Returns true if the queried test would violate and/or trigger the tested boundary types.
  51. public float ClosestDistance; ///< Returns the distance between the queried test object and the closest tested boundary type.
  52. public Vector3 ClosestPoint; ///< Returns the closest point to the queried test object.
  53. public Vector3 ClosestPointNormal; ///< Returns the normal of the closest point to the queried test object.
  54. }
  55. /// <summary>
  56. /// Returns true if the boundary system is currently configured with valid boundary data.
  57. /// </summary>
  58. public bool GetConfigured()
  59. {
  60. if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus)
  61. return OVRPlugin.GetBoundaryConfigured();
  62. else
  63. {
  64. #if UNITY_2017_1_OR_NEWER
  65. return Boundary.configured;
  66. #else
  67. return false;
  68. #endif
  69. }
  70. }
  71. /// <summary>
  72. /// Returns the results of testing a tracked node against the specified boundary type.
  73. /// All points are returned in local tracking space shared by tracked nodes and accessible through OVRCameraRig's trackingSpace anchor.
  74. /// </summary>
  75. public OVRBoundary.BoundaryTestResult TestNode(OVRBoundary.Node node, OVRBoundary.BoundaryType boundaryType)
  76. {
  77. OVRPlugin.BoundaryTestResult ovrpRes = OVRPlugin.TestBoundaryNode((OVRPlugin.Node)node, (OVRPlugin.BoundaryType)boundaryType);
  78. OVRBoundary.BoundaryTestResult res = new OVRBoundary.BoundaryTestResult()
  79. {
  80. IsTriggering = (ovrpRes.IsTriggering == OVRPlugin.Bool.True),
  81. ClosestDistance = ovrpRes.ClosestDistance,
  82. ClosestPoint = ovrpRes.ClosestPoint.FromFlippedZVector3f(),
  83. ClosestPointNormal = ovrpRes.ClosestPointNormal.FromFlippedZVector3f(),
  84. };
  85. return res;
  86. }
  87. /// <summary>
  88. /// Returns the results of testing a 3d point against the specified boundary type.
  89. /// The test point is expected in local tracking space.
  90. /// All points are returned in local tracking space shared by tracked nodes and accessible through OVRCameraRig's trackingSpace anchor.
  91. /// </summary>
  92. public OVRBoundary.BoundaryTestResult TestPoint(Vector3 point, OVRBoundary.BoundaryType boundaryType)
  93. {
  94. OVRPlugin.BoundaryTestResult ovrpRes = OVRPlugin.TestBoundaryPoint(point.ToFlippedZVector3f(), (OVRPlugin.BoundaryType)boundaryType);
  95. OVRBoundary.BoundaryTestResult res = new OVRBoundary.BoundaryTestResult()
  96. {
  97. IsTriggering = (ovrpRes.IsTriggering == OVRPlugin.Bool.True),
  98. ClosestDistance = ovrpRes.ClosestDistance,
  99. ClosestPoint = ovrpRes.ClosestPoint.FromFlippedZVector3f(),
  100. ClosestPointNormal = ovrpRes.ClosestPointNormal.FromFlippedZVector3f(),
  101. };
  102. return res;
  103. }
  104. private static int cachedVector3fSize = Marshal.SizeOf(typeof(OVRPlugin.Vector3f));
  105. private static OVRNativeBuffer cachedGeometryNativeBuffer = new OVRNativeBuffer(0);
  106. private static float[] cachedGeometryManagedBuffer = new float[0];
  107. private List<Vector3> cachedGeometryList = new List<Vector3>();
  108. /// <summary>
  109. /// Returns an array of 3d points (in clockwise order) that define the specified boundary type.
  110. /// All points are returned in local tracking space shared by tracked nodes and accessible through OVRCameraRig's trackingSpace anchor.
  111. /// </summary>
  112. public Vector3[] GetGeometry(OVRBoundary.BoundaryType boundaryType)
  113. {
  114. if (OVRManager.loadedXRDevice != OVRManager.XRDevice.Oculus)
  115. {
  116. #if UNITY_2017_1_OR_NEWER
  117. if (Boundary.TryGetGeometry(cachedGeometryList, (boundaryType == BoundaryType.PlayArea) ? Boundary.Type.PlayArea : Boundary.Type.TrackedArea))
  118. {
  119. Vector3[] arr = cachedGeometryList.ToArray();
  120. return arr;
  121. }
  122. #endif
  123. Debug.LogError("This functionality is not supported in your current version of Unity.");
  124. return null;
  125. }
  126. int pointsCount = 0;
  127. if (OVRPlugin.GetBoundaryGeometry2((OVRPlugin.BoundaryType)boundaryType, IntPtr.Zero, ref pointsCount))
  128. {
  129. if (pointsCount > 0)
  130. {
  131. int requiredNativeBufferCapacity = pointsCount * cachedVector3fSize;
  132. if (cachedGeometryNativeBuffer.GetCapacity() < requiredNativeBufferCapacity)
  133. cachedGeometryNativeBuffer.Reset(requiredNativeBufferCapacity);
  134. int requiredManagedBufferCapacity = pointsCount * 3;
  135. if (cachedGeometryManagedBuffer.Length < requiredManagedBufferCapacity)
  136. cachedGeometryManagedBuffer = new float[requiredManagedBufferCapacity];
  137. if (OVRPlugin.GetBoundaryGeometry2((OVRPlugin.BoundaryType)boundaryType, cachedGeometryNativeBuffer.GetPointer(), ref pointsCount))
  138. {
  139. Marshal.Copy(cachedGeometryNativeBuffer.GetPointer(), cachedGeometryManagedBuffer, 0, requiredManagedBufferCapacity);
  140. Vector3[] points = new Vector3[pointsCount];
  141. for (int i = 0; i < pointsCount; i++)
  142. {
  143. points[i] = new OVRPlugin.Vector3f()
  144. {
  145. x = cachedGeometryManagedBuffer[3 * i + 0],
  146. y = cachedGeometryManagedBuffer[3 * i + 1],
  147. z = cachedGeometryManagedBuffer[3 * i + 2],
  148. }.FromFlippedZVector3f();
  149. }
  150. return points;
  151. }
  152. }
  153. }
  154. return new Vector3[0];
  155. }
  156. /// <summary>
  157. /// Returns a vector that indicates the spatial dimensions of the specified boundary type. (x = width, y = height, z = depth)
  158. /// </summary>
  159. public Vector3 GetDimensions(OVRBoundary.BoundaryType boundaryType)
  160. {
  161. if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus)
  162. return OVRPlugin.GetBoundaryDimensions((OVRPlugin.BoundaryType)boundaryType).FromVector3f();
  163. else
  164. {
  165. #if UNITY_2017_1_OR_NEWER
  166. Vector3 dimensions;
  167. if (Boundary.TryGetDimensions(out dimensions, (boundaryType == BoundaryType.PlayArea) ? Boundary.Type.PlayArea : Boundary.Type.TrackedArea))
  168. return dimensions;
  169. #endif
  170. return Vector3.zero;
  171. }
  172. }
  173. /// <summary>
  174. /// Returns true if the boundary system is currently visible.
  175. /// </summary>
  176. public bool GetVisible()
  177. {
  178. if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus)
  179. return OVRPlugin.GetBoundaryVisible();
  180. else
  181. {
  182. #if UNITY_2017_1_OR_NEWER
  183. return Boundary.visible;
  184. #else
  185. return false;
  186. #endif
  187. }
  188. }
  189. /// <summary>
  190. /// Requests that the boundary system visibility be set to the specified value.
  191. /// The actual visibility can be overridden by the system (i.e., proximity trigger) or by the user (boundary system disabled)
  192. /// </summary>
  193. public void SetVisible(bool value)
  194. {
  195. if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus)
  196. OVRPlugin.SetBoundaryVisible(value);
  197. else
  198. {
  199. #if UNITY_2017_1_OR_NEWER
  200. Boundary.visible = value;
  201. #endif
  202. }
  203. }
  204. }