Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Depth Buffer in HDRP image effect shaders

Discussion in 'High Definition Render Pipeline' started by Untitled_Tools_Projects, Sep 5, 2020.

  1. Untitled_Tools_Projects

    Untitled_Tools_Projects

    Joined:
    Nov 9, 2016
    Posts:
    10
    Hello, I'm using HDRP version 8.2.0 and I cannot for the life of me get the correct Eye Depth value in the image effect shader I'm trying to create. I'm making planetary bodies and am attempting to create an atmosphere shader for them using a post processing effect. Can someone please help me figure this out, here's what I've got:

    Code (CSharp):
    1. Shader "Hidden/Shader/HDRPPlanetAtmospheres"
    2. {
    3.     HLSLINCLUDE
    4.  
    5.     #pragma target 4.5
    6.     #pragma only_renderers d3d11 playstation xboxone vulkan metal switch
    7.  
    8.     #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
    9.     #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
    10.     #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"
    11.     #include "Packages/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/FXAA.hlsl"
    12.     #include "Packages/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/RTUpscale.hlsl"
    13.  
    14.     struct Attributes
    15.     {
    16.         uint vertexID : SV_VertexID;
    17.         UNITY_VERTEX_INPUT_INSTANCE_ID
    18.     };
    19.  
    20.     struct Varyings
    21.     {
    22.         float4 positionCS : SV_POSITION;
    23.         float2 texcoord   : TEXCOORD0;
    24.         UNITY_VERTEX_OUTPUT_STEREO
    25.     };
    26.  
    27.     Varyings Vert(Attributes input)
    28.     {
    29.         Varyings output;
    30.         UNITY_SETUP_INSTANCE_ID(input);
    31.         UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
    32.         output.positionCS = GetFullScreenTriangleVertexPosition(input.vertexID);
    33.         output.texcoord = GetFullScreenTriangleTexCoord(input.vertexID);
    34.         return output;
    35.     }
    36.  
    37.     // List of properties to control your post process effect
    38.     float3 center, sunDir;
    39.     float4x4 camToWorld, invProjMatrix;
    40.     float radius, atmoRadius, farPlane, densityFalloff;
    41.     int inScatterPts;
    42.     TEXTURE2D_X(_InputTexture);
    43.  
    44.     float2 raySphere(float3 origin, float3 dir, float rad)
    45.     {
    46.         float3 offset = origin - center;
    47.         float a = 1, b = 2 * dot(offset, dir), c = dot(offset, offset) - rad * rad,
    48.             d = b * b - 4 * a * c;
    49.         if (d > 0)
    50.         {
    51.             float s = sqrt(d), dstToSphereNear = max(0, (-b - s) / (2 * a)),
    52.                 dstToSphereFar = (-b + s) / (2 * a);
    53.             if (dstToSphereFar >= 0) return float2(dstToSphereNear, dstToSphereFar - dstToSphereNear);
    54.         }
    55.         return float2(2147483646, 0);
    56.     }
    57.  
    58.     float getVol(float3 origin, float3 dir, float length)
    59.     {
    60.         float3 samplePt = origin;
    61.         float stepSize = length / (inScatterPts - 1), atmoHeight = atmoRadius - radius;
    62.         float accumulated = 0;
    63.         for (int i = 0; i < inScatterPts; i++)
    64.         {
    65.             float density = pow(1 - (distance(samplePt, center) - radius) / atmoHeight, 8);
    66.             float2 sunHit = raySphere(samplePt, sunDir, radius);
    67.             if (sunHit.y == 0) accumulated += density * stepSize * 0.00005;
    68.             samplePt += dir * stepSize;
    69.         }
    70.         return (float)accumulated;
    71.     }
    72.  
    73.     const float epsilon = 0.0001f;
    74.  
    75.     float4 CustomPostProcess(Varyings input) : SV_Target
    76.     {
    77.         UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
    78.  
    79.         float3 origin = mul(camToWorld, float4(0.0f, 0.0f, 0.0f, 1.0f)).xyz,
    80.             dir = mul(invProjMatrix, float4(input.texcoord * 2 - 1, 0.0f, 1.0f)).xyz;
    81.         dir = normalize(mul(camToWorld, float4(dir, 0.0f)).xyz);
    82.         float2 atmoHit = raySphere(origin, dir, atmoRadius),
    83.             planetHit = raySphere(origin, dir, radius);
    84.  
    85.         uint2 posSS = input.texcoord * _ScreenSize.xy;
    86.         float3 outCol = LOAD_TEXTURE2D_X(_InputTexture, posSS).xyz;
    87.         float dstToAtmo = atmoHit.x, dstThroughAtmo = atmoHit.y,
    88.             depth = LinearEyeDepth(SampleCameraDepth(input.texcoord), _ScreenParams);
    89.         if (depth != farPlane) dstThroughAtmo = depth - atmoHit.x;
    90.         else dstThroughAtmo = atmoHit.y;
    91.  
    92.         if (dstThroughAtmo > 0)
    93.         {
    94.             float density = getVol(origin + dir * (dstToAtmo + epsilon), dir, dstThroughAtmo - epsilon);
    95.             return float4(outCol * (1 - density) + density, 1);
    96.         }
    97.         return float4(outCol, 1);
    98.     }
    99.  
    100.     ENDHLSL
    101.  
    102.     SubShader
    103.     {
    104.         Pass
    105.         {
    106.             Name "HDRPPlanetAtmospheres"
    107.  
    108.             ZWrite Off
    109.             ZTest Always
    110.             Blend Off
    111.             Cull Off
    112.  
    113.             HLSLPROGRAM
    114.                 #pragma fragment CustomPostProcess
    115.                 #pragma vertex Vert
    116.             ENDHLSL
    117.         }
    118.     }
    119.     Fallback Off
    120. }
    121.  
     
    Last edited: Sep 6, 2020
  2. antoinel_unity

    antoinel_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    271
    Hello,

    There is a small error in your shader when you sample the depth: you are using
    Code (CSharp):
    1.             depth = LinearEyeDepth(SampleCameraDepth(input.texcoord), _ScreenParams);
    2.  
    But you should use _ZBufferParams instead:
    Code (CSharp):
    1.         float depth = LinearEyeDepth(SampleCameraDepth(input.texcoord), _ZBufferParams);
    2.  
     
  3. Untitled_Tools_Projects

    Untitled_Tools_Projects

    Joined:
    Nov 9, 2016
    Posts:
    10
    Thank you! This got the shader working but there is still an inconsistency. If I view the raymarched atmosphere from a glancing angle it starts to go away and I'm not sure if this is a problem with the uvs or the depth.