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); }