OVRComposition.cs 4.0 KB

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