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.

128 lines
3.4 KiB

  1. //#define TEST_FADE_VIEW
  2. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  3. //
  4. // Purpose: CameraFade script adapted to work with SteamVR.
  5. //
  6. // Usage: Add to your top level SteamVR_Camera (the one with ApplyDistoration
  7. // checked) and drag a reference to this component into SteamVR_Camera
  8. // RenderComponents list. Then call the static helper function
  9. // SteamVR_Fade.Start with the desired color and duration.
  10. // Use a duration of zero to set the start color.
  11. //
  12. // Example: Fade down from black over one second.
  13. // SteamVR_Fade.Start(Color.black, 0);
  14. // SteamVR_Fade.Start(Color.clear, 1);
  15. //
  16. // Note: This component is provided to fade out a single camera layer's
  17. // scene view. If instead you want to fade the entire view, use:
  18. // SteamVR_Fade.View(Color.black, 1);
  19. // (Does not affect the game view, however.)
  20. //
  21. //=============================================================================
  22. using UnityEngine;
  23. using Valve.VR;
  24. public class SteamVR_Fade : MonoBehaviour
  25. {
  26. private Color currentColor = new Color(0, 0, 0, 0); // default starting color: black and fully transparent
  27. private Color targetColor = new Color(0, 0, 0, 0); // default target color: black and fully transparent
  28. private Color deltaColor = new Color(0, 0, 0, 0); // the delta-color is basically the "speed / second" at which the current color should change
  29. private bool fadeOverlay = false;
  30. static public void Start(Color newColor, float duration, bool fadeOverlay = false)
  31. {
  32. SteamVR_Events.Fade.Send(newColor, duration, fadeOverlay);
  33. }
  34. static public void View(Color newColor, float duration)
  35. {
  36. var compositor = OpenVR.Compositor;
  37. if (compositor != null)
  38. compositor.FadeToColor(duration, newColor.r, newColor.g, newColor.b, newColor.a, false);
  39. }
  40. #if TEST_FADE_VIEW
  41. void Update()
  42. {
  43. if (Input.GetKeyDown(KeyCode.Space))
  44. {
  45. SteamVR_Fade.View(Color.black, 0);
  46. SteamVR_Fade.View(Color.clear, 1);
  47. }
  48. }
  49. #endif
  50. public void OnStartFade(Color newColor, float duration, bool fadeOverlay)
  51. {
  52. if (duration > 0.0f)
  53. {
  54. targetColor = newColor;
  55. deltaColor = (targetColor - currentColor) / duration;
  56. }
  57. else
  58. {
  59. currentColor = newColor;
  60. }
  61. }
  62. static Material fadeMaterial = null;
  63. static int fadeMaterialColorID = -1;
  64. void OnEnable()
  65. {
  66. if (fadeMaterial == null)
  67. {
  68. fadeMaterial = new Material(Shader.Find("Custom/SteamVR_Fade"));
  69. fadeMaterialColorID = Shader.PropertyToID("fadeColor");
  70. }
  71. SteamVR_Events.Fade.Listen(OnStartFade);
  72. SteamVR_Events.FadeReady.Send();
  73. }
  74. void OnDisable()
  75. {
  76. SteamVR_Events.Fade.Remove(OnStartFade);
  77. }
  78. void OnPostRender()
  79. {
  80. if (currentColor != targetColor)
  81. {
  82. // if the difference between the current alpha and the desired alpha is smaller than delta-alpha * deltaTime, then we're pretty much done fading:
  83. if (Mathf.Abs(currentColor.a - targetColor.a) < Mathf.Abs(deltaColor.a) * Time.deltaTime)
  84. {
  85. currentColor = targetColor;
  86. deltaColor = new Color(0, 0, 0, 0);
  87. }
  88. else
  89. {
  90. currentColor += deltaColor * Time.deltaTime;
  91. }
  92. if (fadeOverlay)
  93. {
  94. var overlay = SteamVR_Overlay.instance;
  95. if (overlay != null)
  96. {
  97. overlay.alpha = 1.0f - currentColor.a;
  98. }
  99. }
  100. }
  101. if (currentColor.a > 0 && fadeMaterial)
  102. {
  103. fadeMaterial.SetColor(fadeMaterialColorID, currentColor);
  104. fadeMaterial.SetPass(0);
  105. GL.Begin(GL.QUADS);
  106. GL.Vertex3(-1, -1, 0);
  107. GL.Vertex3( 1, -1, 0);
  108. GL.Vertex3(1, 1, 0);
  109. GL.Vertex3(-1, 1, 0);
  110. GL.End();
  111. }
  112. }
  113. }