Files
JJBB/Assets/Project/Script/Shader/CameraSpaceBlur.cs
2024-08-23 15:49:34 +08:00

103 lines
3.3 KiB
C#
Raw 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 Games.Events;
using Module.Log;
using UnityEngine;
public class CameraSpaceBlur : MonoBehaviour
{
public Material blurMaterial;
public float maxBlurOffset = 0.1f;
public float maxBright = 0.7f;
public AnimationCurve enterCurve = new AnimationCurve(new Keyframe(0f, 0f), new Keyframe(0.65f, 0.1f), new Keyframe(1f, 1f));
public AnimationCurve exitCurve = new AnimationCurve(new Keyframe(1f, 1f), new Keyframe(0.1f, 0.72f), new Keyframe(0f, 0f));
public float enterTime = 1.7f;
public float exitTime = 1.5f;
private Material _mMaterial;
private Camera _mCamera;
private bool _maxEventSent;
private float _time;
private float _offset;
private float _bright;
private void Start()
{
// Disable if we don't support image effects
if (!SystemInfo.supportsImageEffects)
{
LogModule.DebugLog("Disabling the Camera Blur. Image effects are not supported (do you have Unity Pro?)");
enabled = false;
}
else
{
_mCamera = GetComponent<Camera>();
if (blurMaterial == null)
{
LogModule.ErrorLog(string.Format("{0}未设置BlurMaterial", gameObject.name));
enabled = false;
}
else if (_mCamera == null)
{
LogModule.ErrorLog(string.Format("{0}没有摄像机!", gameObject.name));
enabled = false;
}
else
_mMaterial = Instantiate(blurMaterial);
}
}
private void OnEnable()
{
_time = 0f;
_maxEventSent = false;
}
private void Update()
{
if (_time < enterTime)
{
if (_time <= 0 && SceneMovieManager.Instance.CheckPlaySceneMovie(false, 3f)) //场景过场动画的给个2秒的延迟让其加载
return;
float ratio = _time / enterTime;
ratio = enterCurve.Evaluate(ratio);
_bright = ratio * maxBright;
_offset = ratio * maxBlurOffset;
}
else
{
if (!_maxEventSent)
{
// 刻意保留一帧最大值来遮挡摄像机镜头变换和其他变换效果
_maxEventSent = true;
var ratio = enterCurve.Evaluate(1);
_bright = ratio * maxBright;
_offset = ratio * maxBlurOffset;
EventDispatcher.Instance.Dispatch(Games.Events.EventId.CameraSpaceBlurMax);
}
else if (_time < enterTime + exitTime)
{
float ratio = (_time - enterTime) / exitTime;
ratio = exitCurve.Evaluate(ratio);
_bright = ratio * maxBright;
_offset = ratio * maxBlurOffset;
}
else
{
enabled = false;
SceneMovieManager.Instance.PlaySceneMovie();
}
}
_time += Time.deltaTime;
}
private void OnDestroy()
{
Destroy(_mMaterial);
}
private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
SetMaterial();
Graphics.Blit(source, destination, _mMaterial);
}
private void SetMaterial()
{
_mMaterial.SetFloat("_Offset", _offset);
_mMaterial.SetFloat("_Bright", _bright);
}
}