Search Unity

  1. Unity 2019.4 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  3. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Converting depth values to distances from z-buffer

Discussion in 'Shaders' started by msAnton, Jun 29, 2020 at 10:28 AM.

  1. msAnton

    msAnton

    Joined:
    Apr 17, 2020
    Posts:
    5
    Hi! Do anyone know how to convert values from depth texture into distances from camera?
    I wrote the shader for that, but can't manage the conversion.
    Code (CSharp):
    1. Shader "Custom/MyDepthShader"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.      
    7.     }
    8.     SubShader
    9.     {
    10.         // No culling or depth
    11.         Cull Off ZWrite Off ZTest Always
    12.  
    13.         Pass
    14.         {
    15.             CGPROGRAM
    16.             #pragma vertex vert // compile function vert as vertex shader
    17.             #pragma fragment frag // compile function frag as fragment shader
    18.  
    19.             #include "UnityCG.cginc"
    20.  
    21.             struct appdata
    22.             {
    23.                 float4 vertex : POSITION;
    24.                 float2 uv : TEXCOORD0;
    25.             };
    26.  
    27.             struct v2f
    28.             {
    29.                 float2 uv : TEXCOORD0;
    30.                 float4 vertex : SV_POSITION;
    31.             };
    32.  
    33.             v2f vert (appdata v)
    34.             {
    35.                 v2f o;
    36.                 o.vertex = UnityObjectToClipPos(v.vertex);
    37.                 o.uv = v.uv;
    38.                 return o;
    39.             }
    40.  
    41.             sampler2D _MainTex;
    42.             sampler2D _CameraDepthTexture;
    43.  
    44.             fixed4 frag (v2f i) : SV_Target
    45.             {
    46.                 float depth = tex2D(_CameraDepthTexture, i.uv).r;
    47.                 depth = Linear01Depth(depth);
    48.                 depth = depth * _ProjectionParams.z;
    49.                 return depth;
    50.             }
    51.             ENDCG
    52.         }
    53.     }
    54. }
    As a result I want that texture2D would contain distance values.
     
  2. Namey5

    Namey5

    Joined:
    Jul 5, 2013
    Posts:
    185
    If you just need view space depth (distance along the camera's local z axis) then you can use the LinearEyeDepth function;

    Code (CSharp):
    1. fixed4 frag (v2f i) : SV_Target
    2. {
    3.     return LinearEyeDepth (tex2D (_CameraDepthTexture, i.uv).r);
    4. }
    If you need the actual real world distance from a point in the scene to the camera, then you'll need to reconstruct it's position using camera matrices;

    Code (CSharp):
    1. struct v2f
    2. {
    3.     float4 pos : SV_POSITION;
    4.     float2 uv : TEXCOORD0;
    5.     float4 viewDir : TEXCOORD1;
    6. }
    7.  
    8. v2f vert (appdata v)
    9. {
    10.     v2f o;
    11.     o.pos = UnityObjectToClipPos (v.vertex);
    12.     o.uv = v.uv;
    13.     //Find the view-space direction of the far clip plane from the camera (which, when interpolated, gives us per pixel view dir of the scene position)
    14.     o.viewDir = mul (unity_CameraInvProjection, float4 (o.uv * 2.0 - 1.0, 1.0, 1.0));
    15.     return o;
    16. }
    17.  
    18. half4 frag (v2f i) : SV_Target
    19. {
    20.     float depth01 = Linear01Depth (tex2D (_CameraDepthTexture, i.uv).r);
    21.     //Find the view-space position of the current pixel by multiplying viewDir by depth
    22.     float3 viewPos = (i.viewDir.xyz / i.viewDir.w) * depth01;
    23.     //Length of viewPos is the raw distance to the camera
    24.     return length (viewPos);
    25. }
     
  3. msAnton

    msAnton

    Joined:
    Apr 17, 2020
    Posts:
    5
    Thank you for your help. I now found these values along the Z-axes, but they are biased by 0.5 units for some reason Do you know why?
     
  4. msAnton

    msAnton

    Joined:
    Apr 17, 2020
    Posts:
    5
    I found an answer. It was so stupid that I couldn't deduce it earlier. Biase was from cube's size.
     
unityunity