BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / VR / Scripts / Util / OVRScreenFade.cs

OVRScreenFade.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
OVRScreenFade.cs 5.1 KiB Download Raw
1/************************************************************************************
2Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
3
4Licensed under the Oculus Utilities SDK License Version 1.31 (the "License"); you may not use
5the Utilities SDK except in compliance with the License, which is provided at the time of installation
6or download, or which otherwise accompanies this software in either electronic or hard copy form.
7
8You may obtain a copy of the License at
9https://developer.oculus.com/licenses/utilities-1.31
10
11Unless required by applicable law or agreed to in writing, the Utilities SDK distributed
12under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
13ANY KIND, either express or implied. See the License for the specific language governing
14permissions and limitations under the License.
15************************************************************************************/
16
17using UnityEngine;
18using System.Collections; // required for Coroutines
19
20/// <summary>
21/// Fades the screen from black after a new scene is loaded. Fade can also be controlled mid-scene using SetUIFade and SetFadeLevel
22/// </summary>
23public class OVRScreenFade : MonoBehaviour
24{
25 [Tooltip("Fade duration")]
26 public float fadeTime = 2.0f;
27
28 [Tooltip("Screen color at maximum fade")]
29 public Color fadeColor = new Color(0.01f, 0.01f, 0.01f, 1.0f);
30
31 public bool fadeOnStart = true;
32
33 /// <summary>
34 /// The render queue used by the fade mesh. Reduce this if you need to render on top of it.
35 /// </summary>
36 public int renderQueue = 5000;
37
38 private float uiFadeAlpha = 0;
39
40 private MeshRenderer fadeRenderer;
41 private MeshFilter fadeMesh;
42 private Material fadeMaterial = null;
43 private bool isFading = false;
44
45 public float currentAlpha { get; private set; }
46
47 void Awake()
48 {
49 // create the fade material
50 fadeMaterial = new Material(Shader.Find("Oculus/Unlit Transparent Color"));
51 fadeMesh = gameObject.AddComponent<MeshFilter>();
52 fadeRenderer = gameObject.AddComponent<MeshRenderer>();
53
54 var mesh = new Mesh();
55 fadeMesh.mesh = mesh;
56
57 Vector3[] vertices = new Vector3[4];
58
59 float width = 2f;
60 float height = 2f;
61 float depth = 1f;
62
63 vertices[0] = new Vector3(-width, -height, depth);
64 vertices[1] = new Vector3(width, -height, depth);
65 vertices[2] = new Vector3(-width, height, depth);
66 vertices[3] = new Vector3(width, height, depth);
67
68 mesh.vertices = vertices;
69
70 int[] tri = new int[6];
71
72 tri[0] = 0;
73 tri[1] = 2;
74 tri[2] = 1;
75
76 tri[3] = 2;
77 tri[4] = 3;
78 tri[5] = 1;
79
80 mesh.triangles = tri;
81
82 Vector3[] normals = new Vector3[4];
83
84 normals[0] = -Vector3.forward;
85 normals[1] = -Vector3.forward;
86 normals[2] = -Vector3.forward;
87 normals[3] = -Vector3.forward;
88
89 mesh.normals = normals;
90
91 Vector2[] uv = new Vector2[4];
92
93 uv[0] = new Vector2(0, 0);
94 uv[1] = new Vector2(1, 0);
95 uv[2] = new Vector2(0, 1);
96 uv[3] = new Vector2(1, 1);
97
98 mesh.uv = uv;
99
100 SetFadeLevel(0);
101 }
102
103 /// <summary>
104 /// Start a fade out
105 /// </summary>
106 public void FadeOut()
107 {
108 StartCoroutine(Fade(0,1));
109 }
110
111
112 /// <summary>
113 /// Starts a fade in when a new level is loaded
114 /// </summary>
115 void OnLevelFinishedLoading(int level)
116 {
117 StartCoroutine(Fade(1,0));
118 }
119
120 /// <summary>
121 /// Automatically starts a fade in
122 /// </summary>
123 void Start()
124 {
125 if (fadeOnStart)
126 {
127 StartCoroutine(Fade(1,0));
128 }
129 }
130
131 void OnEnable()
132 {
133 if (!fadeOnStart)
134 {
135 SetFadeLevel(0);
136 }
137 }
138
139 /// <summary>
140 /// Cleans up the fade material
141 /// </summary>
142 void OnDestroy()
143 {
144 if (fadeRenderer != null)
145 Destroy(fadeRenderer);
146
147 if (fadeMaterial != null)
148 Destroy(fadeMaterial);
149
150 if (fadeMesh != null)
151 Destroy(fadeMesh);
152 }
153
154 /// <summary>
155 /// Set the UI fade level - fade due to UI in foreground
156 /// </summary>
157 public void SetUIFade(float level)
158 {
159 uiFadeAlpha = Mathf.Clamp01(level);
160 SetMaterialAlpha();
161 }
162 /// <summary>
163 /// Override current fade level
164 /// </summary>
165 /// <param name="level"></param>
166 public void SetFadeLevel(float level)
167 {
168 currentAlpha = level;
169 SetMaterialAlpha();
170 }
171
172 /// <summary>
173 /// Fades alpha from 1.0 to 0.0
174 /// </summary>
175 IEnumerator Fade(float startAlpha, float endAlpha)
176 {
177 float elapsedTime = 0.0f;
178 while (elapsedTime < fadeTime)
179 {
180 elapsedTime += Time.deltaTime;
181 currentAlpha = Mathf.Lerp(startAlpha, endAlpha, Mathf.Clamp01(elapsedTime / fadeTime));
182 SetMaterialAlpha();
183 yield return new WaitForEndOfFrame();
184 }
185 }
186
187 /// <summary>
188 /// Update material alpha. UI fade and the current fade due to fade in/out animations (or explicit control)
189 /// both affect the fade. (The max is taken)
190 /// </summary>
191 private void SetMaterialAlpha()
192 {
193 Color color = fadeColor;
194 color.a = Mathf.Max(currentAlpha, uiFadeAlpha);
195 isFading = color.a > 0;
196 if (fadeMaterial != null)
197 {
198 fadeMaterial.color = color;
199 fadeMaterial.renderQueue = renderQueue;
200 fadeRenderer.material = fadeMaterial;
201 fadeRenderer.enabled = isFading;
202 }
203 }
204}