Search Unity

RenderTexture depth

Discussion in 'Shaders' started by topitsky, Mar 3, 2021.

  1. topitsky

    topitsky

    Joined:
    Jan 12, 2016
    Posts:
    100
    I'm able to take a "screenshot" when Im rendering a normal color rendertexture, and save it into a texture2d
    When I change to depth mode in the rendertexture, it doesnt input anything.

    Does somebody know how to convert a depth rendertexture to a normal texture2d?
    I have a high "map camera" and I need depth info from above, so I can use that in a shader, but I don't want to render twice.

    I'm using this from the Unity docs:

    Code (CSharp):
    1.     Texture2D RTImage(Camera camera)
    2.     {
    3.         // The Render Texture in RenderTexture.active is the one
    4.         // that will be read by ReadPixels.
    5.         var currentRT = RenderTexture.active;
    6.         RenderTexture.active = camera.targetTexture;
    7.  
    8.         // Render the camera's view.
    9.         camera.Render();
    10.  
    11.         // Make a new texture and read the active Render Texture into it.
    12.         Texture2D image = new Texture2D(camera.targetTexture.width, camera.targetTexture.height);
    13.         image.ReadPixels(new Rect(0, 0, camera.targetTexture.width, camera.targetTexture.height), 0, 0);
    14.         image.Apply();
    15.  
    16.         // Replace the original active Render Texture.
    17.         RenderTexture.active = currentRT;
    18.         return image;
    19.     }
    Also I've tried using this:

    Code (CSharp):
    1.         Shader.SetGlobalTexture("_LevelDepth", DepthCamera.targetTexture, UnityEngine.Rendering.RenderTextureSubElement.Depth);
     
    Last edited: Mar 3, 2021