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.

162 lines
6.8 KiB

  1. // Oculus Headset|SDK_Oculus|003
  2. namespace VRTK
  3. {
  4. #if VRTK_DEFINE_SDK_OCULUS
  5. using UnityEngine;
  6. using System.Collections.Generic;
  7. #endif
  8. /// <summary>
  9. /// The Oculus Headset SDK script provides a bridge to the Oculus SDK.
  10. /// </summary>
  11. [SDK_Description(typeof(SDK_OculusSystem))]
  12. [SDK_Description(typeof(SDK_OculusSystem), 1)]
  13. public class SDK_OculusHeadset
  14. #if VRTK_DEFINE_SDK_OCULUS
  15. : SDK_BaseHeadset
  16. #else
  17. : SDK_FallbackHeadset
  18. #endif
  19. {
  20. #if VRTK_DEFINE_SDK_OCULUS
  21. protected VRTK_VelocityEstimator cachedHeadsetVelocityEstimator;
  22. /// <summary>
  23. /// The ProcessUpdate method enables an SDK to run logic for every Unity Update
  24. /// </summary>
  25. /// <param name="options">A dictionary of generic options that can be used to within the update.</param>
  26. public override void ProcessUpdate(Dictionary<string, object> options)
  27. {
  28. }
  29. /// <summary>
  30. /// The ProcessFixedUpdate method enables an SDK to run logic for every Unity FixedUpdate
  31. /// </summary>
  32. /// <param name="options">A dictionary of generic options that can be used to within the fixed update.</param>
  33. public override void ProcessFixedUpdate(Dictionary<string, object> options)
  34. {
  35. }
  36. /// <summary>
  37. /// The GetHeadset method returns the Transform of the object that is used to represent the headset in the scene.
  38. /// </summary>
  39. /// <returns>A transform of the object representing the headset in the scene.</returns>
  40. public override Transform GetHeadset()
  41. {
  42. cachedHeadset = GetSDKManagerHeadset();
  43. if (cachedHeadset == null)
  44. {
  45. cachedHeadset = VRTK_SharedMethods.FindEvenInactiveGameObject<OVRCameraRig>("TrackingSpace/CenterEyeAnchor", true).transform;
  46. }
  47. return cachedHeadset;
  48. }
  49. /// <summary>
  50. /// The GetHeadsetCamera method returns the Transform of the object that is used to hold the headset camera in the scene.
  51. /// </summary>
  52. /// <returns>A transform of the object holding the headset camera in the scene.</returns>
  53. public override Transform GetHeadsetCamera()
  54. {
  55. cachedHeadsetCamera = GetSDKManagerHeadset();
  56. if (cachedHeadsetCamera == null)
  57. {
  58. cachedHeadsetCamera = GetHeadset();
  59. }
  60. return cachedHeadsetCamera;
  61. }
  62. /// <summary>
  63. /// The GetHeadsetType method returns a string representing the type of headset connected.
  64. /// </summary>
  65. /// <returns>The string of the headset connected.</returns>
  66. public override string GetHeadsetType()
  67. {
  68. switch (OVRPlugin.GetSystemHeadsetType())
  69. {
  70. case OVRPlugin.SystemHeadset.Rift_CV1:
  71. return CleanPropertyString("oculusrift");
  72. case OVRPlugin.SystemHeadset.GearVR_R320:
  73. case OVRPlugin.SystemHeadset.GearVR_R321:
  74. case OVRPlugin.SystemHeadset.GearVR_R322:
  75. case OVRPlugin.SystemHeadset.GearVR_R323:
  76. return CleanPropertyString("oculusgearvr");
  77. case OVRPlugin.SystemHeadset.Rift_DK1:
  78. return CleanPropertyString("oculusriftdk1");
  79. case OVRPlugin.SystemHeadset.Rift_DK2:
  80. return CleanPropertyString("oculusriftdk2");
  81. }
  82. return CleanPropertyString("");
  83. }
  84. /// <summary>
  85. /// The GetHeadsetVelocity method is used to determine the current velocity of the headset.
  86. /// </summary>
  87. /// <returns>A Vector3 containing the current velocity of the headset.</returns>
  88. public override Vector3 GetHeadsetVelocity()
  89. {
  90. #if VRTK_DEFINE_OCULUS_UTILITIES_1_11_0_OR_OLDER
  91. return OVRManager.isHmdPresent ? OVRPlugin.GetEyeVelocity(OVRPlugin.Eye.Left).ToOVRPose().position : Vector3.zero;
  92. #elif VRTK_DEFINE_OCULUS_UTILITIES_1_12_0_OR_NEWER
  93. return OVRManager.isHmdPresent ? OVRPlugin.GetNodeVelocity(OVRPlugin.Node.EyeCenter, OVRPlugin.Step.Render).FromFlippedZVector3f() : Vector3.zero;
  94. #else
  95. return Vector3.zero;
  96. #endif
  97. }
  98. /// <summary>
  99. /// The GetHeadsetAngularVelocity method is used to determine the current angular velocity of the headset.
  100. /// </summary>
  101. /// <returns>A Vector3 containing the current angular velocity of the headset.</returns>
  102. public override Vector3 GetHeadsetAngularVelocity()
  103. {
  104. SetHeadsetCaches();
  105. return cachedHeadsetVelocityEstimator.GetAngularVelocityEstimate();
  106. }
  107. /// <summary>
  108. /// The HeadsetFade method is used to apply a fade to the headset camera to progressively change the colour.
  109. /// </summary>
  110. /// <param name="color">The colour to fade to.</param>
  111. /// <param name="duration">The amount of time the fade should take to reach the given colour.</param>
  112. /// <param name="fadeOverlay">Determines whether to use an overlay on the fade.</param>
  113. public override void HeadsetFade(Color color, float duration, bool fadeOverlay = false)
  114. {
  115. VRTK_ScreenFade.Start(color, duration);
  116. }
  117. /// <summary>
  118. /// The HasHeadsetFade method checks to see if the given game object (usually the camera) has the ability to fade the viewpoint.
  119. /// </summary>
  120. /// <param name="obj">The Transform to check to see if a camera fade is available on.</param>
  121. /// <returns>Returns true if the headset has fade functionality on it.</returns>
  122. public override bool HasHeadsetFade(Transform obj)
  123. {
  124. if (obj.GetComponentInChildren<VRTK_ScreenFade>())
  125. {
  126. return true;
  127. }
  128. return false;
  129. }
  130. /// <summary>
  131. /// The AddHeadsetFade method attempts to add the fade functionality to the game object with the camera on it.
  132. /// </summary>
  133. /// <param name="camera">The Transform to with the camera on to add the fade functionality to.</param>
  134. public override void AddHeadsetFade(Transform camera)
  135. {
  136. if (camera && !camera.GetComponent<VRTK_ScreenFade>())
  137. {
  138. camera.gameObject.AddComponent<VRTK_ScreenFade>();
  139. }
  140. }
  141. protected virtual void SetHeadsetCaches()
  142. {
  143. Transform currentHeadset = GetHeadset();
  144. if (cachedHeadsetVelocityEstimator == null && currentHeadset != null)
  145. {
  146. cachedHeadsetVelocityEstimator = (currentHeadset.GetComponent<VRTK_VelocityEstimator>() != null ? currentHeadset.GetComponent<VRTK_VelocityEstimator>() : currentHeadset.gameObject.AddComponent<VRTK_VelocityEstimator>());
  147. }
  148. }
  149. #endif
  150. }
  151. }