Hello!
I’m hoping to find some help or ideas for an issue I’m unable to solve for a long time now. I’ll try to give as much info in as little space possible
Goal
I need to be able to cut large meshes at runtime (100M vertices+). Meshes sometimes have per-vertex colouring and sometimes have texture maps with multiple materials.
There isn’t much I can do to modify the input data - models are otputs of surveying (laser scanning or photogrammetry).
Problem
I wrote a simple script for mesh cutting that works on per-vertex coloured mesh, but when used on mesh with texture maps it successfully cuts but the texture gets broken.
The script runs through the vertices, identifies which are our of boundaries, and then modifies the list of indices by removing the triangles that are out of boundaries. It doesn’t modify the lists of vertices, normals, or any other part of the mesh, only indices.
Any ideas why this doesn’t work? Any help would be much appreciated!
Mesh mesh = mesh.GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
int[] triangles = mesh.triangles;
//create list for new indices
List<int> newTriangles = new List<int>();
//create list for is-inside-or-outside of boundaries
List<bool> insideOrNot = new List<bool>();
float vertexX;
float vertexY;
float vertexZ;
for (int i = 0; i < vertices.Length; i++)
{
//ensure world coordinates
vertexX = transform.TransformPoint(vertices*).x;*
vertexY = transform.TransformPoint(vertices*).y;*
vertexZ = transform.TransformPoint(vertices*).z;*
//maxX, minX, maxY, minY, maxZ, minZ are boundaries of the box used for cutting
//only the part of the mesh inside this box is to be displayed
if (vertexX < maxX && vertexX > minX && vertexY < maxY && vertexY > minY && vertexZ < maxZ && vertexZ > minZ)
insideOrNot.Add(true);
else
insideOrNot.Add(false);
}
for (int i = 0; i < triangles.Length; i += 3)
{
if (insideOrNot[triangles*] && insideOrNot[triangles[i + 1]] && insideOrNot[triangles[i + 2]])*
{
newTriangles.Add(triangles*);*
newTriangles.Add(triangles[i + 1]);
newTriangles.Add(triangles[i + 2]);
}
}
mesh.triangles = newTriangles.ToArray();