Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JeffreyBennett · Jan 21, 2020 at 10:48 PM · depth of field

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by JeffreyBennett · Jan 22, 2020 at 09:39 PM

Hey, thanks! This worked!

Here's what I did:

  1. I have FirstPersonCharacter (which is the camera).

  2. 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).

  3. I used the code you gave me in a script (see below.)

  4. I put the script onto the Post-process Volume.

  5. 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;
             }
         }
     }
 }

}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JeffreyBennett · Jan 23, 2020 at 02:08 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

119 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Direct X 11 - What if the user doesn't have it? 1 Answer

Depth of field not working 0 Answers

How Do I get Depth of Field working together with 2D Toolkit sprites? 0 Answers

How to get quality Depth Of Field effect with Post Processing Stack V2? 1 Answer

DepthOfField error using 5.4 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges