Search Unity

Unity 2019.2.12f1 HDRP accessing the depth of field focus distance

Discussion in 'High Definition Render Pipeline' started by jexmatex, Nov 20, 2019.

  1. jexmatex

    jexmatex

    Joined:
    Jun 23, 2016
    Posts:
    47
    Hello guy,

    i have an issue with the HDRP, i try to access the focus distance of depth of field parameter added as override on the post processing setting
    i want to link the focus distance to a target i already got in my scene
    my problem is to acess to the parameter,

    using UnityEngine.PostProcessing; is not good anymore
    private DepthOfField DOF; this dont work either

    i really dont know wich way to acces it, documetation is really unclear about it.

    any int on the new way to adress it ?

    cheers
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Hi @jexmatex,

    How do you imagine that would work with .PostProcessing, if you see that the things are clearly somewhere under that Volume setup? :)

    As the post-processing is now integrated to HDRP/URP, you need to check their documentation if anything. I don't think there's anything in Unity's "normal" docs. Sigh.

    But anyway. You do it the same way as you get other volume component type data. First you access the Volume, then get the volume profile's override you want to modify and change the settings. In your case it's the DepthOfField. The answers are already posted somewhere here, and I think I also already posted one similar reply.

    But it's best to dig into the source code that's available in the package folder and check the API documentation, although in many places it just lists the stuff and really doesn't explain anything...
     
  3. jexmatex

    jexmatex

    Joined:
    Jun 23, 2016
    Posts:
    47
    was a guess more than a imagination :)

    i was trying to start from sample code, as i'm not a développer ;)
    i have found tricks with volum but cant find the desired result,
     
  4. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Well, you need to write a small script to access the volume component. Tell me what you need help with.

    EDIT: Check the thread I posted some tips to earlier: It's exact same approach you need to use.
    https://forum.unity.com/threads/how-to-update-volumetric-fog-value-in-runtime.765164/

    You access the volume component, access the specific effect (DOF) and then change it's values.

    Here's a small example, I wrote it so that it's closer to what you need. You need to also define the fields in your script, and use correct name spaces (HDPipeline and UnityEngine.Rendering and maybe some others.)

    Code (CSharp):
    1. Volume volume = gameObject.GetComponent<Volume>();
    2. DepthOfField tempDof;
    3. if (volume.profile.TryGet<DepthOfField>(out tempDof) )
    4. {
    5.    depthOfField = tempDof;
    6. }
    Then, in your update, you would calculate the distance you want to use (or any other parameter) and apply it back to the volume's effect. You do it like this:
    Code (CSharp):
    1. depthOfField.focusDistance.value = yourNewValue;
     
    Last edited: Nov 21, 2019
  5. homemacai

    homemacai

    Joined:
    Jul 22, 2020
    Posts:
    74
    I get this error when trying your method:

    The type 'UnityEngine.Rendering.PostProcessing.DepthOfField' cannot be used as type parameter 'T' in the generic type or method 'VolumeProfile.TryGet<T>(out T)'. There is no implicit reference conversion from 'UnityEngine.Rendering.PostProcessing.DepthOfField' to 'UnityEngine.Rendering.VolumeComponent'.



    Any advice ? Thanks
     
  6. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    @homemacai Something might have changed since I wrote that example. I'd need to test that on a later Unity version. Which version are you using?
     
  7. liccardi

    liccardi

    Joined:
    Dec 6, 2018
    Posts:
    18
    I also have this problem
     
  8. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    @liccardi that volume component access should still be same as it was. I just tested that on Unity 2021.1 since the last time I replied I used 2019.x. Anyway, at least I could get the volume and change values. Can you perhaps provide more information?
     
  9. liccardi

    liccardi

    Joined:
    Dec 6, 2018
    Posts:
    18
    I use Unity 2022 , pipeline HDRP , and my DoF have this parametrs :
     

    Attached Files:

  10. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    @liccardi so where and how are you trying to access the volume and its components?
     
  11. liccardi

    liccardi

    Joined:
    Dec 6, 2018
    Posts:
    18
    via script, but using the TryGet gives an error
     
  12. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Is there a Volume assigned to a field in Inspector or have you assigned a Volume component in some other way to your script so that you can actually access it?
     
  13. liccardi

    liccardi

    Joined:
    Dec 6, 2018
    Posts:
    18

    if I try to log in it gives me this error: The type 'UnityEngine.Rendering.PostProcessing.DepthOfField' cannot be used as type parameter 'T' in the generic type or method 'VolumeProfile.TryGet<T>(out T)'. There is no implicit reference conversion from 'UnityEngine.Rendering.PostProcessing.DepthOfField' to 'UnityEngine.Rendering.VolumeComponent'.
     

    Attached Files:

  14. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Try adding this to the beginning of your script:
    Code (CSharp):
    1. using UnityEngine.Rendering.HighDefinition;
    You are probably missing a reference to the HDRP namespace.
     
    liccardi likes this.
  15. liccardi

    liccardi

    Joined:
    Dec 6, 2018
    Posts:
    18
    done, but it conflicts with "using UnityEngine.Rendering.PostProcessing;"
    and if I remove " using UnityEngine.Rendering.PostProcessing; "
    gives me an error for this line "volume".
     

    Attached Files:

  16. liccardi

    liccardi

    Joined:
    Dec 6, 2018
    Posts:
    18
    I solved!
     
    Olmi likes this.
  17. Predacom

    Predacom

    Joined:
    Mar 2, 2015
    Posts:
    6
    I got the same issue, and tried all the possible setting I could find it.
    But still give me the error message:
    he type 'UnityEngine.Rendering.PostProcessing.DepthOfField' cannot be used as type.......

    Even tried the Unity manual for volume scripting api and the error persist.
    Is there something I`m missing? beside "namespace", "tryget", declaring volume and profile as in example above?

    Some hidden setting to make it global or something?
     
  18. SteveKouts

    SteveKouts

    Joined:
    Aug 23, 2012
    Posts:
    79
    Try use this combination it worked for me:

    using UnityEngine;
    using UnityEngine.Rendering.HighDefinition;
    using UnityEngine.Rendering;

    public class AutoDoF : MonoBehaviour
    {
    public Volume volume;
    public DepthOfField dof;
    VolumeProfile profile;

    void Start() {
    volume = GetComponent<Volume>();
    profile = volume.profile;
    profile.TryGet(out dof);
    }