How to generate a mesh only off of vertices

hello, i am working on a project to map a square grid onto a spheroid, it works well.

my problem is that i only have vertices of this sphere, and nothing else, i tried creating a mesh but my main problem is with the triangles, i cant seem to make them work

i have tried using this script
https://luminaryapps.com/blog/triangulating-3d-polygons-in-unity/

it gave me a stack overflow error

so any steps on this? i want this to be fully automatic as the grid can be of any x,y size

the sides are supposed to be squares, since its off a square grid (except for at the poles)

If it’s grid-based, it should be fairly straightforward - use the same triangles as you’d have had on the square grid. So make a triangle for vertices (x, y), (x + 1, y), (x, y +1), and a triangle for (x, y), (x, y + 1), (x + 1, y + 1). Do this for all vertices except the ones on the top/right edge of the grid. See how this breaks at your poles, and figure it out from there.

1 Like

thanks, i will try this!

just a question… am i supposed to multiply x and y together?

welll… multiplying x and y together and clamping the result to the max worked… sorta


NOT what i was expecting
here is the code that i used to generate the mesh from the points

public Mesh GenerateMeshFromPoints(List<Vector3> points, int cols, int rows)
{
    Mesh mesh = new Mesh();
    List<int> triangles = new List<int>();
    for(int i = 0; i < rows; i++)
    {
        for(int d = 0; d<cols; d++)
        {
            triangles.Add(i * d);
            triangles.Add(Mathf.Min((i + 1) * d, (cols*rows)-1));
            triangles.Add(Mathf.Min(i * (d+1), (cols * rows)-1));
            triangles.Add(i * d);
            triangles.Add(Mathf.Min(i * (d + 1), (cols * rows)-1));
            triangles.Add(Mathf.Min((i+1) * (d + 1), (cols * rows)-1));
        }
    }
    List<Vector3> normals = new List<Vector3>();
    foreach(Vector3 point in points)
    {
        normals.Add(Quaternion.LookRotation(point).eulerAngles);
    }
    mesh.vertices = points.ToArray();
    mesh.normals = points.ToArray();
    mesh.triangles = triangles.ToArray();
    return mesh;
}

UPDATE: I DID IT!


here is the code:

    public Mesh GenerateMeshFromPoints(List<Vector3> points, int cols)
    {
        Mesh mesh = new Mesh();
        List<int> triangles = new List<int>();
        for(int i = 0; i < points.Count; i++)
        {
            triangles.Add(i);
            triangles.Add(Mathf.Min(i + cols, points.Count - 1));
            triangles.Add(Mathf.Min(i + 1, points.Count - 1));
            triangles.Add(Mathf.Min(i + 1, points.Count - 1));
            triangles.Add(Mathf.Min(i + cols, points.Count - 1));
            triangles.Add(Mathf.Min(i + cols+1, points.Count - 1));
        }
        List<Vector3> normals = new List<Vector3>();
        foreach(Vector3 point in points)
        {
            normals.Add(Quaternion.LookRotation(point).eulerAngles);
        }
        mesh.vertices = points.ToArray();
        mesh.normals = points.ToArray();
        mesh.triangles = triangles.ToArray();
        return mesh;
    }
1 Like