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

_CameraDepthTexture doesn't work

Discussion in 'Shaders' started by tfisiche, Oct 30, 2019.

  1. tfisiche

    tfisiche

    Joined:
    Sep 30, 2019
    Posts:
    9
    Hi,

    I'm trying to compute the distance of every objects in the camera frustum by using the _CameraDepthTexture in a shader.
    Here is my code but it doesn't work, I have uninitialized or strange values for each pixels :

    Code (CSharp):
    1. cam = GetComponent <Camera > ();
    2. cam.depthTextureMode = DepthTextureMode.Depth;
    3.  
    4. Texture2D depthTexture = new Texture2D(resWidth, resHeight, TextureFormat.RHalf, false);
    5.         RenderTexture rt = new RenderTexture(resWidth, resHeight, resDepth, RenderTextureFormat.Depth);
    6.  
    7. Graphics.Blit(depthMaterial.mainTexture, rt);
    8.         RenderTexture.active = rt;
    9.         depthTexture.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
    10.         depthTexture.Apply();
    11.  
    12.         Color[] depthData = depthTexture.GetPixels();
    I've tried to use the defaulte render pipeline as well as LWRP but none of them work.
    Can you tell me what am I missing pls ?
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Hi,
    I think you are doing it a bit wrong. Didn't you ask this in another thread elsewhere already?
     
  3. tfisiche

    tfisiche

    Joined:
    Sep 30, 2019
    Posts:
    9
    Hi, yes I am, but I'm a bit lost in this topic, I tried many things but nothing seems to work so I tried to ask the question differently to improve my chances to have an answer.
     
  4. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    If you're still stuck I can maybe prepare an example later tonight.
     
  5. tfisiche

    tfisiche

    Joined:
    Sep 30, 2019
    Posts:
    9
    Hi, sorry for the delay but I've been working on something else meanwhile.
    Yes, I'm still stuck and I would really appreciate an example if possible :)