Search Unity

Get Correct Camera Depth

Discussion in 'Shaders' started by quizcanners, Mar 29, 2015.

  1. quizcanners

    quizcanners

    Joined:
    Feb 6, 2015
    Posts:
    109
    Code (CSharp):
    1. struct v2f {
    2. ...
    3. float2 uv : TEXCOORD0;
    4. }
    5.  
    In vert:

    Code (CSharp):
    1. o.uv=v.texcoords;
    in frag:

    Code (CSharp):
    1. o.col.r = tex2D(_CameraDepthTexture, i.uv.xy);
    So red cube(in the image below) outputs depth as color red on his texture.

    But it is only 0 to 1, while it would seem like _CameraDepthTexture should have larger value, since I get depth<1(red<1) only when object is very close to the camera.

    And second question:
    I don't want to render depth texture as texture, I want shader to read from it before writing pixel to it. I want to read the depth value that will be replaced by this pixel. How do I do it?

    http://s9.postimg.org/mhlw2i0jj/F***eng_shiet.png
     
    Last edited: Mar 29, 2015