IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
SteamVR_Fade.cs
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 
23 using UnityEngine;
24 using Valve.VR;
25 
26 public class SteamVR_Fade : MonoBehaviour
27 {
28  private Color currentColor = new Color(0, 0, 0, 0); // default starting color: black and fully transparent
29  private Color targetColor = new Color(0, 0, 0, 0); // default target color: black and fully transparent
30  private Color deltaColor = new Color(0, 0, 0, 0); // the delta-color is basically the "speed / second" at which the current color should change
31  private bool fadeOverlay = false;
32 
33  static public void Start(Color newColor, float duration, bool fadeOverlay = false)
34  {
35  SteamVR_Events.Fade.Send(newColor, duration, fadeOverlay);
36  }
37 
38  static public void View(Color newColor, float duration)
39  {
40  var compositor = OpenVR.Compositor;
41  if (compositor != null)
42  compositor.FadeToColor(duration, newColor.r, newColor.g, newColor.b, newColor.a, false);
43  }
44 
45 #if TEST_FADE_VIEW
46  void Update()
47  {
48  if (Input.GetKeyDown(KeyCode.Space))
49  {
50  SteamVR_Fade.View(Color.black, 0);
51  SteamVR_Fade.View(Color.clear, 1);
52  }
53  }
54 #endif
55 
56  public void OnStartFade(Color newColor, float duration, bool fadeOverlay)
57  {
58  if (duration > 0.0f)
59  {
60  targetColor = newColor;
61  deltaColor = (targetColor - currentColor) / duration;
62  }
63  else
64  {
65  currentColor = newColor;
66  }
67  }
68 
69  static Material fadeMaterial = null;
70  static int fadeMaterialColorID = -1;
71 
72  void OnEnable()
73  {
74  if (fadeMaterial == null)
75  {
76  fadeMaterial = new Material(Shader.Find("Custom/SteamVR_Fade"));
77  fadeMaterialColorID = Shader.PropertyToID("fadeColor");
78  }
79 
80  SteamVR_Events.Fade.Listen(OnStartFade);
81  SteamVR_Events.FadeReady.Send();
82  }
83 
84  void OnDisable()
85  {
86  SteamVR_Events.Fade.Remove(OnStartFade);
87  }
88 
89  void OnPostRender()
90  {
91  if (currentColor != targetColor)
92  {
93  // if the difference between the current alpha and the desired alpha is smaller than delta-alpha * deltaTime, then we're pretty much done fading:
94  if (Mathf.Abs(currentColor.a - targetColor.a) < Mathf.Abs(deltaColor.a) * Time.deltaTime)
95  {
96  currentColor = targetColor;
97  deltaColor = new Color(0, 0, 0, 0);
98  }
99  else
100  {
101  currentColor += deltaColor * Time.deltaTime;
102  }
103 
104  if (fadeOverlay)
105  {
106  var overlay = SteamVR_Overlay.instance;
107  if (overlay != null)
108  {
109  overlay.alpha = 1.0f - currentColor.a;
110  }
111  }
112  }
113 
114  if (currentColor.a > 0 && fadeMaterial)
115  {
116  fadeMaterial.SetColor(fadeMaterialColorID, currentColor);
117  fadeMaterial.SetPass(0);
118  GL.Begin(GL.QUADS);
119 
120  GL.Vertex3(-1, -1, 0);
121  GL.Vertex3( 1, -1, 0);
122  GL.Vertex3(1, 1, 0);
123  GL.Vertex3(-1, 1, 0);
124  GL.End();
125  }
126  }
127 }
128 
Definition: View.cs:9