This site uses strictly necessary cookies. More Information

X- Home /

**Question**by slythethieve · Sep 10, 2019 at 09:13 PM · scripting problemarrayoptimizationtrianglesvertice manipulation

# Deleting square in Mesh really slow. Need help optimizing code.

Hi everybody, After following the great tutorial of holistic3d on how to delete a square on a mesh at runtime, I decided to add this functionality to my mesh. At the moment I don't really need this, what I need is an highlight of the square, but whatever. While her script works just fine on small meshes like planes or cubes, on mine it takes about 2 seconds to delete a square. I think it has to do either on number of vertices (around 700k) or on how the code was written with multiple array assignments and modifications. This is the code, can anybody help me in coming up with a more optimized solution that will run faster? Maybe parallel for loops? Also could you guys help me out in modifying this, so the square I hit instead of being deleted, I can put a texture on it. Thank you.

```
public class cutHole : MonoBehaviour {
private int triangleStripDown;
private int triangleStripUp;
private int[] triangles;
private Vector3[] vertices;
private int[] oldTriangles;
private int[] newTriangles;
private Mesh mesh;
// Use this for initialization
void Start () {
mesh = transform.GetComponent<MeshFilter>().mesh;
triangles = transform.GetComponent<MeshFilter>().mesh.triangles;
vertices = transform.GetComponent<MeshFilter>().mesh.vertices;
oldTriangles = triangles;
newTriangles = new int[triangles.Length - 6];
}
void highlightSquare (int index1, int index2)
{
}
void deleteSquare(int index1, int index2)
{
Destroy(this.gameObject.GetComponent<MeshCollider>());
//Mesh mesh = transform.GetComponent<MeshFilter>().mesh;
//int[] oldTriangles = triangles;
//int[] newTriangles = new int[triangles.Length-6];
int i = 0;
int j = 0;
while (j < triangles.Length)
{
if(j != index1*3 && j != index2*3)
{
newTriangles[i++] = oldTriangles[j++];
newTriangles[i++] = oldTriangles[j++];
newTriangles[i++] = oldTriangles[j++];
}
else
{
j += 3;
}
}
triangles = newTriangles;
oldTriangles = triangles;
transform.GetComponent<MeshFilter>().mesh.triangles = newTriangles;
this.gameObject.AddComponent<MeshCollider>();
}
int findVertex(Vector3 v)
{
//vertices = transform.GetComponent<MeshFilter>().mesh.vertices;
for(int i = 0; i < vertices.Length; i++)
{
if(vertices[i] == v)
return i;
}
Debug.Log("Not found");
return -1;
}
int findTriangle(Vector3 v1, Vector3 v2, int notTriIndex)
{
//triangles = transform.GetComponent<MeshFilter>().mesh.triangles;
//vertices = transform.GetComponent<MeshFilter>().mesh.vertices;
int i = 0;
int j = 0;
int found = 0;
while (j < triangles.Length)
{
if(j/3 != notTriIndex)
{
if(vertices[triangles[j]] == v1 && (vertices[triangles[j+1]] == v2 || vertices[triangles[j+2]] == v2))
return j/3;
else if(vertices[triangles[j]] == v2 && (vertices[triangles[j+1]] == v1 || vertices[triangles[j+2]] == v1))
return j/3;
else if(vertices[triangles[j+1]] == v2 && (vertices[triangles[j]] == v1 || vertices[triangles[j+2]] == v1))
return j/3;
else if(vertices[triangles[j+1]] == v1 && (vertices[triangles[j]] == v2 || vertices[triangles[j+2]] == v2))
return j/3;
}
j+=3;
}
return -1;
}
void deleteTri(int index)
{
Destroy(this.gameObject.GetComponent<MeshCollider>());
Mesh mesh = transform.GetComponent<MeshFilter>().mesh;
int[] oldTriangles = triangles;
int[] newTriangles = new int[triangles.Length-3];
int i = 0;
int j = 0;
while (j < mesh.triangles.Length)
{
if(j != index*3)
{
newTriangles[i++] = oldTriangles[j++];
newTriangles[i++] = oldTriangles[j++];
newTriangles[i++] = oldTriangles[j++];
}
else
{
j += 3;
}
}
transform.GetComponent<MeshFilter>().mesh.triangles = newTriangles;
triangles = newTriangles;
this.gameObject.AddComponent<MeshCollider>();
}
// Update is called once per frame
void Update ()
{
if(Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit, 1000.0f))
{
int hitTri = hit.triangleIndex;
//get neighbour
//triangles = transform.GetComponent<MeshFilter>().mesh.triangles;
//vertices = transform.GetComponent<MeshFilter>().mesh.vertices;
Vector3 p0 = vertices[triangles[hitTri * 3 + 0]];
Vector3 p1 = vertices[triangles[hitTri * 3 + 1]];
Vector3 p2 = vertices[triangles[hitTri * 3 + 2]];
float edge1 = Vector3.Distance(p0,p1);
float edge2 = Vector3.Distance(p0,p2);
float edge3 = Vector3.Distance(p1,p2);
Vector3 shared1;
Vector3 shared2;
if(edge1 > edge2 && edge1 > edge3)
{
shared1 = p0;
shared2 = p1;
}
else if(edge2 > edge1 && edge2 > edge3)
{
shared1 = p0;
shared2 = p2;
}
else
{
shared1 = p1;
shared2 = p2;
}
int v1 = findVertex(shared1);
int v2 = findVertex(shared2);
deleteSquare(hitTri,findTriangle(vertices[v1], vertices[v2], hitTri));
}
}
}
}
```

I modified it a bit because the original version was freezing Unity.

Edit: I think I know what might be the problem, It could be reassigning the mesh collider that is causing the problem.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.