- Home /
How to Change Depth of Field: Focal Length from a Script (LWRP)?
I would like to change the focal length of a Depth of Field effect when I select an object in the game (using Unity 2019.2 with LWRP).
I'm thinking I would use Post Processing Volume as a game object, with the Depth of Field effect enabled.
The script would use: using UnityEngine; using UnityEngine.Rendering.PostProcessing;
Then I would create a variable of some type, and set it to hold the data of Post Processing Volume, but I cannot find the type in the Unity documents.
Then I would do something like: myVar.focalLength = myNewValue;
I don't know if focalLength is the proper name, because I can't find that in Unity docs either.
Does anybody have a script for something like this?
Answer by Santa · Jan 22, 2020 at 04:01 AM
It's a catchy thing. There could be more than one Volume blending at a time.
You need PostProcessLayer (it is should be in your Camera)
public PostProcessLayer v2_PostProcess;
Then you should go through active Volumes:
List<PostProcessVolume> volList = new List<PostProcessVolume>();
PostProcessManager.instance.GetActiveVolumes(v2_PostProcess, volList, true, true);
//
foreach(PostProcessVolume vol in volList)
{
PostProcessProfile ppp = vol.profile;
if (ppp)
{
DepthOfField dph;
if (ppp.TryGetSettings<DepthOfField>(out dph))
{
dph.focusDistance.value = REQUIRED_DISTANCE;
}
}
}
Focal distance should be changed.
Answer by JeffreyBennett · Jan 22, 2020 at 09:39 PM
Hey, thanks! This worked!
Here's what I did:
I have FirstPersonCharacter (which is the camera).
I have a second game object that is the Post-process Volume (this comes in automatically when you start a new project in the LWRP template).
I used the code you gave me in a script (see below.)
I put the script onto the Post-process Volume.
I dragged the camera object into the public V2_Post Process field in the inspector.
So, thank you very much!
As bonus work, once you'd set me on the path, I also searched through the two scripts DepthOfField.cs and DepthOfFieldComponent.cs and found the names of the other Depth of Field properties, so now I can also address those in that same script (see it below).
So, with this information I can now do things like, when the camera moves, apply a blur to the Camera, and when it stops set the focus length back to normal to clear up the picture. By the way, the name of my script / class is LERP_Focal_Length.cs, but there is no lerp function in it (yet). That's the next objective, so hopefully nobody is confused by that name.
Script: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Rendering.PostProcessing;
public class LERP_Focal_Length : MonoBehaviour {
public int blur_amt = 1;
public PostProcessLayer v2_PostProcess;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
List<PostProcessVolume> volList = new List<PostProcessVolume>();
PostProcessManager.instance.GetActiveVolumes(v2_PostProcess, volList, true, true);
//
foreach (PostProcessVolume vol in volList)
{
PostProcessProfile ppp = vol.profile;
if (ppp)
{
DepthOfField dph;
if (ppp.TryGetSettings<DepthOfField>(out dph))
{
//dph.focusDistance.value = 69;
//dph.aperture.value = 30;
dph.focalLength.value = blur_amt;
//dph.kernelSize.value = KernelSize.VeryLarge;
}
}
}
}
}
Not exactly sure what happened on that cut/paste...the whole script IS there, but it doesn't seem to be formatted properly for some reason. If you're looking at it and asking "why post the script starting on the line that says public int blur = 1; then check the unformatted lines just above that for the rest of it, as well as the trailing } below it.