| 1 | /************************************************************************************ |
| 2 | Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved. |
| 3 | |
| 4 | Licensed under the Oculus Utilities SDK License Version 1.31 (the "License"); you may not use |
| 5 | the Utilities SDK except in compliance with the License, which is provided at the time of installation |
| 6 | or download, or which otherwise accompanies this software in either electronic or hard copy form. |
| 7 | |
| 8 | You may obtain a copy of the License at |
| 9 | https://developer.oculus.com/licenses/utilities-1.31 |
| 10 | |
| 11 | Unless required by applicable law or agreed to in writing, the Utilities SDK distributed |
| 12 | under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 13 | ANY KIND, either express or implied. See the License for the specific language governing |
| 14 | permissions and limitations under the License. |
| 15 | ************************************************************************************/ |
| 16 | |
| 17 | |
| 18 | using System; |
| 19 | using System.Collections.Generic; |
| 20 | using UnityEngine; |
| 21 | using VR = UnityEngine.VR; |
| 22 | using System.Runtime.InteropServices; |
| 23 | #if UNITY_2017_2_OR_NEWER |
| 24 | using Boundary = UnityEngine.Experimental.XR.Boundary; |
| 25 | #elif UNITY_2017_1_OR_NEWER |
| 26 | using Boundary = UnityEngine.Experimental.VR.Boundary; |
| 27 | #endif |
| 28 | |
| 29 | /// <summary> |
| 30 | /// Provides access to the Oculus boundary system. |
| 31 | /// </summary> |
| 32 | public class OVRBoundary |
| 33 | { |
| 34 | /// <summary> |
| 35 | /// Specifies a tracked node that can be queried through the boundary system. |
| 36 | /// </summary> |
| 37 | public enum Node |
| 38 | { |
| 39 | HandLeft = OVRPlugin.Node.HandLeft, ///< Tracks the left hand node. |
| 40 | HandRight = OVRPlugin.Node.HandRight, ///< Tracks the right hand node. |
| 41 | Head = OVRPlugin.Node.Head, ///< Tracks the head node. |
| 42 | } |
| 43 | |
| 44 | /// <summary> |
| 45 | /// Specifies a boundary type surface. |
| 46 | /// </summary> |
| 47 | public enum BoundaryType |
| 48 | { |
| 49 | OuterBoundary = OVRPlugin.BoundaryType.OuterBoundary, ///< Outer boundary that closely matches the user's configured walls. |
| 50 | PlayArea = OVRPlugin.BoundaryType.PlayArea, ///< Smaller convex area inset within the outer boundary. |
| 51 | } |
| 52 | |
| 53 | /// <summary> |
| 54 | /// Provides test results of boundary system queries. |
| 55 | /// </summary> |
| 56 | public struct BoundaryTestResult |
| 57 | { |
| 58 | public bool IsTriggering; ///< Returns true if the queried test would violate and/or trigger the tested boundary types. |
| 59 | public float ClosestDistance; ///< Returns the distance between the queried test object and the closest tested boundary type. |
| 60 | public Vector3 ClosestPoint; ///< Returns the closest point to the queried test object. |
| 61 | public Vector3 ClosestPointNormal; ///< Returns the normal of the closest point to the queried test object. |
| 62 | } |
| 63 | |
| 64 | /// <summary> |
| 65 | /// Returns true if the boundary system is currently configured with valid boundary data. |
| 66 | /// </summary> |
| 67 | public bool GetConfigured() |
| 68 | { |
| 69 | if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus) |
| 70 | return OVRPlugin.GetBoundaryConfigured(); |
| 71 | else |
| 72 | { |
| 73 | #if UNITY_2017_1_OR_NEWER |
| 74 | return Boundary.configured; |
| 75 | #else |
| 76 | return false; |
| 77 | #endif |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /// <summary> |
| 82 | /// Returns the results of testing a tracked node against the specified boundary type. |
| 83 | /// All points are returned in local tracking space shared by tracked nodes and accessible through OVRCameraRig's trackingSpace anchor. |
| 84 | /// </summary> |
| 85 | public OVRBoundary.BoundaryTestResult TestNode(OVRBoundary.Node node, OVRBoundary.BoundaryType boundaryType) |
| 86 | { |
| 87 | OVRPlugin.BoundaryTestResult ovrpRes = OVRPlugin.TestBoundaryNode((OVRPlugin.Node)node, (OVRPlugin.BoundaryType)boundaryType); |
| 88 | |
| 89 | OVRBoundary.BoundaryTestResult res = new OVRBoundary.BoundaryTestResult() |
| 90 | { |
| 91 | IsTriggering = (ovrpRes.IsTriggering == OVRPlugin.Bool.True), |
| 92 | ClosestDistance = ovrpRes.ClosestDistance, |
| 93 | ClosestPoint = ovrpRes.ClosestPoint.FromFlippedZVector3f(), |
| 94 | ClosestPointNormal = ovrpRes.ClosestPointNormal.FromFlippedZVector3f(), |
| 95 | }; |
| 96 | |
| 97 | return res; |
| 98 | } |
| 99 | |
| 100 | /// <summary> |
| 101 | /// Returns the results of testing a 3d point against the specified boundary type. |
| 102 | /// The test point is expected in local tracking space. |
| 103 | /// All points are returned in local tracking space shared by tracked nodes and accessible through OVRCameraRig's trackingSpace anchor. |
| 104 | /// </summary> |
| 105 | public OVRBoundary.BoundaryTestResult TestPoint(Vector3 point, OVRBoundary.BoundaryType boundaryType) |
| 106 | { |
| 107 | OVRPlugin.BoundaryTestResult ovrpRes = OVRPlugin.TestBoundaryPoint(point.ToFlippedZVector3f(), (OVRPlugin.BoundaryType)boundaryType); |
| 108 | |
| 109 | OVRBoundary.BoundaryTestResult res = new OVRBoundary.BoundaryTestResult() |
| 110 | { |
| 111 | IsTriggering = (ovrpRes.IsTriggering == OVRPlugin.Bool.True), |
| 112 | ClosestDistance = ovrpRes.ClosestDistance, |
| 113 | ClosestPoint = ovrpRes.ClosestPoint.FromFlippedZVector3f(), |
| 114 | ClosestPointNormal = ovrpRes.ClosestPointNormal.FromFlippedZVector3f(), |
| 115 | }; |
| 116 | |
| 117 | return res; |
| 118 | } |
| 119 | |
| 120 | private static int cachedVector3fSize = Marshal.SizeOf(typeof(OVRPlugin.Vector3f)); |
| 121 | private static OVRNativeBuffer cachedGeometryNativeBuffer = new OVRNativeBuffer(0); |
| 122 | private static float[] cachedGeometryManagedBuffer = new float[0]; |
| 123 | private List<Vector3> cachedGeometryList = new List<Vector3>(); |
| 124 | /// <summary> |
| 125 | /// Returns an array of 3d points (in clockwise order) that define the specified boundary type. |
| 126 | /// All points are returned in local tracking space shared by tracked nodes and accessible through OVRCameraRig's trackingSpace anchor. |
| 127 | /// </summary> |
| 128 | public Vector3[] GetGeometry(OVRBoundary.BoundaryType boundaryType) |
| 129 | { |
| 130 | if (OVRManager.loadedXRDevice != OVRManager.XRDevice.Oculus) |
| 131 | { |
| 132 | #if UNITY_2017_1_OR_NEWER |
| 133 | if (Boundary.TryGetGeometry(cachedGeometryList, (boundaryType == BoundaryType.PlayArea) ? Boundary.Type.PlayArea : Boundary.Type.TrackedArea)) |
| 134 | { |
| 135 | Vector3[] arr = cachedGeometryList.ToArray(); |
| 136 | return arr; |
| 137 | } |
| 138 | #endif |
| 139 | Debug.LogError("This functionality is not supported in your current version of Unity."); |
| 140 | return null; |
| 141 | } |
| 142 | |
| 143 | int pointsCount = 0; |
| 144 | if (OVRPlugin.GetBoundaryGeometry2((OVRPlugin.BoundaryType)boundaryType, IntPtr.Zero, ref pointsCount)) |
| 145 | { |
| 146 | if (pointsCount > 0) |
| 147 | { |
| 148 | int requiredNativeBufferCapacity = pointsCount * cachedVector3fSize; |
| 149 | if (cachedGeometryNativeBuffer.GetCapacity() < requiredNativeBufferCapacity) |
| 150 | cachedGeometryNativeBuffer.Reset(requiredNativeBufferCapacity); |
| 151 | |
| 152 | int requiredManagedBufferCapacity = pointsCount * 3; |
| 153 | if (cachedGeometryManagedBuffer.Length < requiredManagedBufferCapacity) |
| 154 | cachedGeometryManagedBuffer = new float[requiredManagedBufferCapacity]; |
| 155 | |
| 156 | if (OVRPlugin.GetBoundaryGeometry2((OVRPlugin.BoundaryType)boundaryType, cachedGeometryNativeBuffer.GetPointer(), ref pointsCount)) |
| 157 | { |
| 158 | Marshal.Copy(cachedGeometryNativeBuffer.GetPointer(), cachedGeometryManagedBuffer, 0, requiredManagedBufferCapacity); |
| 159 | |
| 160 | Vector3[] points = new Vector3[pointsCount]; |
| 161 | |
| 162 | for (int i = 0; i < pointsCount; i++) |
| 163 | { |
| 164 | points[i] = new OVRPlugin.Vector3f() |
| 165 | { |
| 166 | x = cachedGeometryManagedBuffer[3 * i + 0], |
| 167 | y = cachedGeometryManagedBuffer[3 * i + 1], |
| 168 | z = cachedGeometryManagedBuffer[3 * i + 2], |
| 169 | }.FromFlippedZVector3f(); |
| 170 | } |
| 171 | |
| 172 | return points; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | return new Vector3[0]; |
| 178 | } |
| 179 | |
| 180 | /// <summary> |
| 181 | /// Returns a vector that indicates the spatial dimensions of the specified boundary type. (x = width, y = height, z = depth) |
| 182 | /// </summary> |
| 183 | public Vector3 GetDimensions(OVRBoundary.BoundaryType boundaryType) |
| 184 | { |
| 185 | if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus) |
| 186 | return OVRPlugin.GetBoundaryDimensions((OVRPlugin.BoundaryType)boundaryType).FromVector3f(); |
| 187 | |
| 188 | else |
| 189 | { |
| 190 | #if UNITY_2017_1_OR_NEWER |
| 191 | Vector3 dimensions; |
| 192 | if (Boundary.TryGetDimensions(out dimensions, (boundaryType == BoundaryType.PlayArea) ? Boundary.Type.PlayArea : Boundary.Type.TrackedArea)) |
| 193 | return dimensions; |
| 194 | #endif |
| 195 | return Vector3.zero; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /// <summary> |
| 200 | /// Returns true if the boundary system is currently visible. |
| 201 | /// </summary> |
| 202 | public bool GetVisible() |
| 203 | { |
| 204 | if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus) |
| 205 | return OVRPlugin.GetBoundaryVisible(); |
| 206 | else |
| 207 | { |
| 208 | #if UNITY_2017_1_OR_NEWER |
| 209 | return Boundary.visible; |
| 210 | #else |
| 211 | return false; |
| 212 | #endif |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /// <summary> |
| 217 | /// Requests that the boundary system visibility be set to the specified value. |
| 218 | /// The actual visibility can be overridden by the system (i.e., proximity trigger) or by the user (boundary system disabled) |
| 219 | /// </summary> |
| 220 | public void SetVisible(bool value) |
| 221 | { |
| 222 | if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus) |
| 223 | OVRPlugin.SetBoundaryVisible(value); |
| 224 | else |
| 225 | { |
| 226 | #if UNITY_2017_1_OR_NEWER |
| 227 | Boundary.visible = value; |
| 228 | #endif |
| 229 | } |
| 230 | } |
| 231 | } |