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.

177 lines
6.4 KiB

  1. // Headset Fade|Presence|70020
  2. namespace VRTK
  3. {
  4. using UnityEngine;
  5. /// <summary>
  6. /// Event Payload
  7. /// </summary>
  8. /// <param name="timeTillComplete">A float that is the duration for the fade/unfade process has remaining.</param>
  9. /// <param name="currentTransform">The current Transform of the object that the Headset Fade script is attached to (Camera).</param>
  10. public struct HeadsetFadeEventArgs
  11. {
  12. public float timeTillComplete;
  13. public Transform currentTransform;
  14. }
  15. /// <summary>
  16. /// Event Payload
  17. /// </summary>
  18. /// <param name="sender">this object</param>
  19. /// <param name="e"><see cref="HeadsetFadeEventArgs"/></param>
  20. public delegate void HeadsetFadeEventHandler(object sender, HeadsetFadeEventArgs e);
  21. /// <summary>
  22. /// Provides the ability to change the colour of the headset view to a specified colour over a given duration.
  23. /// </summary>
  24. /// <remarks>
  25. /// **Script Usage:**
  26. /// * Place the `VRTK_HeadsetFade` script on any active scene GameObject.
  27. /// </remarks>
  28. /// <example>
  29. /// `VRTK/Examples/011_Camera_HeadSetCollisionFading` has collidable walls around the play area and if the user puts their head into any of the walls then the headset will fade to black.
  30. /// </example>
  31. [AddComponentMenu("VRTK/Scripts/Presence/VRTK_HeadsetFade")]
  32. public class VRTK_HeadsetFade : MonoBehaviour
  33. {
  34. /// <summary>
  35. /// Emitted when the user's headset begins to fade to a given colour.
  36. /// </summary>
  37. public event HeadsetFadeEventHandler HeadsetFadeStart;
  38. /// <summary>
  39. /// Emitted when the user's headset has completed the fade and is now fully at the given colour.
  40. /// </summary>
  41. public event HeadsetFadeEventHandler HeadsetFadeComplete;
  42. /// <summary>
  43. /// Emitted when the user's headset begins to unfade back to a transparent colour.
  44. /// </summary>
  45. public event HeadsetFadeEventHandler HeadsetUnfadeStart;
  46. /// <summary>
  47. /// Emitted when the user's headset has completed unfading and is now fully transparent again.
  48. /// </summary>
  49. public event HeadsetFadeEventHandler HeadsetUnfadeComplete;
  50. protected Transform headset;
  51. protected bool isTransitioning = false;
  52. protected bool isFaded = false;
  53. public virtual void OnHeadsetFadeStart(HeadsetFadeEventArgs e)
  54. {
  55. if (HeadsetFadeStart != null)
  56. {
  57. HeadsetFadeStart(this, e);
  58. }
  59. }
  60. public virtual void OnHeadsetFadeComplete(HeadsetFadeEventArgs e)
  61. {
  62. if (HeadsetFadeComplete != null)
  63. {
  64. HeadsetFadeComplete(this, e);
  65. }
  66. }
  67. public virtual void OnHeadsetUnfadeStart(HeadsetFadeEventArgs e)
  68. {
  69. if (HeadsetUnfadeStart != null)
  70. {
  71. HeadsetUnfadeStart(this, e);
  72. }
  73. }
  74. public virtual void OnHeadsetUnfadeComplete(HeadsetFadeEventArgs e)
  75. {
  76. if (HeadsetUnfadeComplete != null)
  77. {
  78. HeadsetUnfadeComplete(this, e);
  79. }
  80. }
  81. /// <summary>
  82. /// The IsFaded method returns true if the headset is currently fading or has completely faded and returns false if it is completely unfaded.
  83. /// </summary>
  84. /// <returns>Returns `true` if the headset is currently fading or faded.</returns>
  85. public virtual bool IsFaded()
  86. {
  87. return isFaded;
  88. }
  89. /// <summary>
  90. /// The IsTransitioning method returns true if the headset is currently fading or unfading and returns false if it is completely faded or unfaded.
  91. /// </summary>
  92. /// <returns>Returns `true` if the headset is currently in the process of fading or unfading.</returns>
  93. public virtual bool IsTransitioning()
  94. {
  95. return isTransitioning;
  96. }
  97. /// <summary>
  98. /// The Fade method initiates a change in the colour of the headset view to the given colour over a given duration.
  99. /// </summary>
  100. /// <param name="color">The colour to fade the headset view to.</param>
  101. /// <param name="duration">The time in seconds to take to complete the fade transition.</param>
  102. public virtual void Fade(Color color, float duration)
  103. {
  104. isFaded = false;
  105. isTransitioning = true;
  106. VRTK_SDK_Bridge.HeadsetFade(color, duration);
  107. OnHeadsetFadeStart(SetHeadsetFadeEvent(headset, duration));
  108. CancelInvoke("UnfadeComplete");
  109. Invoke("FadeComplete", duration);
  110. }
  111. /// <summary>
  112. /// The Unfade method initiates the headset to change colour back to a transparent colour over a given duration.
  113. /// </summary>
  114. /// <param name="duration">The time in seconds to take to complete the unfade transition.</param>
  115. public virtual void Unfade(float duration)
  116. {
  117. isFaded = true;
  118. isTransitioning = true;
  119. VRTK_SDK_Bridge.HeadsetFade(Color.clear, duration);
  120. OnHeadsetUnfadeStart(SetHeadsetFadeEvent(headset, duration));
  121. CancelInvoke("FadeComplete");
  122. Invoke("UnfadeComplete", duration);
  123. }
  124. protected virtual void Awake()
  125. {
  126. VRTK_SDKManager.AttemptAddBehaviourToToggleOnLoadedSetupChange(this);
  127. }
  128. protected virtual void OnEnable()
  129. {
  130. headset = VRTK_DeviceFinder.HeadsetCamera();
  131. isTransitioning = false;
  132. isFaded = false;
  133. VRTK_SharedMethods.AddCameraFade();
  134. }
  135. protected virtual void OnDestroy()
  136. {
  137. VRTK_SDKManager.AttemptRemoveBehaviourToToggleOnLoadedSetupChange(this);
  138. }
  139. protected virtual HeadsetFadeEventArgs SetHeadsetFadeEvent(Transform currentTransform, float duration)
  140. {
  141. HeadsetFadeEventArgs e;
  142. e.timeTillComplete = duration;
  143. e.currentTransform = currentTransform;
  144. return e;
  145. }
  146. protected virtual void FadeComplete()
  147. {
  148. isFaded = true;
  149. isTransitioning = false;
  150. OnHeadsetFadeComplete(SetHeadsetFadeEvent(headset, 0f));
  151. }
  152. protected virtual void UnfadeComplete()
  153. {
  154. isFaded = false;
  155. isTransitioning = false;
  156. OnHeadsetUnfadeComplete(SetHeadsetFadeEvent(headset, 0f));
  157. }
  158. }
  159. }