Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 darkhog · Sep 11, 2015 at 04:18 PM · meshfilterfailurecombinemeshessubmesh

Mesh.CombineMeshes - only "main" meshes are combined

Here's the thing: I'm trying to combine meshes in one object for optimization reasons. But the thing is that it doesn't include stuff that is in different submeshes, only "main" mesh of the object is combined. Here's the code I'm using for that:

             if (LevEditGlobals.currentGO.transform.childCount>0){
                 ArrayList arr = new ArrayList();
                 for (int i = 0;i<LevEditGlobals.currentGO.transform.childCount;i++){
                     MeshFilter mf = LevEditGlobals.currentGO.transform.GetChild(i).gameObject.GetComponent<MeshFilter>();
                     if (mf!=null){
                     //some children may not have meshfilters
                         CombineInstance ci = new CombineInstance();
                         ci.mesh = mf.sharedMesh;
                         ci.transform = mf.transform.localToWorldMatrix;
                         arr.Add (ci);
                     }
                 }
                 CombineInstance ci2 = new CombineInstance();
                 ci2.mesh = LevEditGlobals.currentGO.GetComponent<MeshFilter>().sharedMesh;
                 ci2.transform = LevEditGlobals.currentGO.GetComponent<MeshFilter>().transform.localToWorldMatrix;
                 arr.Add(ci2);
                 CombineInstance[] cis = arr.ToArray(typeof(CombineInstance)) as CombineInstance[];
                 propermesh = new Mesh();
                 propermesh.name = "Combined Object";
                 propermesh.CombineMeshes(cis);
                 meshchanged = true;
                 
             }

It's quite lengthy, but the gist is it walks through children of specific object (if there's any) and if specific child has meshfilter, it adds its mesh to the CombineInstance ArrayList, then at the end adds "parent" object to it, generate CombineInstance array out of arraylist and combine meshes.

How to make it combine submeshes of the object if it has any as well?

Comment
Add comment · Show 7
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 Polymo · Sep 11, 2015 at 04:39 PM 0
Share

Are you sure that it's not just invisible because of a missing material or something? By default, i think, Combine$$anonymous$$eshes() does also include submeshes, if not specified otherwise.

avatar image darkhog Polymo · Sep 11, 2015 at 05:06 PM 0
Share

100% positive - when I pause the game and select object in question Unity's wireframe selector doesn't show "missing" parts.

avatar image Cherno · Sep 11, 2015 at 05:44 PM 0
Share

A look at the Scripting API reveals that Combine$$anonymous$$eshes also accepts a parameter that flattens all submeshes into one mesh, maybe that will help?

avatar image darkhog Cherno · Sep 11, 2015 at 06:30 PM 0
Share

Tried that, unfortunately didn't work.

avatar image Cherno · Sep 11, 2015 at 06:52 PM 0
Share

Bummer. Have you tried creting a new mesh via script, passing it the vehicles and triangles from the meshes that are to be combined manually?

avatar image darkhog Cherno · Sep 12, 2015 at 05:24 PM 0
Share

I don't think I'm advanced enough to do that. Plus, meshes aren't necessarily aligned, pivot-wise so I don't know how to compensate for that either.

avatar image Cherno · Sep 12, 2015 at 05:41 PM 0
Share

Shouldn't be too hard. Something like this:

 public $$anonymous$$esh[] meshArray = new $$anonymous$$esh[0];
 public $$anonymous$$esh new$$anonymous$$esh;
 
 private void CombineAll$$anonymous$$eshes() {
      new$$anonymous$$esh = new $$anonymous$$esh(); 
 
      List<Vector3> vertices = new List<Vector3>();
      List<int> triangles = new List<int>();
      List<Vector2> uvs= new List<Vector2>();
 
      foreach($$anonymous$$esh mesh in meshArray) {
          foreach(Vector3 vertex in mesh.vertices) {
               vertices.Add(vertex);
          } 
          foreach(int triangleIndex in mesh.triangles) {
               triangles.Add(triangleIndex );
          } 
          foreach(Vector2 uv in mesh.uv) {
               uvs.Add(uv);
          } 
          new$$anonymous$$esh.vertices = vertices.ToArray();
          new$$anonymous$$esh.triangles = triangles.ToArray();
          new$$anonymous$$esh.uv= uvs.ToArray();
      }
 }

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.

Follow this Question

Answers Answers and Comments

28 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

Related Questions

Gather all the Meshes into 1 1 Answer

Merging meshes 0 Answers

Are submeshes of combined mesh can share the same mesh instance? 0 Answers

Static batching - Getting submesh 1 Answer

How to know which material is used by a submesh in script ? 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