Search Unity

2D Water Shader

Discussion in 'Shaders' started by MaT227, May 13, 2013.

  1. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    Hi,
    I am trying to build a 2D Water shader and I am using Unity 4.
    Here is an example of what I am trying to reproduce :
    Limbo water shader
    Are there any good tutorial or whitepaper about creating such effect blur and displacement ? I am not talking about waves or something but only the depth of the water.
    Is it a Camera shader or an object shader ?

    Thanks a lot !
     
    Last edited: May 13, 2013
  2. Lulucifer

    Lulucifer

    Joined:
    Jul 8, 2012
    Posts:
    358
    I guess it is a camera shader
     
  3. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    Not a good news. I am simulating 2D water waves on a plane and I need to apply this shader on my object.
     
  4. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,878
    In order to make the blur and ripples of the water, you need to grab the backgrround texture. So, you need unity pro.
    If you have unity pro, then check out my image effects and water shaders in the assetstore which will teach you how to get the effect you want.
     
  5. Duusty

    Duusty

    Joined:
    May 14, 2013
    Posts:
    56
    could send you this shader if you want to...

    just the screen space reflections are bugged as im a bit confused how to statically offset the transformation matrices for those reflections...

    but the shader provides, alpha masks (for scrolling waves), you can color it per texture, you can add a distortion normal map..

    and it's dynamic water (you can see this on those small ripples sometimes) where you just need to define in code where on the x-Axis and how strong you want to generate an impulse

    this script can also modify the vertex colors of the water to simulate depth


    (yeah reflection and refraction is only aviable in pro like aubergine said)
     
  6. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    Yes I have Unity Pro. I saw that the best way to achieve such effect is to use Grabpass in the shader. But there are two problems with this system :

    - It seems that Grabpass is quite heavy for mobile as said here Using Grabpass IOS/Android devices . But as I am learning shader, I don't know any other system to create refraction.
    - There is an issue with foreground objects, they are refracted too. There is a solution here GPU Gems - Refractions . But is this solution available ?

    Another interesting link : Unite 11 - Shadowgun The fluid part is at about 30:00.

    @TehReason I would be happy to look at your shader even if this is not the effect I am trying to create.
     
    Last edited: May 16, 2013
  7. Duusty

    Duusty

    Joined:
    May 14, 2013
    Posts:
    56
    if you're not to lazy.. you could just take a screenshot of everything you would like to get refracted without the object.

    And after the just disort the uv's for that screenshot. that you're gonna use just like the grabpass texture.. should be a good way for small areas.. or even for bigger ones just zoom out take a bigger screen and scale this one down.. that way you can even get a simple blurred refraction
     
  8. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    This is a good idea, but there are dynamic objects being refracted.
     
  9. Duusty

    Duusty

    Joined:
    May 14, 2013
    Posts:
    56
    Mhm maybe place a second ortogprahic camera with a render texture for those objects, and maybe only render those dynamic ones per layer to that texture that way you wont need to render the whole scene twice only the dynamic ones (I have no clue how much impact this will have to mobile hardware)

    I have other theories up my mind, by bulding up a trigger around your water and if a dynamic object enter that one you just switch the shader for the entering object, to a vertex modifying or just a default distorting pixel shader that has the same distortion properties as the water... (maybe you need to this in world space)
    Problems you'll get with that is that the rest of the scene is still statically rendered to a Texture so you need to place the distorted texture in the background of those dynamic objects
     
  10. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    I think that RenderTextures are quite heavy on mobiles. They are good theory but I want to render everything in the shader. I really like your first idea with the "capture".
    I looked at some shader but they all use Grabpass but everybody says that it's heavy and there's the problem of the foreground.
     
  11. Duusty

    Duusty

    Joined:
    May 14, 2013
    Posts:
    56
    Thats the reason why i told you my second approach
     
  12. esco1979

    esco1979

    Joined:
    Mar 18, 2014
    Posts:
    138
    Does anyone have the code for the wavy reflective water effect that is in the video above? The person who created it and offered to share the code doesn't seem to have logged on since June.
     
  13. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Absolutely not. We use them extensively on mobile (devices 3GS and up). You should be able to use a couple and still remain at 60fps on most devices. It's not really the rendering to a texture that's expensive, it's what you're rendering to a texture.

    They are also much, much faster than grabpass.
     
  14. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    As Hippocoder says, Rendertextures are relatively cheap in terms of speed. The two things to keep in mind are:

    1. Memory, which can be pretty significant for full-screen rendertextures on high resolution devices like the iPad 3+. You can usually get away with lower resolutions for at least some of your targets.
    2. Performance is not due to the rendertexture itself, but the process of rendering to it. This is pretty much the same as rendering directly to the screen, so the possible culprits are overdraw, draw calls, and sometimes vertex count.

    GrabPass is murder on mobile and there is never a good reason to use it.
     
  15. Marco-Sperling

    Marco-Sperling

    Joined:
    Mar 5, 2012
    Posts:
    620
    Why is GrabPass so bad? Isn't using it the same as using a rendertexture essentially? There has to be some serious overhead to it then and I wonder where it stems from.
     
  16. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    I believe it's a result of reading from the back buffer, which is a really slow operation on most mobile GPUs.