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.

Transparent RenderTexture with PostProcessing

Discussion in 'Universal Render Pipeline' started by robrab2000-aa, Apr 11, 2022.

  1. robrab2000-aa

    robrab2000-aa

    Joined:
    Feb 8, 2019
    Posts:
    94
    Hi, we recently upgraded from URP 7.5.3 (2019.4) to URP 12.1.6 (2021.2) and are getting some strange behaviour from a RenderTexture which we are using to implement a form of Camera Stacking. This was working great in 2019.4 but that was using PostProcessingV2 so...

    We're essentially trying to have two separate layers of colour grading, one for subject and another for background. To achieve this we have two cameras in the same position/orientation, the first renders the background with its post processing. We then output our second camera (subject) with its own post processing onto a render texture which we then place in front of the first camera. We cull everything but the subject with the second camera and output it to a RenderTexture which we place in front of the first camera (using a Screen Space - Camera UI canvas) so that the two layers are drawn with the subject in front of the background.

    The problem is that if we enable PostProcessing on the second camera, we lose the ability to have a transparent background.

    Post processing on the layered camera disabled:
    Screenshot 2022-04-11 at 12.58.05.png Screenshot 2022-04-11 at 12.58.21.png

    Here is what that looks like when I enable post processing on the layered camera (outputted to render texture):
    Screenshot 2022-04-11 at 12.58.11.png Screenshot 2022-04-11 at 12.58.30.png

    Initially I thought it was the format of the RenderTexture but I've tried different options to no avail. These are the settings we currently use:
    Screenshot 2022-04-11 at 13.09.47.png

    I read here (https://forum.unity.com/threads/ren...-in-urp-coming-from-lwrp.749441/#post-5674399) that others have had similar issues and that assigning a material of type URP/Particles/Simple Lit might help but this hasn't worked for us.
     
  2. Kichang-Kim

    Kichang-Kim

    Joined:
    Oct 19, 2010
    Posts:
    929
  3. robrab2000-aa

    robrab2000-aa

    Joined:
    Feb 8, 2019
    Posts:
    94
    Right, thank you! I haven't done much HLSL before. is this a relatively trivial change to make? (I'm assuming its not and that by adjusting the uber shader, there will be many tertiary considerations/complications)
     
  4. robrab2000-aa

    robrab2000-aa

    Joined:
    Feb 8, 2019
    Posts:
    94
    If I manually adjust that alpha value to 0.25 then I get this:
    Screenshot 2022-04-11 at 17.30.44.png

    I've done this by duplicating the UberPost.shader file into my project, referencing the duplicated shader in my Forward Renderer Data object and changing the return of this function to this:
    return half4(color, 0.25);


    All of this is in line with what you've explained to me. So I expect that if I can grab the alpha value from the input to this shader, then I can pass it through. I don't know how to reference the alpha value.

    It looks like the rest of the colours are fetched using this on line 141:
    color = SAMPLE_TEXTURE2D_X(_SourceTex, sampler_LinearClamp, uvDistorted).xyz;


    Rider doesn't seem to let me check the declaration of this function to work out if I can use it to get at the alpha value.

    Any ideas for how I can get the alpha values?
     
  5. Kichang-Kim

    Kichang-Kim

    Joined:
    Oct 19, 2010
    Posts:
    929
    You can get original alpha value by using
    half alpha = SAMPLE_TEXTURE2D_X(_SourceTex, sampler_LinearClamp, uvDistorted).w;


    Then return it
    return half4(color, alpha)


    (This is sample code for explanation, not ideal solution. Because it takes 2 texture sampling. You can get alpha from original SAMPLE_TXETURE2D_X by storing it to half4 without extra texture sampling)
     
    Kouta-kyun likes this.
  6. robrab2000-aa

    robrab2000-aa

    Joined:
    Feb 8, 2019
    Posts:
    94
    Hooray! Thank you!
     
    shibi2017 likes this.
  7. Master_Davicous

    Master_Davicous

    Joined:
    Apr 29, 2016
    Posts:
    2
    Could you share your final shader code and how you went about adding it to the Forward Renderer Data? I'm having trouble with it.
     
    --julian95-- likes this.
  8. --julian95--

    --julian95--

    Joined:
    Nov 2, 2015
    Posts:
    3
    Yeah it would be awesome if you could share your final solution. Even more awesome it would be if Unity could fix this.
     
    Radivarig likes this.
  9. robrab2000-aa

    robrab2000-aa

    Joined:
    Feb 8, 2019
    Posts:
    94
    Ah, sorry I've only just seen this, here you go!

    Code (CSharp):
    1. Shader "Artificial Artists/Universal Render Pipeline/AA_UberPost"
    2. {
    3.     HLSLINCLUDE
    4.         #pragma exclude_renderers gles
    5.         #pragma multi_compile_local_fragment _ _DISTORTION
    6.         #pragma multi_compile_local_fragment _ _CHROMATIC_ABERRATION
    7.         #pragma multi_compile_local_fragment _ _BLOOM_LQ _BLOOM_HQ _BLOOM_LQ_DIRT _BLOOM_HQ_DIRT
    8.         #pragma multi_compile_local_fragment _ _HDR_GRADING _TONEMAP_ACES _TONEMAP_NEUTRAL
    9.         #pragma multi_compile_local_fragment _ _FILM_GRAIN
    10.         #pragma multi_compile_local_fragment _ _DITHERING
    11.         #pragma multi_compile_local_fragment _ _GAMMA_20 _LINEAR_TO_SRGB_CONVERSION
    12.         #pragma multi_compile_local_fragment _ _USE_FAST_SRGB_LINEAR_CONVERSION
    13.         #pragma multi_compile _ _USE_DRAW_PROCEDURAL
    14.         #pragma multi_compile_fragment _ DEBUG_DISPLAY
    15.  
    16.         #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
    17.         #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Filtering.hlsl"
    18.         #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    19.         #include "Packages/com.unity.render-pipelines.universal/Shaders/PostProcessing/Common.hlsl"
    20.         #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Debug/DebuggingFullscreen.hlsl"
    21.  
    22.         // Hardcoded dependencies to reduce the number of variants
    23.         #if _BLOOM_LQ || _BLOOM_HQ || _BLOOM_LQ_DIRT || _BLOOM_HQ_DIRT
    24.             #define BLOOM
    25.             #if _BLOOM_LQ_DIRT || _BLOOM_HQ_DIRT
    26.                 #define BLOOM_DIRT
    27.             #endif
    28.         #endif
    29.  
    30.         TEXTURE2D_X(_SourceTex);
    31.         TEXTURE2D_X(_Bloom_Texture);
    32.         TEXTURE2D(_LensDirt_Texture);
    33.         TEXTURE2D(_Grain_Texture);
    34.         TEXTURE2D(_InternalLut);
    35.         TEXTURE2D(_UserLut);
    36.         TEXTURE2D(_BlueNoise_Texture);
    37.  
    38.         float4 _Lut_Params;
    39.         float4 _UserLut_Params;
    40.         float4 _Bloom_Params;
    41.         float _Bloom_RGBM;
    42.         float4 _LensDirt_Params;
    43.         float _LensDirt_Intensity;
    44.         float4 _Distortion_Params1;
    45.         float4 _Distortion_Params2;
    46.         float _Chroma_Params;
    47.         half4 _Vignette_Params1;
    48.         float4 _Vignette_Params2;
    49.         float2 _Grain_Params;
    50.         float4 _Grain_TilingParams;
    51.         float4 _Bloom_Texture_TexelSize;
    52.         float4 _Dithering_Params;
    53.  
    54.         #define DistCenter              _Distortion_Params1.xy
    55.         #define DistAxis                _Distortion_Params1.zw
    56.         #define DistTheta               _Distortion_Params2.x
    57.         #define DistSigma               _Distortion_Params2.y
    58.         #define DistScale               _Distortion_Params2.z
    59.         #define DistIntensity           _Distortion_Params2.w
    60.  
    61.         #define ChromaAmount            _Chroma_Params.x
    62.  
    63.         #define BloomIntensity          _Bloom_Params.x
    64.         #define BloomTint               _Bloom_Params.yzw
    65.         #define BloomRGBM               _Bloom_RGBM.x
    66.         #define LensDirtScale           _LensDirt_Params.xy
    67.         #define LensDirtOffset          _LensDirt_Params.zw
    68.         #define LensDirtIntensity       _LensDirt_Intensity.x
    69.  
    70.         #define VignetteColor           _Vignette_Params1.xyz
    71.         #define VignetteCenter          _Vignette_Params2.xy
    72.         #define VignetteIntensity       _Vignette_Params2.z
    73.         #define VignetteSmoothness      _Vignette_Params2.w
    74.         #define VignetteRoundness       _Vignette_Params1.w
    75.  
    76.         #define LutParams               _Lut_Params.xyz
    77.         #define PostExposure            _Lut_Params.w
    78.         #define UserLutParams           _UserLut_Params.xyz
    79.         #define UserLutContribution     _UserLut_Params.w
    80.  
    81.         #define GrainIntensity          _Grain_Params.x
    82.         #define GrainResponse           _Grain_Params.y
    83.         #define GrainScale              _Grain_TilingParams.xy
    84.         #define GrainOffset             _Grain_TilingParams.zw
    85.  
    86.         #define DitheringScale          _Dithering_Params.xy
    87.         #define DitheringOffset         _Dithering_Params.zw
    88.  
    89.         float2 DistortUV(float2 uv)
    90.         {
    91.             // Note: this variant should never be set with XR
    92.             #if _DISTORTION
    93.             {
    94.                 uv = (uv - 0.5) * DistScale + 0.5;
    95.                 float2 ruv = DistAxis * (uv - 0.5 - DistCenter);
    96.                 float ru = length(float2(ruv));
    97.  
    98.                 UNITY_BRANCH
    99.                 if (DistIntensity > 0.0)
    100.                 {
    101.                     float wu = ru * DistTheta;
    102.                     ru = tan(wu) * (rcp(ru * DistSigma));
    103.                     uv = uv + ruv * (ru - 1.0);
    104.                 }
    105.                 else
    106.                 {
    107.                     ru = rcp(ru) * DistTheta * atan(ru * DistSigma);
    108.                     uv = uv + ruv * (ru - 1.0);
    109.                 }
    110.             }
    111.             #endif
    112.  
    113.             return uv;
    114.         }
    115.  
    116.         half4 Frag(Varyings input) : SV_Target
    117.         {
    118.             UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
    119.  
    120.             float2 uv = UnityStereoTransformScreenSpaceTex(input.uv);
    121.             float2 uvDistorted = DistortUV(uv);
    122.  
    123.             half3 color = (0.0).xxx;
    124.  
    125.             #if _CHROMATIC_ABERRATION
    126.             {
    127.                 // Very fast version of chromatic aberration from HDRP using 3 samples and hardcoded
    128.                 // spectral lut. Performs significantly better on lower end GPUs.
    129.                 float2 coords = 2.0 * uv - 1.0;
    130.                 float2 end = uv - coords * dot(coords, coords) * ChromaAmount;
    131.                 float2 delta = (end - uv) / 3.0;
    132.  
    133.                 half r = SAMPLE_TEXTURE2D_X(_SourceTex, sampler_LinearClamp, uvDistorted                ).x;
    134.                 half g = SAMPLE_TEXTURE2D_X(_SourceTex, sampler_LinearClamp, DistortUV(delta + uv)      ).y;
    135.                 half b = SAMPLE_TEXTURE2D_X(_SourceTex, sampler_LinearClamp, DistortUV(delta * 2.0 + uv)).z;
    136.  
    137.                 color = half3(r, g, b);
    138.             }
    139.             #else
    140.             {
    141.                 color = SAMPLE_TEXTURE2D_X(_SourceTex, sampler_LinearClamp, uvDistorted).xyz;
    142.             }
    143.             #endif
    144.  
    145.             // Gamma space... Just do the rest of Uber in linear and convert back to sRGB at the end
    146.             #if UNITY_COLORSPACE_GAMMA
    147.             {
    148.                 color = GetSRGBToLinear(color);
    149.             }
    150.             #endif
    151.  
    152.             #if defined(BLOOM)
    153.             {
    154.                 #if _BLOOM_HQ && !defined(SHADER_API_GLES)
    155.                 half4 bloom = SampleTexture2DBicubic(TEXTURE2D_X_ARGS(_Bloom_Texture, sampler_LinearClamp), uvDistorted, _Bloom_Texture_TexelSize.zwxy, (1.0).xx, unity_StereoEyeIndex);
    156.                 #else
    157.                 half4 bloom = SAMPLE_TEXTURE2D_X(_Bloom_Texture, sampler_LinearClamp, uvDistorted);
    158.                 #endif
    159.  
    160.                 #if UNITY_COLORSPACE_GAMMA
    161.                 bloom.xyz *= bloom.xyz; // γ to linear
    162.                 #endif
    163.  
    164.                 UNITY_BRANCH
    165.                 if (BloomRGBM > 0)
    166.                 {
    167.                     bloom.xyz = DecodeRGBM(bloom);
    168.                 }
    169.  
    170.                 bloom.xyz *= BloomIntensity;
    171.                 color += bloom.xyz * BloomTint;
    172.  
    173.                 #if defined(BLOOM_DIRT)
    174.                 {
    175.                     // UVs for the dirt texture should be DistortUV(uv * DirtScale + DirtOffset) but
    176.                     // considering we use a cover-style scale on the dirt texture the difference
    177.                     // isn't massive so we chose to save a few ALUs here instead in case lens
    178.                     // distortion is active.
    179.                     half3 dirt = SAMPLE_TEXTURE2D(_LensDirt_Texture, sampler_LinearClamp, uvDistorted * LensDirtScale + LensDirtOffset).xyz;
    180.                     dirt *= LensDirtIntensity;
    181.                     color += dirt * bloom.xyz;
    182.                 }
    183.                 #endif
    184.             }
    185.             #endif
    186.  
    187.             // To save on variants we'll use an uniform branch for vignette. Lower end platforms
    188.             // don't like these but if we're running Uber it means we're running more expensive
    189.             // effects anyway. Lower-end devices would limit themselves to on-tile compatible effect
    190.             // and thus this shouldn't too much of a problem (famous last words).
    191.             UNITY_BRANCH
    192.             if (VignetteIntensity > 0)
    193.             {
    194.                 color = ApplyVignette(color, uvDistorted, VignetteCenter, VignetteIntensity, VignetteRoundness, VignetteSmoothness, VignetteColor);
    195.             }
    196.  
    197.             // Color grading is always enabled when post-processing/uber is active
    198.             {
    199.                 color = ApplyColorGrading(color, PostExposure, TEXTURE2D_ARGS(_InternalLut, sampler_LinearClamp), LutParams, TEXTURE2D_ARGS(_UserLut, sampler_LinearClamp), UserLutParams, UserLutContribution);
    200.             }
    201.  
    202.             #if _FILM_GRAIN
    203.             {
    204.                 color = ApplyGrain(color, uv, TEXTURE2D_ARGS(_Grain_Texture, sampler_LinearRepeat), GrainIntensity, GrainResponse, GrainScale, GrainOffset);
    205.             }
    206.             #endif
    207.  
    208.             // When Unity is configured to use gamma color encoding, we ignore the request to convert to gamma 2.0 and instead fall back to sRGB encoding
    209.             #if _GAMMA_20 && !UNITY_COLORSPACE_GAMMA
    210.             {
    211.                 color = LinearToGamma20(color);
    212.             }
    213.             // Back to sRGB
    214.             #elif UNITY_COLORSPACE_GAMMA || _LINEAR_TO_SRGB_CONVERSION
    215.             {
    216.                 color = GetLinearToSRGB(color);
    217.             }
    218.             #endif
    219.  
    220.             #if _DITHERING
    221.             {
    222.                 color = ApplyDithering(color, uv, TEXTURE2D_ARGS(_BlueNoise_Texture, sampler_PointRepeat), DitheringScale, DitheringOffset);
    223.                 // Assume color > 0 and prevent 0 - ditherNoise.
    224.                 // Negative colors can cause problems if fed back to the postprocess via render to FP16 texture.
    225.                 color = max(color, 0);
    226.             }
    227.             #endif
    228.  
    229.             #if defined(DEBUG_DISPLAY)
    230.             half4 debugColor = 0;
    231.  
    232.             if(CanDebugOverrideOutputColor(half4(color, 1), uv, debugColor))
    233.             {
    234.                 return debugColor;
    235.             }
    236.             #endif
    237.  
    238.             half alpha = SAMPLE_TEXTURE2D_X(_SourceTex, sampler_LinearClamp, uvDistorted).w;
    239.             return half4(color, alpha);
    240.         }
    241.  
    242.     ENDHLSL
    243.  
    244.     SubShader
    245.     {
    246.         Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline"}
    247.         LOD 100
    248.         ZTest Always ZWrite Off Cull Off
    249.  
    250.         Pass
    251.         {
    252.             Name "UberPost"
    253.  
    254.             HLSLPROGRAM
    255.                 #pragma vertex FullscreenVert
    256.                 #pragma fragment Frag
    257.             ENDHLSL
    258.         }
    259.     }
    260. }
    261.  
     
    milox777 and shibi2017 like this.
  10. StanSi

    StanSi

    Joined:
    Jan 16, 2017
    Posts:
    2
    I can't find where to reference the shader which you give at yesterday in my Forward Renderer Data object. I used unity 2021.3.4f1c1. There is nothing displayed on the inspector of my PostProcessData.asset.
     

    Attached Files:

    unity_KLb5YtVHF55zzA likes this.
  11. robrab2000-aa

    robrab2000-aa

    Joined:
    Feb 8, 2019
    Posts:
    94
    Heya, you need to replace which shader gets used as the uber post processing shader. In mine this is exposed using this gist: https://gist.github.com/tomkail/ba4136e6aa990f4dc94e0d39ec6a058c
    It basically lets you see the details of a scriptable object without having to go into the object directly.

    From there you just replace the shader being used:
    Screenshot 2022-07-15 at 10.43.30.png
     
    milox777 and Kouta-kyun like this.
  12. MidnightCoffeeInc

    MidnightCoffeeInc

    Joined:
    Feb 28, 2017
    Posts:
    440
    I know this is an older thread, but I just wanted to say that @robrab2000-aa saved me with his solution!
    I wish Unity would make PP transparency optional because in some cases it's important!
    Thanks again :)
     
    robrab2000-aa likes this.
  13. robrab2000-aa

    robrab2000-aa

    Joined:
    Feb 8, 2019
    Posts:
    94
    Ah, thanks! You just made my day!
     
    Yoraiz0r and MidnightCoffeeInc like this.
  14. FrankfurtOceanic

    FrankfurtOceanic

    Joined:
    Feb 23, 2022
    Posts:
    1
    Hey I know this was a month ago but I was hoping you could help me out. When I place the ExtendedScriptableObjectDrawer.cs file in the Packages/Universal RP/editor, it doesn't expose the other variables. Is there a different location it should go in? Thanks
     
  15. robrab2000-aa

    robrab2000-aa

    Joined:
    Feb 8, 2019
    Posts:
    94
    Hey, I don't think ExtendedScriptableObjectDrawer.cs should be in the Packages folder. Just put it somewhere in your project in a folder called `Editor`.
     
  16. Radivarig

    Radivarig

    Joined:
    May 15, 2013
    Posts:
    70
    Is this still the only solution? Has someone opened a bug/request ticket?