• 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 UMSDev · Aug 12, 2018 at 12:31 AM · meshescombinemeshes

Combine Meshes issue

I'm trying to combine "seats prefabs" into a single mesh at runtime to save some draw calls.

My seat contains 2 MeshFilter, a base and a top (= the seat, red on the picture) with 2 different materials. My seat is created from an imported FBX which has read/Write enabled.

I'm using pooling if it matters : I generate 500 seats, then I pick 250 from the pool to create my stands, then I combine them, release the pooled seats and reuse them to combine another stands.

Here is the issue that appears on some seats (not all, only the 5th and 6th stands so maybe linked to some pooling issue ? reusing the same mesh several times), some "tops" of seats are "linked" together issue

Here is the function I use :

   List<CombineInstance> baseList = new List<CombineInstance>();
         List<CombineInstance> topList = new List<CombineInstance>();
 
         Material materialTop = null;
         Material materialBase = null;
 
         //Loop through the array with trees
         for (int i = 0; i < seats.Count; i++)
         {
             GameObject currentSeat = seats[i];
 
 
             //Get all meshfilters from this seat, true to also find deactivated children
             MeshFilter[] meshFilters = currentSeat.GetComponentsInChildren<MeshFilter>(true);
 
 
             //Loop through all children
             for (int j = 0; j < meshFilters.Length; j++)
             {
                 MeshFilter meshFilter = meshFilters[j];
 
                 CombineInstance combine = new CombineInstance();
 
                 //Is it top or base?
                 MeshRenderer meshRender = meshFilter.GetComponent<MeshRenderer>();
                 
                 string materialName = meshRender.gameObject.name;
 
                 if (materialName.Contains("top"))
                 {
                     combine.mesh = meshFilter.mesh;
                     combine.transform = meshFilter.transform.localToWorldMatrix;
 
                     //Add it to the list of leaf mesh data
                     topList.Add(combine);
 
                     if (materialTop == null) materialTop = meshRender.material;
                 }
                 else if (materialName.Contains("base"))
                 {
                     combine.mesh = meshFilter.mesh;
                     combine.transform = meshFilter.transform.localToWorldMatrix;
 
                     //Add it to the list of wood mesh data
                     baseList.Add(combine);
 
                     if (materialBase == null) materialBase = meshRender.material;
                 }
             }
 
 
             //Deactivate the seat the pool 
             GameController.This.Prefabs.DestroyPooledObject(currentSeat);
         }
 
 
         //First we need to combine the bases into one mesh and then the tops into one mesh
         Mesh combinedBaseMesh = new Mesh();
         combinedBaseMesh.CombineMeshes(baseList.ToArray());
 
         Mesh combinedTopMesh = new Mesh();
         combinedTopMesh.CombineMeshes(topList.ToArray());
 
         //Create the array that will form the combined mesh
         CombineInstance[] totalMesh = new CombineInstance[2];
 
         //Add the submeshes in the same order as the material is set in the combined mesh
         totalMesh[0].mesh = combinedBaseMesh;
         totalMesh[0].transform = parent.transform.localToWorldMatrix;
         totalMesh[1].mesh = combinedTopMesh;
         totalMesh[1].transform = parent.transform.localToWorldMatrix;
 
         //Create the final combined mesh
         Mesh combinedAllMesh = new Mesh();
 
         //Make sure it's set to false to get 2 separate meshes
         combinedAllMesh.CombineMeshes(totalMesh, false);
 
         var meshFilterParent = parent.GetComponent<MeshFilter>();
         if (meshFilterParent == null) meshFilterParent = parent.AddComponent<MeshFilter>();
         meshFilterParent.sharedMesh = combinedAllMesh;
 
 
         var meshRenderer = parent.GetComponent<MeshRenderer>();
         if (meshRenderer == null) meshRenderer = parent.AddComponent<MeshRenderer>();
         meshRenderer.materials = new
             Material[]
             {
                 materialBase,
                 materialTop
             };


seats-issue.png (137.2 kB)
Comment

People who like this

0 Show 1
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 UMSDev · Aug 14, 2018 at 12:11 PM 0
Share

i've tried to merge submeshes but then the materials don't work anymore, it's just using the first material for all.

0 Replies

  • Sort: 

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

87 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

Related Questions

Geometry jobs should not be launched for empty tasks error 1 Answer

Merging meshes 1 Answer

How to combine object hierarchy meshes as a single one 3 Answers

Combine N different meshes with M different materials on each 0 Answers

Custom Meshes? 1 Answer


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