- Home /
Procedurally generated mesh performance
Hello. I am generating a hexagon mesh procedurally, and putting it in a game object, like so:
GameObject hex = new GameObject ();
hex.AddComponent<MeshRenderer> ();
hex.GetComponent<MeshRenderer> ().castShadows = false;
hex.GetComponent<MeshRenderer> ().receiveShadows = false;
hex.AddComponent<MeshFilter> ();
hex.GetComponent<MeshFilter> ().mesh = GenerateHexagonMesh ();
//hex.renderer.sharedMaterial = mat;
hex.isStatic = true;
I am instantiating the object about 400 times, without any performance issues. However, if I set a material to the object (tested several), the performance drops to the point that Unity just stops responding. If I assign a material, only 10 or so objects can be instantiated before the performance gets too bad.
Any tips to what might be going wrong here? Thanks
edit: this is how i generate the mesh:
private Mesh GenerateHexagonalMesh (float hexRadius)
{
Mesh mesh = new Mesh ();
Vector3 vertPos = new Vector3 (0, 0, hexRadius);
Vector3[] vertices = new Vector3[7];
Quaternion rot = Quaternion.Euler (0, 60, 0); //360/6 = 60
vertices [0] = new Vector3 (0, 0, 0);
for (int i=0; i< 6; i++) {
vertPos = rot * vertPos;
vertices [i + 1] = vertPos;
}
mesh.vertices = vertices;
int[] tri = new int[]{
0, 1, 2,
0, 2, 3,
0, 3, 4,
0, 4, 5,
0, 5, 6,
0,6,1};
mesh.triangles = tri;
Vector3[] normals = new Vector3[7];
normals [0] = Vector3.up;
normals [1] = Vector3.up;
normals [2] = Vector3.up;
normals [3] = Vector3.up;
normals [4] = Vector3.up;
normals [5] = Vector3.up;
normals [6] = Vector3.up;
mesh.normals = normals;
//mesh.RecalculateNormals ();
mesh.RecalculateBounds ();
mesh.Optimize ();
return mesh;
}
How many triangles are then in the mesh generated by 'GenerateHexagon$$anonymous$$esh'?
I think we need to see the code for GenerateHexagon$$anonymous$$esh... Any chance it's just making a more complicated mesh each time and not reseting something for instance?
Answer by ArkaneX · Oct 07, 2013 at 11:18 AM
It's just a guess, but you're probably using material with a shader expecting a texture, and your hex mesh doesn't have UVs defined. I tested your code using material with Diffuse shader, and there was a big performance hit indeed.
When I set mesh UVs (just after setting mesh.vertices):
var uvs = new Vector2[7];
mesh.uv = uvs;
everything worked ok, and generating 1000 hexes with material didn't result in any performance loss.
Of course if you plan to use texture, then you have to provide proper UVs, not just initialize the array.
I actually had a hunch today that uv mapping might be the problem, and you confirmed it. I had no idea though that i could just assign them like that and it wouldn't be a problem.
Thank you so much! :)
The above lines work, because Vector2
is value type defaulting to Vector2.zero
. So all vertices are UV mapped to the same (0,0) texture coordinate.
Your answer
![](https://koobas.hobune.stream/wayback/20220613115427im_/https://answers.unity.com/themes/thub/images/avi.jpg)