VR Development Framework
v 1.0.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
AVR_CameraFade.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 
5 namespace AVR.Core {
6  /// <summary>
7  /// Will fade-in / fade-out the screen to a given color on command.
8  /// NOTE: The color will *only* be faded for the main camera (Camera.main). If you want it to run for *all* cameras or different ones, inherit from this object and adjust the OnPostRenderCallback function.
9  /// </summary>
10  [AVR.Core.Attributes.DocumentationUrl("class_a_v_r_1_1_core_1_1_a_v_r___camera_fade.html")]
11  public class AVR_CameraFade : AVR.Core.AVR_Effect
12  {
13  /// <summary>
14  /// Material we fade to. Leave this blank for a uniform color.
15  /// </summary>
16  public Material fadeMaterial;
17 
18  /// <summary>
19  /// Value of the "_Color" component of the materials shader.
20  /// </summary>
21  public Color overlayColor;
22 
23  /// <summary>
24  /// Starting alpha value of the color
25  /// </summary>
26  public float start_value = 0.0f;
27 
28  /// <summary>
29  /// Ending alpha value of the color
30  /// </summary>
31  public float end_value = 1.0f;
32 
33  /// <summary>
34  /// Time for fade-in effect to conclude.
35  /// </summary>
36  public float fade_in_duration = 0.5f;
37 
38  /// <summary>
39  /// Time for fade-out effect to conclude.
40  /// </summary>
41  public float fade_out_duration = 0.5f;
42 
43  // Private
44  private float stime = 0.0f;
45  private bool do_fade_in = false;
46  private bool do_fade_out = false;
47 
48  protected virtual void Awake()
49  {
50  if (fadeMaterial == null) fadeMaterial = new Material(Shader.Find("AVR/CameraFade"));
51  overlayColor.a = start_value;
52  fadeMaterial.SetColor("_Color", overlayColor);
53  }
54 
55  protected virtual void OnEnable() {
56  Camera.onPostRender += OnPostRenderCallback;
57  }
58 
59  protected virtual void OnDisable() {
60  Camera.onPostRender -= OnPostRenderCallback;
61  }
62 
63  public override void StartEffect()
64  {
65  do_fade_in = true;
66  stime = Time.time;
67  }
68 
69  public override void EndEffect()
70  {
71  do_fade_out = true;
72  stime = Time.time;
73  }
74 
75  public override void StartEndEffect()
76  {
77  do_fade_in = do_fade_out = true;
78  stime = Time.time;
79  }
80 
81  protected virtual void Update()
82  {
83  if (do_fade_in)
84  {
85  float t = Time.time - stime;
86  overlayColor.a = Mathf.Lerp(start_value, end_value, t / fade_in_duration);
87  fadeMaterial.SetColor("_Color", overlayColor);
88 
89  if (t > fade_in_duration)
90  {
91  do_fade_in = false;
92  stime = Time.time;
93  }
94  }
95 
96  if (do_fade_out && !do_fade_in)
97  {
98  float t = Time.time - stime;
99  overlayColor.a = Mathf.Lerp(end_value, start_value, t / fade_out_duration);
100  fadeMaterial.SetColor("_Color", overlayColor);
101 
102  if (t > fade_out_duration)
103  {
104  do_fade_out = false;
105  }
106  }
107  }
108 
109  public override bool isBusy()
110  {
111  return do_fade_in || do_fade_out;
112  }
113 
114  protected virtual void OnPostRenderCallback(Camera cam)
115  {
116  if (overlayColor.a > 0 && cam==Camera.main)
117  {
118  GL.PushMatrix();
119  GL.LoadOrtho();
120  fadeMaterial.SetPass(0);
121  GL.Begin(GL.QUADS);
122  GL.Color(overlayColor);
123  GL.Vertex3(0, 0, 0);
124  GL.Vertex3(1, 0, 0);
125  GL.Vertex3(1, 1, 0);
126  GL.Vertex3(0, 1, 0);
127  GL.End();
128  GL.PopMatrix();
129  }
130  }
131  }
132 }
virtual void OnDisable()
override bool isBusy()
True if the effect is starting/started or ending but not yet ended.
virtual void OnEnable()
override void StartEndEffect()
Start the effect and automatically end it right after
override void EndEffect()
End the effect
Sets the documentation html file inside of Packages/com.avr.core/Documentation/html of a given class...
virtual void OnPostRenderCallback(Camera cam)
Will fade-in / fade-out the screen to a given color on command. NOTE: The color will only be faded fo...
override void StartEffect()
Start the effect