Unity Shader Example 12 (Bloom 高光)

来源:互联网 发布:js二维数组的遍历 编辑:程序博客网 时间:2024/04/25 05:35
1. 主要参考了


Glow Effect Mobile Friendly 插件的做法。


代码,挂在Camera上的Grow.cs

主要的思想就是:

1. 循环n次,分别把A模糊给B,清空A,再给B模糊给A,进行n次。

2. shaderCamera 是有作用的,主要就是为了让bloom只针对发光的人物,如果没有shaderCamera的存在,整个背景都会bloom起来。

  因为主要由于shaderCamera.backgroundColor = Color.clear;。

<pre class="csharp" name="code">using UnityEngine;using System.Collections;public class Glow : MonoBehaviour {    public Material glowMaterial;    public float blurSpread = 1.0f;    public float glowStrength = 1.2f;    public Color glowColorMultiplier = Color.white;    // renderTexture size    public int downsampleSize = 256;    // 模糊的循环次数    public int blurIterations = 4;    // private     private Camera shaderCamera;    private RenderTexture replaceRenderTexture;// Use this for initializationvoid Start () {        if (!SystemInfo.supportsImageEffects)        {            Debug.Log("Image effects are not supported (do you have Unity Pro?)");            enabled = false;        }        UpdateMatProperty();        shaderCamera = new GameObject("Test", typeof(Camera)).GetComponent<Camera>();        Camera origCamera = GetComponent<Camera>();        replaceRenderTexture = new RenderTexture((int)origCamera.pixelWidth, (int)origCamera.pixelHeight, 0, RenderTextureFormat.ARGB32);        replaceRenderTexture.wrapMode = TextureWrapMode.Clamp;        replaceRenderTexture.useMipMap = false;        replaceRenderTexture.filterMode = FilterMode.Bilinear;        replaceRenderTexture.Create();        glowMaterial.SetTexture("_Glow", replaceRenderTexture);}    void UpdateMatProperty()    {        if (glowMaterial != null)        {            glowMaterial.SetFloat("_BlurSpread", blurSpread);            glowMaterial.SetFloat("_GlowStrength", glowStrength);            glowMaterial.SetColor("_GlowColorMultiplier", glowColorMultiplier);        }    }    public void OnPreRender()    {        Camera origCamera = GetComponent<Camera>();        shaderCamera.CopyFrom(origCamera);        shaderCamera.backgroundColor = Color.clear;        shaderCamera.clearFlags = CameraClearFlags.SolidColor;        shaderCamera.renderingPath = RenderingPath.Forward;        shaderCamera.targetTexture = replaceRenderTexture;        // 这里可以选择哪一些物体才进行发光,渲成replaceRenderTexture之后,再传入glowMaterial的"_Glow"        //shaderCamera.RenderWithShader(glowReplaceShader, "RenderType");    }    public void OnRenderImage(RenderTexture source, RenderTexture destination)    {        if (glowMaterial != null)        {            UpdateMatProperty();            RenderTexture blurA = RenderTexture.GetTemporary(downsampleSize, downsampleSize, 0, RenderTextureFormat.ARGB32);            RenderTexture blurB = RenderTexture.GetTemporary(downsampleSize, downsampleSize, 0, RenderTextureFormat.ARGB32);            // init blurB            Graphics.Blit(replaceRenderTexture, blurB, glowMaterial, 0);            //glowMaterial.SetTexture("_Glow", blurA);            //glowMaterial.SetTexture("_Glow", blurB);            for (int i = 0; i < blurIterations; ++i)            {                // 偶数                if (i % 2 == 0)                {                    blurA.DiscardContents();                    // pass 0 - blur the main texture                    Graphics.Blit(blurB, blurA, glowMaterial, 0);                }                // 奇数                else                {                    blurB.DiscardContents();                    // pass 0 - blur the main texture                    Graphics.Blit(blurA, blurB, glowMaterial, 0);                }            }            Graphics.Blit(source, destination, glowMaterial, 1);            RenderTexture.ReleaseTemporary(blurA);            RenderTexture.ReleaseTemporary(blurB);        }        else        {            Graphics.Blit(source, destination);        }            }}


Grow.shader代码:

Shader "Hidden/Glow"{Properties{_MainTex ("Texture", 2D) = "white" {}}SubShader{pass { // pass 0 - blur the main texture    ZTest Always Cull Off ZWrite OffFog { Mode Off }Blend OffCGPROGRAM#pragma vertex vert#pragma fragment frag#include "UnityCG.cginc"uniform sampler2D _MainTex;uniform half4 _MainTex_TexelSize;uniform half _BlurSpread;struct v2f {half4 pos : SV_POSITION;half2 uv : TEXCOORD0;half2 uv2[4] : TEXCOORD1;};v2f vert (appdata_img v){v2f o;o.pos = mul (UNITY_MATRIX_MVP, v.vertex);       o.uv = v.texcoord;              o.uv2[0] = v.texcoord + _MainTex_TexelSize.xy * half2(_BlurSpread, _BlurSpread);o.uv2[1] = v.texcoord + _MainTex_TexelSize.xy * half2(-_BlurSpread, -_BlurSpread);o.uv2[2] = v.texcoord + _MainTex_TexelSize.xy * half2(_BlurSpread, -_BlurSpread);o.uv2[3] = v.texcoord + _MainTex_TexelSize.xy * half2(-_BlurSpread, _BlurSpread);return o;}fixed4 frag ( v2f i ) : COLOR{fixed4 blur = tex2D(_MainTex, i.uv ) * 0.4;blur += tex2D(_MainTex, i.uv2[0]) * 0.15;blur += tex2D(_MainTex, i.uv2[1]) * 0.15;blur += tex2D(_MainTex, i.uv2[2]) * 0.15;blur += tex2D(_MainTex, i.uv2[3]) * 0.15;return blur;} ENDCG}Pass { // pass 1 - glowname "Glow"ZTest Always Cull Off ZWrite OffFog { Mode Off }Blend OffCGPROGRAM#pragma vertex vert#pragma fragment frag#include "UnityCG.cginc"uniform sampler2D _MainTex;uniform half4 _MainTex_TexelSize;//传入 blur 好的Textureuniform sampler2D _Glow;uniform half _GlowStrength;uniform float4 _GlowColorMultiplier;struct v2f {half4 pos : SV_POSITION;half2 uv : TEXCOORD0;half2 uv1 : TEXCOORD1;};v2f vert (appdata_img v){v2f o;o.pos = mul (UNITY_MATRIX_MVP, v.vertex);       o.uv = v.texcoord;       o.uv1 = v.texcoord;       #if UNITY_UV_STARTS_AT_TOPif (_MainTex_TexelSize.y < 0) {o.uv1.y = 1 - o.uv1.y;}#endifreturn o;}fixed4 frag (v2f i) : COLOR{fixed4 mainTex = tex2D(_MainTex, i.uv);half4 glow = tex2D(_Glow, i.uv1) * _GlowStrength * _GlowColorMultiplier;return mainTex + glow;/*#if GLOWEFFECT_BLEND_SCREENreturn 1 - ((1 - mainTex) * (1 - glow));#elif GLOWEFFECT_BLEND_MULTIPLYreturn mainTex * glow;#elif GLOWEFFECT_BLEND_SUBTRACTreturn mainTex - glow;#else // additivereturn mainTex + glow;#endif*/}ENDCG }}}

1. 提亮

2. 渗透




0 0
原创粉丝点击