comparison Orchestland/Assets/OVR/Scripts/Util/OVRScreenFade.cs @ 1:f7675884f2a1

Add Orchestland project
author Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
date Fri, 17 Jul 2015 23:09:20 +0900
parents
children
comparison
equal deleted inserted replaced
0:347d21cdfc22 1:f7675884f2a1
1 /************************************************************************************
2
3 Copyright : Copyright 2014 Oculus VR, LLC. All Rights reserved.
4
5 Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
6 you may not use the Oculus VR Rift SDK except in compliance with the License,
7 which is provided at the time of installation or download, or which
8 otherwise accompanies this software in either electronic or hard copy form.
9
10 You may obtain a copy of the License at
11
12 http://www.oculusvr.com/licenses/LICENSE-3.2
13
14 Unless required by applicable law or agreed to in writing, the Oculus VR SDK
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19
20 ************************************************************************************/
21
22 using UnityEngine;
23 using System.Collections; // required for Coroutines
24
25 /// <summary>
26 /// Fades the screen from black after a new scene is loaded.
27 /// </summary>
28 public class OVRScreenFade : MonoBehaviour
29 {
30 /// <summary>
31 /// How long it takes to fade.
32 /// </summary>
33 public float fadeTime = 2.0f;
34
35 /// <summary>
36 /// The initial screen color.
37 /// </summary>
38 public Color fadeColor = new Color(0.01f, 0.01f, 0.01f, 1.0f);
39
40 /// <summary>
41 /// The shader to use when rendering the fade.
42 /// </summary>
43 public Shader fadeShader = null;
44
45 private Material fadeMaterial = null;
46 private bool isFading = false;
47
48 /// <summary>
49 /// Initialize.
50 /// </summary>
51 void Awake()
52 {
53 // create the fade material
54 fadeMaterial = (fadeShader != null) ? new Material(fadeShader) : new Material(Shader.Find("Transparent/Diffuse"));
55 }
56
57 /// <summary>
58 /// Starts the fade in
59 /// </summary>
60 void OnEnable()
61 {
62 StartCoroutine(FadeIn());
63 }
64
65 /// <summary>
66 /// Starts a fade in when a new level is loaded
67 /// </summary>
68 void OnLevelWasLoaded(int level)
69 {
70 StartCoroutine(FadeIn());
71 }
72
73 /// <summary>
74 /// Cleans up the fade material
75 /// </summary>
76 void OnDestroy()
77 {
78 if (fadeMaterial != null)
79 {
80 Destroy(fadeMaterial);
81 }
82 }
83
84 /// <summary>
85 /// Fades alpha from 1.0 to 0.0
86 /// </summary>
87 IEnumerator FadeIn()
88 {
89 float elapsedTime = 0.0f;
90 Color color = fadeMaterial.color = fadeColor;
91 isFading = true;
92 while (elapsedTime < fadeTime)
93 {
94 yield return new WaitForEndOfFrame();
95 elapsedTime += Time.deltaTime;
96 color.a = 1.0f - Mathf.Clamp01(elapsedTime / fadeTime);
97 fadeMaterial.color = color;
98 }
99 isFading = false;
100 }
101
102 /// <summary>
103 /// Renders the fade overlay when attached to a camera object
104 /// </summary>
105 void OnPostRender()
106 {
107 if (isFading)
108 {
109 fadeMaterial.SetPass(0);
110 GL.PushMatrix();
111 GL.LoadOrtho();
112 GL.Color(fadeMaterial.color);
113 GL.Begin(GL.QUADS);
114 GL.Vertex3(0f, 0f, -12f);
115 GL.Vertex3(0f, 1f, -12f);
116 GL.Vertex3(1f, 1f, -12f);
117 GL.Vertex3(1f, 0f, -12f);
118 GL.End();
119 GL.PopMatrix();
120 }
121 }
122 }