Unity Shader never started to give up -- advanced Bloom effect

Bloom.cs:

private void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        if (material != null)
        {
            material.SetFloat("_LuminanceThreshold", luminanceThreshold);

            int rtW = source.width / downSample;
            int rtH = source.height / downSample;
            //Allocate a buffer smaller than the screen size so that the number of pixels to be processed is a fraction 
            RenderTexture buffer0 = RenderTexture.GetTemporary(rtW, rtH, 0);
            buffer0.filterMode = FilterMode.Bilinear;

            Graphics.Blit(source, buffer0, material, 0);//Extract the lighter area and store it in buffer0

            for (int i = 0; i < iterations; i++)
            {
                material.SetFloat("_BlurSize", 1.0f + i * blurSpread);

                RenderTexture buffer1 = RenderTexture.GetTemporary(rtW, rtH, 0);

                Graphics.Blit(buffer0, buffer1, material, 1);//The second Pass corresponding to the subshader

                RenderTexture.ReleaseTemporary(buffer0);

                buffer0 = buffer1;
                buffer1 = RenderTexture.GetTemporary(rtW, rtH, 0);

                // Render the horizontal pass
                Graphics.Blit(buffer0, buffer1, material, 2);//The third Pass corresponding to the subshader

                RenderTexture.ReleaseTemporary(buffer0);
                buffer0 = buffer1;
            }

            material.SetTexture("_Bloom", buffer0);
            Graphics.Blit(source, destination, material, 3);//Use the fourth Pass of the subshader for the final blending, and the result is stored in the destination of the target rendering texture.

            RenderTexture.ReleaseTemporary(buffer0);

        }
        else
        {
            Graphics.Blit(source, destination);
        }
    }

The above code is basically the same as the code used by Gaussian blur, but some modifications have been made. Bloom effect needs three steps: first, extract the brighter area in the image, use Graphics.Blit(source, buffer0, material, 0) to use the first Pass in the shader to extract the brighter area in the image, and store it in buffer0. Then the iterative processing of Gauss blur is carried out. The brighter area after blur will be in buffer0. In this case, buffer0 will be passed to the "bloom" texture attribute in the material, and the fourth Pass in the shader will be used for final blending.

 

shader:

SubShader {
		CGINCLUDE

		#include "UnityCG.cginc"

		sampler2D _MainTex;
		half4 _MainTex_TexelSize;//Size of texture pixel x contains 1.0/width y contains 1.0 / height Z contains width W contains height
		sampler2D _Bloom;
		float _LuminanceThreshold;
		float _BlurSize;

		struct v2f {
			float4 pos:SV_POSITION;
			half2 uv:TEXCOORD0;
		};

		v2f vertExtractBright(appdata_img v) {
			v2f o;
			o.pos = UnityObjectToClipPos(v.vertex);
			o.uv = v.texcoord;

			return o;
		}

		fixed luminance(fixed4 color) {
			return 0.2125 * color.r + 0.7154 * color.g + 0.0721 * color.b;
		}

		fixed4 fragExtractBright(v2f i) :SV_Target{
			fixed4 c = tex2D(_MainTex, i.uv);
			fixed val = clamp(luminance(c) - _LuminanceThreshold, 0.0, 1.0); //The result is between 0 and 1.

			return c * val;
		}

		struct v2fBloom {
			float4 pos:SV_POSITION;
			half4 uv:TEXCOORD0;
		};

		v2fBloom vertBloom(appdata_img v) {
			v2fBloom o;

			o.pos = UnityObjectToClipPos(v.vertex);
			o.uv.xy = v.texcoord;
			o.uv.zw = v.texcoord;

			#if UNITY_UV_STARTS_AT_TOP
			if (_MainTex_TexelSize.y < 0.0)
				o.uv.w = 1.0 - o.uv.w;
			#endif

			return o;
		}

		fixed4 fragBloom(v2fBloom i) :SV_Target{
			return tex2D(_MainTex, i.uv.xy) + tex2D(_Bloom, i.uv.zw);
		}

		ENDCG

		ZTest Always Cull off ZWrite off

		Pass {
			CGPROGRAM

			#pragma vertex vertExtractBright
			#pragma fragment fragExtractBright

			ENDCG
		}

		//Pass needs to be capitalized
		UsePass "myShader/Gaussian Blur/GAUSSIAN_BLUR_VERTICAL"

		UsePass "myShader/Gaussian Blur/GAUSSIAN_BLUR_HORIZONTAL"

		Pass {
			CGPROGRAM

			#pragma vertex vertBloom
			#pragma fragment fragBloom

			ENDCG
		}
	}

Design sketch:

Tags: Fragment Attribute

Posted on Fri, 01 Nov 2019 19:11:33 -0700 by sycoj0ker