• 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
Question by Tempest · Jan 16, 2010 at 05:10 PM · meshsubmesh

Combining Meshes with subMeshes

How do you combine meshes with submeshes?

I have two meshes, and each mesh has two submeshes. What I want, after combining them, is one mesh, and two submeshes.

Comment
sun_stealer

People who like this

1 Show 0
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

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by mstultz · Apr 27, 2010 at 07:16 PM

I commented on the previous answer, but I just tested my theory and it seems to work. To recap: I create a NEW mesh for each sub-mesh in an original mesh. I then combine these (sub-mesh) meshes. So for 4 objects that use identical 2-sub-mesh meshes, instead of getting a single combined mesh with 8 sub-meshes, I get 2 combined meshes, with 1 "sub-mesh" each. This brings my draw calls down from 17 to 6, in my test case.

Briefly, I performed 4 steps to combine objects that use meshes with multiple sub-meshes:

  1. Gather a list of GameObjects that I wish to combine.
  2. Go through each mesh filter in the GameObject list from step 1. Create and cache newly constructed Meshes from each sub-mesh (I used this: http://www.unifycommunity.com/wiki/index.php?title=MeshCreationHelper).
  3. For each mesh filter, create a list of CombineInstance data for EACH sub-mesh of the mesh. You'll want the CombineInstance.mesh to point to the cached (sub-mesh) Mesh created in step 2.
  4. For each sub-mesh list of CombineInstances, perform the combine.

Sorry if the wording is poor. Hope this helps.

Update: There appears to be a bug with the MeshCreationHelper script that I linked to above. I don't believe it's properly constructing the new triangle list. I edited the wiki discussion for that page, so you can view my suggested change via the discussion:

http://www.unifycommunity.com/wiki/index.php?title=Talk:MeshCreationHelper

Comment
Tetrad

People who like this

1 Show 0 · 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

Answer by raziel_anarki · Jun 24, 2016 at 04:13 PM

just found this question using google, and i found a simpler answer while trying to figure this out:

you can use the .subMeshIndex property on your CombineInstance-s, so for each submesh you want to combine you need to add a CombineInstance with the correct subMeshIndex.

this is how i did it:

(the script is placed on a parent GO, with an empty mesh filter, and a mesh renderer. the script combines child meshes on awake, while keeping the mesh in the same position as the originals were. the resulting mesh uses only 1 material)

         void Awake ()
         {
             // save the parent GO-s pos+rot
             Vector3 position = transform.position;
             Quaternion rotation = transform.rotation;
 
             // move to the origin for combining
             transform.position = Vector3.zero;
             transform.rotation = Quaternion.identity;
 
             MeshFilter[] filters = GetComponentsInChildren<MeshFilter> ();
             List<CombineInstance> combine = new List<CombineInstance> ();
 
             for (int i = 0; i < filters.Length; i++)
             {
                 // skip the empty parent GO
                 if (filters[i].sharedMesh == null)
                     continue;
 
                 // combine submeshes
                 for (int j = 0; j < filters[i].sharedMesh.subMeshCount; j++)
                 {
                     CombineInstance ci = new CombineInstance ();
 
                     ci.mesh = filters[i].sharedMesh;
                     ci.subMeshIndex = j;
                     ci.transform = filters[i].transform.localToWorldMatrix;
 
                     combine.Add (ci);
                 }                
 
                 // disable child mesh GO-s
                 filters[i].gameObject.SetActive (false);
             }
 
             MeshFilter filter = GetComponent<MeshFilter> ();
             filter.mesh = new Mesh ();
             filter.mesh.CombineMeshes (combine.ToArray (), true, true);
 
             // restore the parent GO-s pos+rot
             transform.position = position;
             transform.rotation = rotation;
         }
Comment
Ninekorn
TrentR
PacoLabs
animus_digital

People who like this

4 Show 1 · 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 sun_stealer · Jan 18, 2017 at 11:56 AM 0
Share

Does not work for me. I always end up with mesh that only contains the 1st submesh. CombineMeshes seems to ignore all other submeshes. Had to write my own code for this.

avatar image

Answer by Ashkan_gc · Jan 16, 2010 at 06:30 PM

you can combine meshes with with combinechildren script in standard assets. if you import standard assets you can find it in component menu in mesh sub menu. you can combine a mesh with it's children with this script. you can easily modify the code to do what you want because the main part of this script is the function that combines two meshes.

Comment
mstultz

People who like this

0 Show 3 · 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 Tempest · Jan 16, 2010 at 07:19 PM 0
Share

I understand thoroughly how to combine two meshes. What I am looking for is the specific way in which you combine them while keeping the submeshes persistent.

avatar image duck ♦♦ · Jan 16, 2010 at 07:47 PM 0
Share

Could you describe the results that you're getting currently, when you use combine children?

avatar image mstultz · Apr 27, 2010 at 04:53 PM 0
Share

I've run into the same issue myself. My current thought is that perhaps it is possible to extract the submeshes into their own mesh, and then combine those. So I have 4 instances of a 2-submesh mesh. Right now I have a combined (1) mesh of 8 submeshes (8 draws). I'm hoping I can get a final result of 2 combined meshes that consist of one sub-mesh each (2 draws).

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Where is the submesh Reference of vertex/triangle? 1 Answer

Modifying Sub-Meshes at Runtime & MeshRenderer 2 Answers

Unity Multiplayer - Animations - mesh subobject movements conveyed 1 Answer

Submesh Triangles 1 Answer

Are submeshes of combined mesh can share the same mesh instance? 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