Нужна помощь при генерации меша.
Создаю набор вершин и полигонов. Храню их в списках.
При создании меша использую следующий код:
Синтаксис:
Используется csharp
int vertexCount = polygons.Length * 3;
int[] indices = new int[vertexCount];
Vector3[] vertices = new Vector3[vertexCount];
Vector3[] normals = new Vector3[vertexCount];
Vector2[] uv = new Vector2[vertexCount];
Color32[] colors = new Color32[vertexCount];
for (int i = 0; i < polygons.Length; i++)
{
indices[i * 3 + 0] = i * 3 + 0;
indices[i * 3 + 1] = i * 3 + 1;
indices[i * 3 + 2] = i * 3 + 2;
vertices[i * 3 + 0] = polygons[i].Vertices[0].Position;
vertices[i * 3 + 1] = polygons[i].Vertices[1].Position;
vertices[i * 3 + 2] = polygons[i].Vertices[2].Position;
normals[i * 3 + 0] = polygons[i].Vertices[0].Normal;
normals[i * 3 + 1] = polygons[i].Vertices[1].Normal;
normals[i * 3 + 2] = polygons[i].Vertices[2].Normal;
uv[i * 3 + 0] = polygons[i].Vertices[0].UV;
uv[i * 3 + 1] = polygons[i].Vertices[1].UV;
uv[i * 3 + 2] = polygons[i].Vertices[2].UV;
colors[i * 3 + 0] = polygons[i].Vertices[0].Color; // задаем цвет первой вершины полигона по установленному цвету соответствующего Vertex
colors[i * 3 + 1] = polygons[i].Vertices[1].Color; // задаем цвет второй вершины полигона по установленному цвету соответствующего Vertex
colors[i * 3 + 2] = polygons[i].Vertices[2].Color; // задаем цвет третьей вершины полигона по установленному цвету соответствующего Vertex
}
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = indices;
mesh.normals = normals;
mesh.uv = uv;
mesh.colors32 = colors;
if (recalculateNormals) mesh.RecalculateNormals();
if (recalculateTangents) mesh.RecalculateTangents();
if (recalculateBounds) mesh.RecalculateBounds();
int[] indices = new int[vertexCount];
Vector3[] vertices = new Vector3[vertexCount];
Vector3[] normals = new Vector3[vertexCount];
Vector2[] uv = new Vector2[vertexCount];
Color32[] colors = new Color32[vertexCount];
for (int i = 0; i < polygons.Length; i++)
{
indices[i * 3 + 0] = i * 3 + 0;
indices[i * 3 + 1] = i * 3 + 1;
indices[i * 3 + 2] = i * 3 + 2;
vertices[i * 3 + 0] = polygons[i].Vertices[0].Position;
vertices[i * 3 + 1] = polygons[i].Vertices[1].Position;
vertices[i * 3 + 2] = polygons[i].Vertices[2].Position;
normals[i * 3 + 0] = polygons[i].Vertices[0].Normal;
normals[i * 3 + 1] = polygons[i].Vertices[1].Normal;
normals[i * 3 + 2] = polygons[i].Vertices[2].Normal;
uv[i * 3 + 0] = polygons[i].Vertices[0].UV;
uv[i * 3 + 1] = polygons[i].Vertices[1].UV;
uv[i * 3 + 2] = polygons[i].Vertices[2].UV;
colors[i * 3 + 0] = polygons[i].Vertices[0].Color; // задаем цвет первой вершины полигона по установленному цвету соответствующего Vertex
colors[i * 3 + 1] = polygons[i].Vertices[1].Color; // задаем цвет второй вершины полигона по установленному цвету соответствующего Vertex
colors[i * 3 + 2] = polygons[i].Vertices[2].Color; // задаем цвет третьей вершины полигона по установленному цвету соответствующего Vertex
}
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = indices;
mesh.normals = normals;
mesh.uv = uv;
mesh.colors32 = colors;
if (recalculateNormals) mesh.RecalculateNormals();
if (recalculateTangents) mesh.RecalculateTangents();
if (recalculateBounds) mesh.RecalculateBounds();
Вопрос:
Если я изменяю позиции вершин - как правильнее - заново пересоздавать весь меш или обновлять позиции его вершин?
Если обновлять, то как? Можете дать совет?