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.

133 lines
5.6 KiB

  1. // Unity Headset|SDK_Unity|002
  2. namespace VRTK
  3. {
  4. using UnityEngine;
  5. using System.Collections.Generic;
  6. /// <summary>
  7. /// The Unity Headset SDK script provides a bridge to the base Unity headset support.
  8. /// </summary>
  9. [SDK_Description(typeof(SDK_UnitySystem))]
  10. [SDK_Description(typeof(SDK_UnitySystem), 1)]
  11. [SDK_Description(typeof(SDK_UnitySystem), 2)]
  12. [SDK_Description(typeof(SDK_UnitySystem), 3)]
  13. [SDK_Description(typeof(SDK_UnitySystem), 4)]
  14. [SDK_Description(typeof(SDK_UnitySystem), 5)]
  15. public class SDK_UnityHeadset : SDK_BaseHeadset
  16. {
  17. protected VRTK_VelocityEstimator cachedHeadsetVelocityEstimator;
  18. /// <summary>
  19. /// The ProcessUpdate method enables an SDK to run logic for every Unity Update
  20. /// </summary>
  21. /// <param name="options">A dictionary of generic options that can be used to within the update.</param>
  22. public override void ProcessUpdate(Dictionary<string, object> options)
  23. {
  24. }
  25. /// <summary>
  26. /// The ProcessFixedUpdate method enables an SDK to run logic for every Unity FixedUpdate
  27. /// </summary>
  28. /// <param name="options">A dictionary of generic options that can be used to within the fixed update.</param>
  29. public override void ProcessFixedUpdate(Dictionary<string, object> options)
  30. {
  31. }
  32. /// <summary>
  33. /// The GetHeadset method returns the Transform of the object that is used to represent the headset in the scene.
  34. /// </summary>
  35. /// <returns>A transform of the object representing the headset in the scene.</returns>
  36. public override Transform GetHeadset()
  37. {
  38. cachedHeadset = GetSDKManagerHeadset();
  39. if (cachedHeadset == null)
  40. {
  41. GameObject foundHeadset = VRTK_SharedMethods.FindEvenInactiveGameObject<SDK_UnityHeadsetTracker>(null, true);
  42. if (foundHeadset != null)
  43. {
  44. cachedHeadset = foundHeadset.transform;
  45. }
  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. return GetHeadset();
  56. }
  57. /// <summary>
  58. /// The GetHeadsetType method returns a string representing the type of headset connected.
  59. /// </summary>
  60. /// <returns>The string of the headset connected.</returns>
  61. public override string GetHeadsetType()
  62. {
  63. return ScrapeHeadsetType();
  64. }
  65. /// <summary>
  66. /// The GetHeadsetVelocity method is used to determine the current velocity of the headset.
  67. /// </summary>
  68. /// <returns>A Vector3 containing the current velocity of the headset.</returns>
  69. public override Vector3 GetHeadsetVelocity()
  70. {
  71. SetHeadsetCaches();
  72. return cachedHeadsetVelocityEstimator.GetVelocityEstimate();
  73. }
  74. /// <summary>
  75. /// The GetHeadsetAngularVelocity method is used to determine the current angular velocity of the headset.
  76. /// </summary>
  77. /// <returns>A Vector3 containing the current angular velocity of the headset.</returns>
  78. public override Vector3 GetHeadsetAngularVelocity()
  79. {
  80. SetHeadsetCaches();
  81. return cachedHeadsetVelocityEstimator.GetAngularVelocityEstimate();
  82. }
  83. /// <summary>
  84. /// The HeadsetFade method is used to apply a fade to the headset camera to progressively change the colour.
  85. /// </summary>
  86. /// <param name="color">The colour to fade to.</param>
  87. /// <param name="duration">The amount of time the fade should take to reach the given colour.</param>
  88. /// <param name="fadeOverlay">Determines whether to use an overlay on the fade.</param>
  89. public override void HeadsetFade(Color color, float duration, bool fadeOverlay = false)
  90. {
  91. VRTK_ScreenFade.Start(color, duration);
  92. }
  93. /// <summary>
  94. /// The HasHeadsetFade method checks to see if the given game object (usually the camera) has the ability to fade the viewpoint.
  95. /// </summary>
  96. /// <param name="obj">The Transform to check to see if a camera fade is available on.</param>
  97. /// <returns>Returns true if the headset has fade functionality on it.</returns>
  98. public override bool HasHeadsetFade(Transform obj)
  99. {
  100. return (obj.GetComponentInChildren<VRTK_ScreenFade>());
  101. }
  102. /// <summary>
  103. /// The AddHeadsetFade method attempts to add the fade functionality to the game object with the camera on it.
  104. /// </summary>
  105. /// <param name="camera">The Transform to with the camera on to add the fade functionality to.</param>
  106. public override void AddHeadsetFade(Transform camera)
  107. {
  108. if (camera != null && !camera.GetComponent<VRTK_ScreenFade>())
  109. {
  110. camera.gameObject.AddComponent<VRTK_ScreenFade>();
  111. }
  112. }
  113. protected virtual void SetHeadsetCaches()
  114. {
  115. Transform currentHeadset = GetHeadset();
  116. if (cachedHeadsetVelocityEstimator == null && currentHeadset != null)
  117. {
  118. cachedHeadsetVelocityEstimator = currentHeadset.GetComponent<VRTK_VelocityEstimator>();
  119. }
  120. }
  121. }
  122. }