OVRScreenFade.cs 5.1 KB

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