Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Adjust Aperture at runtime

Discussion in 'Cinemachine' started by HeyBishop, Sep 26, 2023.

  1. HeyBishop

    HeyBishop

    Joined:
    Jun 22, 2017
    Posts:
    238
    I'd like to adjust the aperture of the Depth of Field via the Cinemachine Volume Settings. Alas, it's not working as I expect.

    As you can see in the video below, I can adjust the aperture in editor no problem.
    But at runtime when I manipulate the aperture by code, the effect does change - despite seeing the value change in the inspector.



    Below is the code I'm using to change the aperture value. I feel like I must be missing something.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using MalbersAnimations.Scriptables;
    5. using Cinemachine.PostFX;
    6. using UnityEngine.Rendering.Universal;
    7.  
    8. public class SetFocusTransform : MonoBehaviour
    9. {
    10.     public TransformVar focusTarget;
    11.     public CinemachineVolumeSettings volumeSettings;
    12.     [Tooltip("If -1, override is disabled.")]
    13.     public float overrideAperture = -1f;
    14.     DepthOfField depthOfField;
    15.     float apertureValueAtEnable;
    16.  
    17.     private void OnEnable()
    18.     {
    19.         volumeSettings.m_FocusTarget = focusTarget.Value;
    20.         volumeSettings.m_Profile.TryGet<DepthOfField>(out depthOfField);
    21.         apertureValueAtEnable = depthOfField.aperture.value;
    22.  
    23.         if (overrideAperture > 0)
    24.             AdjustAperture(overrideAperture);
    25.     }
    26.  
    27.     private void OnDisable()
    28.     {
    29.         AdjustAperture(apertureValueAtEnable);
    30.     }
    31.  
    32.     public void AdjustAperture(float apature)
    33.     {
    34.         depthOfField.aperture.value = apature;
    35.     }
    36. }
    37.  
    38.  
     
  2. antoinecharton

    antoinecharton

    Unity Technologies

    Joined:
    Jul 22, 2020
    Posts:
    156
    Heyyoo :)

    Is this error comming from a cinemachine script?
    Screenshot 2023-09-27 at 1.09.09 PM.png
     
  3. HeyBishop

    HeyBishop

    Joined:
    Jun 22, 2017
    Posts:
    238
    I didn't notice that at the time I recorded the video. I double checked, whatever caused that error isn't happening anymore. Yet my non-updating aperture issue is unchanged.
     
    Last edited: Sep 27, 2023
  4. HeyBishop

    HeyBishop

    Joined:
    Jun 22, 2017
    Posts:
    238
    @antoinecharton, could this problem have something to do with the fact that two Cinemachine cameras are using the same post processing global volume? I have a follow cam, and a seperate cam that is enabled for special events - but even with that camera disabled the aperture still won't adjust via my above code.
     
  5. antoinecharton

    antoinecharton

    Unity Technologies

    Joined:
    Jul 22, 2020
    Posts:
    156
  6. HeyBishop

    HeyBishop

    Joined:
    Jun 22, 2017
    Posts:
    238
  7. antoinecharton

    antoinecharton

    Unity Technologies

    Joined:
    Jul 22, 2020
    Posts:
    156
    Have you tried interpolating two volumes? In that case gregory suggest to do it through a vcam interpolation but you could also use the built in volume weight for that.

    You create your own volume override and you use the weight instead (You use 2 volume in your scene). That's the easiest way I could think of to achieve this.
    Screenshot 2023-10-02 at 11.18.02 AM.png

    Let us know if that solves your issue.
     
    HeyBishop and Gregoryl like this.
  8. HeyBishop

    HeyBishop

    Joined:
    Jun 22, 2017
    Posts:
    238
    ooooooh, this could be a way to do what I'm considering. Thank you.
     
    antoinecharton likes this.