123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- using UnityEngine;
- using Valve.VR;
- namespace Valve.VR
- {
- public class SteamVR_Fade : MonoBehaviour
- {
- private Color currentColor = new Color(0, 0, 0, 0);
- private Color targetColor = new Color(0, 0, 0, 0);
- private Color deltaColor = new Color(0, 0, 0, 0);
- private bool fadeOverlay = false;
- static public void Start(Color newColor, float duration, bool fadeOverlay = false)
- {
- SteamVR_Events.Fade.Send(newColor, duration, fadeOverlay);
- }
- static public void View(Color newColor, float duration)
- {
- var compositor = OpenVR.Compositor;
- if (compositor != null)
- compositor.FadeToColor(duration, newColor.r, newColor.g, newColor.b, newColor.a, false);
- }
- #if TEST_FADE_VIEW
- void Update()
- {
- if (Input.GetKeyDown(KeyCode.Space))
- {
- SteamVR_Fade.View(Color.black, 0);
- SteamVR_Fade.View(Color.clear, 1);
- }
- }
- #endif
- public void OnStartFade(Color newColor, float duration, bool fadeOverlay)
- {
- if (duration > 0.0f)
- {
- targetColor = newColor;
- deltaColor = (targetColor - currentColor) / duration;
- }
- else
- {
- currentColor = newColor;
- }
- }
- static Material fadeMaterial = null;
- static int fadeMaterialColorID = -1;
- void OnEnable()
- {
- if (fadeMaterial == null)
- {
- fadeMaterial = new Material(Shader.Find("Custom/SteamVR_Fade"));
- fadeMaterialColorID = Shader.PropertyToID("fadeColor");
- }
- SteamVR_Events.Fade.Listen(OnStartFade);
- SteamVR_Events.FadeReady.Send();
- }
- void OnDisable()
- {
- SteamVR_Events.Fade.Remove(OnStartFade);
- }
- void OnPostRender()
- {
- if (currentColor != targetColor)
- {
-
- if (Mathf.Abs(currentColor.a - targetColor.a) < Mathf.Abs(deltaColor.a) * Time.deltaTime)
- {
- currentColor = targetColor;
- deltaColor = new Color(0, 0, 0, 0);
- }
- else
- {
- currentColor += deltaColor * Time.deltaTime;
- }
- if (fadeOverlay)
- {
- var overlay = SteamVR_Overlay.instance;
- if (overlay != null)
- {
- overlay.alpha = 1.0f - currentColor.a;
- }
- }
- }
- if (currentColor.a > 0 && fadeMaterial)
- {
- fadeMaterial.SetColor(fadeMaterialColorID, currentColor);
- fadeMaterial.SetPass(0);
- GL.Begin(GL.QUADS);
- GL.Vertex3(-1, -1, 0);
- GL.Vertex3(1, -1, 0);
- GL.Vertex3(1, 1, 0);
- GL.Vertex3(-1, 1, 0);
- GL.End();
- }
- }
- }
- }
|