How to output depth images

I have recently started learning Unity in order to create a simulation environment for collecting point cloud data. I would like to get a depth image, but I am having trouble with it.

[What I have done so far]
In C# script (PassDepth.cs), I assigned RGB and depth information from the camera to colorBuffer and depthBuffer, respectively, and created colorRenderTexture and depthRenderTexture.

Quad was created and the depth buffer was verified to be correctly loaded into depthRenderTexture using Material with depthRenderTexture.

[What I want to solve]
I tried to save the depthRenderTexture created above as a PNG file as a depth image, but I could not get an image like the scene view and a blank image was saved.

I thought perhaps the depth was not reflected in the depthRenderTexture, but I do not know how to improve it. The source code and screenshots are shown below. I hope you can help me to solve this problem.

  • PassDepth.cs
using System.Collections;
using System.Collections.Generic;
using System.IO;

using UnityEngine;

public class PassDepth : MonoBehaviour
{
    [SerializeField]
    public Camera camera;
    [SerializeField]
    public RenderTexture colorRenderTexture;
    [SerializeField]
    public RenderTexture depthRenderTexture;

    // Start is called before the first frame update
    void Start()
    {
        var colorBuffer = colorRenderTexture.colorBuffer;
        var depthBuffer = depthRenderTexture.depthBuffer;

        camera.SetTargetBuffers(colorBuffer, depthBuffer);

    }

    // Update is called once per frame
    void Update()
    {
        Graphics.SetRenderTarget(colorRenderTexture);
        camera.Render();
    }
}
  • The shader I used to create the material to attach to Quad
Shader "Unlit/MyUnlit"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

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

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

            sampler2D _MainTex;
            float4 _MainTex_ST;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);

                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 col = tex2D(_MainTex, i.uv);
                col.g = col.r;
                col.b = col.r;

                return col;
            }
            ENDCG
        }
    }
}
  • Source code attempted when saving the created depthRenderTexture
Texture2D tex = new Texture2D(RenderTextureRef.width, RenderTextureRef.height, TextureFormat.RGB24, false);
RenderTexture.active = RenderTextureRef;
tex.ReadPixels(new Rect(0, 0, RenderTextureRef.width, RenderTextureRef.height), 0, 0);
tex.Apply();

// Encode texture into PNG
byte[] bytes = tex.EncodeToPNG();
Object.Destroy(tex);

//Write to a file in the project folder
File.WriteAllBytes(Application.dataPath + "/../DepthRenderTexture.png", bytes);
  • Screenshot of my Unity screen

Thank you!