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.

97 lines
4.3 KiB

  1. // Headset Collision Fade|Presence|70030
  2. namespace VRTK
  3. {
  4. using UnityEngine;
  5. /// <summary>
  6. /// Initiates a fade of the headset view when a headset collision event is detected.
  7. /// </summary>
  8. /// <remarks>
  9. /// **Required Components:**
  10. /// * `VRTK_HeadsetCollision` - A Headset Collision script to determine when the HMD has collided with valid geometry.
  11. /// * `VRTK_HeadsetFade` - A Headset Fade script to alter the visible colour on the HMD view.
  12. ///
  13. /// **Script Usage:**
  14. /// * Place the `VRTK_HeadsetCollisionFade` script on any active scene GameObject.
  15. /// </remarks>
  16. /// <example>
  17. /// `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.
  18. /// </example>
  19. [AddComponentMenu("VRTK/Scripts/Presence/VRTK_HeadsetCollisionFade")]
  20. public class VRTK_HeadsetCollisionFade : MonoBehaviour
  21. {
  22. [Header("Collision Fade Settings")]
  23. [Tooltip("The amount of time to wait until a fade occurs.")]
  24. public float timeTillFade = 0f;
  25. [Tooltip("The fade blink speed on collision.")]
  26. public float blinkTransitionSpeed = 0.1f;
  27. [Tooltip("The colour to fade the headset to on collision.")]
  28. public Color fadeColor = Color.black;
  29. [Tooltip("A specified VRTK_PolicyList to use to determine whether any objects will be acted upon by the Headset Collision Fade.")]
  30. public VRTK_PolicyList targetListPolicy;
  31. [Header("Custom Settings")]
  32. [Tooltip("The VRTK Headset Collision script to use when determining headset collisions. If this is left blank then the script will need to be applied to the same GameObject.")]
  33. public VRTK_HeadsetCollision headsetCollision;
  34. [Tooltip("The VRTK Headset Fade script to use when fading the headset. If this is left blank then the script will need to be applied to the same GameObject.")]
  35. public VRTK_HeadsetFade headsetFade;
  36. protected virtual void OnEnable()
  37. {
  38. headsetFade = (headsetFade != null ? headsetFade : FindObjectOfType<VRTK_HeadsetFade>());
  39. headsetCollision = (headsetCollision != null ? headsetCollision : FindObjectOfType<VRTK_HeadsetCollision>());
  40. if (headsetFade == null)
  41. {
  42. VRTK_Logger.Error(VRTK_Logger.GetCommonMessage(VRTK_Logger.CommonMessageKeys.REQUIRED_COMPONENT_MISSING_FROM_GAMEOBJECT, "VRTK_HeadsetCollisionFade", "VRTK_HeadsetFade", "the same or child"));
  43. return;
  44. }
  45. if (headsetCollision == null)
  46. {
  47. VRTK_Logger.Error(VRTK_Logger.GetCommonMessage(VRTK_Logger.CommonMessageKeys.REQUIRED_COMPONENT_MISSING_FROM_GAMEOBJECT, "VRTK_HeadsetCollisionFade", "VRTK_HeadsetCollision", "the same or child"));
  48. return;
  49. }
  50. headsetCollision.HeadsetCollisionDetect += new HeadsetCollisionEventHandler(OnHeadsetCollisionDetect);
  51. headsetCollision.HeadsetCollisionEnded += new HeadsetCollisionEventHandler(OnHeadsetCollisionEnded);
  52. }
  53. protected virtual void OnDisable()
  54. {
  55. if (headsetCollision != null)
  56. {
  57. headsetCollision.HeadsetCollisionDetect -= new HeadsetCollisionEventHandler(OnHeadsetCollisionDetect);
  58. headsetCollision.HeadsetCollisionEnded -= new HeadsetCollisionEventHandler(OnHeadsetCollisionEnded);
  59. }
  60. }
  61. protected virtual void OnHeadsetCollisionDetect(object sender, HeadsetCollisionEventArgs e)
  62. {
  63. if (ValidTarget(e.collider))
  64. {
  65. Invoke("StartFade", timeTillFade);
  66. }
  67. }
  68. protected virtual void OnHeadsetCollisionEnded(object sender, HeadsetCollisionEventArgs e)
  69. {
  70. if (ValidTarget(e.collider))
  71. {
  72. CancelInvoke("StartFade");
  73. headsetFade.Unfade(blinkTransitionSpeed);
  74. }
  75. }
  76. protected virtual void StartFade()
  77. {
  78. headsetFade.Fade(fadeColor, blinkTransitionSpeed);
  79. }
  80. protected virtual bool ValidTarget(Collider target)
  81. {
  82. return (target != null && !(VRTK_PolicyList.Check(target.gameObject, targetListPolicy)));
  83. }
  84. }
  85. }