So we now have a randomly-generated world filled with cube. Cool! I’m pretty happy of the outcome. However, there are way too many gameobject being drawn and the FPS drop quite low easily. We now need to do some culling to only draw what is in front of us.
Unity already does a bit of culling for us, it doesn’t draw object that are not in your sight of view! However, we need to determine ourselves what we want to be drawn on the camera view. In the code we have so far, we draw all of the cube. An easy way to remove a lot of cube is to avoid drawing cube that are enclosed by 6 cubes around it. There is no way such a cube will ever be seen by the user. This isn’t really hard to do, before drawing a cube, you look around it in the blocksArray. If all cube around it are solid, don’t draw it. In my code, I decided to not create the object at all. This will easier to process for now.
I also added a small GUI to the project. When you press F1, you can see some information about the cube being created and the cube being occluded. This allow to see the number of cube being removed. Here are the hotkey :
F1 – See information on cubes
Q – Generate and draw the cubes
R – Remove all the cubes
Z – Turn Culling ON/OFF, can be interesting to test.
I added the Voxel Engine on GitHub. It contains the whole Unity Project. You can find the project as it is at the end of this part here. The master branch will hold the up-to-date version of the project.
If you just want the code-source without the rest of the Unity’s Project, here it is! However, you will need to create the Unity project yourself. Just put PlayerHUD and CubeGeneration.cs on the __GameMaster GameObject and it will work fine. You can change the size of the area created in Chunk.cs Have Fun!