Search Unity

Post Process Volume include Background

Discussion in 'Shaders' started by falkenbrew, Jun 25, 2020.

  1. falkenbrew

    falkenbrew

    Joined:
    Apr 21, 2020
    Posts:
    146
    I'm usind Post Process Volume to Invert my colors, but it does not affect the Background/Clear Color. Is there an option to change this? It seems like the rendering is done independantly of the clear color and then they are combined after the post processing. It's all rendered on the same camera and I am using Bloom and then my Invert.

     
  2. falkenbrew

    falkenbrew

    Joined:
    Apr 21, 2020
    Posts:
    146
    I can probably fix this by inverting the background-color myself.... still if anyone has an idea it would be nice to know!
     
  3. Samuel_Herb

    Samuel_Herb

    Joined:
    Jun 23, 2020
    Posts:
    8
    When is the post process effect happening? If it's AfterStack, it should get everything.
    https://docs.unity3d.com/Packages/c...endering.PostProcessing.PostProcessEvent.html
    Although...even BeforeTransparent should get the clear color...this probably isn't the issue.

    Is this using HDR? If so, it's possible the background is in the wrong value range when you invert. You could clamp from 0 to 1 before the invert.
     
  4. falkenbrew

    falkenbrew

    Joined:
    Apr 21, 2020
    Posts:
    146
    It is AfterStack and I am using HDR. The fix with inverting the background myself seems to work.
    I must say I have a very hard time understanding how the effects are handled in sequence. If I understand whats going on I will report.
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    Clear color is done at the start of the frame before any other rendering occurs. It's always literally the first thing rendered by the camera to clear the previous frames' rendering. The only time this isn't true is for the HDRP which by default doesn't use a clear color at all and instead uses the sky rendering to clear the background. That still happens well before any post processing would occur.

    The only way I can see the above happening is if you're using a custom invert shader that's still using the frame buffer's alpha to do the compositing.
     
  6. falkenbrew

    falkenbrew

    Joined:
    Apr 21, 2020
    Posts:
    146
    Thanks for the reply! I'm not sure how I would be doing that. The shader looks like this, probably my blend mode is off...
    Code (CSharp):
    1. Shader "Hidden/Custom/Invert"
    2. {
    3.     HLSLINCLUDE
    4.  
    5.         #include "Packages/com.unity.postprocessing/PostProcessing/Shaders/StdLib.hlsl"
    6.  
    7.         TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
    8.         //float _Blend;
    9.  
    10.         float4 Frag(VaryingsDefault i) : SV_Target
    11.         {
    12.             return SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord);
    13.         }
    14.  
    15.     ENDHLSL
    16.  
    17.     SubShader
    18.     {
    19.         Cull Off ZWrite Off ZTest Always
    20.         Blend Zero OneMinusSrcColor
    21.  
    22.         Pass
    23.         {
    24.             HLSLPROGRAM
    25.  
    26.                 #pragma vertex VertDefault
    27.                 #pragma fragment Frag
    28.  
    29.             ENDHLSL
    30.         }
    31.     }
    32. }
    I did find a solution by just inverting the color in the fragment shader instead of using the blend mode. (the Invert effect in the image below is the one from the bad shader above)

    I'm still having quite some trouble with the effects in the volume. They do not run in the sequence I would expect. As you can see, the mirror should be applied first. Then bloom, then a ripple effect. In the screenshot you can see the ripple effect has been mirrored in both horizontal and vertical direction. What am I missing? I just updated to the newest Unity version hoping this would fix it.

     
    Last edited: Jun 27, 2020
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    That particular blend mode might be problematic. It's multiplying the destination by one minus the source color. Depending on what your destination color is, that can lead to some weirdness. It's also multiplying the destination alpha by one minus the source color, which depending on when it happens within the post processing stack might not be a good thing.

    I'd recommend doing that as:
    Code (csharp):
    1. Blend One Zero
    2.  
    3. // ...
    4.  
    5. float4 Frag(VaryingsDefault i) : SV_Target
    6. {
    7.     float4 col = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord);
    8.     return float4(saturate(1.0 - col.rgb), col.a);
    9. }
     
  8. falkenbrew

    falkenbrew

    Joined:
    Apr 21, 2020
    Posts:
    146
    Ahhh yes that makes sense, thanks for the explanation! I will open another thread with my follow up question.