using System;
|
|
using UnityEngine;
|
|
using System.Collections;
|
|
[ExecuteInEditMode]
|
|
public class PartBloom : MonoBehaviour
|
|
{
|
|
//采样率
|
|
public int samplerScale = 1;
|
|
//高亮部分提取阈值
|
|
public Color colorThreshold = Color.gray;
|
|
//Bloom泛光颜色
|
|
public Color bloomColor = Color.white;
|
|
//Bloom权值
|
|
[Range(0.0f, 1.0f)]
|
|
public float bloomFactor = 0.5f;
|
|
|
|
/// <summary>
|
|
/// Bloom材质球
|
|
/// </summary>
|
|
public Material m_bloom_mat = null;
|
|
|
|
/// <summary>
|
|
/// 特定渲染图
|
|
/// </summary>
|
|
[SerializeField]
|
|
private RenderTexture m_R;
|
|
RenderTexture temp1;
|
|
RenderTexture temp2;
|
|
private Camera rt_bloom_camera;
|
|
void Start()
|
|
{
|
|
rt_bloom_camera = GameObject.Find("BloomCamera").GetComponent<Camera>();
|
|
Debug.Log(rt_bloom_camera.name);
|
|
}
|
|
|
|
void OnRenderImage(RenderTexture source, RenderTexture destination)
|
|
{
|
|
if (m_bloom_mat != null && rt_bloom_camera != null)
|
|
{
|
|
m_R = rt_bloom_camera.targetTexture;
|
|
// m_R = RenderTexture.GetTemporary(source.width / 2, source.height / 2, 0);
|
|
temp1 = RenderTexture.GetTemporary(m_R.width, m_R.height, 0);
|
|
temp2 = RenderTexture.GetTemporary(m_R.width, m_R.height, 0);
|
|
|
|
//复制泛光图
|
|
Graphics.Blit(m_R, temp1);
|
|
|
|
|
|
//根据阈值提取高亮部分,使用pass0进行高亮提取
|
|
m_bloom_mat.SetVector("_colorThreshold", colorThreshold);
|
|
Graphics.Blit(temp1, temp2, m_bloom_mat, 0);
|
|
|
|
//高斯模糊,两次模糊,横向纵向,使用pass1进行高斯模糊
|
|
m_bloom_mat.SetVector("_offsets", new Vector4(0, samplerScale, 0, 0));
|
|
Graphics.Blit(temp2, temp1, m_bloom_mat, 1);
|
|
m_bloom_mat.SetVector("_offsets", new Vector4(samplerScale, 0, 0, 0));
|
|
Graphics.Blit(temp1, temp2, m_bloom_mat, 1);
|
|
|
|
//Bloom,将模糊后的图作为Material的Blur图参数
|
|
m_bloom_mat.SetTexture("_BlurTex", temp2);
|
|
m_bloom_mat.SetVector("_bloomColor", bloomColor);
|
|
m_bloom_mat.SetFloat("_bloomFactor", bloomFactor);
|
|
|
|
//使用pass2进行景深效果计算,清晰场景图直接从source输入到shader的_MainTex中
|
|
Graphics.Blit(source, destination, m_bloom_mat, 2);
|
|
rt_bloom_camera.targetTexture = m_R;
|
|
//释放申请的RT
|
|
RenderTexture.ReleaseTemporary(temp1);
|
|
RenderTexture.ReleaseTemporary(temp2);
|
|
}
|
|
}
|
|
|
|
public void SetMaterialRT(RenderTexture rt)
|
|
{
|
|
// m_R = rt;
|
|
}
|
|
}
|
|
|
|
|