Search Unity

Adding sharp intersection lines for a Water Shader

Discussion in 'Shaders' started by dann96, Jun 25, 2017.

  1. dann96

    dann96

    Joined:
    Dec 6, 2014
    Posts:
    113
    I'm currently in the process of developing a water shader that detects objects and creates a sort of "foam" edging around said objects that the water intersects with. What would the code look like to implement this? upload_2017-6-25_18-37-32.png
     
  2. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,493
    There is 4 ways to do it:
    - The easy way with quad animated at the surface of water at the point of impacts
    - the sloppy way, using a shader that compare depth of water with depth of scene, everything in a range between surface and a given distance is a proportional gradient to that distance, then use that as a mask for a foam texture.
    - The hard way, you project the objects shape as a mesh (like doom3 shadow) and extrude it, then use that as a mask for a foam texture.
    - The old way, render a quad at the point of impacts, render the object as a silouhette from the point of view of the impact, scale a bit, use that as a mask to a foam texture.

    Have fun
     
  3. dann96

    dann96

    Joined:
    Dec 6, 2014
    Posts:
    113
    I was thinking more of integrating the method as a shader feature for the water shader, I guess I'll take the sloppy way.

    Is there any way I could get some code on that, then? I'm afraid I'm still a bit new with shaders.
     
  4. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,493
    You can do it with shader forge and probably amplify shader stuff.
     
  5. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    Also, problem with that method is you will only get foam where the water actually overlaps the object, so while this can work alright for a shoreline, a floating object will have the foam be cut-off at the sides of the object since the depth value there will be whatever is far in the background.