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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Shaking Custom Post Processing in HDRP

Discussion in 'High Definition Render Pipeline' started by IedSoftworks, Mar 11, 2022.

  1. IedSoftworks

    IedSoftworks

    Joined:
    May 27, 2021
    Posts:
    12
    Hello there,
    I'm currently trying to make a outline post processing shader in HDRP.
    I roughly follow the article from VertexFragment (https://www.vertexfragment.com/ramblings/unity-postprocessing-sobel-outline/) and it works (mostly), but I have this wierd issue, where the outline jumpes around all the time. Its really distracting. Additionally it only happens in game view, not in scene view.

    I think it has something to do with the depth and GBuffer2 texture, since a different effect (also using the depth texture) has a similar problem.

    To my testing enviroment: Sample HDRP-Scene and a simple post processing script that displays the parameters.

    Perhaps someone can help me with that issue.

    Shader:
    Code (CSharp):
    1. /* Shader based on:
    2. * https://www.vertexfragment.com/ramblings/unity-postprocessing-sobel-outline/
    3. */
    4.  
    5. Shader "Hidden/Shader/OutlineShader"
    6. {
    7.     HLSLINCLUDE
    8.  
    9.     #pragma target 4.5
    10.     #pragma only_renderers d3d11 playstation xboxone xboxseries vulkan metal switch
    11.  
    12.     #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
    13.     #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
    14.     #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"
    15.     #include "Packages/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/FXAA.hlsl"
    16.     #include "Packages/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/RTUpscale.hlsl"
    17.  
    18.     struct Attributes
    19.     {
    20.         uint vertexID : SV_VertexID;
    21.         UNITY_VERTEX_INPUT_INSTANCE_ID
    22.     };
    23.  
    24.     struct Varyings
    25.     {
    26.         float4 positionCS : SV_POSITION;
    27.         float2 texcoord   : TEXCOORD0;
    28.         UNITY_VERTEX_OUTPUT_STEREO
    29.     };
    30.  
    31.     Varyings Vert(Attributes input)
    32.     {
    33.         Varyings output;
    34.         UNITY_SETUP_INSTANCE_ID(input);
    35.         UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
    36.         output.positionCS = GetFullScreenTriangleVertexPosition(input.vertexID);
    37.         output.texcoord = GetFullScreenTriangleTexCoord(input.vertexID);
    38.         return output;
    39.     }
    40.  
    41.     // List of properties to control your post process effect
    42.     float _Intensity;
    43.     float _OutlineThickness;
    44.     float _OutlineDepthMultiplier;
    45.     float _OutlineDepthBias;
    46.     float _OutlineNormalMultiplier;
    47.     float _OutlineNormalBias;
    48.  
    49.     float3 _OutlineColor;
    50.  
    51.     TEXTURE2D_X(_InputTexture);  
    52.     TEXTURE2D_X(_GBufferTexture1);
    53.  
    54.     float SobelDepth(float center, float left, float right, float up, float down) {
    55.      
    56.         return abs(left - center) + abs(right - center) + abs(up - center) + abs(down - center);
    57.     }
    58.  
    59.     float SobelSampleDepth(float2 uv, float3 offset) {
    60.         float pixelCenter = LinearEyeDepth(LOAD_TEXTURE2D_X(_CameraDepthTexture, uv).r, _ZBufferParams);
    61.         float pixelLeft = LinearEyeDepth(LOAD_TEXTURE2D_X(_CameraDepthTexture, uv - offset.xz).r, _ZBufferParams);
    62.         float pixelRight = LinearEyeDepth(LOAD_TEXTURE2D_X(_CameraDepthTexture, uv + offset.xz).r, _ZBufferParams);
    63.         float pixelUp = LinearEyeDepth(LOAD_TEXTURE2D_X(_CameraDepthTexture, uv + offset.zy).r, _ZBufferParams);
    64.         float pixelDown = LinearEyeDepth(LOAD_TEXTURE2D_X(_CameraDepthTexture, uv - offset.zy).r, _ZBufferParams);
    65.  
    66.         return SobelDepth(pixelCenter, pixelLeft, pixelRight, pixelUp, pixelDown);
    67.     }
    68.  
    69.     float4 SobelNormal(float2 uv, float3 offset)
    70.     {
    71.         float4 pixelCenter = LOAD_TEXTURE2D_X(_GBufferTexture1, uv);
    72.         float4 pixelLeft = LOAD_TEXTURE2D_X(_GBufferTexture1, uv - offset.xz);
    73.         float4 pixelRight = LOAD_TEXTURE2D_X(_GBufferTexture1, uv + offset.xz);
    74.         float4 pixelUp = LOAD_TEXTURE2D_X(_GBufferTexture1, uv + offset.zy);
    75.         float4 pixelDown = LOAD_TEXTURE2D_X(_GBufferTexture1, uv - offset.zy);
    76.  
    77.         return abs(pixelLeft - pixelCenter) + abs(pixelRight - pixelCenter) + abs(pixelUp - pixelCenter) + abs(pixelDown - pixelCenter);
    78.     }
    79.  
    80.     float4 CustomPostProcess(Varyings input) : SV_Target
    81.     {
    82.         UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
    83.  
    84.         uint2 positionSS = input.texcoord * _ScreenSize.xy;
    85.         //return float4(input.texcoord, 0, 1);
    86.         float3 outColor = LOAD_TEXTURE2D_X(_InputTexture, positionSS).xyz;
    87.      
    88.         //return float4(LOAD_TEXTURE2D_X(_GBufferTexture1, positionSS).rgb, 1);
    89.  
    90.         float3 offset = float3((1.0 / _ScreenParams.x), (1.0 / _ScreenParams.y), 0.0) * _OutlineThickness;
    91.         float sobelDepth = SobelSampleDepth(positionSS, offset);
    92.         sobelDepth = pow(saturate(sobelDepth) * _OutlineDepthMultiplier, _OutlineDepthBias);
    93.      
    94.         float4 sobelNormalCalc = SobelNormal(positionSS, offset);
    95.         float sobelNormal = sobelNormalCalc.x + sobelNormalCalc.y + sobelNormalCalc.z;
    96.         sobelNormal = pow(sobelNormal * _OutlineNormalMultiplier, _OutlineNormalBias);
    97.      
    98.         float outline = saturate(max(sobelDepth, sobelNormal));
    99.  
    100.         float3 outlineColor = lerp(outColor, _OutlineColor, _Intensity);
    101.         outColor = lerp(outColor, outlineColor, outline);
    102.  
    103.  
    104.  
    105.         return float4(outColor, 1);
    106.     }
    107.  
    108.     ENDHLSL
    109.  
    110.     SubShader
    111.     {
    112.         Pass
    113.         {
    114.             Name "OutlineShader"
    115.  
    116.             ZWrite Off
    117.             ZTest Always
    118.             Blend Off
    119.             Cull Off
    120.  
    121.             HLSLPROGRAM
    122.                 #pragma fragment CustomPostProcess
    123.                 #pragma vertex Vert
    124.             ENDHLSL
    125.         }
    126.     }
    127.     Fallback Off
    128. }
    129.  
     
    Last edited: Mar 11, 2022
  2. IedSoftworks

    IedSoftworks

    Joined:
    May 27, 2021
    Posts:
    12
    Found A solution, but not a great one, tbh.

    The shaking comes from the Anti-Ailasing. Specifically from Temporal Anti-Aliasing.
    My solution is switching from TAA to anything else (None, FXAA, SMAA) on my main camera.
     
    sabint likes this.