0
\$\begingroup\$

The terrain in my game must be created using tiled assets. I have unique textures for each tile (all with the same height and width that can be used to compose the terrain) and the information of each map is baked in binary files; these binary files contains each tile height and texture.

I'm thinking about creating the terrain at runtime (or create a tool to bake the terrains in unity's proper formats). Each tile would be an mesh composed by 2 triangles. This way I can represent individually each tile and associate a unique tile texture for each tile. My concerns is about performance as it will end up with several different meshes just for the terrain.

Am I thinking in the right direction? Is there other better way to implement a tiled terrain like this in Unity3d?

\$\endgroup\$
4
  • \$\begingroup\$ Have you considered using a texture atlas or array texture so that you can draw many of these tiles on a single mesh, for better batching, rather than drawing many batches with just two triangles in them? GPUs are generally most efficient when working on large batches. \$\endgroup\$
    – DMGregory
    Nov 25, 2020 at 18:58
  • \$\begingroup\$ Yes I thought, but I was worried that this would take too much "unnecessary" disk space as the game have have hundreds of different maps composed by hundreds of tiles each of them. If I bake each map atlases for each map it will end up taking very much more disk space as it will end up with many repeated tiles (and I plan to deploy the game to mobile devices). Let me know if I understood what you suggested! And thanks for the answer. \$\endgroup\$ Nov 25, 2020 at 19:40
  • 1
    \$\begingroup\$ There's no requirement to save every permutation of the atlas to disc. Just build in memory the one you need for the current level. Many games update their terrain texture atlas on the fly as the player navigates the world. \$\endgroup\$
    – DMGregory
    Nov 25, 2020 at 20:05
  • \$\begingroup\$ Oh now I got it! Yes I think this can handle the whole thing. Thanks again for the help @DMGregory! \$\endgroup\$ Nov 25, 2020 at 20:10

1 Answer 1

1
\$\begingroup\$

Whether performance is an issue depends on the size and quantity of the tiles. If the environment is 10x10 tiles, there shouldn't be any performance issues. If the environment is 1000 x 1000 tiles, spawning 1 million tiles at once is probably going to kill performance.

If the map is very large (in terms of number of tiles) but only a small portion of it is visible at a time (e.g. due to a top-down camera angle), you can use a pooling system. E.g. say the map is 1000 x 1000 tiles but only 10 x 10 tiles are visible at once. You could spawn a 12 x 12 block of tiles, which gives you 1 row of padding on each side. When the camera moves towards one edge of the screen, you move the tiles from the opposite edge of the screen over to create the new row of tiles that's coming into view.

Here's a diagram with a 2x2 viewable area and 1 row of padding around each edge.

enter image description here

\$\endgroup\$
3
  • \$\begingroup\$ So I was thinking in the right direction? I was afraid there could be a better way to implement this system as Unity seems so polished and simple to use. \$\endgroup\$ Nov 25, 2020 at 19:44
  • 1
    \$\begingroup\$ I can't say for certain. You haven't provided enough information. Is your terrain flat or 3D? What do the tiles look like? How many tiles will there be in one map? How many tiles will be visible at once? (If your tiles are going to be flat, use a Tilemap: learn.unity.com/tutorial/introduction-to-tilemaps# ) \$\endgroup\$
    – Kevin
    Nov 25, 2020 at 19:49
  • \$\begingroup\$ the terrain is 3D. Each vertex of a tile can have a different height component. There are hundreds of maps and each of them are composed by hundreds of tiles. \$\endgroup\$ Nov 25, 2020 at 19:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .