Search Unity

[SOLVED] _CameraDepthTexture values don't change linear

Discussion in 'Shaders' started by dorukeker, Apr 16, 2020.

  1. dorukeker

    dorukeker

    Joined:
    Dec 6, 2016
    Posts:
    37
    Hello All,

    TL;DR
    The depth values from the _CameraDepthTexture is not changing linear between the near and far clipping planes even though I am using Linear01Depth(). They are the correct values on the near clipping plane and the far clipping plane (0 and 1) but the change is not linear between those points. E.g. on mid-way it not 0.5 but 0.6. Any ideas?

    More details about the setup:
    Unity 2019.3.x

    The camera clear flags are "Depth Only"
    It has a target texture. The Color Format of the render texture is DEPTH_AUTO

    I have a material with a custom shader which has a Texture property _CameraDepthTexture. The above mentioned Render Texture is assigned there. (NOTE: what I will mention below happens even the texture property has a different name)

    The fragment part of the shader is as below

    Code (CSharp):
    1. // Read texture into a fixed4
    2. fixed4 colCameraDepth = tex2D(_CameraDepthTexture, float2(i.uv.x , i.uv.y));
    3. // Linearise the depth values
    4. colCameraDepth = Linear01Depth(colCameraDepth);
    5. // Return a red-only values
    6. return fixed4(colCameraDepth.r,0,0,1);
    Near clipping plane is 0.01 and far is 50
    When the object is at the near clipping plane depth value is 0
    When the object is at the far clipping plane depth value is 1
    When moved to mid-way I expect to be 0.5 but it is 0.6
    1/4 of the way: 0.33
    3/4: 0.82

    Any idea what I am missing here?
    Any help is very welcome.

    Cheers,
    Doruk
     
    Last edited: Apr 16, 2020
  2. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,263
    How are you measuring these values? Perhaps your render texture in Gamma (sRGB) color space rather than Linear.
     
  3. dorukeker

    dorukeker

    Joined:
    Dec 6, 2016
    Posts:
    37
    The color space of the project was Gamma indeed. I switched it to Linear. and the values changed as below:
    Near clipping: 0
    Far clipping: 1
    Half way: 0.8
    1/4: 0.6
    3/4: 0.9

    The Color Format of the render texture is DEPTH_AUTO. How do you change the color space of a render texture?
    Did some quick Googling on this just now but could not find an answer :)
     
  4. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,263
    The RenderTextureReadWrite parameter determines the RT's color space, that may do the trick

    Code (CSharp):
    1. RenderTexture rt = new RenderTexture(512, 512, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
     
  5. dorukeker

    dorukeker

    Joined:
    Dec 6, 2016
    Posts:
    37
    Tried the below and still similarly strange values

    Code (CSharp):
    1. renderTex = new RenderTexture(1920 , 1080, 0, RenderTextureFormat.Depth, RenderTextureReadWrite.Linear);
     
  6. dorukeker

    dorukeker

    Joined:
    Dec 6, 2016
    Posts:
    37
    Got another input. I skip all the calculation in the shader and return
    Code (CSharp):
    1. return fixed4(0.5,0,0,1);
    Meaning it will draw 0.5 red everywhere. But when I measure this value; it is also not 0.5.
    I might be missin something else then the color values linearity.

    BTW the color space of the project is linear.
     
  7. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,263
    How are you measuring it? If you have something like tonemapping and lighting enabled, color-picking the screen won't get you the exact value the shader outputs. In that case, it's best to step through the Frame Debugger to the point where your shader is rendered, and color pick then
     
  8. dorukeker

    dorukeker

    Joined:
    Dec 6, 2016
    Posts:
    37
    I am measuring using the default Mac Digital Colur Meter. And no there are no light or effects or etc in the scene.
     
    Last edited: Apr 16, 2020
  9. dorukeker

    dorukeker

    Joined:
    Dec 6, 2016
    Posts:
    37
    Hello All,
    We have a working setup now. Thanks for all the help. If someone finds this thread here is the working setup for us.

    - The project color space is Gamma
    - We are using colCameraDepth = Linear01Depth(colCameraDepth); to linearise the depth values. So the shader code from the first example is used as is.
    - The important thing the near and far clipping planes and of all the cameras in the scene need to be the same. That was the trick we did not realise.

    My assumption, when calculating the depth values; shader does not only take the values of the clipping planes of the depth camera into account; but also the other cameras.

    Probably with more understanding of the entire shader realm we would have a better solution and explanation. But here is what works for us.

    I hope this helps someone else as well.
    Cheers,
    Doruk