using System;
using UnityEngine;
using Framework.Services;
namespace Framework.Cameras {
/// Fades a camea in and out
public class CameraFader : ScriptComponent {
/// How long it takes for the camera to fade in or out
///
/// Used when the camera is told to automatically fade in or out.
///
public float AutoFadeTimeSeconds = 2.0f;
/// Whether the camera is currently doing an automatic fade
public bool IsAutoFading = false;
/// Whether the camera should be faded out
public bool Faded = false;
/// How far the camera is currently faded
public float FadeLevel;
/// Material that is used for fading the camera
///
/// If this is not set, the camera will be faded using old-style GUI
/// code. This however doesn't work for VR applications.
///
public Material FadeMaterial;
/// Fades the view out
/// Whether to fade out with no delay
public void FadeOut(bool instantaneous) {
this.Faded = true;
if(instantaneous) {
this.FadeLevel = 1.0f;
this.IsAutoFading = false;
} else {
this.IsAutoFading = true;
}
}
/// Fades the view in
/// Whether to fade in with no delay
public void FadeIn(bool instantaneous) {
this.Faded = false;
if(instantaneous) {
this.FadeLevel = 0.0f;
this.IsAutoFading = false;
} else {
this.IsAutoFading = true;
}
}
/// Called when the script component gets loaded into a game object
protected override void Awake() {
base.Awake();
if(this.FadeMaterial == null) {
this.fadeTexture = new Texture2D(1, 1, TextureFormat.RGB24, false);
this.fadeTexture.SetPixel(0, 0, Color.black);
}
}
/// Called after the scene had been rendered
protected void OnPostRender() {
if(this.FadeMaterial != null) {
if(this.FadeLevel > 0.0f) {
this.FadeMaterial.SetPass(0);
this.FadeMaterial.SetColor("_Color", new Color(0.0f, 0.0f, 0.0f, this.FadeLevel));
GL.PushMatrix();
GL.LoadOrtho();
GL.Color(this.FadeMaterial.color);
GL.Begin(GL.QUADS);
GL.Vertex3(0f, 0f, -12f);
GL.Vertex3(0f, 1f, -12f);
GL.Vertex3(1f, 1f, -12f);
GL.Vertex3(1f, 0f, -12f);
GL.End();
GL.PopMatrix();
}
}
}
/// Called once per visual frame
protected virtual void Update() {
if(this.IsAutoFading) {
float fadeAmount = Time.deltaTime * (1.0f / this.AutoFadeTimeSeconds);
if(this.Faded) {
this.FadeLevel += fadeAmount;
if(this.FadeLevel >= 1.0f) {
this.FadeLevel = 1.0f;
this.IsAutoFading = false;
}
} else {
this.FadeLevel -= fadeAmount;
if(this.FadeLevel <= 0.0f) {
this.FadeLevel = 0.0f;
this.IsAutoFading = false;
}
}
}
}
/// Called when Unity wants to draw its GUI
protected virtual void OnGUI() {
if(this.FadeMaterial == null) {
if(this.FadeLevel > 0.0f) {
Color previousColor = GUI.color;
// This should be easier on resources then writing to the texture in
// order to change its alpha value.
GUI.color = new Color(0.0f, 0.0f, 0.0f, this.FadeLevel);
try {
GUI.DrawTexture(
new Rect(0, 0, Screen.width, Screen.height),
this.fadeTexture
);
}
finally {
GUI.color = previousColor;
}
}
}
}
/// Camera manager to which the camera reports
private ICameraManager cameraManager;
/// Texture that will be used to fade the screen in and out
private Texture2D fadeTexture;
}
} // namespace Framework.Cameras