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

Flowing Water

Discussion in 'Scripting' started by mtewks, Aug 5, 2011.

  1. mtewks

    mtewks

    Joined:
    Jun 10, 2011
    Posts:
    82
    I'm looking for either a shader or a script to get a flowing water effect.
    Just something simple flowing on a flat plane or moves the texture on the plane.

    If anyone knows of a script, shader, or a place where I can learn more it be appreciated.
     
  2. JayShades

    JayShades

    Joined:
    Mar 13, 2011
    Posts:
    125
    Have you looked at the Unity Standard water scripts lol? That should get you started :D
     
  3. Bikebreck

    Bikebreck

    Joined:
    Aug 4, 2010
    Posts:
    112
  4. mtewks

    mtewks

    Joined:
    Jun 10, 2011
    Posts:
    82
  5. mtewks

    mtewks

    Joined:
    Jun 10, 2011
    Posts:
    82
    Okay the scene doesn't work for IOS build so that doesn't help. =\
     
  6. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    the concept of flowing water is actually pretty easy. At least until you get to motion in the water, and even that is not that bad.

    Assume you have a texture attached to a water surface and you wanted to make it look like it was moving in one direction... use the Material.mainTextureOffset to adjust how the texture lies on the surface.

    http://unity3d.com/support/documentation/ScriptReference/Material-mainTextureOffset.html

    use Mathf.Repeat(value + nextoffset * Time.deltaTime, 1.0). Keep adding the nextoffset and it repeats the texture over and over.

    Something a little more complex is adjusting the vertices as you go. In the case of a waterfall or some such, you may want the water to sway slightly. You could adjust the vertices to make the water move while it is flowing.

    A big part of any water falling are the key elements of the splashing at the bottom and the top where the water breaks the natural tendency to continue on the same path. This is easily done with well placed particles. The last effect that you need will be the impact effect on the base material. This is done with a similar offsetting effect but you will need a second set of texture verts in the shader to handle alpha. This effect would look like a bright froth coming from under the particles and cascades over the water that it is on, eventually fading out.

    Basic effects primer:

    If you had a strip of quads each one 10x10 and textured the same. When you animated the texture over it, the water would look like it is traveling at the same speed across it's surface. However, when you change the distance between vertices, without changing the texture vertices you suddenly get an effect where the water passes more quickly over some areas but not others. This effect is incredibly important when creating things like streams or streams and such and hugely important when creating effects for lava.

    Do not discount the importance of normals and parallax mapping in water. Some of the best effects I have ever seen are derived from a clear surface which gives off specular effects from parallax mapping. Imagine if you will a concrete surface. Down a center section the concrete is molded and looks damp. Put a simple water effect over the damp surface with a offsetting parallax map on it. It gives off specular effects which make it look like the water is crystal clear and moving along the concrete to a drain. To heighten this sense, we use the second texture vertices to make the edges clear. This means that you will not have a seam where the water mesh meets the concrete.

    All of these effects are completely doable in Unity Free... Just in case any one wanted to know. ;)
     
  7. mtewks

    mtewks

    Joined:
    Jun 10, 2011
    Posts:
    82
    What about if I'm doing this on an object that's not a complete flat plane? Like it's a waterfall then the outgoing river, followed by the a steep dip downwards?

    What I'm currently working with from the artists and that might be affecting how good moving texture looks. I also had to scale the thing down
    by .01 if that has any effect.
     
  8. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    OK, any place that the water is moving faster at.. the texture vertices in that area are farther apart. Don't do a isntant shift.. like 5, 5, 25, 5.. that wouldn't look right, but more do a few verts of speed increase. 5, 5, 8, 12, 25. This stepped approach will net you smoother flowing water with a gradual build up.

    As far as a waterfall is concerned. There are many tricks. Most of them involve pretty advanced shaders to do the quick falling. However, you can accomplish it by still using the same basic formula above... the more vertical, the faster the water, the faster the water, the longer the texture verts. Couple this with a frothing shader that does a secondary effect from top to bottom that fads at the top and you will get some interesting effects.

    Lastly, of course I stress the importance of proper use of particle emitters to give the look and feel.

    Water in general is shiny so don't be afraid of lots of specular highlights. They will add to the realism.

    A flowing stream could easily be accomplished by using several effects. Clear water, Parallax mapping and particles where rocks and impedance points are. Lots of specular highlights on the parallax mapping gives the illusion that there is water there.

    Also a big part of a stream like that is that the water continuously flows around things.. So creating lanes of water are important here.
     
  9. mtewks

    mtewks

    Joined:
    Jun 10, 2011
    Posts:
    82
    Actually that sample code gave us exactly what we were looking for.
    We're not needing anything to fancy for this game. :3

    Also this is for iPhone and iPad so using some of those shaders isn't a good idea.
     
    Last edited: Aug 5, 2011