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

Question [URP] How to implement waterline...

Discussion in 'General Graphics' started by JasontreksInteractive, Sep 11, 2023.

  1. JasontreksInteractive

    JasontreksInteractive

    Joined:
    Mar 7, 2023
    Posts:
    5
    I implemented a water plane, and I have to implement Waterline for different post-processing effects in both areas as the camera is submerged. But strangely enough, there's no Waterline tutorial for Unity anywhere.
    The way I'm thinking is, I'm going to create a camera that only renders water planes, and I'm going to create render textures. In the render texture, the pixel overlapping the Near Plane is rendered as 0(->boundary), the pixel above the boundary is rendered as (1, 0, 0), and the pixel under the boundary is rendered as (0, 1, 0), and this texture is taken from the post-processing shader for use. The post-processing shaders will then be able to compare each pixel and give a different effect.(Fog, color, etc.)
    But I can't think of any way to make this implementation possible with code...

    How to detect a collision between the water plane and the camera's Near Plane? And there are already shaders and materials used in water planes, and it is impossible to use more than one material. Does the way I came up with make sense?

    And even if i figured out the pixel coordinates of the boundary where the water plane and the camera's near plane overlap, there is still a problem. When a pixel shader is called, it is not possible to know the result of the operation of other pixels in advance. That is, there is no way to find out which pixels are above or below Waterline. I don't think there's a way in the shader like "Fill in Color" like Paint.

    Please let me know if there is any other way to implement it...Or I would appreciate it if you could let me know in detail if I could realize this method that I thought of...

    upload_2023-9-11_22-0-3.png
    upload_2023-9-11_22-4-12.png
    -> Render Texture
    -> Use In Post Process Shader
     

    Attached Files:

  2. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    547
    Don't fully understand all of your questions but you could use the stencil buffer to do this. If you need the information very early in the frame, render the water surface after the depth prepass (but only store stencil bits at this point).
     
  3. JasontreksInteractive

    JasontreksInteractive

    Joined:
    Mar 7, 2023
    Posts:
    5
    Thank you for your reply. Stencil buffer, yes. I've heard of that, too. A stencil buffer is known as a buffer that stores the order in which each pixel is drawn within the pipeline. I have a question about this.

    What and what should I compare in the stencil buffer?
    Also, what should I do with the shader to compare stencil values?

    I couldn't find any tutorials on how to use the stencil buffer either. I'm sorry. I resent the limits of my knowledge...
     
  4. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    547
    No, it has nothing to do with the order.

    It's an 8 bit byte value that you can use in various different ways. For example, you could configure the stencil unit such that it sets the stencil value to 1 for all the pixel that pass the depth test when rendering the water plane. Then in the post-processing pass, you can configure it such that all pixels that don't have a stencil value of 1 fail the stencil test and are discarded (it's similar to a depth test).

    Just google for it, there are many tutorials which explain it better than I can.
     
  5. JasontreksInteractive

    JasontreksInteractive

    Joined:
    Mar 7, 2023
    Posts:
    5
    Okay, from what I've learned currently, Stencil Buffer can be seen as a way of handling areas where pixels cross.
    But no matter how hard I think about it, I can't think of a way to express Waterline with Stencil Buffer. If the water is a cube, not a plane, and the intersection of the water cube with the plane filling the background, I think it would be possible if it was filmed outside the water. Probably draw it like this.
    upload_2023-9-13_6-0-31.png
    But the scene where the waterline is represented is any point in the water plane. So I need to test the Stencil Buffer on the plane to find the area of intersection with something. But I think this is all i can see no matter what i dip in the plane while looking at the plane diagonally.

    upload_2023-9-13_6-11-34.png
    Can the Stencil Buffer indicate that two different areas around the interface where the water was cut off by the camera's Near Plane? And when I look at the implementation that I've discovered, the question of whether Stencil Buffer is really the answer doesn't go away.
    https://twitter.com/i/status/1411339780009385987
     

    Attached Files:

    Last edited: Sep 12, 2023
  6. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    547
    Ok, now I finally understand what you were asking - sorry.

    Yeah, that's not easy to achieve. You have an answer in the thread you posted, but as you can see, it took a lot of effort.

    Allegedly, there is a waterline sample for HDRP but I haven't checked. If yes, you should be able to look at the source and see how Unity does it.
     
  7. kripto289

    kripto289

    Joined:
    Feb 21, 2013
    Posts:
    466
    For my KWS water system I use this method
    1) Render your mesh to texture with "Cull Off" and "VFACE" feature.
    return saturate(-vface);
    You will have mask with any volumetric mesh (cube/sphere,etc) like this
    upload_2023-10-7_18-41-31.png
     
    adamgolden likes this.