- Home /
Creating 3D representation of procedurally generated 2D tile map
I am trying to build a simple procedurally generated map, which basically is just a 3D representation of a 2D tile map; let's assume, for simplicity's sake, that there are only two types of tiles: "wall" and "floor".
Now I'm looking for a good way to build mesh of such map. My first (somewhat naive) attempt was to create walls by cloning and positioning a cube model (the floor is just one big plane mesh).
This is somewhat working, but now I get random dark lines between wall blocks (pointed by red arrow on the screenshot). Also, I don't like it anyway, because there always will be a lot of faces that will never be displayed, which is probably not very optimal.
So, the question is, what would be the best way to build such map? I don't want to just generate the whole mesh vertex by vertex, because later I will probably want to make models of wall blocks a bit more complicated, by adding some details to them. I thought about constructing the map by cloning not cubes, but separate faces (squares); I haven't tried that yet, but I'm not really sure if it'll help me with that "dark line" glitch. May be I should not just create group of cube/square clones, but rather combine them into a single mesh somehow? Or is there some other way I haven't thought about?
Answer by Eric5h5 · Mar 18, 2012 at 03:00 AM
You should use the Mesh class and construct the maze as a mesh, or a group of meshes. That's what I did with Realmaze3D...the walls, ceilings, and floors are quads built completely procedurally, whereas the details such as ladders, lights, and wall ornaments are meshes made in Blender. They are stored as an array of meshes in Unity, and the appropriate mesh data is copied into the maze mesh as needed. It's rather more complicated than just instantiating objects, but it's also far faster than having hundreds/thousands of separate objects, and there are no seams. The maze is broken into a number of smaller mesh sections, which are enabled/disabled based on the player's view, in order to provide custom occlusion culling (since it's impossible to use Unity's built-in occlusion culling in a dynamically-generated environment).