Dynamic depth of field is an effect that can be used in FPS games to blur the background while reloading a weapon or focusing on the target as the player aims through iron sights of their weapon while blurring the rest of the screen. It can also be used to create a camera that you can use for taking screenshots for portfolio or any other reason you would want DoF effect without bothering with all the post process adjustments every time.
Here I'll give you the bare bones of how to implement this effect to a player camera using TP_FirstPersonBP template project, which later on you can implement on any camera and change the settings depending on your needs.
Here you can see the overall network inside the character blueprint's Event graph, if you have experience with Blueprints and just want to get the network and recreate it right away. Now more details.
We are going to be using two events, Event Tick and an Input event. We don't want to have this effect all the time in this case, so we use Right Mouse Button to enable Tick to fire a line trace from the camera which will tell us if/where/which object is the center of focus. And when we release the input, it will reverse the Timeline and stop the effect. Event Tick will also update the camera's post process settings as we will see in the following sections.
Trace Distance node is a collapsed graph to reduce the crowd in the network. You can see the expanded view down below:
Return Value 1 is the camera's location and 5000 is the distance we are going to be tracing.
We are going to use a 1 second long Timeline track going from 0.0 to 0.5 to set the intensity of depth of field effect(which basically activates/deactivates the effect) After a trace happens every tick we check the return value(boolean).If it is true then it continues the rest of the calculations. If not, it reverses the Timeline to stop the effect.
Considering the trace hit an object, we break that result so that we can get the Hit Location. And to get the distance between the camera and the hit location we will use a macro like this:
Or, you can use Vector Length node instead if you prefer. Subtract location of the camera from focus location and get the length of that output, and you will get the distance.
Now that we have the information we need we can use it to manipulate our camera's depth of field settings.
First we get the post process settings of the camera, which will give us the last focal distance so that we can use it for a soft blend when the trace hits points too far from each other.
To get the Break/Make post process nodes;
- Get First Person Camera
- Drag from first person camera and Get Post Process Settings
- Drag from post process settings and you'll see Break and Make PostProcessSettings nodes
Now in the details panel of Break Postprocesssettings node enable only Depth of Field Focal Distance setting. Currently(version 4.1.1) post process setting nodes are a little messy so you'll have to turn all of the rest of the settings manually for easier control. When we drag from Depth of Field Focal Distance and Set the variable(named Last Focus) it gets stored as the last focal distance. And finally in order to get a smooth blend between the last focus distance and the current hit location we use FInterp To node.
When it's all done, this is what you will get every time you press and hold the right mouse button:
You can download a sample project icluding a character BP with this function here.
- Setting the interp speed depending on the distance is optional, but highly suggested.
- You can adjust Near and Far Transition depending on how you want it to look like. Add distance for softer transition / Subtract distance for sharper transition.