Search Unity

[SOLVED] Depth-based transparency with position displacement? (water shader)

Discussion in 'Shaders' started by smallwizard, Oct 6, 2019.

  1. smallwizard

    smallwizard

    Joined:
    Oct 6, 2019
    Posts:
    8
    I cannot seem to be able to figure out how to get this to work. I made a shader for water that distorts a mesh to create waves and applies depth-based transparency using scene depth (in the same shader). The problem is that the calculated transparency is always misplaced because of the position displacement for the waves.

    Here's a screenshot: ocean_shader.png

    The transparency is supposed to allow the little raft to not be covered by the water color, but the transparent area is shifted to the right (and also not the same exact shape, because of the distortion).

    The shader graph for the transparency:
    ocean_shader_graph.png

    The cause of the problem is (I think) that the scene depth is sampled at the position before displacement? I tried shifting it using the UV input to scene depth, but I don't know if it even makes sense to do that. I also thought it might work if I moved the position displacement into another shader, which would be applied before the texture/transparency, but I'm also not sure how to do that.

    The wave distortion is proving to be pretty annoying for a lot of things. I'm wondering if just calculating the geometry for the mesh in a C# script would solve a lot of the problems I have. Here's another problem I found: ocean_waves.png
    When looking into waves, it renders incorrectly, with parts of waves behind being rendered on top of closer waves. I'm more concerned with the depth transparency issue right now, though.

    My knowledge of shaders and everything else involved here is not great, so I apologize if these are easy fixes. I spent a decent amount of time trying, but I couldn't find any good examples or figure it out myself. Any help is appreciated.

    Edit: I think I solved it by converting the adjusted position to screen space and feeding that into the UV input for scene depth. fixed.png
     
    Last edited: Oct 7, 2019