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.

165 lines
6.6 KiB

  1. // SteamVR Headset|SDK_SteamVR|003
  2. namespace VRTK
  3. {
  4. #if VRTK_DEFINE_SDK_STEAMVR
  5. using UnityEngine;
  6. using System.Collections.Generic;
  7. using Valve.VR;
  8. #endif
  9. /// <summary>
  10. /// The SteamVR Headset SDK script provides a bridge to the SteamVR SDK.
  11. /// </summary>
  12. [SDK_Description(typeof(SDK_SteamVRSystem))]
  13. public class SDK_SteamVRHeadset
  14. #if VRTK_DEFINE_SDK_STEAMVR
  15. : SDK_BaseHeadset
  16. #else
  17. : SDK_FallbackHeadset
  18. #endif
  19. {
  20. #if VRTK_DEFINE_SDK_STEAMVR
  21. /// <summary>
  22. /// The ProcessUpdate method enables an SDK to run logic for every Unity Update
  23. /// </summary>
  24. /// <param name="options">A dictionary of generic options that can be used to within the update.</param>
  25. public override void ProcessUpdate(Dictionary<string, object> options)
  26. {
  27. }
  28. /// <summary>
  29. /// The ProcessFixedUpdate method enables an SDK to run logic for every Unity FixedUpdate
  30. /// </summary>
  31. /// <param name="options">A dictionary of generic options that can be used to within the fixed update.</param>
  32. public override void ProcessFixedUpdate(Dictionary<string, object> options)
  33. {
  34. }
  35. /// <summary>
  36. /// The GetHeadset method returns the Transform of the object that is used to represent the headset in the scene.
  37. /// </summary>
  38. /// <returns>A transform of the object representing the headset in the scene.</returns>
  39. public override Transform GetHeadset()
  40. {
  41. cachedHeadset = GetSDKManagerHeadset();
  42. if (cachedHeadset == null)
  43. {
  44. #if (UNITY_5_4_OR_NEWER)
  45. SteamVR_Camera foundCamera = VRTK_SharedMethods.FindEvenInactiveComponent<SteamVR_Camera>(true);
  46. #else
  47. SteamVR_GameView foundCamera = VRTK_SharedMethods.FindEvenInactiveComponent<SteamVR_GameView>(true);
  48. #endif
  49. if (foundCamera != null)
  50. {
  51. cachedHeadset = foundCamera.transform;
  52. }
  53. }
  54. return cachedHeadset;
  55. }
  56. /// <summary>
  57. /// The GetHeadsetCamera method returns the Transform of the object that is used to hold the headset camera in the scene.
  58. /// </summary>
  59. /// <returns>A transform of the object holding the headset camera in the scene.</returns>
  60. public override Transform GetHeadsetCamera()
  61. {
  62. cachedHeadsetCamera = GetSDKManagerHeadset();
  63. if (cachedHeadsetCamera == null)
  64. {
  65. SteamVR_Camera foundCamera = VRTK_SharedMethods.FindEvenInactiveComponent<SteamVR_Camera>(true);
  66. if (foundCamera != null)
  67. {
  68. cachedHeadsetCamera = foundCamera.transform;
  69. }
  70. }
  71. return cachedHeadsetCamera;
  72. }
  73. /// <summary>
  74. /// The GetHeadsetType method returns a string representing the type of headset connected.
  75. /// </summary>
  76. /// <returns>The string of the headset connected.</returns>
  77. public override string GetHeadsetType()
  78. {
  79. if (SteamVR.instance != null)
  80. {
  81. string manufactuer = CleanPropertyString(SteamVR.instance.GetStringProperty(ETrackedDeviceProperty.Prop_ManufacturerName_String));
  82. string model = CleanPropertyString(SteamVR.instance.GetStringProperty(ETrackedDeviceProperty.Prop_ModelNumber_String));
  83. //Check for specific manufacturer models
  84. switch (manufactuer)
  85. {
  86. case "htc":
  87. if (model.Contains("vive"))
  88. {
  89. return "htcvive";
  90. }
  91. break;
  92. case "oculus":
  93. return "oculusrift";
  94. case "windowsmr":
  95. return "windowsmixedreality";
  96. }
  97. //If no model check required then just return manufacturer
  98. return CleanPropertyString(manufactuer);
  99. }
  100. return CleanPropertyString("");
  101. }
  102. /// <summary>
  103. /// The GetHeadsetVelocity method is used to determine the current velocity of the headset.
  104. /// </summary>
  105. /// <returns>A Vector3 containing the current velocity of the headset.</returns>
  106. public override Vector3 GetHeadsetVelocity()
  107. {
  108. return SteamVR_Controller.Input((int)SteamVR_TrackedObject.EIndex.Hmd).velocity;
  109. }
  110. /// <summary>
  111. /// The GetHeadsetAngularVelocity method is used to determine the current angular velocity of the headset.
  112. /// </summary>
  113. /// <returns>A Vector3 containing the current angular velocity of the headset.</returns>
  114. public override Vector3 GetHeadsetAngularVelocity()
  115. {
  116. return SteamVR_Controller.Input((int)SteamVR_TrackedObject.EIndex.Hmd).angularVelocity;
  117. }
  118. /// <summary>
  119. /// The HeadsetFade method is used to apply a fade to the headset camera to progressively change the colour.
  120. /// </summary>
  121. /// <param name="color">The colour to fade to.</param>
  122. /// <param name="duration">The amount of time the fade should take to reach the given colour.</param>
  123. /// <param name="fadeOverlay">Determines whether to use an overlay on the fade.</param>
  124. public override void HeadsetFade(Color color, float duration, bool fadeOverlay = false)
  125. {
  126. SteamVR_Fade.Start(color, duration, fadeOverlay);
  127. }
  128. /// <summary>
  129. /// The HasHeadsetFade method checks to see if the given game object (usually the camera) has the ability to fade the viewpoint.
  130. /// </summary>
  131. /// <param name="obj">The Transform to check to see if a camera fade is available on.</param>
  132. /// <returns>Returns true if the headset has fade functionality on it.</returns>
  133. public override bool HasHeadsetFade(Transform obj)
  134. {
  135. if (obj.GetComponentInChildren<SteamVR_Fade>() != null)
  136. {
  137. return true;
  138. }
  139. return false;
  140. }
  141. /// <summary>
  142. /// The AddHeadsetFade method attempts to add the fade functionality to the game object with the camera on it.
  143. /// </summary>
  144. /// <param name="camera">The Transform to with the camera on to add the fade functionality to.</param>
  145. public override void AddHeadsetFade(Transform camera)
  146. {
  147. if (camera != null && camera.GetComponent<SteamVR_Fade>() == null)
  148. {
  149. camera.gameObject.AddComponent<SteamVR_Fade>();
  150. }
  151. }
  152. #endif
  153. }
  154. }