1
\$\begingroup\$

I am new in Unity and specializing in another field. But now I have to rapidly study it for a new project. I will be very thankful if anyone explain me how to read values of the depth buffer. I wrote a shader, which creates depth image:

Shader "Custom/MyDepthShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        
    }
    SubShader
    {
        // No culling or depth
        Cull Off ZWrite Off ZTest Always

        Pass
        {
            CGPROGRAM
            #pragma vertex vert // compile function vert as vertex shader
            #pragma fragment frag // compile function frag as fragment shader

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            sampler2D _MainTex;
            sampler2D _CameraDepthTexture;

            fixed4 frag (v2f i) : SV_Target
            {
                float depth = tex2D(_CameraDepthTexture, i.uv).r;
                depth = Linear01Depth(depth);
                depth = depth * _ProjectionParams.z;
                return depth;
            }
            ENDCG
        }
    }
}

Now I need to get depth information into a texture and save float values of this texture to for example txt file for further processing. I didn't found yet answer to this question after two days of googling and reading guides. For testing this shader I am using OnImageRender(RT src, RT dst, material).

\$\endgroup\$
4
  • \$\begingroup\$ It looks to me like you've already solved the problem of reading the depth buffer. Now all that remains is saving the rendered screen to a file, which you can find existing documentation and Q&A about how to do. \$\endgroup\$
    – DMGregory
    Jun 26, 2020 at 11:06
  • \$\begingroup\$ Okei. Thank you. I thought that it was solved but couldnt check that. Now I checked. \$\endgroup\$ Jun 26, 2020 at 11:52
  • \$\begingroup\$ If you've solved your problem, want to write up your solution as an Answer below that can help future users? \$\endgroup\$
    – DMGregory
    Jun 26, 2020 at 12:16
  • \$\begingroup\$ Yep, I know. I am now in a trip. I will write the answer when came home. Thanks for your hint \$\endgroup\$ Jun 26, 2020 at 14:09

1 Answer 1

1
\$\begingroup\$

I found an answer to my question. Thanks DMGregory for your participation. I only needed to add two rows of code into the OnPostRender()-function:

Graphics.Blit(renderTexture, depthRendTex, mat);
depthTex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);

depthText (Texture2D) now contains all the values. By using function GetPixel(i,j) I gained access to each value in the texture.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .