Hi!
I’m currently experimenting with a tile-grid-based 3d prototype and have huge performance issues. Every tile is an individual gameobject with its own attributes. However, as soon as I have 100+ tiles in the scene, I get severe performance problems. Naturally, because there’s so many objects in the scene. But how do games like minecraft where theres hundreds of thousands of blocks in the scene at all times handle this? What is the magic?
How complex are the blocks? Are the blocks updating something in the update? The performance depends on your scripts, as well as the graphics.
Does this lag happen in a built version of the game?
On the graphics side, It’s a bad idea to render a multitude of individual small meshes. The GPU does a better job at rendering huge meshes at once. So it’s often a good idea to combine the small meshes into one big meshes. This process is called batching. Note that Unity does this optimization automatically for object marked as static. Or for some dynamic small meshes when the option is enable.
Yeah, you can’t really have 100 active game objects in your scene without huge performance problems.
The way games like minecraft handle this is by grouping cubes into chunks of size 16x16 in X an Z direction and infinite in Y direction, after what you end up only with their surface in a single mesh.
For example if you have a flat area you’ll end up with a plane just like the unity’s built-in plane (the built-in plane is actually 10x10). You can see here why Y direction doesn’t matter much when combining the cubes.
Notice that the bottom and side parts of those cubes wont be in the final chunk in case there are other blocks next to it, otherwise it will have those sides and bottom.
Also a single chunk might end up having a lot of small “parts” so you would have a lot of sides in a single chunk and will have a lot of geometry that will have to be recreated every time a single cube has changed and this is why you have to limit the size of individual chunks.
This can be complicated for beginners to accomplish without using specialized tools, since you have to procedurally generate the terrain mesh every time it changes and since I see you have only 13 posts so far I’d suggest instead making a game with lower experience barrier and maybe return to this when you are at least lvl 40. That’s around the time when you should have enough skill points to acquire the Generate Mesh spell, but you might need to first stack some skill points in the Procedural skill tree.
Game development is an exercise in seeing how much you can cheat without making the player aware. If you’re curious about Minecraft in general I recommend reading through the forum thread that spawned a few years back.
http://forum.unity3d.com/threads/after-playing-minecraft.63149/
if you’re looking into making a huge number of tiles, you can look into the flyweight design pattern.
the gist of the pattern is that there is only one instance with a data structure holding the state data for all the tiles in the level (like an array or more likely a Hashtable by position for minecraft). the tiles can’t Update themselves since they don’t carry their own instance, alternatively actions are performed on them through a stateless/immutable class using the injected data passed in from that single instance class holding the data structure.
Minecraft likely does a similar thing. ever load into a custom map and you see sand/gravel, or lava/water float in the air? they don’t update because the blocks can’t update themselves, something has to tell them to update (either the player, neighboring blocks in their update, or an event like an explosion). the blocks likely only store a single number representing the block’s ID, and possibly a light level as well
the fact that theres only one instance reduces the overhead on the memory and garbage collection, while only allowing updates through explicit calls to a block’s update reduce CPU overhead
This is a huge field you’re getting into, but hey… it’s a fun one. Here’s a great tutorial covering many of the strategies that stick out to me for helping your project.