#Creating A Mesh Based On A List Of Points

1 messages · Page 1 of 1 (latest)

arctic umbra
#

I am trying to simulate water. The physics are working meaning that I have a layer of dots which I now need to connect into a mesh. I have a List<Vector2> containing all of the points. in the list they are ordered from left to right.
I think that all of the trapezoids would be drawn from 2 triangles, but how do I create this mesh? Everything online is not scalable enough to apply here. I am really stuck and would be grateful for any kind of help

#

Creating A Mesh Based On A List Of Points

pseudo river
#

So if you have a list of points along the top of that shape, from that you can figure out an equal amount of points for the 'bottom' and that'll give you a 2 dimensional array of vectors of all the vertices of the mesh. (though it is just stored in a single array)

Vector3[] vertices = new Vector3[]
{
    // top 'waves'
    new Vector3(1, 0, 5),
    new Vector3(2, 0, 5.5f),
    new Vector3(3, 0, 10),
    new Vector3(4, 0, 6),

    // bottom
    new Vector3(1, 0, 0),
    new Vector3(2, 0, 0),
    new Vector3(3, 0, 0),
    new Vector3(4, 0, 0),
};

From this we can create the triangle indices array. The triangle array of a Mesh is a list of indices into the vertex array. [ 0, 1, 2, 1, 2, 3 , ... ]

// dimensions of vertex array
Vector2Int dimensions = new Vector2Int(4, 2);
        
// number of triangles is the number of 'squares' * 6 (3 verts per triangle, 2 triangle per square)
int numTries = 6 * (dimensions.x -1) * (dimensions.y - 1);
int[] triangles = new int[numTries];

as the vertices are laid out width ways in the array if you draw triangles between the verts in a clockwise direction you'll end up with something like this for the first 'square'

[0, 1, 4,  1, 5, 4  .....

to do this in a loop:...

int j = 0;
for(int i = 0; i < dimensions.x - 1; i++)
{
    // tri indices clockwise to face 'up'? I think
    triangles[j] = i;
    triangles[j + 1] = i + 1;
    triangles[j + 2] = i + dimensions.x;            
                
    triangles[j + 3] = i + 1;
    triangles[j + 4] = i + 1 + dimensions.x;
    triangles[j + 5] = i + dimensions.x;
                    
    j+=6;            
}

Mesh mesh = new Mesh{ name = "Plane" };
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateNormals();
mesh.RecalculateTangents();