• 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 $$anonymous$$ · Aug 24, 2018 at 11:13 PM · meshcombinemeshes

How do I combine meshes in code?

So let's say I have six separate sides that I created that represents a cube. But I'll need to mash all six side meshes into one mesh that would represent a cube. And to be even more efficient, I need to take all cube meshes within a 16x16x256 area and combine them into a chunk mesh, which will then be attached to a GameObject. So I know how to create all the different sides (right, left, top, bottom, front, back) but I just can't figure out to combine the meshes together. I also need to have multiple materials for the same mesh if that is possible. The code below demonstrates how I go about creating a side.

 public Mesh CreateSide(Vector3[] vertices, int[] triangles){
     Mesh mesh = new Mesh ();
     mesh.vertices = vertices;
     mesh.triangles = triangles;
     mesh.uv = standardMeshUV;
     MeshUtility.Optimize (mesh);
     mesh.RecalculateNormals ();
     return mesh;
 }
 public Mesh CreateFrontSide(){
     Vector3[] vertices = new Vector3[] {
         new Vector3 (-1, 1, 1),
         new Vector3 (1, 1, 1),
         new Vector3 (-1, -1, 1),
         new Vector3 (1, -1, 1)
     };
     int[] triangles = new int[] {
         0, 2, 3,
         3, 1, 0,
     };
     return CreateSide (meshFilter, vertices, triangles);
 }
Comment

People who like this

0 Show 3
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 Hellium · Aug 24, 2018 at 11:25 PM 0
Share

Did you try the example of the documentation?

https://docs.unity3d.com/ScriptReference/Mesh.CombineMeshes.html

avatar image $$anonymous$$ Hellium · Aug 24, 2018 at 11:51 PM 0
Share

There's a lot going on in that example, I'm looking for the simplest example of combining a list of meshes into one mesh.

avatar image $$anonymous$$ Hellium · Aug 25, 2018 at 08:09 AM 0
Share

Okay, I don't think Mesh.CombineMeshes is what I'm looking for, I think that I just need to add in all the vertices and triangles and such from every mesh that I'm looking at.

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by $$anonymous$$ · Aug 27, 2018 at 02:02 AM

Apparently, the simplest option is to just store all the meshes in GameObjects, combine them using mesh.CombineMeshes, and then delete all the GameObjects that were created. I'm sure there is a better option in terms of taking up fewer resources, but it seems to work alright.

     public GameObject CreateChunk(short chunkX, short chunkY){
         
         List<GameObject> physicalBlocks = new List<GameObject> ();
 
         for (short y = 0; y < height; y++) {
             for (short x = (short)(chunkX * chunkSize); x < (short)(chunkX * chunkSize) + chunkSize; x++) {
                 for (short z = (short)(chunkY * chunkSize); z < (short)(chunkY * chunkSize) + chunkSize; z++) {
                     GameObject physicalBlock = CreatePhysicalBlock (blocks [x, y, z]);
                     if (physicalBlock != null) {
                         physicalBlocks.Add (physicalBlock);
                         physicalBlock.name = x + "," + y + "," + z;
                     }
                 }
             }
         }
         //Merge physical blocks into one chunk.
         CombineInstance[] combine = new CombineInstance[physicalBlocks.Count];
 
         for(int i = 0; i < physicalBlocks.Count; i++){
             combine[i].mesh = physicalBlocks[i].GetComponent<MeshFilter> ().sharedMesh;
             combine[i].transform = physicalBlocks[i].transform.localToWorldMatrix;
         }
 
         GameObject chunk = new GameObject ();
         chunk.AddComponent<MeshCollider> ();
         chunk.AddComponent<MeshFilter> ();
         chunk.AddComponent<MeshRenderer> ();
         chunk.transform.GetComponent<MeshFilter> ().mesh = new Mesh ();
         chunk.transform.GetComponent<MeshFilter> ().mesh.CombineMeshes (combine, true);
         chunk.GetComponent<MeshCollider> ().sharedMesh = chunk.GetComponent<MeshFilter> ().sharedMesh;
         chunk.GetComponent<MeshRenderer>().material = atlasMaterial;
         chunk.transform.GetComponent<MeshFilter> ().mesh.RecalculateBounds ();
         chunk.transform.GetComponent<MeshFilter> ().mesh.RecalculateNormals ();
         MeshUtility.Optimize (chunk.GetComponent<MeshFilter> ().mesh);
 
         for (int i = physicalBlocks.Count; i > 0; i--) {
             Destroy (physicalBlocks[i - 1]);
         }
 
         chunk.name = "Chunk_" + chunkX + "_" + chunkY;
         return chunk;
     }
Comment

People who like this

0 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

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

98 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

Related Questions

How to avoid or turn-off "Combined mesh (root scene)" feature. 3 Answers

Why is raycast not working with mesh collider 0 Answers

How to remove internal triangle/faces when combining mesh 0 Answers

Combining meshes clears old mesh 0 Answers

Combine meshes at runtime without all mesh as children of mesh 0? 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