OVRBoundary.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using VR = UnityEngine.VR;
  5. using System.Runtime.InteropServices;
  6. /// <summary>
  7. /// Provides access to the Oculus boundary system.
  8. /// </summary>
  9. public class OVRBoundary
  10. {
  11. /// <summary>
  12. /// Specifies a tracked node that can be queried through the boundary system.
  13. /// </summary>
  14. public enum Node
  15. {
  16. HandLeft = OVRPlugin.Node.HandLeft, ///< Tracks the left hand node.
  17. HandRight = OVRPlugin.Node.HandRight, ///< Tracks the right hand node.
  18. Head = OVRPlugin.Node.Head, ///< Tracks the head node.
  19. }
  20. /// <summary>
  21. /// Specifies a boundary type surface.
  22. /// </summary>
  23. public enum BoundaryType
  24. {
  25. OuterBoundary = OVRPlugin.BoundaryType.OuterBoundary, ///< Outer boundary that closely matches the user's configured walls.
  26. PlayArea = OVRPlugin.BoundaryType.PlayArea, ///< Smaller convex area inset within the outer boundary.
  27. }
  28. /// <summary>
  29. /// Provides test results of boundary system queries.
  30. /// </summary>
  31. public struct BoundaryTestResult
  32. {
  33. public bool IsTriggering; ///< Returns true if the queried test would violate and/or trigger the tested boundary types.
  34. public float ClosestDistance; ///< Returns the distance between the queried test object and the closest tested boundary type.
  35. public Vector3 ClosestPoint; ///< Returns the closest point to the queried test object.
  36. public Vector3 ClosestPointNormal; ///< Returns the normal of the closest point to the queried test object.
  37. }
  38. /// <summary>
  39. /// Specifies the boundary system parameters that can be configured. Can be overridden by the system or user.
  40. /// </summary>
  41. public struct BoundaryLookAndFeel
  42. {
  43. public Color Color;
  44. }
  45. /// <summary>
  46. /// Returns true if the boundary system is currently configured with valid boundary data.
  47. /// </summary>
  48. public bool GetConfigured()
  49. {
  50. return OVRPlugin.GetBoundaryConfigured();
  51. }
  52. /// <summary>
  53. /// Returns the results of testing a tracked node against the specified boundary type.
  54. /// All points are returned in local tracking space shared by tracked nodes and accessible through OVRCameraRig's trackingSpace anchor.
  55. /// </summary>
  56. public OVRBoundary.BoundaryTestResult TestNode(OVRBoundary.Node node, OVRBoundary.BoundaryType boundaryType)
  57. {
  58. OVRPlugin.BoundaryTestResult ovrpRes = OVRPlugin.TestBoundaryNode((OVRPlugin.Node)node, (OVRPlugin.BoundaryType)boundaryType);
  59. OVRBoundary.BoundaryTestResult res = new OVRBoundary.BoundaryTestResult()
  60. {
  61. IsTriggering = (ovrpRes.IsTriggering == OVRPlugin.Bool.True),
  62. ClosestDistance = ovrpRes.ClosestDistance,
  63. ClosestPoint = ovrpRes.ClosestPoint.FromFlippedZVector3f(),
  64. ClosestPointNormal = ovrpRes.ClosestPointNormal.FromFlippedZVector3f(),
  65. };
  66. return res;
  67. }
  68. /// <summary>
  69. /// Returns the results of testing a 3d point against the specified boundary type.
  70. /// The test point is expected in local tracking space.
  71. /// All points are returned in local tracking space shared by tracked nodes and accessible through OVRCameraRig's trackingSpace anchor.
  72. /// </summary>
  73. public OVRBoundary.BoundaryTestResult TestPoint(Vector3 point, OVRBoundary.BoundaryType boundaryType)
  74. {
  75. OVRPlugin.BoundaryTestResult ovrpRes = OVRPlugin.TestBoundaryPoint(point.ToFlippedZVector3f(), (OVRPlugin.BoundaryType)boundaryType);
  76. OVRBoundary.BoundaryTestResult res = new OVRBoundary.BoundaryTestResult()
  77. {
  78. IsTriggering = (ovrpRes.IsTriggering == OVRPlugin.Bool.True),
  79. ClosestDistance = ovrpRes.ClosestDistance,
  80. ClosestPoint = ovrpRes.ClosestPoint.FromFlippedZVector3f(),
  81. ClosestPointNormal = ovrpRes.ClosestPointNormal.FromFlippedZVector3f(),
  82. };
  83. return res;
  84. }
  85. /// <summary>
  86. /// Requests that the visual look and feel of the boundary system be changed as specified. Can be overridden by the system or user.
  87. /// </summary>
  88. public void SetLookAndFeel(OVRBoundary.BoundaryLookAndFeel lookAndFeel)
  89. {
  90. OVRPlugin.BoundaryLookAndFeel lf = new OVRPlugin.BoundaryLookAndFeel()
  91. {
  92. Color = lookAndFeel.Color.ToColorf()
  93. };
  94. OVRPlugin.SetBoundaryLookAndFeel(lf);
  95. }
  96. /// <summary>
  97. /// Resets the visual look and feel of the boundary system to the initial system settings.
  98. /// </summary>
  99. public void ResetLookAndFeel()
  100. {
  101. OVRPlugin.ResetBoundaryLookAndFeel();
  102. }
  103. private static int cachedVector3fSize = Marshal.SizeOf(typeof(OVRPlugin.Vector3f));
  104. private static OVRNativeBuffer cachedGeometryNativeBuffer = new OVRNativeBuffer(0);
  105. private static float[] cachedGeometryManagedBuffer = new float[0];
  106. /// <summary>
  107. /// Returns an array of 3d points (in clockwise order) that define the specified boundary type.
  108. /// All points are returned in local tracking space shared by tracked nodes and accessible through OVRCameraRig's trackingSpace anchor.
  109. /// </summary>
  110. public Vector3[] GetGeometry(OVRBoundary.BoundaryType boundaryType)
  111. {
  112. int pointsCount = 0;
  113. if (OVRPlugin.GetBoundaryGeometry2((OVRPlugin.BoundaryType)boundaryType, IntPtr.Zero, ref pointsCount))
  114. {
  115. if (pointsCount > 0)
  116. {
  117. int requiredNativeBufferCapacity = pointsCount * cachedVector3fSize;
  118. if (cachedGeometryNativeBuffer.GetCapacity() < requiredNativeBufferCapacity)
  119. cachedGeometryNativeBuffer.Reset(requiredNativeBufferCapacity);
  120. int requiredManagedBufferCapacity = pointsCount * 3;
  121. if (cachedGeometryManagedBuffer.Length < requiredManagedBufferCapacity)
  122. cachedGeometryManagedBuffer = new float[requiredManagedBufferCapacity];
  123. if (OVRPlugin.GetBoundaryGeometry2((OVRPlugin.BoundaryType)boundaryType, cachedGeometryNativeBuffer.GetPointer(), ref pointsCount))
  124. {
  125. Marshal.Copy(cachedGeometryNativeBuffer.GetPointer(), cachedGeometryManagedBuffer, 0, requiredManagedBufferCapacity);
  126. Vector3[] points = new Vector3[pointsCount];
  127. for (int i = 0; i < pointsCount; i++)
  128. {
  129. points[i] = new OVRPlugin.Vector3f()
  130. {
  131. x = cachedGeometryManagedBuffer[3 * i + 0],
  132. y = cachedGeometryManagedBuffer[3 * i + 1],
  133. z = cachedGeometryManagedBuffer[3 * i + 2],
  134. }.FromFlippedZVector3f();
  135. }
  136. return points;
  137. }
  138. }
  139. }
  140. return new Vector3[0];
  141. }
  142. /// <summary>
  143. /// Returns a vector that indicates the spatial dimensions of the specified boundary type. (x = width, y = height, z = depth)
  144. /// </summary>
  145. public Vector3 GetDimensions(OVRBoundary.BoundaryType boundaryType)
  146. {
  147. return OVRPlugin.GetBoundaryDimensions((OVRPlugin.BoundaryType)boundaryType).FromVector3f();
  148. }
  149. /// <summary>
  150. /// Returns true if the boundary system is currently visible.
  151. /// </summary>
  152. public bool GetVisible()
  153. {
  154. return OVRPlugin.GetBoundaryVisible();
  155. }
  156. /// <summary>
  157. /// Requests that the boundary system visibility be set to the specified value.
  158. /// The actual visibility can be overridden by the system (i.e., proximity trigger) or by the user (boundary system disabled)
  159. /// </summary>
  160. public void SetVisible(bool value)
  161. {
  162. OVRPlugin.SetBoundaryVisible(value);
  163. }
  164. }