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. Dismiss Notice

Linear01Depth - is it working?

Discussion in 'Shaders' started by RyuMaster, Jun 2, 2011.

  1. RyuMaster

    RyuMaster

    Joined:
    Sep 13, 2010
    Posts:
    468
    Camera is set to render depth texture.
    I have few objects on screen.

    And surface shader uses this:

    Still, all objects on screen are white. Even that they have different distance.
    I thought, Linear01Depth should return equalized depth buffer (0...1), which will contain full color range from 0 to 255, no matter what near or far clipping plane is.

    Am I wrong on this?
     
  2. RyuMaster

    RyuMaster

    Joined:
    Sep 13, 2010
    Posts:
    468
    Sight, I'm really lost on this one. was trying different shaders for more then 2 days now.
    I have feeling, that I'm completely on the wrong track.

    What I want to do, is always display depthbuffer within 0.255 range. Like, if I have 2 objects near the camera, and there is no more, then I want those object fully utilize 0-255 color range, without playing with near and far clip planes.

    I guess it is not possible? Even if I will get linear depth buffer, still precision will be too small to cover whole 0.255 range for my objects?
     
  3. RyuMaster

    RyuMaster

    Joined:
    Sep 13, 2010
    Posts:
    468
    I've tried many shaders from search result, and ones from Unity documentation. Even if I put Linear01Depth , still I do not see full color range outputed to screen. What is that linearization/normalization about then?

    Does it just put object within 0.1 range, but do not guarantee, that they will cover it fully? just, that they will stay between those values, and that is all?
     
  4. ViniciusGraciano

    ViniciusGraciano

    Joined:
    May 19, 2013
    Posts:
    13
    Ok... I am 8 years late to the party... but I don't care... let's try to answer the question anyway...

    Here's the code for Linear01Depth:

    Code (CSharp):
    1. // Z buffer to linear 0..1 depth
    2. inline float Linear01Depth( float z )
    3. {
    4.     return 1.0 / (_ZBufferParams.x * z + _ZBufferParams.y);
    5. }
    Unity Documentation states that _ZBufferParams are "Used to linearize Z buffer values. x is (1-far/near), y is (far/near), z is (x/far) and w is (y/far)."

    Hence, if z is zero, LinearFloat01Depth will return near/far, and if z is one, it will return one (just substitute the values to see why). Therefore, your assumption is correct, the function maps z values within the [0,1] range, but it does not cover the full range. You can then remap [near/far, 1] to [0, 1] if you will.

    Another related function is LinearEyeDepth:

    Code (CSharp):
    1. // Z buffer to linear depth
    2. inline float LinearEyeDepth( float z )
    3. {
    4.     return 1.0 / (_ZBufferParams.z * z + _ZBufferParams.w);
    5. }
    Using the same logic, it is easy to see that LinearEyeDepth maps the non-linear z values into the range [near, far].
     
    ruicheng1995 likes this.