- Home /
Generating Meshes Is Slow
Finally mananged to have 1 gameobject that created several 1x1x1 cube meshes and stiches them together. When the program runs it creates 10x10x10 cube, but it takes about 10 seconds to load and uses 300mb memory. what is wrong!?
NewMesh Class:
public class NewMesh : MonoBehaviour {
void Start()
{
}
public static Mesh ExtendMesh(Mesh first,Mesh second,int counter)
{
int [] trianglesooo = second.triangles;
for (int i = 0; i < trianglesooo.Length;i++)
{
trianglesooo[i] +=8*counter;
}
Vector3[] Lvertices = new Vector3[first.vertices.Length + second.vertices.Length];
int[] Ltriangles = new int[first.triangles.Length + trianglesooo.Length];
first.vertices.CopyTo(Lvertices , 0);
second.vertices.CopyTo(Lvertices,first.vertices.Length);
first.triangles.CopyTo(Ltriangles , 0);
trianglesooo.CopyTo(Ltriangles,first.triangles.Length);
second.vertices = Lvertices;
second.triangles = Ltriangles;
return second;
}
public static Mesh createNewMesh(Vector3 position)
{
Mesh returnMesh = new Mesh();
returnMesh.name = "Chunk" + position;
Vector3[] vertices = new Vector3[8] {new Vector3(Mathf.Floor(position.x) , position.y, Mathf.Floor(position.z ) ),
new Vector3((Mathf.Floor(position.x) + 1) , position.y, Mathf.Floor(position.z ) ),
new Vector3(Mathf.Floor(position.x) , position.y, (Mathf.Floor(position.z ) + 1) ),
new Vector3((Mathf.Floor(position.x) + 1) , position.y, (Mathf.Floor(position.z ) + 1) ),
new Vector3((Mathf.Floor(position.x) + 1) , position.y+1, (Mathf.Floor(position.z ) + 1) ),
new Vector3((Mathf.Floor(position.x) + 0) , position.y+1, (Mathf.Floor(position.z ) + 1) ),
new Vector3((Mathf.Floor(position.x) + 0) , position.y+1, (Mathf.Floor(position.z-1 ) + 1) ),
new Vector3((Mathf.Floor(position.x) + 1) , position.y+1, (Mathf.Floor(position.z-1 ) + 1) )};
int[] triangles = new int[39] {0,2,1, 1,2,3, 2,3,4, 4,5,2, 5,0,2, 5,6,0, 6,7,0, 0,7,1, 3,4,1, 1,7,4, 6,4,7, 4,6,5, 1,4,3};
returnMesh.vertices = vertices;
returnMesh.triangles = triangles;
return returnMesh;
}
}
Class attached to gameobject:
using UnityEngine;
using System.Collections;
public class Chunk : MonoBehaviour
{
int counter;
void Start ()
{
this.gameObject.renderer.material.color = Color.green;
GetComponent<MeshFilter>().mesh = NewMesh.createNewMesh(new Vector3(0,0,0));
counter+=1;
Mesh secondMesh = new Mesh();
for(int x = 0; x< 10; x++)
{
for(int y = 0; y< 10; y++)
{
for(int z = 0; z< 10; z++)
{
GetComponent<MeshFilter>().mesh = NewMesh.ExtendMesh(GetComponent<MeshFilter>().mesh,NewMesh.createNewMesh(new Vector3(x,y,z)),counter); //Adds second mesh to mainmesh.
counter+=1;
}
}
}
}
void Update ()
{
}
}
this is a rare "sophisticated duplicate" question :)
http://answers.unity3d.com/questions/315059/how-to-improve-performance-while-generating-extrud.html
http://answers.unity3d.com/questions/417483/is-there-a-way-to-assign-meshverticesi-directly.html
http://answers.unity3d.com/questions/321428/adding-mesh-collider-in-run-time-slow.html
Answer by whydoidoit · Apr 22, 2013 at 06:29 AM
Well firstly don't do this:
first.vertices.Length
Because each time you do that you make a copy of the vertices array, same for triangles etc. That's why there is a vertexCount property. So in the ExtendMesh you are getting multiple copies of the vertex array - each time allocating memory to contain them.
Consider the result of mesh.vertices etc to be an array that is yours to do with as you see fit, but call it only when you need it and keep the result so you don't keep creating new copies.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
cube based enging 1 Answer
Random cube length on instantiate... 1 Answer
Creating a World? 2 Answers