• 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
1
Question by scarletshark · May 29, 2015 at 07:15 PM · c#gameobjectmeshperformanceoptimization

Mesh.CombineMeshes does not work when mergeSubMeshes=false

In my script, I am trying to combine all objects that share the same material color into individual submeshes on a merged GameObject. So far, I'm getting extremely frustrated with the Mesh.CombineMeshes "mergeSubMeshes" flag.

It appears that when I set this flag to "false," I end up with only one visible submesh, and the inspector for the GameObject says, "This renderer has more materials that the Mesh has submeshes." I'm grateful for any help!

 private Dictionary<Color32, Material> materialsByColor = new Dictionary<Color32, Material>();
 
 GameObject CombineMeshes (List<GameObject> gameObjects){
     var numGameObjects = gameObjects.Count;
     var combinedGameObject = (GameObject)GameObject.Instantiate(combinedMesh);
     var combinedMeshFilter = combinedGameObject.GetComponent<MeshFilter>();
     var combineInstances = new CombineInstance[numGameObjects];
     var colors = new List<Color32>(materialsByColor.Keys);

     GameObject obj;
     CombineInstance combineInstance;     
     MeshFilter objMeshFilter;
     int submeshIndex;
     
     for(int i = 0; i < numGameObjects; i++) {
         obj = gameObjects[i];
         combineInstance = combineInstances[i];
         objMeshFilter = obj.GetComponent<MeshFilter>();
         
         combineInstance.mesh = objMeshFilter.sharedMesh;
         combineInstance.transform = objMeshFilter.transform.localToWorldMatrix;

         // I want to put each object in a submesh with objects of the same material
         submeshIndex = colors.IndexOf(obj.GetComponent<Renderer>().material.color);
         
         // Prints exactly what I expect.
         print (submeshIndex);
         
         combineInstance.subMeshIndex = submeshIndex;
     }
     
     combinedMeshFilter.mesh = new Mesh();
     combinedMeshFilter.mesh.CombineMeshes(combineInstances, false, true);
     combinedGameObject.GetComponent<Renderer>().materials = new List<Material>(materialsByColor.Values).ToArray();
     
     // Prints an unreasonably low number, and it appears only one submesh is rendering
     print(combinedMeshFilter.sharedMesh.subMeshCount);
     
     return combinedGameObject;
 }


Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Pixelome · Oct 30, 2017 at 10:08 PM

I know it has been a while but I'm hoping that this will help somebody else since I lost a few hours trying to figure out why this is happening.

When using "mesh.CombineMeshes(combineInstances, false, true);" the mesh renderer needs to have a material for each submesh. This is why only the first submesh is visible, because there are no materials for other submeshes.

Comment
Add comment · Share
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
0

Answer by Lostwoods · Oct 25, 2016 at 02:15 PM

same issue here!! please help!

Comment
Add comment · Share
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

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

21 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

Related Questions

Problem with CombineMeshes on large number of meshes 1 Answer

Merging together all GameObjects of an Array? 1 Answer

Most optimal way of changing all textures on scene 1 Answer

Colliders vs Callbacks: Which is better for performance in my case? 0 Answers

Attaching a model to a blank GameObject? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges