- Home /
Combine N different meshes with M different materials on each
The situation is as follows: input parameter(meshFilters) - are parts of the pattern of clothing (such as the sleeve, back, front), they all differ from each other and all have N different materials on each.
The task: to unite all the meshes into one, keeping all the materials on each piece of clothing.
At the moment I have such a code. Output mesh uses standard material. But we don't know in advance about the number of parts and the number of materials on each of them.
public static void MeshesToFile(List<MeshFilter> meshFilters, string filename)
{
Mesh resultMesh = new Mesh();
GameObject GO = new GameObject();
MeshFilter MF = GO.AddComponent<MeshFilter>();
MeshRenderer MR = GO.AddComponent<MeshRenderer>();
CombineInstance[] combiners = new CombineInstance[meshFilters.Count];
for (int i = 0; i < meshFilters.Count; i++)
{
combiners[i].subMeshIndex = 0;
combiners[i].mesh = meshFilters[i].sharedMesh;
combiners[i].transform = meshFilters[i].transform.localToWorldMatrix;
}
resultMesh.CombineMeshes(combiners);
MF.sharedMesh = resultMesh;
MR.sharedMaterial = new Material(Shader.Find("Diffuse"));
MeshToFile(MF, filename);
Object.Destroy(GO);
}
How can I do that?
Do you have multiple materials per submesh? Also, you set sub$$anonymous$$eshIndex to 0 for each sub$$anonymous$$esh. maybe use i? As far as i know, the only way to set different materials per mesh is to use SetTriangles. but it uses triangles and materials. If you don't know what the triangles are and which materials they use, you will need a custom shader.
red the docs and its half the truth. Using i as submesh index is definitely correct. each subject corresponds to a material though. this means providing an array of $$anonymous$$aterials as large as the amount of submeshes should work.
Your answer
Follow this Question
Related Questions
Materials on an imported mesh 1 Answer
How do I use the same material on 2 meshes without distortion? 2 Answers
The best way to have multiple models with only one material 2 Answers
Merging meshes 0 Answers
Mobile Material Optimization 0 Answers