1. All Unity Pro perpetual license customers: your special offer to subscribe is ready! Click here.
  2. We're running a survey about the usage of our graphics pipeline, help give us your feedback.
  3. Unity 5.6 beta is now available for download.
  4. Unity 5.5 is now released.
  5. Check out all the fixes for 5.5 in patch releases 1 & 2.
  6. Enter the Google Play Indie Games Contest in Europe. Read more about it here.
  7. Get prepared for the Tizen Mobile App Incentive Program! Read more about the upcoming program here.
  8. Enter the Microsoft Developer Challenge for a chance to win prizes. Read more about it here.

2d Water

Discussion in 'Scripting' started by xcube, Jun 30, 2012.

  1. xcube

    xcube

    Joined:
    Nov 27, 2011
    Messages:
    34
    Hello



    Which means you can get this water?
    How would you do it?
     
  2. jister

    jister

    Joined:
    Oct 9, 2009
    Messages:
    1,438
    the waves aren't that hard i think, you could get that with some peril noise script, the collision on the other hand... i would start with looking into cloth collision or fluid collision...?
     
  3. xcube

    xcube

    Joined:
    Nov 27, 2011
    Messages:
    34
    Thank you for your help!
    I think the fluid and the cloth is too expensive for ios. (Is this true?)

    I need to make simply lines as on as on video.
    RageSpline for surface creation can use? But than to do waves?
     
  4. jister

    jister

    Joined:
    Oct 9, 2009
    Messages:
    1,438
    the waves i would do with planes and a peril noise script on it, then just put 3 planes at different height.
    and well yes the collision i think i would have a look into vertex collision, how i could collide mesh colliders with verts.
     
  5. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Messages:
    18,322
    you really don't need complicated stuff like perlin noise or anything like that. Just move verts up and down nicely and you're set. You can even do this in your modelling package and export as a low res boned wave mesh. Keep it simple.
     
  6. xcube

    xcube

    Joined:
    Nov 27, 2011
    Messages:
    34
    Thanks to all !
     
  7. Brian-Stone

    Brian-Stone

    Joined:
    Jun 9, 2012
    Messages:
    221
    The classic solution to water ripples is to treat each vertex or pixel as a spring that is connected to the other vertices next to it.

    I've coded a C# mono class that demonstrates the basic algorithm. To use it, just create an new project and attach the script to the main camera. Run the script, then position the scene view on the XY plane and zoom out until you can see the entire line of spheres. Select any sphere and drag it up and down using the Translation widget to play with the simulation.

    $WaterDemo.jpg

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Wave : MonoBehaviour
    6. {
    7.     static int size = 200; // Number of vertices
    8.     static float velocityDamping = 0.999999f; // Proprotional velocity damping, must be less than or equal to 1.
    9.     static float timeScale = 50f;
    10.    
    11.     float[] newHeight = new float[size];
    12.     float[] velocity = new float[size];
    13.    
    14.     GameObject[] vertex = new GameObject[size];
    15.    
    16.     void Start ()
    17.     {
    18.         // we'll use spheres to represent each vertex for demonstration purposes
    19.         for(int i=0; i<size; i++)
    20.         {
    21.             vertex[i] = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    22.             vertex[i].transform.position = new Vector3(i - size/2, 0, 0);
    23.         }
    24.     }
    25.    
    26.     void Update ()
    27.     {
    28.         // Water tension is simulated by a simple linear convolution over the height field.
    29.         for(int i=1; i<size-1; i++)
    30.         {
    31.             int j=i-1;
    32.             int k=i+1;
    33.             newHeight[i] = (vertex[i].transform.position.y + vertex[j].transform.position.y + vertex[k].transform.position.y) / 3.0f;
    34.         }
    35.        
    36.         // Velocity and height are updated...
    37.         for(int i=0; i<size; i++)
    38.         {
    39.             // update velocity and height
    40.             velocity[i] = (velocity[i] + (newHeight[i] - vertex[i].transform.position.y)) * velocityDamping;
    41.            
    42.             float timeFactor = Time.deltaTime * timeScale;
    43.             if (timeFactor > 1f) timeFactor = 1f;
    44.            
    45.             newHeight[i] += velocity[i] * timeFactor;
    46.            
    47.             // update the vertex position
    48.             Vector3 newPosition = new Vector3(
    49.                 vertex[i].transform.position.x,
    50.                 newHeight[i],
    51.                 vertex[i].transform.position.z);
    52.             vertex[i].transform.position = newPosition;
    53.         }
    54.     }
    55. }
    56.  
     
  8. xcube

    xcube

    Joined:
    Nov 27, 2011
    Messages:
    34
    Cool !
    It works.
    Thank you very much !!!
     
  9. TheValar

    TheValar

    Joined:
    Nov 12, 2012
    Messages:
    684
    I went ahead and used Brian Stone's posted code as a base and implemented it using a procedural mesh. The water is made out of a bunch of rectangles and instead of manipulating the translate position of a sphere I used the same algorithm to update the vertex positions of the mesh. You can see the results here: http://triton.towson.edu/~mholtz2/WebPlayer/WaterDemo.html

    Ignore the bad texture mapping. This is my first time coding with meshes and the whole UV mapping/shader stuff is way beyond me at this point.

    Planning to add some splashes and stuff later
     
  10. mww565

    mww565

    Joined:
    Jan 28, 2013
    Messages:
    1
    Hey Valar,

    Great implementation, are you able to post up the source?
     
  11. faraz

    faraz

    Joined:
    Aug 4, 2014
    Messages:
    44