Search Unity

Something wrong with _CameraDepthTexture in SGraph

Discussion in 'Shaders' started by DAPPSD, Mar 18, 2019.

  1. DAPPSD

    DAPPSD

    Joined:
    Apr 26, 2018
    Posts:
    47
    UPDATE: (new problem)
    I exposed the color properties and it seems to fix the issue that caused the plane to not be blue at all... and back to the main problem (that deep places on the plane aren't colored with dark blues and shallow places with ligh blues) and it's now seems to me like it was all along a problem with the 'Screen Position' node rather than the camera depth thing, because the 'A' of the 'Screen Position' node returns 0 no matter what I do...

    Here's the shader:

    Here's the properties: (note that I exposed the color properties, because when these properties are hidden the plane gets all black)

    And here's teh result of the shader:


    I don't know why it doesn't work.. in the preview it's clearly working and detecting the depth... :/
     
    Last edited: Mar 18, 2019
  2. DAPPSD

    DAPPSD

    Joined:
    Apr 26, 2018
    Posts:
    47
    Updated the post... still having problems :(
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    You can't just sample the depth texture directly and expect to get useful data out of it. It's in a special format that has no obvious relationship to the stuff that's on screen. Specifically it's in a reversed, non-linear 0.0 to 1.0 range. 1.0 is at the near plane, 0.0 is at the far plane, and 0.5 is not remotely close half way between the two. The data you need to convert the depth texture into something useful is available, but last time I tried to access them resulted in shader errors due to redefining the properties.

    It's also a recording of the screen depth, so it's a distance from the camera along the forward view vector, not the z height of the screen. You need to compare it against some other value to know how "deep" it is, usually the against the pixel depth of the current object.

    The best solution is to use the built in Scene Depth node and the Camera node's Far Plane value. Here's a version that also corrects for view direction.
    https://forum.unity.com/threads/how...section-of-other-objects.638872/#post-4317604
     
  4. DAPPSD

    DAPPSD

    Joined:
    Apr 26, 2018
    Posts:
    47
    I'm not that good with shaders so I'm trying to recreate some regular shaders in shader graph to get good with shader graph.. I'm trying to recreate this shader: https://roystan.net/articles/toon-water.html

    I don't really understand what the shader you linked does but I copied the nodes to my shader and it shows everything in pink...
    I think for some reason the 'scene depth' node doesn't work or something, because I tried inserting the camera depth texture to it and it showed only pink in my shader too... (I'm using LWRP, don't know if that has anything to do with it..)
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    The output of the subtract node is the same as that links "depthDifference" variable (though the inputs into the subtract are inverted from that tutorial). Delete everything else.
     
  6. DAPPSD

    DAPPSD

    Joined:
    Apr 26, 2018
    Posts:
    47
    But if I flip the input of subtract node I get everything in black (because for some reason the 'A' of the 'Screen Position' node is always zero...

    By doing this:

    I'm trying to achieve this:
    Code (CSharp):
    1. float existingDepth01 = tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.screenPosition)).r;
    2. float existingDepthLinear = LinearEyeDepth(existingDepth01);
    3.  
    4. float depthDifference = existingDepthLinear - i.screenPosition.w;
     
    Last edited: Mar 19, 2019
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    Yes, look at the nodes in the graph I linked.

    The screen position node and screenPosition variable in that tutorial are not equivalent. The .w in the tutorial shader is the linear depth, which is the same as the view space z, which is what I use in my graph.

    You cannot use the _CameraDepthTexture directly in a Shader Graph and get anything useable. You have to use the Scene Depth node and the Camera Far Plane node.
     
  8. DAPPSD

    DAPPSD

    Joined:
    Apr 26, 2018
    Posts:
    47
    I don't know why but the shader turns all pink when I try to do it like in the image you linked...
    Here's the shader:
     
  9. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    Is the shader set to Opaque or Transparent? I don't think the SceneDepth works if it's opaque, even though the depth texture is available for forward rendering and the LWRP.
     
  10. DAPPSD

    DAPPSD

    Joined:
    Apr 26, 2018
    Posts:
    47
    It's set to transparent...
     
  11. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    Actually, what version of the LWRP are you using? Your Scene Depth node has a "Sampling Mode". I've seen that in the documentation, but never seen it in any of the versions of the LWRP I've tried. Set that to Linear (instead of Linear 01) and you can remove the Camera and multiply nodes.

    As for why it's pink, I have no idea. You'd have to look in your log to see if any warnings on your shader have popped up.
     
  12. DAPPSD

    DAPPSD

    Joined:
    Apr 26, 2018
    Posts:
    47
    You are right, there was an update in the package manager, I'm now on version 4.10.0 (I don't remember the previous version, I think 4.08.0 or 4.0.8 or something like that), and even after updating it still turned everything pink but after playing around with the shader you linked, it finally somehow works (the same node as in the image you linked)

    Thanks for the help!
     
    Last edited: Mar 19, 2019