OVRMixedRealityCaptureTest.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #if UNITY_ANDROID && !UNITY_EDITOR
  2. #define OVR_ANDROID_MRC
  3. #endif
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. public class OVRMixedRealityCaptureTest : MonoBehaviour {
  8. bool inited = false;
  9. enum CameraMode
  10. {
  11. Normal = 0,
  12. OverrideFov,
  13. ThirdPerson,
  14. }
  15. CameraMode currentMode = CameraMode.Normal;
  16. public Camera defaultExternalCamera;
  17. OVRPlugin.Fovf defaultFov;
  18. // Use this for initialization
  19. void Start () {
  20. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || OVR_ANDROID_MRC
  21. if (!defaultExternalCamera)
  22. {
  23. Debug.LogWarning("defaultExternalCamera undefined");
  24. }
  25. #if !OVR_ANDROID_MRC
  26. // On Quest, we enable MRC automatically through the configuration
  27. if (!OVRManager.instance.enableMixedReality)
  28. {
  29. OVRManager.instance.enableMixedReality = true;
  30. }
  31. #endif
  32. #endif
  33. }
  34. void Initialize()
  35. {
  36. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || OVR_ANDROID_MRC
  37. if (inited)
  38. return;
  39. #if OVR_ANDROID_MRC
  40. if (!OVRPlugin.Media.GetInitialized())
  41. return;
  42. #else
  43. if (!OVRPlugin.IsMixedRealityInitialized())
  44. return;
  45. #endif
  46. OVRPlugin.ResetDefaultExternalCamera();
  47. Debug.LogFormat("GetExternalCameraCount before adding manual external camera {0}", OVRPlugin.GetExternalCameraCount());
  48. UpdateDefaultExternalCamera();
  49. Debug.LogFormat("GetExternalCameraCount after adding manual external camera {0}", OVRPlugin.GetExternalCameraCount());
  50. // obtain default FOV
  51. {
  52. OVRPlugin.CameraIntrinsics cameraIntrinsics;
  53. OVRPlugin.CameraExtrinsics cameraExtrinsics;
  54. OVRPlugin.Posef calibrationRawPose;
  55. OVRPlugin.GetMixedRealityCameraInfo(0, out cameraExtrinsics, out cameraIntrinsics, out calibrationRawPose);
  56. defaultFov = cameraIntrinsics.FOVPort;
  57. }
  58. inited = true;
  59. #endif
  60. }
  61. void UpdateDefaultExternalCamera()
  62. {
  63. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || OVR_ANDROID_MRC
  64. // always build a 1080p external camera
  65. const int cameraPixelWidth = 1920;
  66. const int cameraPixelHeight = 1080;
  67. const float cameraAspect = (float)cameraPixelWidth / cameraPixelHeight;
  68. string cameraName = "UnityExternalCamera";
  69. OVRPlugin.CameraIntrinsics cameraIntrinsics = new OVRPlugin.CameraIntrinsics();
  70. OVRPlugin.CameraExtrinsics cameraExtrinsics = new OVRPlugin.CameraExtrinsics();
  71. // intrinsics
  72. cameraIntrinsics.IsValid = OVRPlugin.Bool.True;
  73. cameraIntrinsics.LastChangedTimeSeconds = Time.time;
  74. float vFov = defaultExternalCamera.fieldOfView * Mathf.Deg2Rad;
  75. float hFov = Mathf.Atan(Mathf.Tan(vFov * 0.5f) * cameraAspect) * 2.0f;
  76. OVRPlugin.Fovf fov = new OVRPlugin.Fovf();
  77. fov.UpTan = fov.DownTan = Mathf.Tan(vFov * 0.5f);
  78. fov.LeftTan = fov.RightTan = Mathf.Tan(hFov * 0.5f);
  79. cameraIntrinsics.FOVPort = fov;
  80. cameraIntrinsics.VirtualNearPlaneDistanceMeters = defaultExternalCamera.nearClipPlane;
  81. cameraIntrinsics.VirtualFarPlaneDistanceMeters = defaultExternalCamera.farClipPlane;
  82. cameraIntrinsics.ImageSensorPixelResolution.w = cameraPixelWidth;
  83. cameraIntrinsics.ImageSensorPixelResolution.h = cameraPixelHeight;
  84. // extrinsics
  85. cameraExtrinsics.IsValid = OVRPlugin.Bool.True;
  86. cameraExtrinsics.LastChangedTimeSeconds = Time.time;
  87. cameraExtrinsics.CameraStatusData = OVRPlugin.CameraStatus.CameraStatus_Calibrated;
  88. cameraExtrinsics.AttachedToNode = OVRPlugin.Node.None;
  89. Camera mainCamera = Camera.main;
  90. OVRCameraRig cameraRig = mainCamera.GetComponentInParent<OVRCameraRig>();
  91. if (cameraRig)
  92. {
  93. Transform trackingSpace = cameraRig.trackingSpace;
  94. OVRPose trackingSpacePose = trackingSpace.ToOVRPose(false);
  95. OVRPose cameraPose = defaultExternalCamera.transform.ToOVRPose(false);
  96. OVRPose relativePose = trackingSpacePose.Inverse() * cameraPose;
  97. cameraExtrinsics.RelativePose = relativePose.ToPosef();
  98. }
  99. else
  100. {
  101. cameraExtrinsics.RelativePose = OVRPlugin.Posef.identity;
  102. }
  103. if (!OVRPlugin.SetDefaultExternalCamera(cameraName, ref cameraIntrinsics, ref cameraExtrinsics))
  104. {
  105. Debug.LogError("SetDefaultExternalCamera() failed");
  106. }
  107. #endif
  108. }
  109. // Update is called once per frame
  110. void Update () {
  111. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || OVR_ANDROID_MRC
  112. if (!inited)
  113. {
  114. Initialize();
  115. return;
  116. }
  117. if (!defaultExternalCamera)
  118. {
  119. return;
  120. }
  121. #if OVR_ANDROID_MRC
  122. if (!OVRPlugin.Media.GetInitialized())
  123. {
  124. return;
  125. }
  126. #else
  127. if (!OVRPlugin.IsMixedRealityInitialized())
  128. {
  129. return;
  130. }
  131. #endif
  132. if (OVRInput.GetDown(OVRInput.Button.One))
  133. {
  134. if (currentMode == CameraMode.ThirdPerson)
  135. {
  136. currentMode = CameraMode.Normal;
  137. }
  138. else
  139. {
  140. currentMode = currentMode + 1;
  141. }
  142. Debug.LogFormat("Camera mode change to {0}", currentMode);
  143. }
  144. if (currentMode == CameraMode.Normal)
  145. {
  146. UpdateDefaultExternalCamera();
  147. OVRPlugin.OverrideExternalCameraFov(0, false, new OVRPlugin.Fovf());
  148. OVRPlugin.OverrideExternalCameraStaticPose(0, false, OVRPlugin.Posef.identity);
  149. }
  150. else if (currentMode == CameraMode.OverrideFov)
  151. {
  152. OVRPlugin.Fovf fov = defaultFov;
  153. OVRPlugin.Fovf newFov = new OVRPlugin.Fovf();
  154. newFov.LeftTan = fov.LeftTan * 2.0f;
  155. newFov.RightTan = fov.RightTan * 2.0f;
  156. newFov.UpTan = fov.UpTan * 2.0f;
  157. newFov.DownTan = fov.DownTan * 2.0f;
  158. OVRPlugin.OverrideExternalCameraFov(0, true, newFov);
  159. OVRPlugin.OverrideExternalCameraStaticPose(0, false, OVRPlugin.Posef.identity);
  160. if (!OVRPlugin.GetUseOverriddenExternalCameraFov(0))
  161. {
  162. Debug.LogWarning("FOV not overridden");
  163. }
  164. }
  165. else if (currentMode == CameraMode.ThirdPerson)
  166. {
  167. Camera camera = GetComponent<Camera>();
  168. if (camera == null)
  169. {
  170. return;
  171. }
  172. float vFov = camera.fieldOfView * Mathf.Deg2Rad;
  173. float hFov = Mathf.Atan(Mathf.Tan(vFov * 0.5f) * camera.aspect) * 2.0f;
  174. OVRPlugin.Fovf fov = new OVRPlugin.Fovf();
  175. fov.UpTan = fov.DownTan = Mathf.Tan(vFov * 0.5f);
  176. fov.LeftTan = fov.RightTan = Mathf.Tan(hFov * 0.5f);
  177. OVRPlugin.OverrideExternalCameraFov(0, true, fov);
  178. Camera mainCamera = Camera.main;
  179. OVRCameraRig cameraRig = mainCamera.GetComponentInParent<OVRCameraRig>();
  180. if (cameraRig)
  181. {
  182. Transform trackingSpace = cameraRig.trackingSpace;
  183. OVRPose trackingSpacePose = trackingSpace.ToOVRPose(false);
  184. OVRPose cameraPose = transform.ToOVRPose(false);
  185. OVRPose relativePose = trackingSpacePose.Inverse() * cameraPose;
  186. OVRPlugin.Posef relativePosef = relativePose.ToPosef();
  187. OVRPlugin.OverrideExternalCameraStaticPose(0, true, relativePosef);
  188. }
  189. else
  190. {
  191. OVRPlugin.OverrideExternalCameraStaticPose(0, false, OVRPlugin.Posef.identity);
  192. }
  193. if (!OVRPlugin.GetUseOverriddenExternalCameraFov(0))
  194. {
  195. Debug.LogWarning("FOV not overridden");
  196. }
  197. if (!OVRPlugin.GetUseOverriddenExternalCameraStaticPose(0))
  198. {
  199. Debug.LogWarning("StaticPose not overridden");
  200. }
  201. }
  202. #endif
  203. }
  204. }