c1_unity/Assets/Scripts/MonkeyPost.cs
2023-04-03 11:04:31 +08:00

97 lines
2.6 KiB
C#

using UnityEngine;
[ExecuteInEditMode, ImageEffectAllowedInSceneView]
public class MonkeyPost : MonoBehaviour
{
public Material _material;
[Header("辉光")]
[Range(0f, 1f)]
public float _threshold = 0.5f;
[Range(0f, 1f)]
public float _intensity = 0.5f;
[Range(0f, 3)]
public float _soft = 1f;
[Header("调色")]
[Range(0f, 2f)]
public float _brightness = 1.7f;
[Range(0f, 2f)]
public float _contrast = 1.05f;
[Range(0f, 2f)]
public float _saturation = 1.05f;
public Color _gamma = new Color(1, 1, 1, 1);
private int _width;
private int _height;
private RenderTexture _source;
private RenderTexture _target;
#if UNITY_EDITOR
private void Update()
{
Init();
}
#endif
private void Awake()
{
Init();
}
void OnRenderImage(RenderTexture source, RenderTexture destination)
{
if (_material)
{
int _times = 3;
_source = source;
_width = _source.width;
_height = _source.height;
_target = RenderTexture.GetTemporary(_width, _height);
Graphics.Blit(_source, _target, _material, 3);
_source = _target;
RenderTexture.ReleaseTemporary(_target);
for (int i = 0; i < _times; i++)
{
_width /= 2;
_height /= 2;
DoPost(0);
}
for (int i = 0; i < _times - 1; i++)
{
_width *= 2;
_height *= 2;
DoPost(1);
}
_material.SetTexture("_Bloom", _source);
RenderTexture.ReleaseTemporary(_source);
Graphics.Blit(source, destination, _material, 2);
}
else
{
Graphics.Blit(source, destination);
}
}
private void DoPost(int _pass)
{
_material.SetFloat("_OffsetX", _soft / _width);
_material.SetFloat("_OffsetY", _soft / _height);
_target = RenderTexture.GetTemporary(_width, _height);
Graphics.Blit(_source, _target, _material, _pass);
_source = _target;
RenderTexture.ReleaseTemporary(_target);
}
public void Init()
{
if (_material)
{
_material.SetFloat("_Intensity", _intensity);
_material.SetFloat("_Threshold", _threshold);
_material.SetFloat("_Brightness", _brightness);
_material.SetFloat("_Contrast", _contrast);
_material.SetFloat("_Saturation", _saturation);
_material.SetVector("_Gamma", _gamma);
}
}
}