Files
Main/Assets/Plugins/Code/Cinematic/Effect/CinematicSportBlur.cs
2025-01-25 04:38:09 +08:00

146 lines
5.2 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
using System.Collections;
namespace Thousandto.Cinematic
{
/// <summary>
/// 相机运动模糊效果
/// </summary>
public class CinematicSportBlur : MonoBehaviour
{
//-------------------变量声明部分-------------------
#region Variables
public Shader CurShader;//着色器实例
private Material CurMaterial;//当前的材质
//shader需要的参数 [-0.5, 0.5]
private float Intensity = 0;
private float OffsetX = 0.5f;
private float OffsetY = 0.5f;
private Vector2 CenterV2;
//外部控制变量
private float _speed;
private float _halfDuration;
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()
{
CenterV2 = new Vector2(OffsetX, OffsetY);
//找到当前的Shader文件
CurShader = Shader.Find("Funcell/Cinematic/CinematicSportBlur");
//判断是否支持屏幕特效
if (!SystemInfo.supportsImageEffects)
{
enabled = false;
return;
}
}
//-------------------------------------【OnRenderImage()函数】------------------------------------
// 说明:此函数在当完成所有渲染图片后被调用,用来渲染图片后期效果
//--------------------------------------------------------------------------------------------------------
void OnRenderImage(RenderTexture sourceTexture, RenderTexture destTexture)
{
//着色器实例不为空,就进行参数设置
if (CurShader != null)
{
//设置Shader中的外部变量
material.SetFloat("_Value", Intensity);
material.SetVector("_Center", CenterV2);
//拷贝源纹理到目标渲染纹理,加上我们的材质效果
Graphics.Blit(sourceTexture, destTexture, material);
}
//着色器实例为空,直接拷贝屏幕上的效果。此情况下是没有实现屏幕特效的
else
{
//直接拷贝源纹理到目标渲染纹理
Graphics.Blit(sourceTexture, destTexture);
}
}
//-----------------------------------------【OnValidate()函数】--------------------------------------
// 说明:此函数在编辑器中该脚本的某个值发生了改变后被调用
//--------------------------------------------------------------------------------------------------------
void OnValidate()
{
//将编辑器中的值赋值回来,确保在编辑器中值的改变立刻让结果生效
CenterV2 = new Vector2(OffsetX, OffsetY);
}
//-----------------------------------------【Update()函数】------------------------------------------
// 说明:此函数在每一帧中都会被调用
//--------------------------------------------------------------------------------------------------------
void Update()
{
if (!Enable) return;
if(Duration > 0)
{
var timeDelta = Time.deltaTime;
Duration -= timeDelta;
if(Duration > _halfDuration)
Intensity += timeDelta * _speed;
else
Intensity -= timeDelta * _speed;
}
else
{
Duration = 0;
enabled = false;
Enable = false;
}
}
//-----------------------------------------【OnDisable()函数】---------------------------------------
// 说明:当对象变为不可用或非激活状态时此函数便被调用
//--------------------------------------------------------------------------------------------------------
void OnDisable()
{
if (CurMaterial)
{
enabled = false;
}
}
public void StartBlur(float duration, float strength = 0.5f)
{
Duration = duration;
_halfDuration = duration / 2;
_speed = strength / duration;
Enable = true;
enabled = true;
}
}
}