Search Unity

Question Depth Mask for Ocean

Discussion in 'Shader Graph' started by Horus_Sungod42, Mar 30, 2020.

  1. Horus_Sungod42

    Horus_Sungod42

    Joined:
    Oct 30, 2014
    Posts:
    99
    Hello, I am trying to remake an ocean shader I previously made in Unreal, but am unable to reach the results in unity
    wata.jpg
    Its main complex features is being able to make a depth gradient between the surface of the water and the depth of an object below. That mask is used to fade and color the water, pretty classic stuff for an ocean shader.

    Here's the unreal and unity parts of the graph that make this happen. Also, I have a subgraph to reproduce the pixel depth node that in unreal (I did not make that subgraph, I got it from an FX pack)
    za1.jpg

    Instead of producing the soft gradient that goes down where the object intersect then sink into the water, it only produces a flickering pure white/black mask, depending on my camera distance and angle.

    The only change between the two is the Water Height vector is in the green channel, since it is that axis, in Unity, that is vertical.

    The master node is already set to transparent, since I think that's important for a bunch of depth-related nodes. I did not change it's depth test.

    Am I missing something? Is the Pixel Depth subgraph correct? Should I swizzle other parts of the graph, to switch the green and blue channels?

    Thank you
     
  2. Elizabeth_LeGros

    Elizabeth_LeGros

    Unity Technologies

    Joined:
    Jan 7, 2020
    Posts:
    50
    I think you should be able to just get object position in view space and get the z component from that to get depth rather than screen position
     
  3. alexanderameye

    alexanderameye

    Joined:
    Nov 27, 2013
    Posts:
    1,383
    The following subgraph can be used for object depth (pixel depth).



    Then using it like this will give a depth fade effect.



    This output can then be divided by a property 'water depth' and after saturating (clamp01) you can use it to lerp between colors or whatever you want to do.

    Also be sure that you are generating the depth texture in your render pipeline settings.
     
  4. Horus_Sungod42

    Horus_Sungod42

    Joined:
    Oct 30, 2014
    Posts:
    99
    Cool, thanks, I'll try that right away!
     
  5. alexanderameye

    alexanderameye

    Joined:
    Nov 27, 2013
    Posts:
    1,383
    Ah, I've never done that in HDRP so I'm not sure but I think so, you just need the render pipeline to generate a depth texture.
     
    Horus_Sungod42 likes this.
  6. Horus_Sungod42

    Horus_Sungod42

    Joined:
    Oct 30, 2014
    Posts:
    99
    It seems to work very well, thank you!
     

    Attached Files:

    • za2.jpg
      za2.jpg
      File size:
      387.4 KB
      Views:
      604
  7. alexanderameye

    alexanderameye

    Joined:
    Nov 27, 2013
    Posts:
    1,383
    Neat! Glad it works :)