• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Andrew-Black · Jul 13, 2018 at 03:05 PM · meshmaterialmaterialsmeshescombinemeshes

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image madks13 · Jul 13, 2018 at 03:23 PM 0
Share

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.

avatar image hexagonius madks13 · Jul 13, 2018 at 07:00 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

112 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Materials on an imported mesh 1 Answer

CombineMeshes with Different Materials 7 Answers

How to use multiple Unity materials on external meshes 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges