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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Using _CameraDepthTexture with Orthographic Camera

Discussion in 'Shaders' started by GreatWall, Mar 6, 2019.

  1. GreatWall

    GreatWall

    Joined:
    Aug 7, 2014
    Posts:
    57
    Hi Guy
    I am working on a water shader(brought from asset store and do some modify).It works well with perspective camera.But when using Orthographic Camera,It seems can't calculate screen depth right. the attachments are shader code and images..
     

    Attached Files:

  2. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    Working on a similar thing! Your code is linearly sampling the depth texture, but with the orthographic camera the coordinates are already linear.
    You can see my code snippet here in this thread's OP. It should solve your issue. The key is lerping between the sample straight from the depth texture and a linear remapping of it, depending on the camera's render mode.
    https://forum.unity.com/threads/inconsistent-unity_reversed_z.639433/
    The CheckPerspective code is simply:
    Code (CSharp):
    1. // Check if the camera is perspective.
    2. // (returns 1.0 when orthographic)
    3. float CheckPerspective(float x)
    4. {
    5.     return lerp(x, 1.0, unity_OrthoParams.w);
    6. }
     
  3. GreatWall

    GreatWall

    Joined:
    Aug 7, 2014
    Posts:
    57
    I replace
    Code (CSharp):
    1. float sceneZ = max(0,LinearEyeDepth (UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)))) - _ProjectionParams.g);
    with
    Code (CSharp):
    1. float sceneZ = max(0,UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos))) - _ProjectionParams.g);
    But the result not change.
     
  4. Alber20

    Alber20

    Joined:
    Mar 7, 2019
    Posts:
    2
    Depth map estimation is divided into three main parts:

    The measured values were obtained by stereo vision and combined with the prior depth

    Depth map propagation from frame to frame

    Regularization and removal of outliers
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,248
    What ... 100% of that was nonsense.



    The depth in orthographic mode is linear, like @Yandalf mentioned. It’s a zero to one value that goes from the near to far clip planes.

    float sceneZ = lerp(_ProjectionParams.y, _ProjectionParams.z, UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, i.projPos)));
     
  6. GreatWall

    GreatWall

    Joined:
    Aug 7, 2014
    Posts:
    57
    Solved,thanks
     
  7. Anjimo

    Anjimo

    Joined:
    Apr 22, 2014
    Posts:
    14
    could you pleas share the final depth shader? Still can't solve this problem(