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

Question How to get depth buffer of an orthographic camera

Discussion in 'Shader Graph' started by Fakkau, May 7, 2020.

  1. Fakkau

    Fakkau

    Joined:
    Mar 11, 2013
    Posts:
    22
    I have a shader that works fine in perspective view, but if I set my camera to orthographic, it no longer works. This is because my Graph uses the Scene Depth node, which, I believe, assumes you're using a perspective camera and thus doesn't give me the correct depth buffer value when my camera is set to orthographic.

    TLDR How do I get the depth buffer of an orthographic camera in Shader Graph?




    (Documentation of the Scene Depth node: https://docs.unity3d.com/Packages/com.unity.shadergraph@7.3/manual/Scene-Depth-Node.html)
     
  2. alexanderameye

    alexanderameye

    Joined:
    Nov 27, 2013
    Posts:
    1,383
  3. Fakkau

    Fakkau

    Joined:
    Mar 11, 2013
    Posts:
    22
    Hmmm ok, so I took a look at that article and it helped (thanks!), but the effect isn't quite perfect yet. I'll try to give some more information on what I'm trying to accomplish and what isn't working.

    I'm making a shader that renders the intersection between a sphere and other geometry. I took the Graph from this thread: ( https://forum.unity.com/threads/highlight-intersection-shader.731177/ ), which works fine in Perspective camera mode.

    result.png
    The (otherwise invisible) sphere leaves a red mark wherever it intersects with geometry

    But, I want to use an orthographic camera, which doesn't render the sphere at all because of the Scene Depth node. I took the Graph from the thread I mentioned and tried to replace the Scene Depth node with what I read in the article from the reply above. The sphere now renders in orthographic mode (yay!), but when the object is too far from the camera view's center, it gets partially cut off. (awww...) Note that I'm not "zooming out" the camera; if the camera moves to the left or right for instance, the object gradually gets cut off more and more as it gets closer to the edge of the screen.

    wallMarkFull.png wallMarkCut.png
    First image is when the camera is centered above the red circle. Second image is after the camera moves to the left

    I'm not sure why it's getting cut off. (I'm new to shaders and Shader Graph and am just throwing some nodes together and trying to learn how everything works as I go along.) Below is the Graph that I have right now. The main thing I'm unsure about, is the Lerp node at the far right of the "Orthographic Depth" group, as I didn't fully understand the article when it said "we also likely need to Lerp [the result] with the near and far clip planes to have the depth in terms of View space units"

    orthoGraph.png

    Sorry for the wall of text! But if anyone has tips on how to possibly fix this, I would much appreciate it! :)
     
  4. Fakkau

    Fakkau

    Joined:
    Mar 11, 2013
    Posts:
    22