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 $$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
Add comment · 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/$$anonymous$$esh.Combine$$anonymous$$eshes.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 $$anonymous$$esh.Combine$$anonymous$$eshes 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

· Add your reply
  • Sort: 
avatar image
0
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
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.

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

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

500K upper bound on total number of meshes? 0 Answers

Mesh.CombineMesh() results in different heights between objects 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