OVRMixedReality.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #if UNITY_ANDROID && !UNITY_EDITOR
  14. #define OVR_ANDROID_MRC
  15. #endif
  16. using System;
  17. using System.Runtime.InteropServices;
  18. using System.Text.RegularExpressions;
  19. using System.Collections.Generic;
  20. using UnityEngine;
  21. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_ANDROID
  22. /// <summary>
  23. /// Manages mix-reality elements
  24. /// </summary>
  25. internal static class OVRMixedReality
  26. {
  27. /// <summary>
  28. /// For Debugging purpose, we can use preset parameters to fake a camera when external camera is not available
  29. /// </summary>
  30. public static bool useFakeExternalCamera = false;
  31. public static Vector3 fakeCameraFloorLevelPosition = new Vector3(0.0f, 2.0f, -0.5f);
  32. public static Vector3 fakeCameraEyeLevelPosition = fakeCameraFloorLevelPosition - new Vector3(0.0f, 1.8f, 0.0f);
  33. public static Quaternion fakeCameraRotation = Quaternion.LookRotation((new Vector3(0.0f, fakeCameraFloorLevelPosition.y, 0.0f) - fakeCameraFloorLevelPosition).normalized, Vector3.up);
  34. public static float fakeCameraFov = 60.0f;
  35. public static float fakeCameraAspect = 16.0f / 9.0f;
  36. /// <summary>
  37. /// Composition object
  38. /// </summary>
  39. public static OVRComposition currentComposition = null;
  40. /// <summary>
  41. /// Updates the internal state of the Mixed Reality Camera. Called by OVRManager.
  42. /// </summary>
  43. public static void Update(GameObject parentObject, Camera mainCamera, OVRManager.CompositionMethod compositionMethod, bool useDynamicLighting, OVRManager.CameraDevice cameraDevice, OVRManager.DepthQuality depthQuality)
  44. {
  45. if (!OVRPlugin.initialized)
  46. {
  47. Debug.LogError("OVRPlugin not initialized");
  48. return;
  49. }
  50. if (!OVRPlugin.IsMixedRealityInitialized())
  51. {
  52. OVRPlugin.InitializeMixedReality();
  53. if (OVRPlugin.IsMixedRealityInitialized())
  54. {
  55. Debug.Log("OVRPlugin_MixedReality initialized");
  56. }
  57. else
  58. {
  59. Debug.LogError("Unable to initialize OVRPlugin_MixedReality");
  60. return;
  61. }
  62. }
  63. if (!OVRPlugin.IsMixedRealityInitialized())
  64. {
  65. return;
  66. }
  67. OVRPlugin.UpdateExternalCamera();
  68. #if !OVR_ANDROID_MRC
  69. OVRPlugin.UpdateCameraDevices();
  70. #endif
  71. #if OVR_ANDROID_MRC
  72. useFakeExternalCamera = OVRPlugin.Media.UseMrcDebugCamera();
  73. #endif
  74. if (currentComposition != null && currentComposition.CompositionMethod() != compositionMethod)
  75. {
  76. currentComposition.Cleanup();
  77. currentComposition = null;
  78. }
  79. if (compositionMethod == OVRManager.CompositionMethod.External)
  80. {
  81. if (currentComposition == null)
  82. {
  83. currentComposition = new OVRExternalComposition(parentObject, mainCamera);
  84. }
  85. }
  86. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
  87. else if (compositionMethod == OVRManager.CompositionMethod.Direct)
  88. {
  89. if (currentComposition == null)
  90. {
  91. currentComposition = new OVRDirectComposition(parentObject, mainCamera, cameraDevice, useDynamicLighting, depthQuality);
  92. }
  93. }
  94. #endif
  95. else
  96. {
  97. Debug.LogError("Unknown CompositionMethod : " + compositionMethod);
  98. return;
  99. }
  100. currentComposition.Update(parentObject, mainCamera);
  101. }
  102. public static void Cleanup()
  103. {
  104. if (currentComposition != null)
  105. {
  106. currentComposition.Cleanup();
  107. currentComposition = null;
  108. }
  109. if (OVRPlugin.IsMixedRealityInitialized())
  110. {
  111. OVRPlugin.ShutdownMixedReality();
  112. }
  113. }
  114. public static void RecenterPose()
  115. {
  116. if (currentComposition != null)
  117. {
  118. currentComposition.RecenterPose();
  119. }
  120. }
  121. }
  122. #endif