Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.2 KiB

  1. namespace VRTK
  2. {
  3. using UnityEngine;
  4. public class VRTK_ScreenFade : MonoBehaviour
  5. {
  6. public static VRTK_ScreenFade instance;
  7. protected Material fadeMaterial = null;
  8. protected Color currentColor = new Color(0f, 0f, 0f, 0f);
  9. protected Color targetColor = new Color(0f, 0f, 0f, 0f);
  10. protected Color deltaColor = new Color(0f, 0f, 0f, 0f);
  11. public static void Start(Color newColor, float duration)
  12. {
  13. if (instance)
  14. {
  15. instance.StartFade(newColor, duration);
  16. }
  17. }
  18. public virtual void StartFade(Color newColor, float duration)
  19. {
  20. if (duration > 0.0f)
  21. {
  22. targetColor = newColor;
  23. deltaColor = (targetColor - currentColor) / duration;
  24. }
  25. else
  26. {
  27. currentColor = newColor;
  28. }
  29. }
  30. protected virtual void Awake()
  31. {
  32. fadeMaterial = new Material(Shader.Find("Unlit/TransparentColor"));
  33. instance = this;
  34. }
  35. protected virtual void OnPostRender()
  36. {
  37. if (currentColor != targetColor)
  38. {
  39. if (Mathf.Abs(currentColor.a - targetColor.a) < Mathf.Abs(deltaColor.a) * Time.deltaTime)
  40. {
  41. currentColor = targetColor;
  42. deltaColor = new Color(0, 0, 0, 0);
  43. }
  44. else
  45. {
  46. currentColor += deltaColor * Time.deltaTime;
  47. }
  48. }
  49. if (currentColor.a > 0 && fadeMaterial)
  50. {
  51. currentColor.a = (targetColor.a > currentColor.a && currentColor.a > 0.98f ? 1f : currentColor.a);
  52. fadeMaterial.color = currentColor;
  53. fadeMaterial.SetPass(0);
  54. GL.PushMatrix();
  55. GL.LoadOrtho();
  56. GL.Color(fadeMaterial.color);
  57. GL.Begin(GL.QUADS);
  58. GL.Vertex3(0f, 0f, 0.9999f);
  59. GL.Vertex3(0f, 1f, 0.9999f);
  60. GL.Vertex3(1f, 1f, 0.9999f);
  61. GL.Vertex3(1f, 0f, 0.9999f);
  62. GL.End();
  63. GL.PopMatrix();
  64. }
  65. }
  66. }
  67. }