Hey All,
I’m trying to follow this method for creating a neat 2D fluid effect: http://gamedev.tutsplus.com/tutorials/implementation/make-a-splash-with-2d-water-effects/ This is my first time with procedural mesh generation, so be gentle ![]()
My problem is creating the triangle strips from the vertices in the mesh I generated. I’m generating 200 points right now for the surface of the water, and an additional 200 to form the base. Obviously, manually assigning all of the triangles would be a major pain, but I can’t think of a better way of doing it.
Any and all help would be appreciated!
Here’s what I got so far:
using UnityEngine;
using System.Collections;
public class Wave_Mesh : MonoBehaviour
{
Mesh myMesh;
static int size = 200; // Number of vertices
public Vector3[] waveVerts = new Vector3[size];
Vector3[] wave_depthVerts = new Vector3[size];
Vector3[] totalVerts = new Vector3[size*2];
Vector2[] waveUVs = new Vector2[size];
int[] waveTris = new int[size-2];
Mesh waveMesh;
public float waterDepth = 10f;
static float velocityDamping = 0.999999f; // Proprotional velocity damping, must be less than or equal to 1.
static float timeScale = 50f;
float[] newHeight = new float[size];
float[] velocity = new float[size];
GameObject[] vertex = new GameObject[size];
public void SplashAtPoint(Vector3 p){
}
void Init(){ //create initial water mass
waveVerts = new Vector3[size];
for(int i = 0; i<size; i++){
if(i < size){
waveVerts[i] = new Vector3 (i - size/2f,transform.position.y,transform.position.z);
totalVerts[i] = waveVerts[i];
}
else{
wave_depthVerts[i-size] = new Vector3 (
waveVerts[i-size].x, waveVerts[i-size].y - waterDepth, waveVerts[i-size].z);
totalVerts[i] = wave_depthVerts[i-size];
}
}
Mesh mesh = new Mesh();
myMesh = mesh;
(GetComponent<MeshFilter>() as MeshFilter).mesh = mesh;
mesh.vertices = waveVerts;
mesh.uv = waveUVs;
mesh.triangles = waveTris;
waveMesh = mesh;
}
void UpdateWaves(){
//waveMesh.MarkDynamic();
// Water tension is simulated by a simple linear convolution over the height field.
for(int i=1; i<size-1; i++)
{
int j=i-1;
int k=i+1;
newHeight[i] = (waveVerts[i].y + waveVerts[j].y + waveVerts[k].y) / 3.0f;
}
// Velocity and height are updated...
for(int i=0; i<size; i++)
{
// update velocity and height
velocity[i] = (velocity[i] + (newHeight[i] - waveVerts[i].y)) * velocityDamping;
float timeFactor = Time.deltaTime * timeScale;
if (timeFactor > 1f) { timeFactor = 1f;}
newHeight[i] += velocity[i] * timeFactor;
// update the vertex position
Vector3 newPosition = new Vector3(
waveVerts[i].x,
newHeight[i],
waveVerts[i].z);
Vector3 new_wave_depthVerts = new Vector3(
newPosition.x,
newPosition.y - waterDepth,
newPosition.z
);
waveVerts[i] = newPosition;
wave_depthVerts[i] = new_wave_depthVerts;
}
for(int v = 1, t = 1; v < waveVerts.Length; v++, t += 3){
waveTris[t] = v;
waveTris[t + 1] = v + 1;
}
waveMesh.Clear();
waveMesh.vertices = waveVerts;
waveMesh.triangles = waveTris;
myMesh = waveMesh;
}
void Start ()
{
Init();
}
void Update ()
{
UpdateWaves();
}
void OnDrawGizmos(){
for (int i = 0; i < size*2; i++){
if(i < size)
Gizmos.color = Color.cyan;
else
Gizmos.color = Color.red;
Gizmos.DrawCube(totalVerts[i], new Vector3(0.3f,0.3f,0.3f));
}
}
}
And here’s another script I found floating on the forums (I’m using this as a base):
using UnityEngine;
using System.Collections;
public class Wave : MonoBehaviour
{
static int size = 200; // Number of vertices
static float velocityDamping = 0.999999f; // Proprotional velocity damping, must be less than or equal to 1.
static float timeScale = 50f;
float[] newHeight = new float[size];
float[] velocity = new float[size];
GameObject[] vertex = new GameObject[size];
void Start ()
{
// we'll use spheres to represent each vertex for demonstration purposes
for(int i=0; i<size; i++)
{
vertex[i] = GameObject.CreatePrimitive(PrimitiveType.Sphere);
vertex[i].transform.position = new Vector3(i - size/2, 0, 0);
}
}
void Update ()
{
// Water tension is simulated by a simple linear convolution over the height field.
for(int i=1; i<size-1; i++)
{
int j=i-1;
int k=i+1;
newHeight[i] = (vertex[i].transform.position.y + vertex[j].transform.position.y + vertex[k].transform.position.y) / 3.0f;
}
// Velocity and height are updated...
for(int i=0; i<size; i++)
{
// update velocity and height
velocity[i] = (velocity[i] + (newHeight[i] - vertex[i].transform.position.y)) * velocityDamping;
float timeFactor = Time.deltaTime * timeScale;
if (timeFactor > 1f) timeFactor = 1f;
newHeight[i] += velocity[i] * timeFactor;
// update the vertex position
Vector3 newPosition = new Vector3(
vertex[i].transform.position.x,
newHeight[i],
vertex[i].transform.position.z);
vertex[i].transform.position = newPosition;
}
}
}

