2
0

SteamVR_Fade.cs 4.2 KB

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