156 lines
5.3 KiB
C#
156 lines
5.3 KiB
C#
using UnityEngine;
|
||
using System.Collections;
|
||
|
||
namespace Thousandto.Cinematic
|
||
{
|
||
/// <summary>
|
||
/// 屏幕渐黑渐变
|
||
/// </summary>
|
||
public class CinematicFadeBlack : MonoBehaviour
|
||
{
|
||
//-------------------变量声明部分-------------------
|
||
#region Variables
|
||
public Shader CurShader;//着色器实例
|
||
private Material CurMaterial;//当前的材质
|
||
|
||
//shader需要的参数 [0, 1]
|
||
private float Intensity = 1;
|
||
|
||
//外部控制变量
|
||
private float _speed = 1;
|
||
private float _halfDuration = 0f;
|
||
private Status _status;
|
||
|
||
public float Duration = 2.0f;
|
||
public float Strength = 0.5f;
|
||
|
||
public bool Enable = false;
|
||
#endregion
|
||
|
||
|
||
//-------------------------材质的get&set----------------------------
|
||
#region MaterialGetAndSet
|
||
Material material
|
||
{
|
||
get
|
||
{
|
||
if (CurMaterial == null)
|
||
{
|
||
CurMaterial = new Material(CurShader);
|
||
CurMaterial.hideFlags = HideFlags.HideAndDontSave;
|
||
}
|
||
return CurMaterial;
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
//-----------------------------------------【Start()函数】---------------------------------------------
|
||
// 说明:此函数仅在Update函数第一次被调用前被调用
|
||
//--------------------------------------------------------------------------------------------------------
|
||
void Start()
|
||
{
|
||
//找到当前的Shader文件
|
||
CurShader = Shader.Find("Funcell/Cinematic/CinematicBlackBoard");
|
||
}
|
||
|
||
//-------------------------------------【OnRenderImage()函数】------------------------------------
|
||
// 说明:此函数在当完成所有渲染图片后被调用,用来渲染图片后期效果
|
||
//--------------------------------------------------------------------------------------------------------
|
||
void OnRenderImage(RenderTexture sourceTexture, RenderTexture destTexture)
|
||
{
|
||
//着色器实例不为空,就进行参数设置
|
||
if (CurShader != null)
|
||
{
|
||
//设置Shader中的外部变量
|
||
material.SetFloat("_BlackRate", Intensity);
|
||
|
||
//拷贝源纹理到目标渲染纹理,加上我们的材质效果
|
||
Graphics.Blit(sourceTexture, destTexture, material);
|
||
}
|
||
//着色器实例为空,直接拷贝屏幕上的效果。此情况下是没有实现屏幕特效的
|
||
else
|
||
{
|
||
//直接拷贝源纹理到目标渲染纹理
|
||
Graphics.Blit(sourceTexture, destTexture);
|
||
}
|
||
|
||
}
|
||
|
||
//-----------------------------------------【Update()函数】------------------------------------------
|
||
// 说明:此函数在每一帧中都会被调用
|
||
//--------------------------------------------------------------------------------------------------------
|
||
void Update()
|
||
{
|
||
if (!Enable) return;
|
||
|
||
var timeDelta = Time.deltaTime;
|
||
switch (_status)
|
||
{
|
||
case Status.ToBlack:
|
||
Intensity -= timeDelta * _speed;
|
||
if(Intensity <= 0)
|
||
{
|
||
Intensity = 0;
|
||
_status = Status.Duration;
|
||
}
|
||
break;
|
||
case Status.Duration:
|
||
Duration -= timeDelta;
|
||
if(Duration <= 0)
|
||
{
|
||
Duration = 0;
|
||
_status = Status.ToWhite;
|
||
}
|
||
break;
|
||
case Status.ToWhite:
|
||
Intensity += timeDelta * _speed;
|
||
if(Intensity >= 1)
|
||
{
|
||
Intensity = 1;
|
||
_status = Status.Finish;
|
||
}
|
||
break;
|
||
case Status.Finish:
|
||
Duration = 0;
|
||
enabled = false;
|
||
Enable = false;
|
||
break;
|
||
}
|
||
}
|
||
|
||
|
||
//-----------------------------------------【OnDisable()函数】---------------------------------------
|
||
// 说明:当对象变为不可用或非激活状态时此函数便被调用
|
||
//--------------------------------------------------------------------------------------------------------
|
||
void OnDisable()
|
||
{
|
||
if (CurMaterial)
|
||
{
|
||
enabled = false;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 开始变换
|
||
/// </summary>
|
||
/// <param name="duration">变换持续时间</param>
|
||
/// <param name="transDuration"></param>
|
||
public void StartTrans(float duration = 1)
|
||
{
|
||
Intensity = 1;
|
||
Duration = duration;
|
||
Enable = true;
|
||
enabled = true;
|
||
_status = Status.ToBlack;
|
||
}
|
||
|
||
private enum Status
|
||
{
|
||
ToBlack, //渐黑
|
||
Duration, //持续时间
|
||
ToWhite, //渐白
|
||
Finish, //完成
|
||
}
|
||
}
|
||
|
||
} |