Search Unity

accessing the Depth of Field parameter via script?

Discussion in 'Cinemachine' started by MSachs, Nov 19, 2018.

  1. MSachs

    MSachs

    Joined:
    Nov 22, 2017
    Posts:
    122
    Hi,

    I would like to access the Depth of Field parameter (especially Focal Length) via script. I am using Cinemachine 2.2.7 and the Post Processing Stack 2.0.17 if that matters.

    Thanks in advance :)
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,717
    Cinemachine itself can only set basic lens settings such as FOV and near/far clip planes. For depth of field you need to use the PostProcessing stack. Put a layer on the main camera, and then create a PostProcessing extension on your vcam, with a profile that includes Depth of Field:

    upload_2018-11-19_8-41-38.png

    The profile settings themselves live in an asset, and are not meant to be modified at runtime. However, the CinemachinePostProcessing extension makes a special case for depth of field, and gives you some runtime control.

    If you check Focus Tracks Target, then the focus distance will dynamically track the LookAt target of the vcam. The Offset parameter controls the offset from the target to the point that will be in focus. 0 puts the target exactly in focus, -1 put the focus point at 1m closer to the camera, and 1 will put it at 1m farther from the camera. If there is no LookAt target, then the offset is relative to the camera.

    From code, just do vcam.GetComponent<CinemachinePostProcessing>(), and you'll have access to fields m_FocusTracksTarget and m_FocusOffset.
     
  3. MSachs

    MSachs

    Joined:
    Nov 22, 2017
    Posts:
    122

    Okay thanks. I am already using it like that but I don't think I can get the actual result I want like that... do you know if there is a way to exchange Post-Processing Profiles at runtime? Or would it be possible to attach 2 Cinemachine Post Processing Scripts on a single camera and activate/deactivate the components as I need the one or the other?

    Otherwise I guess there is always the possibility to use multiple vcams with different Post Processing Profiles attached.
     
    Last edited: Nov 20, 2018
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,717
    Any of those things are possibilities. You just have to try them and see what works best for you.
     
    MSachs likes this.
  5. MSachs

    MSachs

    Joined:
    Nov 22, 2017
    Posts:
    122
    Thanks a lot!