• 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 pwhitby8 · Dec 14, 2020 at 07:17 PM · meshproceduralcombinecombinemeshes

How to merge generated meshes

Hi, I have a vision of a procedural tile based (I like the tile based aesthetic) terrain generation but I am having trouble understanding how mesh merging works. I will attach my code and hopefully someone can point me in the right direction (or even suggest a better approach to this problem).

I expected this code to produce a 5 x 5 grid of tiles at y coord 0, but I don't see anything when I run it. I think my problem lies in setting the transform in the CombineInstance. At the moment I am using Matrix4x4.TRS to produce a transform but to be honest I don't really understand what I am doing, and the documentation is pretty sparse. In fact if anybody knows of a good resource for learning these kinds of processes I would be very grateful.

Thanks for reading, and for any help

 public class TilesGenerate : MonoBehaviour
 {
 
     Mesh currentMesh;
     public float tileHeight = 0.2f;
 
     public int xWidth = 5;
     public int zDepth = 5;
 
     // Start is called before the first frame update
     void Start()
     {
         gameObject.AddComponent<MeshFilter>();
         gameObject.AddComponent<MeshRenderer>();
         currentMesh = GenerateMesh();
         GetComponent<MeshFilter>().sharedMesh = currentMesh;
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 
     Mesh CreateTile(float x, float z)
     {
         Mesh mesh = new Mesh();
         Vector3[] Vertices = new Vector3[] {
             //top quad
             new Vector3(x + 0, 0, z + 0), //0
             new Vector3(x + 1, 0, z + 0), //1
             new Vector3(x + 1, 0, z + 1), //2
             new Vector3(x + 0, 0, z + 1), //3
 
             //bottom quad
             new Vector3(x + 0, -tileHeight, z + 0),     //4
             new Vector3(x + 1, -tileHeight, z + 0),     //5
             new Vector3(x + 1, -tileHeight, z + 1),     //6
             new Vector3(x + 0, -tileHeight, z + 1),     //7
 
 
             //top quad copy for sides
             new Vector3(x + 0, 0, z + 0),  //8
             new Vector3(x + 1, 0, z + 0),  //9
             new Vector3(x + 1, 0, z + 1),  //10
             new Vector3(x + 0, 0, z + 1),  //11
 
             //bottom quad copy for sides
             new Vector3(x + 0, -tileHeight, z + 0), //12
             new Vector3(x + 1, -tileHeight, z + 0), //13
             new Vector3(x + 1, -tileHeight, z + 1), //14
             new Vector3(x + 0, -tileHeight, z + 1)  //15
 
             
         };
 
         int[] Triangles = new int[] {
             //top quad
             2, 1, 0,
             3, 2, 0,
 
             //bottom quad
             4, 5, 6,
             4, 6, 7,
 
             //sides
             12, 15, 11,
             12, 11, 8,
 
             12, 8, 9,
             9, 13, 12,
 
             10, 14, 13,
             13, 9, 10, 
 
             15, 14, 10, 
             10, 11, 15
         };
 
         mesh.vertices = Vertices;
         mesh.triangles = Triangles;
         mesh.RecalculateNormals();
 
         return mesh;
     }
 
     Mesh GenerateMesh() {
         CombineInstance[] combine = new CombineInstance[xWidth * zDepth];
         int i = 0;
         for (int z = 0; z < zDepth; z++)
         {
             for (int x = 0; x < xWidth; x++)
             {
                 Mesh tile = CreateTile(x, z);
                 combine[i].mesh = tile;
                 combine[i].transform = Matrix4x4.TRS(new Vector3(x, 0, z), Quaternion.identity, Vector3.one);
                 i++;   
             }
         }
 
         Mesh mesh = new Mesh();
         mesh.CombineMeshes(combine);
 
         return mesh;
 
 
         
     }
 
     void OnDrawGizmos()
     {
         if (currentMesh != null)
         {
             foreach (Vector3 vertex in currentMesh.vertices)
             {
                 Gizmos.DrawSphere(vertex, 0.15f);
             }
         }
     }
 }
 
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 pwhitby8 · Dec 13, 2020 at 06:17 PM 0
Share

I have decided on a different and much more sensible approach following a tutorial by b3agz on youtube to create a minecraft-like game, and then simply scale the whole mesh down on the y axis to make the tiles flat instead of cubes. the first video is here: https://www.youtube.com/watch?v=h66IN1Pndd0&list=PLVsTSlfj0qsWEJ-5eMtXsYp03Y9yF1dEn

I highly, highly recommend his videos briliiantly explained, even for (almost) beginners

2 Replies

  • Sort: 
avatar image

Answer by marcos4503 · May 27 at 04:50 PM

It is possible to combine generated meshes in runtime with the plugin "Easy Mesh Combiner MT", available in the Asset Store. It is able to combine meshes without loss of quality. Unfortunately, it's not free... But it's pretty cheap. It's worth checking out. Here is the link: https://u3d.as/1rN6

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
avatar image

Answer by Rachel546 · May 29 at 05:06 AM

Load the Meshes: Load the individual generated meshes that you want to merge. Each mesh should be represented by a set of vertices and faces/triangles. Align the Meshes: If the meshes are not already aligned properly, you may need to align them so that they fit together correctly. This typically involves translating, rotating, and scaling the meshes appropriately. There are various algorithms and techniques available for mesh alignment, such as Iterative Closest Point (ICP) or Procrustes analysis. prepaidgiftbalance

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

165 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

CombineMeshes not working on one machine but is on another 1 Answer

Could anyone tell me what's wrong with my voxel rendering? 1 Answer

CombineMeshes() Not Working Properly? 0 Answers

Assertion failed on expression: '(int)lastVertex < GetVertexCount()' 1 Answer

Create a mesh from a sub mesh 2 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