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

Post processing effects

Discussion in 'Scripting' started by Quiet_Apples_Studios, Jul 24, 2020.

  1. Quiet_Apples_Studios

    Quiet_Apples_Studios

    Joined:
    Jan 27, 2020
    Posts:
    17
    Hello can anyone help me increment the intensity setting on the post processing vignette via script?
    I looked at the API and im having a hard time wrapping my head around it


    For further context, im making a script that increases the vignette intensity setting by 1 as Time.delta time increases
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    Are you using either the post-processing in HDRP, or Post Processing Stack V2?

    If so, then you should be able to get a handle on the Vignette object. Make sure you've check the box to allow overrides to the intensity. Then, in code, you can just do something like this:

    Code (CSharp):
    1. _postProcessingVignette.intensity.Override(someFloatValue);
    That will set the intensity to whatever 'someFloatValue' equals. If you want to do this over time, you can either set this in an Update statement, or in a coroutine.
     
  3. Quiet_Apples_Studios

    Quiet_Apples_Studios

    Joined:
    Jan 27, 2020
    Posts:
    17
    I'm using post processing stack V2, im not quiet sure how to call it.

    i tried making a public vignette
    but I can't drag it in
     
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    Your script should have a public property for either the VolumeProfile, or the Volume that the profile is on. Then you can call
    profile.GetComponent<Vignette>()
    to get the Vignette component.

    Here's a simplified example from my code. This is using HDRP's post processing, but I think it's nearly identical to V2.

    Code (CSharp):
    1. public Volume RenderVolume;
    2.  
    3. private Vignette _postProcessingVignette;
    4.  
    5. void Start() {
    6.     _postProcessingVignette = RenderVolume.profile.GetComponent<Vignette>();
    7. }
    8.  
    9. void Update() {
    10.     _postProcessingVignette.intensity.Override(whateverTheIntensityShouldBe);
    11. }
     
  5. Quiet_Apples_Studios

    Quiet_Apples_Studios

    Joined:
    Jan 27, 2020
    Posts:
    17
    apparently "Post processprofile does not contain a defnition for get component"
    what should i used instead?
     
  6. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    You probably have a typo or something. Show your code, both the code that calls GetComponent, and the code that declares the variable you're calling it on.
     
  7. Quiet_Apples_Studios

    Quiet_Apples_Studios

    Joined:
    Jan 27, 2020
    Posts:
    17
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Rendering.PostProcessing;
    5.  
    6. public class PostProcessing : MonoBehaviour
    7. {
    8.     public PostProcessVolume RenderVolume;
    9.  
    10.     private Vignette _postProcessingVignette;
    11.  
    12.     void Start()
    13.     {
    14.         _postProcessingVignette = RenderVolume.profile.GetComponent<Vignette>();
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.  
    21.         _postProcessingVignette.intensity.Override(1);
    22.     }
    23. }
    24.  
     
  8. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    Ah, sorry about that. My fault. I completely forgot I created an extension method (a code shortcut) so I could call GetComponent on a volume. Instead, of GetComponent, you'll need something like this:

    (Note: This is for HDRP. For PPv2, change "components" to "settings".)
    _postProcessingVignette = RenderVolume.profile.components.FirstOrDefault(c => c is Vignette) as Vignette;


    Be sure to add
    using System.Linq;
    to use this.


    And here's that extension method, though, in case you want to use it, since the above code is kind of awkward:

    Code (CSharp):
    1.     public static class VolumeProfileUtil
    2.     {
    3.  
    4.         public static T GetComponent<T>(this VolumeProfile profile) where T : VolumeComponent
    5.         {
    6.             // Note: This is for HDRP. For PPv2, change "components" to "settings".
    7.             return profile.components.FirstOrDefault(s => s is T) as T;
    8.         }
    9.  
    10.     }
     
    Last edited: Jul 26, 2020
  9. Quiet_Apples_Studios

    Quiet_Apples_Studios

    Joined:
    Jan 27, 2020
    Posts:
    17
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Rendering.PostProcessing;
    5. using System.Linq;
    6.  
    7. public class PostProcessing : MonoBehaviour
    8. {
    9.     public PostProcessVolume RenderVolume;
    10.  
    11.     private Vignette _postProcessingVignette;
    12.  
    13.     void Start()
    14.     {
    15.         _postProcessingVignette = RenderVolume.profile.components.FirstOrDefault(c => c is Vignette) as Vignette;
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.  
    22.         _postProcessingVignette.intensity.Override(1);
    23.     }
    24.  
    25.  
    26. }
    27.  
    28. components still seems to be highlighted and saying there is no assembly reference
    29.  
     
  10. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    Looks like in HDRP, it's called "components", while in PPv2 it's called "settings". Just change ".components" to ".settings" and I think it should work.
     
    Quiet_Apples_Studios likes this.
  11. Quiet_Apples_Studios

    Quiet_Apples_Studios

    Joined:
    Jan 27, 2020
    Posts:
    17
    You are my hero thank you so much