OVRDirectComposition.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. using UnityEngine;
  14. using System.Collections;
  15. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
  16. public class OVRDirectComposition : OVRCameraComposition
  17. {
  18. private GameObject previousMainCameraObject = null;
  19. public GameObject directCompositionCameraGameObject = null;
  20. public Camera directCompositionCamera = null;
  21. public RenderTexture boundaryMeshMaskTexture = null;
  22. public override OVRManager.CompositionMethod CompositionMethod() { return OVRManager.CompositionMethod.Direct; }
  23. public OVRDirectComposition(GameObject parentObject, Camera mainCamera, OVRManager.CameraDevice cameraDevice, bool useDynamicLighting, OVRManager.DepthQuality depthQuality)
  24. : base(parentObject, mainCamera, cameraDevice, useDynamicLighting, depthQuality)
  25. {
  26. RefreshCameraObjects(parentObject, mainCamera);
  27. }
  28. private void RefreshCameraObjects(GameObject parentObject, Camera mainCamera)
  29. {
  30. if (!hasCameraDeviceOpened)
  31. {
  32. Debug.LogWarning("[OVRDirectComposition] RefreshCameraObjects(): Unable to open camera device " + cameraDevice);
  33. return;
  34. }
  35. if (mainCamera.gameObject != previousMainCameraObject)
  36. {
  37. Debug.LogFormat("[OVRDirectComposition] Camera refreshed. Rebind camera to {0}", mainCamera.gameObject.name);
  38. OVRCompositionUtil.SafeDestroy(ref directCompositionCameraGameObject);
  39. directCompositionCamera = null;
  40. RefreshCameraRig(parentObject, mainCamera);
  41. Debug.Assert(directCompositionCameraGameObject == null);
  42. directCompositionCameraGameObject = Object.Instantiate(mainCamera.gameObject);
  43. directCompositionCameraGameObject.name = "OculusMRC_DirectCompositionCamera";
  44. directCompositionCameraGameObject.transform.parent = cameraInTrackingSpace ? cameraRig.trackingSpace : parentObject.transform;
  45. if (directCompositionCameraGameObject.GetComponent<AudioListener>())
  46. {
  47. Object.Destroy(directCompositionCameraGameObject.GetComponent<AudioListener>());
  48. }
  49. if (directCompositionCameraGameObject.GetComponent<OVRManager>())
  50. {
  51. Object.Destroy(directCompositionCameraGameObject.GetComponent<OVRManager>());
  52. }
  53. directCompositionCamera = directCompositionCameraGameObject.GetComponent<Camera>();
  54. directCompositionCamera.stereoTargetEye = StereoTargetEyeMask.None;
  55. directCompositionCamera.depth = float.MaxValue;
  56. directCompositionCamera.rect = new Rect(0.0f, 0.0f, 1.0f, 1.0f);
  57. directCompositionCamera.cullingMask = mainCamera.cullingMask & (~OVRManager.instance.extraHiddenLayers);
  58. Debug.Log("DirectComposition activated : useDynamicLighting " + (useDynamicLighting ? "ON" : "OFF"));
  59. RefreshCameraFramePlaneObject(parentObject, directCompositionCamera, useDynamicLighting);
  60. previousMainCameraObject = mainCamera.gameObject;
  61. }
  62. }
  63. public override void Update(GameObject gameObject, Camera mainCamera)
  64. {
  65. if (!hasCameraDeviceOpened)
  66. {
  67. return;
  68. }
  69. RefreshCameraObjects(gameObject, mainCamera);
  70. if (!OVRPlugin.SetHandNodePoseStateLatency(OVRManager.instance.handPoseStateLatency))
  71. {
  72. Debug.LogWarning("HandPoseStateLatency is invalid. Expect a value between 0.0 to 0.5, get " + OVRManager.instance.handPoseStateLatency);
  73. }
  74. directCompositionCamera.clearFlags = mainCamera.clearFlags;
  75. directCompositionCamera.backgroundColor = mainCamera.backgroundColor;
  76. directCompositionCamera.cullingMask = mainCamera.cullingMask & (~OVRManager.instance.extraHiddenLayers);
  77. directCompositionCamera.nearClipPlane = mainCamera.nearClipPlane;
  78. directCompositionCamera.farClipPlane = mainCamera.farClipPlane;
  79. if (OVRMixedReality.useFakeExternalCamera || OVRPlugin.GetExternalCameraCount() == 0)
  80. {
  81. OVRPose trackingSpacePose = new OVRPose();
  82. trackingSpacePose.position = OVRManager.instance.trackingOriginType == OVRManager.TrackingOrigin.EyeLevel ?
  83. OVRMixedReality.fakeCameraEyeLevelPosition :
  84. OVRMixedReality.fakeCameraFloorLevelPosition;
  85. trackingSpacePose.orientation = OVRMixedReality.fakeCameraRotation;
  86. directCompositionCamera.fieldOfView = OVRMixedReality.fakeCameraFov;
  87. directCompositionCamera.aspect = OVRMixedReality.fakeCameraAspect;
  88. if (cameraInTrackingSpace)
  89. {
  90. directCompositionCamera.transform.FromOVRPose(trackingSpacePose, true);
  91. }
  92. else
  93. {
  94. OVRPose worldSpacePose = new OVRPose();
  95. worldSpacePose = OVRExtensions.ToWorldSpacePose(trackingSpacePose);
  96. directCompositionCamera.transform.FromOVRPose(worldSpacePose);
  97. }
  98. }
  99. else
  100. {
  101. OVRPlugin.CameraExtrinsics extrinsics;
  102. OVRPlugin.CameraIntrinsics intrinsics;
  103. OVRPlugin.Posef calibrationRawPose;
  104. // So far, only support 1 camera for MR and always use camera index 0
  105. if (OVRPlugin.GetMixedRealityCameraInfo(0, out extrinsics, out intrinsics, out calibrationRawPose))
  106. {
  107. float fovY = Mathf.Atan(intrinsics.FOVPort.UpTan) * Mathf.Rad2Deg * 2;
  108. float aspect = intrinsics.FOVPort.LeftTan / intrinsics.FOVPort.UpTan;
  109. directCompositionCamera.fieldOfView = fovY;
  110. directCompositionCamera.aspect = aspect;
  111. if (cameraInTrackingSpace)
  112. {
  113. OVRPose trackingSpacePose = ComputeCameraTrackingSpacePose(extrinsics, calibrationRawPose);
  114. directCompositionCamera.transform.FromOVRPose(trackingSpacePose, true);
  115. }
  116. else
  117. {
  118. OVRPose worldSpacePose = ComputeCameraWorldSpacePose(extrinsics, calibrationRawPose);
  119. directCompositionCamera.transform.FromOVRPose(worldSpacePose);
  120. }
  121. }
  122. else
  123. {
  124. Debug.LogWarning("Failed to get external camera information");
  125. }
  126. }
  127. if (hasCameraDeviceOpened)
  128. {
  129. if (boundaryMeshMaskTexture == null || boundaryMeshMaskTexture.width != Screen.width || boundaryMeshMaskTexture.height != Screen.height)
  130. {
  131. boundaryMeshMaskTexture = new RenderTexture(Screen.width, Screen.height, 0, RenderTextureFormat.R8);
  132. boundaryMeshMaskTexture.Create();
  133. }
  134. UpdateCameraFramePlaneObject(mainCamera, directCompositionCamera, boundaryMeshMaskTexture);
  135. directCompositionCamera.GetComponent<OVRCameraFrameCompositionManager>().boundaryMeshMaskTexture = boundaryMeshMaskTexture;
  136. }
  137. }
  138. public override void Cleanup()
  139. {
  140. base.Cleanup();
  141. OVRCompositionUtil.SafeDestroy(ref directCompositionCameraGameObject);
  142. directCompositionCamera = null;
  143. Debug.Log("DirectComposition deactivated");
  144. }
  145. }
  146. #endif