- Home /
The question is answered on stackoverflow
Procedural mesh slows down scene
Im creating a pretty simple game where I need to create multiple Meshes. The code that I create is pretty simple and yet after having more than 8 Meshes at the same time, the peerformance reduces considerably to just a couple of fps (~8 fps). The Mesh that I create is just a simple square so I really don´t know where the problem is, here´s my code:
using UnityEngine;
using System.Collections;
public class TetraGenerator : MonoBehaviour {
public int slices;
public GameObject forceSource;
void OnMouseDown(){
var arcLength = Mathf.PI / slices;
var distance = 10;
var height = 1;
var origin = Random.Range(-slices,slices);
Vector3[] vertices = new Vector3[4];
vertices [0] = new Vector3 (Mathf.Cos(origin*arcLength),Mathf.Sin(origin*arcLength));
vertices [1] = new Vector3 (Mathf.Cos(origin*arcLength),Mathf.Sin(origin*arcLength));
vertices [2] = new Vector3 (Mathf.Cos((origin+1)*arcLength),Mathf.Sin((origin+1)*arcLength));
vertices [3] = new Vector3 (Mathf.Cos((origin+1)*arcLength),Mathf.Sin((origin+1)*arcLength));
vertices [0] *= distance;
vertices [1] *= (distance+height);
vertices [2] *= (distance+height);
vertices [3] *= distance;
Vector3 frameRef = new Vector3(Mathf.Cos(origin*arcLength+(arcLength/2)),Mathf.Sin(origin*arcLength+(arcLength/2)));
frameRef *= distance;
vertices [0] -= frameRef;
vertices [1] -= frameRef;
vertices [2] -= frameRef;
vertices [3] -= frameRef;
int[] triangles = new int[]{0,1,2,2,3,0};
Mesh mesh = new Mesh ();
mesh.vertices = vertices;
mesh.triangles = triangles;
GameObject tile = new GameObject("tile",typeof(MeshFilter),typeof(MeshRenderer));
tile.transform.position = frameRef;
MeshFilter meshFilter = tile.GetComponent<MeshFilter> ();
meshFilter.mesh = mesh;
}
}
It could be that the mesh is missing the normals required by the shaders. You should use the RecalculateNormals() method after you specify the meshes vertices and triangles.
$$anonymous$$esh mesh = new $$anonymous$$esh ();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateNormals();
Thanks, I tried it, but it still stays the same =(
You could also try adding the UV parameters of the mesh. Do you get any warnings in the console when you run your project ?
You could try to add material and UV data to the gameobject that has the mesh renderer:
mesh.uv = uvList; // uvList is an array of uv data for the mesh.
GameObject tile = new GameObject("tile",typeof($$anonymous$$eshFilter),typeof($$anonymous$$eshRenderer));
tile.renderer.material = m$$anonymous$$aterial; // You set m$$anonymous$$aterial to be a material instance.
The problem is you're constantly creating new vertex arrays and meshes. This is much more expensive than simply reusing the same mesh with the same vertex array, again and again. Every new mesh is a new OpenGL or DirectX object.
As the comments have said constantly creating new objects on the fly like this will be expensive. If you absolutely have to generate meshes like this, then the first thing I would suggest is reduce the amount of trigonometry calls you are making, Sin and Cos functions can be costly and you are sometimes using the results twice so you could store the results in a variable and recycle.
The problem was that I wasn´t assigning the material, and the shader. Here´s the answer:
http://stackoverflow.com/questions/22926739/procedural-mesh-slows-down-unity-scene/22927214#22927214
Thanks everyone!
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Fixing mesh normals 1 Answer
Where is the submesh Reference of vertex/triangle? 1 Answer
Distribute terrain in zones 3 Answers
Mesh creation by code not working? 0 Answers