| 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 | #if UNITY_ANDROID && !UNITY_EDITOR |
| 18 | #define OVR_ANDROID_MRC |
| 19 | #endif |
| 20 | |
| 21 | using UnityEngine; |
| 22 | using System.Collections; |
| 23 | |
| 24 | #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_ANDROID |
| 25 | |
| 26 | public abstract class OVRComposition { |
| 27 | |
| 28 | public bool cameraInTrackingSpace = false; |
| 29 | public OVRCameraRig cameraRig = null; |
| 30 | |
| 31 | protected OVRComposition(GameObject parentObject, Camera mainCamera) |
| 32 | { |
| 33 | OVRCameraRig cameraRig = mainCamera.GetComponentInParent<OVRCameraRig>(); |
| 34 | if (cameraRig == null) |
| 35 | { |
| 36 | cameraRig = parentObject.GetComponent<OVRCameraRig>(); |
| 37 | } |
| 38 | cameraInTrackingSpace = (cameraRig != null && cameraRig.trackingSpace != null); |
| 39 | this.cameraRig = cameraRig; |
| 40 | Debug.Log(cameraRig == null ? "[OVRComposition] CameraRig not found" : "[OVRComposition] CameraRig found"); |
| 41 | } |
| 42 | |
| 43 | public abstract OVRManager.CompositionMethod CompositionMethod(); |
| 44 | |
| 45 | public abstract void Update(Camera mainCamera); |
| 46 | public abstract void Cleanup(); |
| 47 | |
| 48 | public virtual void RecenterPose() { } |
| 49 | |
| 50 | protected bool usingLastAttachedNodePose = false; |
| 51 | protected OVRPose lastAttachedNodePose = new OVRPose(); // Sometimes the attach node pose is not readable (lose tracking, low battery, etc.) Use the last pose instead when it happens |
| 52 | |
| 53 | public OVRPose ComputeCameraWorldSpacePose(OVRPlugin.CameraExtrinsics extrinsics, OVRPlugin.Posef calibrationRawPose) |
| 54 | { |
| 55 | OVRPose trackingSpacePose = ComputeCameraTrackingSpacePose(extrinsics, calibrationRawPose); |
| 56 | OVRPose worldSpacePose = OVRExtensions.ToWorldSpacePose(trackingSpacePose); |
| 57 | return worldSpacePose; |
| 58 | } |
| 59 | |
| 60 | public OVRPose ComputeCameraTrackingSpacePose(OVRPlugin.CameraExtrinsics extrinsics, OVRPlugin.Posef calibrationRawPose) |
| 61 | { |
| 62 | OVRPose trackingSpacePose = new OVRPose(); |
| 63 | |
| 64 | OVRPose cameraTrackingSpacePose = extrinsics.RelativePose.ToOVRPose(); |
| 65 | #if OVR_ANDROID_MRC |
| 66 | OVRPose rawPose = OVRPlugin.GetTrackingTransformRawPose().ToOVRPose(); |
| 67 | cameraTrackingSpacePose = rawPose * (calibrationRawPose.ToOVRPose().Inverse() * cameraTrackingSpacePose); |
| 68 | #endif |
| 69 | trackingSpacePose = cameraTrackingSpacePose; |
| 70 | |
| 71 | if (extrinsics.AttachedToNode != OVRPlugin.Node.None && OVRPlugin.GetNodePresent(extrinsics.AttachedToNode)) |
| 72 | { |
| 73 | if (usingLastAttachedNodePose) |
| 74 | { |
| 75 | Debug.Log("The camera attached node get tracked"); |
| 76 | usingLastAttachedNodePose = false; |
| 77 | } |
| 78 | OVRPose attachedNodePose = OVRPlugin.GetNodePose(extrinsics.AttachedToNode, OVRPlugin.Step.Render).ToOVRPose(); |
| 79 | lastAttachedNodePose = attachedNodePose; |
| 80 | trackingSpacePose = attachedNodePose * trackingSpacePose; |
| 81 | } |
| 82 | else |
| 83 | { |
| 84 | if (extrinsics.AttachedToNode != OVRPlugin.Node.None) |
| 85 | { |
| 86 | if (!usingLastAttachedNodePose) |
| 87 | { |
| 88 | Debug.LogWarning("The camera attached node could not be tracked, using the last pose"); |
| 89 | usingLastAttachedNodePose = true; |
| 90 | } |
| 91 | trackingSpacePose = lastAttachedNodePose * trackingSpacePose; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return trackingSpacePose; |
| 96 | } |
| 97 | |
| 98 | } |
| 99 | |
| 100 | #endif |