- Home /
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 :)
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[i]).x;
vertexY = transform.TransformPoint(vertices[i]).y;
vertexZ = transform.TransformPoint(vertices[i]).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[i]] && insideOrNot[triangles[i + 1]] && insideOrNot[triangles[i + 2]])
{
newTriangles.Add(triangles[i]);
newTriangles.Add(triangles[i + 1]);
newTriangles.Add(triangles[i + 2]);
}
}
mesh.triangles = newTriangles.ToArray();
Answer by tuinal · Nov 10, 2020 at 02:10 AM
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.
Hi @tuinal, thanks for the help, but I don't think I can do that.
I see your points and all of them are valid! But, I'm working with meshes of heriatage buildings (from laser scanning or photogrammetry) and I'm essentially not able to decimate/modify this input data (it's imported at runtime). Also, I plan to add exporting of the slice later on, so I don't think I can use the shader here.
The CPU approach actually works pretty fast (as far as polygons go), the only problem I have is in the texture. If you forget for a second that using CPU here isn't the best option, could you maybe take a guess at why are my textures distorted?
Thanks again!
Your answer
Follow this Question
Related Questions
How to modify mesh of object when other object trigger like cut 0 Answers
Melting a mesh on top of another mesh? 1 Answer
how to cut a mesh? 1 Answer
modify mesh in real time 0 Answers