源战役客户端
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

79 行
2.6 KiB

  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4. [ExecuteInEditMode]
  5. public class PartBloom : MonoBehaviour
  6. {
  7. //采样率
  8. public int samplerScale = 1;
  9. //高亮部分提取阈值
  10. public Color colorThreshold = Color.gray;
  11. //Bloom泛光颜色
  12. public Color bloomColor = Color.white;
  13. //Bloom权值
  14. [Range(0.0f, 1.0f)]
  15. public float bloomFactor = 0.5f;
  16. /// <summary>
  17. /// Bloom材质球
  18. /// </summary>
  19. public Material m_bloom_mat = null;
  20. /// <summary>
  21. /// 特定渲染图
  22. /// </summary>
  23. [SerializeField]
  24. private RenderTexture m_R;
  25. RenderTexture temp1;
  26. RenderTexture temp2;
  27. private Camera rt_bloom_camera;
  28. void Start()
  29. {
  30. rt_bloom_camera = GameObject.Find("BloomCamera").GetComponent<Camera>();
  31. Debug.Log(rt_bloom_camera.name);
  32. }
  33. void OnRenderImage(RenderTexture source, RenderTexture destination)
  34. {
  35. if (m_bloom_mat != null && rt_bloom_camera != null)
  36. {
  37. m_R = rt_bloom_camera.targetTexture;
  38. // m_R = RenderTexture.GetTemporary(source.width / 2, source.height / 2, 0);
  39. temp1 = RenderTexture.GetTemporary(m_R.width, m_R.height, 0);
  40. temp2 = RenderTexture.GetTemporary(m_R.width, m_R.height, 0);
  41. //复制泛光图
  42. Graphics.Blit(m_R, temp1);
  43. //根据阈值提取高亮部分,使用pass0进行高亮提取
  44. m_bloom_mat.SetVector("_colorThreshold", colorThreshold);
  45. Graphics.Blit(temp1, temp2, m_bloom_mat, 0);
  46. //高斯模糊,两次模糊,横向纵向,使用pass1进行高斯模糊
  47. m_bloom_mat.SetVector("_offsets", new Vector4(0, samplerScale, 0, 0));
  48. Graphics.Blit(temp2, temp1, m_bloom_mat, 1);
  49. m_bloom_mat.SetVector("_offsets", new Vector4(samplerScale, 0, 0, 0));
  50. Graphics.Blit(temp1, temp2, m_bloom_mat, 1);
  51. //Bloom,将模糊后的图作为Material的Blur图参数
  52. m_bloom_mat.SetTexture("_BlurTex", temp2);
  53. m_bloom_mat.SetVector("_bloomColor", bloomColor);
  54. m_bloom_mat.SetFloat("_bloomFactor", bloomFactor);
  55. //使用pass2进行景深效果计算,清晰场景图直接从source输入到shader的_MainTex中
  56. Graphics.Blit(source, destination, m_bloom_mat, 2);
  57. rt_bloom_camera.targetTexture = m_R;
  58. //释放申请的RT
  59. RenderTexture.ReleaseTemporary(temp1);
  60. RenderTexture.ReleaseTemporary(temp2);
  61. }
  62. }
  63. public void SetMaterialRT(RenderTexture rt)
  64. {
  65. // m_R = rt;
  66. }
  67. }