Search Unity

Change ChromaticAberration (Cinemachine's) via script

Discussion in 'High Definition Render Pipeline' started by sergio93, Mar 28, 2021.

  1. sergio93

    sergio93

    Joined:
    Aug 28, 2020
    Posts:
    52
    Hello All,

    I'm struggling a bit on this one. I have a cinemachine virtual camera with Volume Settings extension and a chromatic aberration override.

    I am just trying to change via code the intensity of the chromatic aberration but can't seem to be able to find anywhere how to access it.

    I can see with

    Code (CSharp):
    1. foreach (UnityEngine.Rendering.VolumeComponent volumeComponent in volumeSettings.m_Profile.components) {
    2.     Debug.Log(volumeComponent.name);
    3. }
    That a component with name "ChromaticAberration" exist. I also think that the class I want to use is
    UnityEngine.Rendering.HighDefinition.ChromaticAberration

    How can I now convert the VolumeComponent into the ChromaticAberration so I can change its intensity?

    Thanks
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,727
    Is there a reason why the chromatic aberration effect is attached to the vcam's volume? Can you instead just make a global volume with only that effect and adjust the volume's weight? That would be the easiest approach.
     
  3. sergio93

    sergio93

    Joined:
    Aug 28, 2020
    Posts:
    52
    Thank you very much for your reply. I am using Cinemachine and HDRP, I thought that in this case the global volume does not work. If I try to set up a post processing layer and volume I don't see any change in the game view.

    Is there a way to use the post processing global volume with Cinemachine and HDRP? If not, is there a way to acces the components in the vcam's volume?
     
  4. sergio93

    sergio93

    Joined:
    Aug 28, 2020
    Posts:
    52
    I found a way to do it:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering;
    3. using UnityEngine.Rendering.HighDefinition;
    4.  
    5.  
    6. public class PostProcessingManager : MonoBehaviour
    7. {
    8.  
    9.     public Cinemachine.PostFX.CinemachineVolumeSettings volumeSettings;
    10.  
    11.     private LensDistortion lensDistortion;
    12.     private ChromaticAberration chromaticAberration;
    13.  
    14.     void Start()
    15.     {
    16.  
    17.         foreach (VolumeComponent volumeComponent in volumeSettings.m_Profile.components) {
    18.             if (volumeComponent.name == "LensDistortion") lensDistortion = volumeComponent as LensDistortion;
    19.             if (volumeComponent.name == "ChromaticAberration") chromaticAberration = volumeComponent as ChromaticAberration;
    20.         }
    21.      
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.  
    28.         if (Input.GetKeyDown(KeyCode.M)) {
    29.             if (chromaticAberration) {
    30.                 chromaticAberration.intensity.value = 1f;
    31.                 chromaticAberration.intensity.overrideState = true;
    32.             }
    33.         }
    34.         if (Input.GetKeyDown(KeyCode.N)) {
    35.             if (chromaticAberration) {
    36.                 chromaticAberration.intensity.value = 0f;
    37.                 chromaticAberration.intensity.overrideState = true;
    38.             }
    39.         }
    40.  
    41.     }
    42.  
    43. }
    44.