In a project I generate a grid mesh in code. This mesh is then added to a MeshFilter and a MeshCollider on a new GameObject. Below is a simplified form of the code I use in this project. The time it takes to add the MeshCollider and assign the mesh is logged in the console.
On my PC, in Unity 2017.3.0b9 the time it takes to add the MeshCollider and assign the mesh is 17ms. In Unity 2017.3.0b10 it takes 159ms, which is over 9 times slower. For larger or more complex meshes the difference increases even further; in my actual project the difference is 62ms vs 840ms, or 13.5 times slower.
The default CookingOptions appear to be the same between b9 and b10 (
CookForFasterSimulation, EnableMeshCleaning, WeldColocatedVertices). Enabling mesh cleaning by removing line 23 in the script below has little or no effect in b9, while it dramatically improves the performance in b10 (though still much slower than in b9, from 159ms to 44ms in my case).
In addition to the speed difference, there is an issue with the geometry when generating more than 65.000 vertices (visible by looking at the generated mesh in wireframe view). I haven’t read up on those changes yet though, I may have to perform some extra steps to get this to work properly.
Steps to reproduce:
- Create an empty project
- Add an empty GameObject to the scene
- Add the script below to the empty GameObject
- Run the project
- Compare the output between Unity 2017.3.0b9 and 2017.3.0b10
using UnityEngine;
public class MeshGenerator : MonoBehaviour
{
public int Columns = 200;
public int Rows = 200;
void Start()
{
Mesh gridMesh = GenerateGridMesh();
GameObject meshObject = new GameObject("Generated Grid");
MeshRenderer meshRenderer = meshObject.AddComponent<MeshRenderer>();
meshRenderer.sharedMaterial = new Material(Shader.Find("Diffuse"));
MeshFilter meshFilter = meshObject.AddComponent<MeshFilter>();
meshFilter.sharedMesh = gridMesh;
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
MeshCollider meshCollider = meshObject.AddComponent<MeshCollider>();
meshCollider.cookingOptions -= MeshColliderCookingOptions.EnableMeshCleaning;
meshCollider.sharedMesh = gridMesh;
Debug.LogFormat("Adding the MeshCollider took {0}ms.", stopwatch.ElapsedMilliseconds);
}
private Mesh GenerateGridMesh()
{
int numberOfTiles = Columns * Rows;
Vector3[] vertices = new Vector3[numberOfTiles * 4];
int[] triangles = new int[numberOfTiles * 6];
for (int col = 0; col < Columns; col++)
{
for (int row = 0; row < Rows; row++)
{
GenerateTile(vertices, triangles, col, row);
}
}
return new Mesh()
{
vertices = vertices,
triangles = triangles
};
}
private void GenerateTile(Vector3[] vertices, int[] triangles, int col, int row)
{
int tileVertexIndex = (col * Columns + row) * 4;
int tileTriangleIndex = (col * Columns + row) * 6;
Vector3 tileCenter = new Vector3(col, 0, row);
vertices[tileVertexIndex + 0] = new Vector3(-0.5f, 0, -0.5f) + tileCenter;
vertices[tileVertexIndex + 1] = new Vector3(-0.5f, 0, 0.5f) + tileCenter;
vertices[tileVertexIndex + 2] = new Vector3(0.5f, 0, 0.5f) + tileCenter;
vertices[tileVertexIndex + 3] = new Vector3(0.5f, 0, -0.5f) + tileCenter;
triangles[tileTriangleIndex + 0] = tileVertexIndex + 0;
triangles[tileTriangleIndex + 1] = tileVertexIndex + 1;
triangles[tileTriangleIndex + 2] = tileVertexIndex + 2;
triangles[tileTriangleIndex + 3] = tileVertexIndex + 2;
triangles[tileTriangleIndex + 4] = tileVertexIndex + 3;
triangles[tileTriangleIndex + 5] = tileVertexIndex + 0;
}
}