Implementing frosted glass blur effect in unity

Full screen frosted glass blur effect

principle

The principle of the full-screen frosted glass blur effect is to use RenderTexture to capture the current screen image, then use a blur algorithm to blur the RenderTexture image, and finally display the processed image on the screen. This method can achieve a full-screen frosted glass effect, but requires higher performance.

The principle of frosted glass blur for some objects is to use two cameras, one camera only renders the object that needs to be blurred, and the other camera renders other objects and the background. Then use RenderTexture to capture the image rendered by the second camera, blur the RenderTexture image, and finally paste the processed image as a material on the object rendered by the first camera. This method can achieve the frosted glass effect of some objects, but it requires higher performance.

Complete code

// Create a RenderTexture as an intermediate rendering target
private RenderTexture rt;

// Get the object that needs to be blurred
private GameObject targetObj;

// Get the camera that needs to be blurred
private Camera blurCamera;

void Start()
{ // Create a RenderTexture, the resolution can be as needed Adjust rt = new RenderTexture(Screen.width, Screen.height, 0); // Get the object that needs to be blurred targetObj = GameObject.Find("TargetObject"); // Get the camera that needs to be blurred blurCamera = GameObject.Find("BlurCamera ").GetComponent<Camera>(); // Use RenderTexture as the rendering target of BlurCamera blurCamera.targetTexture = rt; // Paste the rendered image as a material on the TargetObject targetObj.GetComponent<Renderer>().material.SetTexture ("_BlurTex", rt); }
















void OnRenderImage(RenderTexture src, RenderTexture dest)
{ // Blur the RenderTexture image Graphics.Blit(src, rt, blurMaterial); // Display the processed image on the screen Graphics.Blit(rt, dest); }







Some objects are blurred by frosted glass, but the main body is not blurry.

principle

The principle of frosted glass blur for some objects is to use two cameras, one camera only renders the object that needs to be blurred, and the other camera renders other objects and the background. Then use RenderTexture to capture the image rendered by the second camera, blur the RenderTexture image, and finally paste the processed image as a material on the object rendered by the first camera. This method can achieve the frosted glass effect of some objects, but it requires higher performance.

Complete code

// Create a RenderTexture as an intermediate rendering target
private RenderTexture rt;

// Get the object that needs to be blurred
private GameObject targetObj;

// Get the camera that needs to be blurred
private Camera blurCamera;

void Start()
{ // Create a RenderTexture, the resolution can be as needed Adjust rt = new RenderTexture(Screen.width, Screen.height, 0); // Get the object that needs to be blurred targetObj = GameObject.Find("TargetObject"); // Get the camera that needs to be blurred blurCamera = GameObject.Find("BlurCamera ").GetComponent<Camera>(); // Use RenderTexture as the rendering target of BlurCamera blurCamera.targetTexture = rt; // Paste the rendered image as a material on the TargetObject targetObj.GetComponent<Renderer>().material.SetTexture ("_BlurTex", rt); }
















void OnRenderImage(RenderTexture src, RenderTexture dest)
{ // Blur the RenderTexture image Graphics.Blit(src, rt, blurMaterial); // Display the processed image on the screen Graphics.Blit(rt, dest); }








Implemented using URP features

6db35c17efd6b9e7d8968df425aad0a8.jpeg

66dc3f0aa2becd52cc3f29dbe222b03a.jpeg

1d623be5098f100bd4e05b7d60f28495.jpeg

e42e8222a8c49c7467e08fd9ca341b4d.jpeg

642157c4d11f914d38bc7ef6b87861f1.jpeg

Guess you like

Origin blog.csdn.net/shguxudong11/article/details/129422391