| 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 | using System; |
| 18 | using System.Runtime.InteropServices; |
| 19 | using System.Text.RegularExpressions; |
| 20 | using UnityEngine; |
| 21 | using System.Collections.Generic; |
| 22 | |
| 23 | #if UNITY_2017_2_OR_NEWER |
| 24 | using InputTracking = UnityEngine.XR.InputTracking; |
| 25 | using Node = UnityEngine.XR.XRNode; |
| 26 | using NodeState = UnityEngine.XR.XRNodeState; |
| 27 | using Settings = UnityEngine.XR.XRSettings; |
| 28 | #elif UNITY_2017_1_OR_NEWER |
| 29 | using InputTracking = UnityEngine.VR.InputTracking; |
| 30 | using Node = UnityEngine.VR.VRNode; |
| 31 | using NodeState = UnityEngine.VR.VRNodeState; |
| 32 | using Settings = UnityEngine.VR.VRSettings; |
| 33 | #else |
| 34 | using Node = UnityEngine.VR.VRNode; |
| 35 | using Settings = UnityEngine.VR.VRSettings; |
| 36 | #endif |
| 37 | |
| 38 | /// <summary> |
| 39 | /// Manages an Oculus Rift head-mounted display (HMD). |
| 40 | /// </summary> |
| 41 | public class OVRDisplay |
| 42 | { |
| 43 | /// <summary> |
| 44 | /// Contains full fov information per eye |
| 45 | /// Under Symmetric Fov mode, UpFov == DownFov and LeftFov == RightFov. |
| 46 | /// </summary> |
| 47 | public struct EyeFov |
| 48 | { |
| 49 | public float UpFov; |
| 50 | public float DownFov; |
| 51 | public float LeftFov; |
| 52 | public float RightFov; |
| 53 | } |
| 54 | |
| 55 | /// <summary> |
| 56 | /// Specifies the size and field-of-view for one eye texture. |
| 57 | /// </summary> |
| 58 | public struct EyeRenderDesc |
| 59 | { |
| 60 | /// <summary> |
| 61 | /// The horizontal and vertical size of the texture. |
| 62 | /// </summary> |
| 63 | public Vector2 resolution; |
| 64 | |
| 65 | /// <summary> |
| 66 | /// The angle of the horizontal and vertical field of view in degrees. |
| 67 | /// For Symmetric FOV interface compatibility |
| 68 | /// Note this includes the fov angle from both sides |
| 69 | /// </summary> |
| 70 | public Vector2 fov; |
| 71 | |
| 72 | /// <summary> |
| 73 | /// The full information of field of view in degrees. |
| 74 | /// When Asymmetric FOV isn't enabled, this returns the maximum fov angle |
| 75 | /// </summary> |
| 76 | public EyeFov fullFov; |
| 77 | } |
| 78 | |
| 79 | /// <summary> |
| 80 | /// Contains latency measurements for a single frame of rendering. |
| 81 | /// </summary> |
| 82 | public struct LatencyData |
| 83 | { |
| 84 | /// <summary> |
| 85 | /// The time it took to render both eyes in seconds. |
| 86 | /// </summary> |
| 87 | public float render; |
| 88 | |
| 89 | /// <summary> |
| 90 | /// The time it took to perform TimeWarp in seconds. |
| 91 | /// </summary> |
| 92 | public float timeWarp; |
| 93 | |
| 94 | /// <summary> |
| 95 | /// The time between the end of TimeWarp and scan-out in seconds. |
| 96 | /// </summary> |
| 97 | public float postPresent; |
| 98 | public float renderError; |
| 99 | public float timeWarpError; |
| 100 | } |
| 101 | |
| 102 | private bool needsConfigureTexture; |
| 103 | private EyeRenderDesc[] eyeDescs = new EyeRenderDesc[2]; |
| 104 | private bool recenterRequested = false; |
| 105 | private int recenterRequestedFrameCount = int.MaxValue; |
| 106 | private OVRPose previousRelativeTrackingSpacePose; |
| 107 | private OVRManager.TrackingOrigin previousTrackingOrigin; |
| 108 | |
| 109 | /// <summary> |
| 110 | /// Creates an instance of OVRDisplay. Called by OVRManager. |
| 111 | /// </summary> |
| 112 | public OVRDisplay() |
| 113 | { |
| 114 | UpdateTextures(); |
| 115 | if (OVRPlugin.GetSystemHeadsetType() == OVRPlugin.SystemHeadset.Oculus_Quest) |
| 116 | { |
| 117 | previousTrackingOrigin = OVRManager.instance.trackingOriginType; |
| 118 | OVRManager.TrackingOrigin relativeOrigin = (previousTrackingOrigin != OVRManager.TrackingOrigin.Stage) ? OVRManager.TrackingOrigin.Stage : OVRManager.TrackingOrigin.EyeLevel; |
| 119 | previousRelativeTrackingSpacePose = OVRPlugin.GetTrackingTransformRelativePose((OVRPlugin.TrackingOrigin)relativeOrigin).ToOVRPose(); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /// <summary> |
| 124 | /// Updates the internal state of the OVRDisplay. Called by OVRManager. |
| 125 | /// </summary> |
| 126 | public void Update() |
| 127 | { |
| 128 | UpdateTextures(); |
| 129 | |
| 130 | if (recenterRequested && Time.frameCount > recenterRequestedFrameCount) |
| 131 | { |
| 132 | if (RecenteredPose != null) |
| 133 | { |
| 134 | RecenteredPose(); |
| 135 | } |
| 136 | recenterRequested = false; |
| 137 | recenterRequestedFrameCount = int.MaxValue; |
| 138 | } |
| 139 | if (OVRPlugin.GetSystemHeadsetType() == OVRPlugin.SystemHeadset.Oculus_Quest) |
| 140 | { |
| 141 | OVRManager.TrackingOrigin relativeOrigin = (OVRManager.instance.trackingOriginType != OVRManager.TrackingOrigin.Stage) ? OVRManager.TrackingOrigin.Stage : OVRManager.TrackingOrigin.EyeLevel; |
| 142 | OVRPose relativeTrackingSpacePose = OVRPlugin.GetTrackingTransformRelativePose((OVRPlugin.TrackingOrigin)relativeOrigin).ToOVRPose(); |
| 143 | //If the tracking origin type hasn't switched and the relative pose changes, a recenter occurred. |
| 144 | if (previousTrackingOrigin == OVRManager.instance.trackingOriginType && previousRelativeTrackingSpacePose != relativeTrackingSpacePose && RecenteredPose != null) |
| 145 | { |
| 146 | RecenteredPose(); |
| 147 | } |
| 148 | previousRelativeTrackingSpacePose = relativeTrackingSpacePose; |
| 149 | previousTrackingOrigin = OVRManager.instance.trackingOriginType; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /// <summary> |
| 154 | /// Occurs when the head pose is reset. |
| 155 | /// </summary> |
| 156 | public event System.Action RecenteredPose; |
| 157 | |
| 158 | /// <summary> |
| 159 | /// Recenters the head pose. |
| 160 | /// </summary> |
| 161 | public void RecenterPose() |
| 162 | { |
| 163 | #if UNITY_2017_2_OR_NEWER |
| 164 | UnityEngine.XR.InputTracking.Recenter(); |
| 165 | #else |
| 166 | UnityEngine.VR.InputTracking.Recenter(); |
| 167 | #endif |
| 168 | |
| 169 | // The current poses are cached for the current frame and won't be updated immediately |
| 170 | // after UnityEngine.VR.InputTracking.Recenter(). So we need to wait until next frame |
| 171 | // to trigger the RecenteredPose delegate. The application could expect the correct pose |
| 172 | // when the RecenteredPose delegate get called. |
| 173 | recenterRequested = true; |
| 174 | recenterRequestedFrameCount = Time.frameCount; |
| 175 | |
| 176 | #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN |
| 177 | OVRMixedReality.RecenterPose(); |
| 178 | #endif |
| 179 | } |
| 180 | |
| 181 | /// <summary> |
| 182 | /// Gets the current linear acceleration of the head. |
| 183 | /// </summary> |
| 184 | public Vector3 acceleration |
| 185 | { |
| 186 | get { |
| 187 | if (!OVRManager.isHmdPresent) |
| 188 | return Vector3.zero; |
| 189 | |
| 190 | Vector3 retVec = Vector3.zero; |
| 191 | if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.Head, NodeStatePropertyType.Acceleration, OVRPlugin.Node.Head, OVRPlugin.Step.Render, out retVec)) |
| 192 | return retVec; |
| 193 | return Vector3.zero; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /// <summary> |
| 198 | /// Gets the current angular acceleration of the head in radians per second per second about each axis. |
| 199 | /// </summary> |
| 200 | public Vector3 angularAcceleration |
| 201 | { |
| 202 | get |
| 203 | { |
| 204 | if (!OVRManager.isHmdPresent) |
| 205 | return Vector3.zero; |
| 206 | |
| 207 | Vector3 retVec = Vector3.zero; |
| 208 | if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.Head, NodeStatePropertyType.AngularAcceleration, OVRPlugin.Node.Head, OVRPlugin.Step.Render, out retVec)) |
| 209 | return retVec; |
| 210 | return Vector3.zero; |
| 211 | |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | /// <summary> |
| 216 | /// Gets the current linear velocity of the head in meters per second. |
| 217 | /// </summary> |
| 218 | public Vector3 velocity |
| 219 | { |
| 220 | get |
| 221 | { |
| 222 | if (!OVRManager.isHmdPresent) |
| 223 | return Vector3.zero; |
| 224 | |
| 225 | Vector3 retVec = Vector3.zero; |
| 226 | if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.Head, NodeStatePropertyType.Velocity, OVRPlugin.Node.Head, OVRPlugin.Step.Render, out retVec)) |
| 227 | return retVec; |
| 228 | return Vector3.zero; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /// <summary> |
| 233 | /// Gets the current angular velocity of the head in radians per second about each axis. |
| 234 | /// </summary> |
| 235 | public Vector3 angularVelocity |
| 236 | { |
| 237 | get { |
| 238 | if (!OVRManager.isHmdPresent) |
| 239 | return Vector3.zero; |
| 240 | |
| 241 | Vector3 retVec = Vector3.zero; |
| 242 | if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.Head, NodeStatePropertyType.AngularVelocity, OVRPlugin.Node.Head, OVRPlugin.Step.Render, out retVec)) |
| 243 | return retVec; |
| 244 | return Vector3.zero; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /// <summary> |
| 249 | /// Gets the resolution and field of view for the given eye. |
| 250 | /// </summary> |
| 251 | #if UNITY_2017_2_OR_NEWER |
| 252 | public EyeRenderDesc GetEyeRenderDesc(UnityEngine.XR.XRNode eye) |
| 253 | #else |
| 254 | public EyeRenderDesc GetEyeRenderDesc(UnityEngine.VR.VRNode eye) |
| 255 | #endif |
| 256 | { |
| 257 | return eyeDescs[(int)eye]; |
| 258 | } |
| 259 | |
| 260 | /// <summary> |
| 261 | /// Gets the current measured latency values. |
| 262 | /// </summary> |
| 263 | public LatencyData latency |
| 264 | { |
| 265 | get { |
| 266 | if (!OVRManager.isHmdPresent) |
| 267 | return new LatencyData(); |
| 268 | |
| 269 | string latency = OVRPlugin.latency; |
| 270 | |
| 271 | var r = new Regex("Render: ([0-9]+[.][0-9]+)ms, TimeWarp: ([0-9]+[.][0-9]+)ms, PostPresent: ([0-9]+[.][0-9]+)ms", RegexOptions.None); |
| 272 | |
| 273 | var ret = new LatencyData(); |
| 274 | |
| 275 | Match match = r.Match(latency); |
| 276 | if (match.Success) |
| 277 | { |
| 278 | ret.render = float.Parse(match.Groups[1].Value); |
| 279 | ret.timeWarp = float.Parse(match.Groups[2].Value); |
| 280 | ret.postPresent = float.Parse(match.Groups[3].Value); |
| 281 | } |
| 282 | |
| 283 | return ret; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | /// <summary> |
| 288 | /// Gets application's frame rate reported by oculus plugin |
| 289 | /// </summary> |
| 290 | public float appFramerate |
| 291 | { |
| 292 | get |
| 293 | { |
| 294 | if (!OVRManager.isHmdPresent) |
| 295 | return 0; |
| 296 | |
| 297 | return OVRPlugin.GetAppFramerate(); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | /// <summary> |
| 302 | /// Gets the recommended MSAA level for optimal quality/performance the current device. |
| 303 | /// </summary> |
| 304 | public int recommendedMSAALevel |
| 305 | { |
| 306 | get |
| 307 | { |
| 308 | int result = OVRPlugin.recommendedMSAALevel; |
| 309 | |
| 310 | if (result == 1) |
| 311 | result = 0; |
| 312 | |
| 313 | return result; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | /// <summary> |
| 318 | /// Gets the list of available display frequencies supported by this hardware. |
| 319 | /// </summary> |
| 320 | public float[] displayFrequenciesAvailable |
| 321 | { |
| 322 | get { return OVRPlugin.systemDisplayFrequenciesAvailable; } |
| 323 | } |
| 324 | |
| 325 | /// <summary> |
| 326 | /// Gets and sets the current display frequency. |
| 327 | /// </summary> |
| 328 | public float displayFrequency |
| 329 | { |
| 330 | get |
| 331 | { |
| 332 | return OVRPlugin.systemDisplayFrequency; |
| 333 | } |
| 334 | set |
| 335 | { |
| 336 | OVRPlugin.systemDisplayFrequency = value; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | private void UpdateTextures() |
| 341 | { |
| 342 | #if UNITY_2017_2_OR_NEWER |
| 343 | ConfigureEyeDesc(UnityEngine.XR.XRNode.LeftEye); |
| 344 | ConfigureEyeDesc(UnityEngine.XR.XRNode.RightEye); |
| 345 | #else |
| 346 | ConfigureEyeDesc(UnityEngine.VR.VRNode.LeftEye); |
| 347 | ConfigureEyeDesc(UnityEngine.VR.VRNode.RightEye); |
| 348 | #endif |
| 349 | } |
| 350 | |
| 351 | #if UNITY_2017_2_OR_NEWER |
| 352 | private void ConfigureEyeDesc(UnityEngine.XR.XRNode eye) |
| 353 | #else |
| 354 | private void ConfigureEyeDesc(UnityEngine.VR.VRNode eye) |
| 355 | #endif |
| 356 | { |
| 357 | if (!OVRManager.isHmdPresent) |
| 358 | return; |
| 359 | |
| 360 | int eyeTextureWidth = Settings.eyeTextureWidth; |
| 361 | int eyeTextureHeight = Settings.eyeTextureHeight; |
| 362 | |
| 363 | eyeDescs[(int)eye] = new EyeRenderDesc(); |
| 364 | eyeDescs[(int)eye].resolution = new Vector2(eyeTextureWidth, eyeTextureHeight); |
| 365 | |
| 366 | OVRPlugin.Frustumf2 frust; |
| 367 | if (OVRPlugin.GetNodeFrustum2((OVRPlugin.Node)eye, out frust)) |
| 368 | { |
| 369 | eyeDescs[(int)eye].fullFov.LeftFov = Mathf.Rad2Deg * Mathf.Atan(frust.Fov.LeftTan); |
| 370 | eyeDescs[(int)eye].fullFov.RightFov = Mathf.Rad2Deg * Mathf.Atan(frust.Fov.RightTan); |
| 371 | eyeDescs[(int)eye].fullFov.UpFov = Mathf.Rad2Deg * Mathf.Atan(frust.Fov.UpTan); |
| 372 | eyeDescs[(int)eye].fullFov.DownFov = Mathf.Rad2Deg * Mathf.Atan(frust.Fov.DownTan); |
| 373 | } |
| 374 | else |
| 375 | { |
| 376 | OVRPlugin.Frustumf frustOld = OVRPlugin.GetEyeFrustum((OVRPlugin.Eye)eye); |
| 377 | eyeDescs[(int)eye].fullFov.LeftFov = Mathf.Rad2Deg * frustOld.fovX * 0.5f; |
| 378 | eyeDescs[(int)eye].fullFov.RightFov = Mathf.Rad2Deg * frustOld.fovX * 0.5f; |
| 379 | eyeDescs[(int)eye].fullFov.UpFov = Mathf.Rad2Deg * frustOld.fovY * 0.5f; |
| 380 | eyeDescs[(int)eye].fullFov.DownFov = Mathf.Rad2Deg * frustOld.fovY * 0.5f; |
| 381 | } |
| 382 | |
| 383 | // Symmetric Fov uses the maximum fov angle |
| 384 | float maxFovX = Mathf.Max(eyeDescs[(int)eye].fullFov.LeftFov, eyeDescs[(int)eye].fullFov.RightFov); |
| 385 | float maxFovY = Mathf.Max(eyeDescs[(int)eye].fullFov.UpFov, eyeDescs[(int)eye].fullFov.DownFov); |
| 386 | eyeDescs[(int)eye].fov.x = maxFovX * 2.0f; |
| 387 | eyeDescs[(int)eye].fov.y = maxFovY * 2.0f; |
| 388 | |
| 389 | if (!OVRPlugin.AsymmetricFovEnabled) |
| 390 | { |
| 391 | eyeDescs[(int)eye].fullFov.LeftFov = maxFovX; |
| 392 | eyeDescs[(int)eye].fullFov.RightFov = maxFovX; |
| 393 | |
| 394 | eyeDescs[(int)eye].fullFov.UpFov = maxFovY; |
| 395 | eyeDescs[(int)eye].fullFov.DownFov = maxFovY; |
| 396 | } |
| 397 | |
| 398 | |
| 399 | } |
| 400 | } |