I have a script that is designed so each outer edge point is supposed to create a triangle with the next point and the center of the mesh. The problem is, the mesh that is created is either flipped or there are missing triangles.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MeshGenerator : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public static Mesh GenerateMesh(Vector3[] points)
{
Vector3[] vertices = new Vector3[points.Length + 1];
Vector3 avg = Vector3.zero;
Vector3 min = Vector3.one * 1000000;
Vector3 max = Vector3.one * -1000000;
for (int i = 0; i < points.Length; i++)
{
vertices[i] = points[i];
avg += points[i];
min = Vector3.Min(min, points[i]);
max = Vector3.Max(max, points[i]);
}
avg /= points.Length;
vertices[vertices.Length - 1] = avg;
int[] tris = new int[points.Length * 3];
Vector2[] uv = new Vector2[vertices.Length];
for (int i = 0; i < points.Length; i++)
{
tris[i + 2] = vertices.Length - 1;
tris[i] = (i < points.Length - 1) ? i + 1 : 0;
tris[i + 1] = i;
Debug.Log(vertices[vertices.Length - 1] + " " + vertices[(i < points.Length - 1) ? i + 1 : 0] + " " + vertices[i]);
}
for (int i = 0; i < vertices.Length; i++)
{
uv[i] = new Vector2(Mathf.InverseLerp(min.x, max.x, vertices[i].x), Mathf.InverseLerp(min.z, max.z, vertices[i].z));
}
Mesh mesh = new Mesh();
mesh.vertices = vertices;
mesh.triangles = tris;
mesh.uv = uv;
mesh.RecalculateNormals();
return mesh;
}
}
If there is a better way of doing this, please tell me.