• 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 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
Add comment · 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-5e$$anonymous$$tXsYp03Y9yF1dEn

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

163 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

Related Questions

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

Drawcalls vs MeshCombiners 1 Answer

skinned mesh renderer, combine meshes can't see the result, what am I doing wrong/ missing? 0 Answers

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

How to combine multiple new meshes without instantiating? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges