Texture problems when cutting large meshes at runtime

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 :slight_smile:

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();

Anyone?

You’re fundamentally in a bad place trying to render 100M+ vertices realtime, and in a worse place trying to use the CPU to handle slicing. It would be normal to optimise this mesh first. Simply whacking it into Blender and doing a Decimate would probably help.

If what you need is a clear ‘clip plane’ type cut, that intersects the model, then a shader would be a lot more efficient than CPU code at doing this. A simple shadergraph with a branch+alpha clipping based on xyz world space coordinate would cleanly clip the model along a given plane. Looking at the code you seem only interested in what’s ‘inside’, so this would work for that.

If you’re literally trying to dissect the mesh, so you could (e.g.) sword slice something in two, and have the bits fall apart, then the CPU approach would be needed, but it would be on the constraint of relatively low vertex counts. If it were trivial to slice complicated meshes into two discrete meshes in realtime, you’d be seeing that in games - I don’t think you’re trying to do this so I’d suggest looking at shaders.