首先你要有两个Camera,一个是场景相机,一个是特效相机。
场景相机的Culling Mask设置为Player或Backgroud等等,它负责显示角色和特效等所有和场景相关的对象。
特效相机的Culling Mask设置为Effect,它只负责显示要高亮的部分,无大招时它并不显示任何对象。
在释放大招时,动态将Player和其他要高亮的对象(如特效)的Layer设置为Effect层, 这时特效相机就可以看到这些对象了,而场景相机无法看到这些物体了。最后是对场景相机作压暗后处理, 其他对象和背景就变暗了!大招结束将这些处于高亮层的对象还原致原来的层中。
效果:
具体如何对场景对象作压暗处理呢?
1.创建一个ScreenFade.shader
2.新建一个ScreenFade.cs ,将其挂到场景相机上,在释放大招时开启。
大致表现是:持续5秒0.8alpha的变暗效果,5秒后会在2秒内渐隐消失回到正常。具体时长应该是你的大招时长!
Shader "Unlit/ScreenFade"
{
Properties{
_Color("Color", Color) = (0,0,0,1)
}
SubShader{
Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Opaque" }
LOD 100
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
Color[_Color]
Pass{}
}
}
using UnityEngine;
using System.Collections;
public class ScreenFade : MonoBehaviour
{
public float lastTime = 5.0f;
public float fadeTime = 2.0f;
public Color fadeColor = new Color(0.01f, 0.01f, 0.01f, 1.0f);
private Material fadeMaterial = null;
private bool isFading = false;
private YieldInstruction fadeInstruction = new WaitForEndOfFrame();
void Awake()
{
fadeMaterial = new Material(Shader.Find("Unlit/ScreenFade"));
}
void OnEnable()
{
StartCoroutine(FadeIn());
}
void OnLevelWasLoaded(int level)
{
StartCoroutine(FadeIn());
}
void OnDestroy()
{
if (fadeMaterial != null)
{
Destroy(fadeMaterial);
}
}
IEnumerator FadeIn()
{
float elapsedTime = 0.0f;
fadeMaterial.color = fadeColor;
Color color = fadeColor;
isFading = true;
while (elapsedTime < lastTime)
{
yield return fadeInstruction;
elapsedTime += Time.deltaTime;
color.a = 0.8f;
fadeMaterial.color = color;
}
elapsedTime -= lastTime;
while (elapsedTime < fadeTime)
{
yield return fadeInstruction;
elapsedTime += Time.deltaTime;
color.a = color.a - Mathf.Clamp01(elapsedTime / fadeTime);
fadeMaterial.color = color;
}
isFading = false;
}
void OnPostRender()
{
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();
}
}
}
网友评论