How to realize screen Gaussian blur with shader
Today, I will talk to you about how to use shader to achieve Gaussian blur on the screen, many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
1. Create a new C# script, the modified script must be hung on the camera
Using UnityEngine;using System.Collections;/// High-period blur / public class GaussianBlur: PostEffectsBase {/ / adjusts the number of Gaussian blur iterations [Range (0,4)] public int iterations = 3; / / the fuzzy range [Range (0.2f, 3.0f)] public float blurSpread = 0.6f / / the larger the zoom factor / / downSample, the fewer pixels you need to process, and you can further improve the blur program, / / but too large downSample may pixel the image. [Range (1,8)] public int downSample = 2; public Shader gaussianBlurShader; private Material gaussianBlurMaterial = null; public Material material {get {gaussianBlurMaterial = CheckShaderAndCreateMaterial (gaussianBlurShader, gaussianBlurMaterial); return gaussianBlurMaterial }} void OnRenderImage (RenderTexture src, RenderTexture dest) {if (material! = null) {/ / uses scaling to desample the image, thus reducing the number of pixels to be processed and improving performance. Int rtW = src.width / downSample; int rtH = src.height / downSample; / / allocates a buffer because Gaussian blur needs to call two Pass, and we need to use an intermediate cache to / / store the blurred results after the first Pass is executed. RenderTexture buffer0 = RenderTexture.GetTemporary (rtW, rtH, 0); / / sets the filtering mode of the temporary rendered texture to bilinear. In this way, when we call the first Pass, the number of / / pixels we need to deal with is a fraction of the original. Downsampling the image can not only reduce the number of pixels to be processed and improve the performance, but also get a better blur effect with appropriate mining reduction. Buffer0.filterMode = FilterMode.Bilinear; Graphics.Blit (src, buffer0); / / the number of Gaussian blur iterations for (int iS0; I