In this short video, I will show you how I created a dynamic Defocus effect in Unity, focusing only on the objects in the center of the camera's view while blurring the others.



Video Transcript

Hello everyone! In this short video, I will show you how I created a dynamic Defocus effect in Unity, focusing only on the objects in the center of the camera's view while blurring the others.

Right now, I’m showcasing the final effect by exaggerating the blur, but this effect is customizable, and soon I will show you how to do it.

The video and the script were created using Unity 2022 and the Universal Rendering Pipeline.

To correctly use the script, you need to add a Volume component to the Main Camera, so you can access the Depth of Field sub-component.

You need to change the mode of the Defocus component to Bokeh, as this mode allows you to specify the distance of the focus point and other effects, making the final effect more realistic.

Let's specifically enable the Focus Distance and Aperture parameters.

For the Focus Distance, we can use the default value because we will modify this value during gameplay via a C# script.

Regarding the Aperture parameter, it's worth noting that the lower this value, the greater the selective focus effect will be, as the depth of field will be shallower.

To show you the initial example, where the effect was intentionally exaggerated, I set the value to 2.4, but of course, you will need to set a value appropriate for your project after doing some testing.

Before showing you the code for implementing the script, I'll briefly describe how it works.

The script will project an invisible ray called a Raycast from the camera to the center of the frame in the virtual scene.

When the ray hits an object with a Collider component, it will calculate the distance of that object from the camera and write this value into the Focus Distance field of the Depth of Field sub-component.

The operation is very simple, but it’s important to keep in mind that for objects to be detected, they must have a Collider component, which can also be set to Trigger mode if you want to prevent these objects from obstructing the character's movement.

Now let's take a look at the very simple script in C# that I named Dynamic Defocus.

To run the script correctly, you need to import the Rendering and Rendering Universal modules.

The private variables I defined are only used to access the Volume component and its Depth of Field sub-component, so that we can modify its parameters.

Specifically, the Volume and Depth of Field variables are initialized in the Start function of the script.

Inside the Update function, which runs automatically at each frame of execution, the Raycast ray is projected.

The colliding object is written into the hit variable, but it might not exist, for example, because the user is focusing on the background or a portion of the scene where there are no objects with Collider components.

For this reason, before assigning the Distance value of the hit variable to the Focus Distance parameter of the Depth of Field component, it’s necessary to check that the Collider of hit is not null.

The name of the object intercepted by the ray can be retrieved by accessing the Collider component of the hit variable and then, from that component, to its Game Object and finally to the name.

After saving the script, do some testing in your project and adjust the value of the Aperture parameter until you achieve the desired result.

I hope this short video has been helpful! See you soon!

This website is intended solely to showcase some of my work and has no promotional purpose. Please note that I am not currently seeking - nor will I respond to - requests for custom work, consulting services, or any other form of professional collaboration.


EXTENDED PRIVACY AND COOKIE USAGE POLICY