SteamVR_Fade.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using UnityEngine;
  3. using Valve.VR;
  4. public class SteamVR_Fade : MonoBehaviour
  5. {
  6. public static void Start(Color newColor, float duration, bool fadeOverlay = false)
  7. {
  8. SteamVR_Utils.Event.Send("fade", new object[]
  9. {
  10. newColor,
  11. duration,
  12. fadeOverlay
  13. });
  14. }
  15. public static void View(Color newColor, float duration)
  16. {
  17. CVRCompositor compositor = OpenVR.Compositor;
  18. if (compositor != null)
  19. {
  20. compositor.FadeToColor(duration, newColor.r, newColor.g, newColor.b, newColor.a, false);
  21. }
  22. }
  23. public void OnStartFade(params object[] args)
  24. {
  25. Color color = (Color)args[0];
  26. float num = (float)args[1];
  27. this.fadeOverlay = (args.Length > 2 && (bool)args[2]);
  28. if (num > 0f)
  29. {
  30. this.targetColor = color;
  31. this.deltaColor = (this.targetColor - this.currentColor) / num;
  32. }
  33. else
  34. {
  35. this.currentColor = color;
  36. }
  37. }
  38. private void OnEnable()
  39. {
  40. if (SteamVR_Fade.fadeMaterial == null)
  41. {
  42. SteamVR_Fade.fadeMaterial = new Material(Shader.Find("Custom/SteamVR_Fade"));
  43. }
  44. SteamVR_Utils.Event.Listen("fade", new SteamVR_Utils.Event.Handler(this.OnStartFade));
  45. SteamVR_Utils.Event.Send("fade_ready", new object[0]);
  46. }
  47. private void OnDisable()
  48. {
  49. SteamVR_Utils.Event.Remove("fade", new SteamVR_Utils.Event.Handler(this.OnStartFade));
  50. }
  51. private void OnPostRender()
  52. {
  53. if (this.currentColor != this.targetColor)
  54. {
  55. if (Mathf.Abs(this.currentColor.a - this.targetColor.a) < Mathf.Abs(this.deltaColor.a) * Time.deltaTime)
  56. {
  57. this.currentColor = this.targetColor;
  58. this.deltaColor = new Color(0f, 0f, 0f, 0f);
  59. }
  60. else
  61. {
  62. this.currentColor += this.deltaColor * Time.deltaTime;
  63. }
  64. if (this.fadeOverlay)
  65. {
  66. SteamVR_Overlay instance = SteamVR_Overlay.instance;
  67. if (instance != null)
  68. {
  69. instance.alpha = 1f - this.currentColor.a;
  70. }
  71. }
  72. }
  73. if (this.currentColor.a > 0f && SteamVR_Fade.fadeMaterial)
  74. {
  75. GL.PushMatrix();
  76. GL.LoadOrtho();
  77. SteamVR_Fade.fadeMaterial.SetPass(0);
  78. GL.Begin(7);
  79. GL.Color(this.currentColor);
  80. GL.Vertex3(0f, 0f, 0f);
  81. GL.Vertex3(1f, 0f, 0f);
  82. GL.Vertex3(1f, 1f, 0f);
  83. GL.Vertex3(0f, 1f, 0f);
  84. GL.End();
  85. GL.PopMatrix();
  86. }
  87. }
  88. private Color currentColor = new Color(0f, 0f, 0f, 0f);
  89. private Color targetColor = new Color(0f, 0f, 0f, 0f);
  90. private Color deltaColor = new Color(0f, 0f, 0f, 0f);
  91. private bool fadeOverlay;
  92. private static Material fadeMaterial;
  93. }