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

131 lines
3.6 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 Module.Log;
using UnityEngine;
public class CameraRadialBlur : MonoBehaviour
{
public Material blurMaterial;
public float enterTime = 1f;
public float exitTime = 1f;
private int _width;
private int _height;
private float _startTime;
private Material _instance;
private int _layerMask;
private Camera _camera;
private RenderTexture _source;
private bool _init;
private bool _exit;
private void OnEnable()
{
if (_init)
{
if (!SystemInfo.supportsImageEffects)
{
LogModule.DebugLog("Disabling the RadialBlur. Image effects are not supported");
enabled = false;
}
else if (_camera == null)
enabled = false;
else
{
_width = Screen.width;
_height = Screen.height;
if (!PlayerPreferenceData.SystemQualityHighResolution)
{
_width /= 2;
_height /= 2;
}
_startTime = Time.time;
_instance = new Material(blurMaterial);
_source = RenderTexture.GetTemporary(_width, _height, 24, RenderTextureFormat.ARGB32);
_camera.targetTexture = _source;
_camera.Render();
_camera.targetTexture = null;
//_exit = false;
_exit = true;
}
}
}
private void Start ()
{
if (_camera == null)
_camera = GetComponent<Camera>();
_init = true;
OnEnable();
}
public void Exit()
{
_exit = true;
}
private void OnDisable()
{
if (!GameManager.applicationQuit)
{
if (_instance != null)
{
Destroy(_instance);
_instance = null;
}
if (_source != null)
{
RenderTexture.ReleaseTemporary(_source);
_source = null;
}
}
}
private bool CheckMovieOver = false;
private void Update()
{
if (CheckMovieOver == false && SceneMovieManager.Instance.CheckPlaySceneMovie(false, 3f)) //场景过场动画的给个2秒的延迟让其加载
{
_startTime = Time.time;
return;
}
CheckMovieOver = true;
if (Time.time < _startTime + enterTime)
{
var ratio = Mathf.Clamp01((Time.time - _startTime) / enterTime);
_instance.SetFloat("_BlurRatio", ratio);
}
else if (!_exit)
_instance.SetFloat("_BlurRatio", 1f);
else
{
if (Time.time < _startTime + enterTime + exitTime)
{
var ratio = Mathf.Clamp01((_startTime + enterTime + exitTime - Time.time) / exitTime);
_instance.SetFloat("_BlurRatio", ratio);
}
else
{
enabled = false;
SceneMovieManager.Instance.PlaySceneMovie();
}
}
}
private void OnPreRender()
{
_layerMask = _camera.cullingMask;
_camera.cullingMask = 0;
}
private void OnPostRender()
{
_camera.cullingMask = _layerMask;
}
private void OnRenderImage(RenderTexture src, RenderTexture dest)
{
var tempA = RenderTexture.GetTemporary(_width, _height);
//var tempB = RenderTexture.GetTemporary(_width, _height);
Graphics.Blit(_source, tempA, _instance);
Graphics.Blit(tempA, dest, _instance);
//Graphics.Blit(tempB, dest, _instance);
}
}