Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
6.6 KiB

  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. }