- Home /
fade from outside of your screen to the center
Hallo,
I need somehelp with a script I'm making. I created a script that creates a screenfade to black if something happend.
The fade I've now goes from nothing to black over the entire screen. Like every normal fade will do.
But I want a fade like this : https://www.youtube.com/watch?v=IPmr7M3-6Zw (Don't worry its a youtube link ;] )
Question 1: Is this possible?
Question 2: Can I create this with my script or with shaders (if yess, how do I set this up)?
ScreenFade script:
using UnityEngine;
using System.Collections;
public class ScreenFade : MonoBehaviour
{
public float fadeTime = 2.0f;
public Color fadeColor = new Color(0.01f, 0.01f, 0.01f, 1.0f);
public Shader fadeShader = null;
private Material fadeMaterial = null;
private bool isFading = false;
void Awake()
{
// create the fade material
fadeMaterial = (fadeShader != null) ? new Material(fadeShader) : new Material(Shader.Find("Transparent/Diffuse"));
}
void OnDestroy()
{
if (fadeMaterial != null)
{
Destroy(fadeMaterial);
}
}
/* IEnumerator FadeIn()
{
float elapsedTime = 0.0f;
Color color = fadeMaterial.color = fadeColor;
isFading = true;
while (elapsedTime < fadeTime)
{
yield return new WaitForEndOfFrame();
elapsedTime += Time.deltaTime;
color.a = 1.0f - Mathf.Clamp01(elapsedTime / fadeTime);
fadeMaterial.color = color;
}
isFading = false;
} */
#if UNITY_ANDROID && !UNITY_EDITOR
void OnCustomPostRender()
#else
void OnPostRender()
#endif
{
if (isFading)
{
fadeMaterial.SetPass(0);
GL.PushMatrix();
GL.LoadOrtho();
GL.Color(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();
}
}
public void StartFadeOutAndLoadLevel(int level)
{
StartCoroutine(FadeOutAndLoadLevel(level));
}
IEnumerator FadeOutAndLoadLevel(int level)
{
float elapsedTime = 0.0f;
Color color = fadeMaterial.color = new Color(0.01f, 0.01f, 0.01f, 0.0f);
isFading = true;
while (elapsedTime < fadeTime)
{
yield return new WaitForEndOfFrame();
elapsedTime += Time.deltaTime;
color.a = Mathf.Clamp01(elapsedTime / fadeTime);
fadeMaterial.color = color;
}
Debug.Log("Fade Ended");
}
}
NOTE: Where the Debug is would normaly be a "application.loadlevel".
EDIT: I can create muliple area's where the fade happend with (GL.Vertex3).
Thank you for your replay.
Your answer
Follow this Question
Related Questions
What keyword string should I add to enable/disable when changing shader properties ? 1 Answer
Separating Procedurally Generated Mesh, Based On Triangle Height 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Why when changing the doors colors it's not changing anything ? 1 Answer