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