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.

164 lines
6.5 KiB

  1. // WindowsMR Headset|SDK_WindowsMR|003
  2. namespace VRTK
  3. {
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. /// <summary>
  7. /// The WindowsMR Headset SDK script provides a bridge to the WindowsMR XR.
  8. /// </summary>
  9. [SDK_Description(typeof(SDK_WindowsMR))]
  10. public class SDK_WindowsMRHeadset
  11. #if VRTK_DEFINE_SDK_WINDOWSMR && UNITY_2017_2_OR_NEWER
  12. : SDK_BaseHeadset
  13. #else
  14. : SDK_FallbackHeadset
  15. #endif
  16. {
  17. #if VRTK_DEFINE_SDK_WINDOWSMR && UNITY_2017_2_OR_NEWER
  18. protected Vector3 currentHeadsetPosition;
  19. protected Vector3 previousHeadsetPosition;
  20. protected Vector3 currentHeadsetVelocity;
  21. protected Quaternion currentHeadsetRotation;
  22. protected Quaternion previousHeadsetRotation;
  23. #region Overriden base functions
  24. /// <summary>
  25. /// The ProcessFixedUpdate method enables an SDK to run logic for every Unity FixedUpdate
  26. /// </summary>
  27. /// <param name="options">A dictionary of generic options that can be used to within the fixed update.</param>
  28. public override void ProcessFixedUpdate(Dictionary<string, object> options)
  29. {
  30. UpdateVelocity();
  31. UpdateRotation();
  32. }
  33. /// <summary>
  34. /// The ProcessUpdate method enables an SDK to run logic for every Unity Update
  35. /// </summary>
  36. /// <param name="options">A dictionary of generic options that can be used to within the update.</param>
  37. public override void ProcessUpdate(Dictionary<string, object> options)
  38. {
  39. UpdateVelocity();
  40. UpdateRotation();
  41. }
  42. /// <summary>
  43. /// The GetHeadset method returns the Transform of the object that is used to represent the headset in the scene.
  44. /// </summary>
  45. /// <returns>A transform of the object representing the headset in the scene.</returns>
  46. public override Transform GetHeadset()
  47. {
  48. cachedHeadset = GetSDKManagerHeadset();
  49. if (cachedHeadset == null)
  50. {
  51. WindowsMR_Camera foundCamera = VRTK_SharedMethods.FindEvenInactiveComponent<WindowsMR_Camera>(true);
  52. if (foundCamera != null)
  53. {
  54. cachedHeadset = foundCamera.transform;
  55. }
  56. }
  57. return cachedHeadset;
  58. }
  59. /// <summary>
  60. /// The GetHeadsetAngularVelocity method is used to determine the current angular velocity of the headset.
  61. /// </summary>
  62. /// <returns>A Vector3 containing the current angular velocity of the headset.</returns>
  63. public override Vector3 GetHeadsetAngularVelocity()
  64. {
  65. Quaternion deltaRotation = currentHeadsetRotation * Quaternion.Inverse(previousHeadsetRotation);
  66. return new Vector3(Mathf.DeltaAngle(0, deltaRotation.eulerAngles.x), Mathf.DeltaAngle(0, deltaRotation.eulerAngles.y), Mathf.DeltaAngle(0, deltaRotation.eulerAngles.z));
  67. }
  68. /// <summary>
  69. /// The GetHeadsetCamera method returns the Transform of the object that is used to hold the headset camera in the scene.
  70. /// </summary>
  71. /// <returns>A transform of the object holding the headset camera in the scene.</returns>
  72. public override Transform GetHeadsetCamera()
  73. {
  74. // For Immersive MR the camera is the same as the headset.
  75. return GetHeadset();
  76. }
  77. /// <summary>
  78. /// The GetHeadsetVelocity method is used to determine the current velocity of the headset.
  79. /// </summary>
  80. /// <returns>A Vector3 containing the current velocity of the headset.</returns>
  81. public override Vector3 GetHeadsetVelocity()
  82. {
  83. UpdateVelocity();
  84. return currentHeadsetVelocity;
  85. }
  86. /// <summary>
  87. /// The HasHeadsetFade method checks to see if the given game object (usually the camera) has the ability to fade the viewpoint.
  88. /// </summary>
  89. /// <param name="obj">The Transform to check to see if a camera fade is available on.</param>
  90. /// <returns>Returns true if the headset has fade functionality on it.</returns>
  91. public override bool HasHeadsetFade(Transform obj)
  92. {
  93. if (obj.GetComponentInChildren<VRTK_ScreenFade>() != null)
  94. {
  95. return true;
  96. }
  97. return false;
  98. }
  99. /// <summary>
  100. /// The HeadsetFade method is used to apply a fade to the headset camera to progressively change the colour.
  101. /// </summary>
  102. /// <param name="color">The colour to fade to.</param>
  103. /// <param name="duration">The amount of time the fade should take to reach the given colour.</param>
  104. /// <param name="fadeOverlay">Determines whether to use an overlay on the fade.</param>
  105. public override void HeadsetFade(Color color, float duration, bool fadeOverlay = false)
  106. {
  107. VRTK_ScreenFade.Start(color, duration);
  108. }
  109. /// <summary>
  110. /// The AddHeadsetFade method attempts to add the fade functionality to the game object with the camera on it.
  111. /// </summary>
  112. /// <param name="camera">The Transform to with the camera on to add the fade functionality to.</param>
  113. public override void AddHeadsetFade(Transform camera)
  114. {
  115. if (camera != null && camera.GetComponent<VRTK_ScreenFade>() == null)
  116. {
  117. camera.gameObject.AddComponent<VRTK_ScreenFade>();
  118. }
  119. }
  120. /// <summary>
  121. /// The GetHeadsetType method returns a string representing the type of headset connected.
  122. /// </summary>
  123. /// <returns>The string of the headset connected.</returns>
  124. public override string GetHeadsetType()
  125. {
  126. return CleanPropertyString("windowsmixedreality");
  127. }
  128. #endregion
  129. /// <summary>
  130. /// Update rotation values of headset.
  131. /// </summary>
  132. protected virtual void UpdateRotation()
  133. {
  134. previousHeadsetRotation = currentHeadsetRotation;
  135. currentHeadsetRotation = GetHeadset().transform.rotation;
  136. }
  137. /// <summary>
  138. /// Update velocity values of headset.
  139. /// </summary>
  140. protected virtual void UpdateVelocity()
  141. {
  142. previousHeadsetPosition = currentHeadsetPosition;
  143. currentHeadsetPosition = GetHeadset().transform.position;
  144. currentHeadsetVelocity = (previousHeadsetPosition - currentHeadsetPosition) / Time.deltaTime;
  145. }
  146. #endif
  147. }
  148. }