Search Unity

Question Accessing Shader Variables of Custom Pass Volume's passes

Discussion in 'High Definition Render Pipeline' started by pille1974, Nov 22, 2020.

  1. pille1974

    pille1974

    Joined:
    Dec 18, 2019
    Posts:
    6
    Hi there,

    when implementing a working HDRP Custom Pass Volume and its material/shader, I see that the CustomPass entries (in the "customPasses" array of a Custom Pass Volume) don't expose much of the inner workings to code to access the materials/shaders fields at run-time.

    Q: Is there a way/API to get to the material (instance) of a Custom Pass Volume's passes and set the pass' shader variables at Editor run-time from code, basically the same variables we already see and can change here (and only here?) in the Material's inspector right below the volume's passes?
     
    Last edited: Nov 22, 2020
  2. antoinel_unity

    antoinel_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    262
    Hello,

    You can access the material of a pass with the customPasses accessor in the Custom Pass Volume. Each element in this list is a CustomPass, in other words, it's an instance of a class derived from CustomPass like the fullscreen custom pass or DrawRenerer custom pass we provide in HDRP.

    You have to test the type of a custom pass before accessing it like this:

    Code (CSharp):
    1.  
    2.             foreach (var pass in customPassVolume.customPasses)
    3.             {
    4.                 if (pass is FullScreenCustomPass f)
    5.                     f.fullscreenPassMaterial ...
    6.                 if (pass is  DrawRenderersCustomPass d)
    7.                     d.overrideMaterial ...
    8.             }
    9.  
    There is no generic way to getting a custom pass material because some user-written custom passes may not use any material, or some may use multiple of them, it completely depends on the use case.
     
    pille1974 likes this.
  3. pille1974

    pille1974

    Joined:
    Dec 18, 2019
    Posts:
    6
    Thanks a lot!

    That was a bit silly of me, for some reason I didn't think about casting right away, although intuition should have told me that the passes are in a polymorphic list and not some other inspector/type magic going on here. ;)
     
    antoinel_unity likes this.