- Home /
Combine Meshes issue
I'm trying to combine "seats prefabs" into a single mesh at runtime to save some draw calls.
My seat contains 2 MeshFilter, a base and a top (= the seat, red on the picture) with 2 different materials. My seat is created from an imported FBX which has read/Write enabled.
I'm using pooling if it matters : I generate 500 seats, then I pick 250 from the pool to create my stands, then I combine them, release the pooled seats and reuse them to combine another stands.
Here is the issue that appears on some seats (not all, only the 5th and 6th stands so maybe linked to some pooling issue ? reusing the same mesh several times), some "tops" of seats are "linked" together
Here is the function I use :
List<CombineInstance> baseList = new List<CombineInstance>();
List<CombineInstance> topList = new List<CombineInstance>();
Material materialTop = null;
Material materialBase = null;
//Loop through the array with trees
for (int i = 0; i < seats.Count; i++)
{
GameObject currentSeat = seats[i];
//Get all meshfilters from this seat, true to also find deactivated children
MeshFilter[] meshFilters = currentSeat.GetComponentsInChildren<MeshFilter>(true);
//Loop through all children
for (int j = 0; j < meshFilters.Length; j++)
{
MeshFilter meshFilter = meshFilters[j];
CombineInstance combine = new CombineInstance();
//Is it top or base?
MeshRenderer meshRender = meshFilter.GetComponent<MeshRenderer>();
string materialName = meshRender.gameObject.name;
if (materialName.Contains("top"))
{
combine.mesh = meshFilter.mesh;
combine.transform = meshFilter.transform.localToWorldMatrix;
//Add it to the list of leaf mesh data
topList.Add(combine);
if (materialTop == null) materialTop = meshRender.material;
}
else if (materialName.Contains("base"))
{
combine.mesh = meshFilter.mesh;
combine.transform = meshFilter.transform.localToWorldMatrix;
//Add it to the list of wood mesh data
baseList.Add(combine);
if (materialBase == null) materialBase = meshRender.material;
}
}
//Deactivate the seat the pool
GameController.This.Prefabs.DestroyPooledObject(currentSeat);
}
//First we need to combine the bases into one mesh and then the tops into one mesh
Mesh combinedBaseMesh = new Mesh();
combinedBaseMesh.CombineMeshes(baseList.ToArray());
Mesh combinedTopMesh = new Mesh();
combinedTopMesh.CombineMeshes(topList.ToArray());
//Create the array that will form the combined mesh
CombineInstance[] totalMesh = new CombineInstance[2];
//Add the submeshes in the same order as the material is set in the combined mesh
totalMesh[0].mesh = combinedBaseMesh;
totalMesh[0].transform = parent.transform.localToWorldMatrix;
totalMesh[1].mesh = combinedTopMesh;
totalMesh[1].transform = parent.transform.localToWorldMatrix;
//Create the final combined mesh
Mesh combinedAllMesh = new Mesh();
//Make sure it's set to false to get 2 separate meshes
combinedAllMesh.CombineMeshes(totalMesh, false);
var meshFilterParent = parent.GetComponent<MeshFilter>();
if (meshFilterParent == null) meshFilterParent = parent.AddComponent<MeshFilter>();
meshFilterParent.sharedMesh = combinedAllMesh;
var meshRenderer = parent.GetComponent<MeshRenderer>();
if (meshRenderer == null) meshRenderer = parent.AddComponent<MeshRenderer>();
meshRenderer.materials = new
Material[]
{
materialBase,
materialTop
};
i've tried to merge submeshes but then the materials don't work anymore, it's just using the first material for all.