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.

155 lines
6.3 KiB

  1. // Ximmerse Headset|SDK_Ximmerse|003
  2. namespace VRTK
  3. {
  4. #if VRTK_DEFINE_SDK_XIMMERSE
  5. using UnityEngine;
  6. using System.Collections.Generic;
  7. using Ximmerse.InputSystem;
  8. using Ximmerse.VR;
  9. #endif
  10. /// <summary>
  11. /// The Ximmerse Headset SDK script provides a bridge to the Ximmerse SDK.
  12. /// </summary>
  13. [SDK_Description(typeof(SDK_XimmerseSystem))]
  14. [SDK_Description(typeof(SDK_XimmerseSystem), 1)]
  15. public class SDK_XimmerseHeadset
  16. #if VRTK_DEFINE_SDK_XIMMERSE
  17. : SDK_BaseHeadset
  18. #else
  19. : SDK_FallbackHeadset
  20. #endif
  21. {
  22. #if VRTK_DEFINE_SDK_XIMMERSE
  23. private Quaternion previousHeadsetRotation;
  24. private Quaternion currentHeadsetRotation;
  25. private Vector3 previousHeadsetPosition;
  26. private Vector3 currentHeadsetPosition;
  27. /// <summary>
  28. /// The ProcessUpdate method enables an SDK to run logic for every Unity Update
  29. /// </summary>
  30. /// <param name="options">A dictionary of generic options that can be used to within the update.</param>
  31. public override void ProcessUpdate(Dictionary<string, object> options)
  32. {
  33. var device = GetHeadset();
  34. previousHeadsetRotation = currentHeadsetRotation;
  35. currentHeadsetRotation = device.transform.rotation;
  36. previousHeadsetPosition = currentHeadsetPosition;
  37. currentHeadsetPosition = device.transform.position;
  38. }
  39. /// <summary>
  40. /// The ProcessFixedUpdate method enables an SDK to run logic for every Unity FixedUpdate
  41. /// </summary>
  42. /// <param name="options">A dictionary of generic options that can be used to within the fixed update.</param>
  43. public override void ProcessFixedUpdate(Dictionary<string, object> options)
  44. {
  45. }
  46. /// <summary>
  47. /// The GetHeadsetType method returns a string representing the type of headset connected.
  48. /// </summary>
  49. /// <returns>The string of the headset connected.</returns>
  50. public override string GetHeadsetType()
  51. {
  52. return ScrapeHeadsetType();
  53. }
  54. /// <summary>
  55. /// The GetHeadsetVelocity method is used to determine the current velocity of the headset.
  56. /// </summary>
  57. /// <returns>A Vector3 containing the current velocity of the headset.</returns>
  58. public override Vector3 GetHeadsetVelocity()
  59. {
  60. var deltaMovement = currentHeadsetPosition - previousHeadsetPosition;
  61. deltaMovement /= Time.deltaTime;
  62. return deltaMovement;
  63. }
  64. /// <summary>
  65. /// The GetHeadsetAngularVelocity method is used to determine the current angular velocity of the headset.
  66. /// </summary>
  67. /// <returns>A Vector3 containing the current angular velocity of the headset.</returns>
  68. public override Vector3 GetHeadsetAngularVelocity()
  69. {
  70. var deltaRotation = currentHeadsetRotation * Quaternion.Inverse(previousHeadsetRotation);
  71. return new Vector3(Mathf.DeltaAngle(0, deltaRotation.eulerAngles.x), Mathf.DeltaAngle(0, deltaRotation.eulerAngles.y), Mathf.DeltaAngle(0, deltaRotation.eulerAngles.z));
  72. }
  73. /// <summary>
  74. /// The GetHeadset method returns the Transform of the object that is used to represent the headset in the scene.
  75. /// </summary>
  76. /// <returns>A transform of the object representing the headset in the scene.</returns>
  77. public override Transform GetHeadset()
  78. {
  79. cachedHeadset = GetSDKManagerHeadset();
  80. if (cachedHeadset == null)
  81. {
  82. var foundHeadset = VRTK_SharedMethods.FindEvenInactiveGameObject<VRContext>("TrackingSpace/CenterEyeAnchor", true);
  83. if (foundHeadset)
  84. {
  85. cachedHeadset = foundHeadset.transform;
  86. }
  87. }
  88. return cachedHeadset;
  89. }
  90. /// <summary>
  91. /// The GetHeadsetCamera method returns the Transform of the object that is used to hold the headset camera in the scene.
  92. /// </summary>
  93. /// <returns>A transform of the object holding the headset camera in the scene.</returns>
  94. public override Transform GetHeadsetCamera()
  95. {
  96. cachedHeadsetCamera = GetSDKManagerHeadset();
  97. if (cachedHeadsetCamera == null)
  98. {
  99. var foundCamera = VRContext.GetAnchor(VRNode.CenterEye);
  100. if (foundCamera)
  101. {
  102. cachedHeadsetCamera = foundCamera.transform;
  103. }
  104. }
  105. return cachedHeadsetCamera;
  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. #endif
  142. }
  143. }