Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to convert FX/Water (Simple) to be translucent

Discussion in 'Shaders' started by JoeStrout, Oct 1, 2011.

  1. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I wanted a dynamic water texture that was translucent. Note that I didn't need any fancy reflections or refractions; I just want to be able to see through the water a bit. I found some other (old) posts here and there on the net seeming to ask for the same thing, so thought I'd post my solution for posterity. Here's what I did.

    1. Copied the FX/Water (Simple) shader from the Standard Asests, and changed the name of the shader in the first line to "Fx/Water (Transparent)".
    2. Changed the default _horizonColor in line 3 to have an alpha of 0.9 rather than 0.
    3. Changed the "Tags" on line 60 to:
    Code (csharp):
    1.     Tags { "Queue"="Transparent" "RenderType"="Transparent" }
    4. Added one more line right after that (i.e. inserted at line 61):
    Code (csharp):
    1.     Blend SrcAlpha OneMinusSrcAlpha
    That last bit is pretty critical; without a Blend specifier, the alpha on your shader is ignored as far as I can tell.

    With these changes, I know have a beautiful shimmering water texture, and you can see through it to the stuff underneath. It's not highly realistic, as the stuff underneath doesn't shimmer and dance as it should, but it looks good enough for my purposes.

    Enjoy,
    - Joe
     
    ilikesnow likes this.
  2. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,608
    Pics to show differences?
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Differences in the code, or in the result? The only differences in the code are as described above, and the result looks exactly the same, unless you have something behind/under the water... in which case, you can see it a little (how much depends on the alpha you pick for your horizon color).
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    One other enhancement to this shader worth pointing out: as it comes in the standard assets, it doesn't work from the other side. To make it so you can see the water surface from both sides, you need to make two more changes:

    1. Add a "Cull Off" line right after the "Blend" line. This tells the renderer to draw it even when the triangles are facing away from the camera.

    2. Change the line that calculates "fresnel" in the frag() function, from:
    Code (csharp):
    1.     half fresnel = dot( i.viewDir, bump );
    to
    Code (csharp):
    1.     half fresnel = abs( dot( i.viewDir, bump ) );
    In other words, slap an abs() (i.e. absolute value) around the dot product. Without this, you won't get any of the cool bump mapping on the backside of the surface.

    Cheers,
    - Joe
     
  5. vARDAmir88

    vARDAmir88

    Joined:
    Sep 9, 2015
    Posts:
    36
    What a great stuff!
    Using it in Unity 2018.1.9f2 and it works well.
    Thanks a lot!
     
    JoeStrout likes this.