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

How to get scene depth via HLSL code.

Discussion in 'Universal Render Pipeline' started by Modafuka, Jun 11, 2020.

  1. Modafuka

    Modafuka

    Joined:
    Nov 21, 2019
    Posts:
    45
    My current code:
    Code (CSharp):
    1. SubShader{
    2.         LOD 100
    3.  
    4.         Tags {
    5.             "Queue" = "Transparent"
    6.             "RenderType" = "Transparent"
    7.             "RenderPipeline" = "UniversalPipeline"
    8.             "IgnoreProjector" = "True"
    9.         }
    10.  
    11.         ZWrite Off
    12.         Blend SrcAlpha OneMinusSrcAlpha
    13.  
    14.         Pass {
    15.             Tags {"LightMode" = "UniversalForward"}
    16.  
    17.             HLSLPROGRAM
    18.             #pragma vertex MainVertex
    19.             #pragma fragment MainFragment
    20.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    21.             #include "MainCore.hlsl"
    22.  
    23.             struct Attributes {
    24.                 float4 positionOS : POSITION;
    25.                 float2 texCoord : TEXCOORD0;
    26.             };
    27.  
    28.             struct Varyings {
    29.                 float4 positionCS : SV_POSITION;
    30.                 float2 texCoord : TEXCOORD0;
    31.                 float4 screenPos : TEXCOORD1;
    32.             };
    33.  
    34.             TEXTURE2D(_BaseMap); SAMPLER(sampler_BaseMap); float4 _BaseMap_ST;
    35.  
    36.             half4 _Color;
    37.  
    38.             Varyings MainVertex(Attributes atb) {
    39.                 Varyings vriOut;
    40.                 vriOut.texCoord = TRANSFORM_TEX(atb.texCoord, _BaseMap);
    41.  
    42.                 float3 positionWS = TransformObjectToWorld(atb.positionOS.xyz);
    43.                 vriOut.positionCS = mul(UNITY_MATRIX_VP, float4(positionWS, 1));
    44.                 vriOut.screenPos = ComputeScreenPos(vriOut.positionCS);
    45.  
    46.                 return vriOut;
    47.             }
    48.  
    49.             half4 MainFragment(Varyings vri) : SV_Target {
    50.                 float zRaw = SampleSceneDepth(vri.screenPos.xy); // this function from shader graph
    51.                 float z01 = Linear01Depth(SampleSceneDepth(vri.screenPos.xy), _ZBufferParams);
    52.                 float zEye = LinearEyeDepth(SampleSceneDepth(vri.screenPos.xy), _ZBufferParams);
    53.                 return zRaw;
    54.             }
    55.             ENDHLSL
    56.         }
    57.     }
    I don't know why it not work, please help me.
     
  2. unity_8E69BCBFE12B3D1C76EF

    unity_8E69BCBFE12B3D1C76EF

    Joined:
    Apr 27, 2022
    Posts:
    1
    Hey :D

    the problem in your code is that you forgot to map the depth in the correct uv.

    first, you have to calculate the correct uv based on screenPosition, and finally use this uvs to calculate your SceneDepth:

      1. half4 MainFragment(Varyings vri) : SV_Target {

        float2 screenUVs = vri.screenPos.xy / vri.screenPos.w;
      2. float zRaw = SampleSceneDepth(screenUVs);
      3. float z01 = Linear01Depth(SampleSceneDepth(screenUVs), _ZBufferParams);
      4. float zEye = LinearEyeDepth(SampleSceneDepth(screenUVs), _ZBufferParams);
      5. return zRaw;
      6. }
    1. hope it helps you :3