Hey. I was messing around with an idea that I had a couple of days back for implementing voxels (kinda; the implementation in the BGE’s just cubes) into the BGE, and the results are pretty encouraging.
This has a lot of implications for both Minecraft-like block based games, as well as games with a large amount of place-able and interact-able geometry.
(I asked if anyone wanted to guess to see how I achieved it; it was a stupid question. :p)
Okay, so here’s how I did it. Usually, other engines don’t just cull the blocks so that blocks behind other blocks aren’t drawn, and neither do they only draw the blocks in such a way so that only the block faces that are visible are drawn (though that does do a huge speed increase). They also draw the blocks in as few batches as possible - they merge the blocks added into a single mesh.
Unfortunately, the BGE can’t do this - neither can it add or delete faces from meshes. However, you can manipulate the faces that are there. So, what I did was create many, many cubes in a single object (they don’t have to be cubes; they can just be detached faces). Then, I wrote a function that I call Flatten, which takes other objects’ meshes and applies their vertex values to the source mesh (the cube-packed ‘fodder’ object that you see below).
So the idea is that the game first places a lot of objects (cube placeholders, really), and they are all set to be invisible. Then, the game adds the faces for the cubes, which are all in one object to draw in one batch. Finally, the Flatten function moves each of those vertices (and UV and normal values) to align to each vertex in each invisible cube placeholder, ending up with the result you see above. The remaining faces are pushed down 100000 BU so that they’re outside the camera’s view frustum (and so, the camera won’t draw those faces).
Here’s the Flatten script for those who are interested. There’s one major upside to this - it runs very quickly. Unfortunately, there are also two downsides to this method.
All faces have to have the same material and texture. Since the faces aren’t individual objects, you can’t replace their mesh, and changing materials for faces isn’t supported by the BGE (as of yet). Fortunately, the script copies UV values, so different blocks would indeed appear different, as long as they were all on a single texture image.
It requires that you have enough faces available in your game to supplement each ‘chunk’ of land. The larger the chunk, the more faces each ‘fodder’ object must have to cover it. This makes the game start up slowly if your chunk data’s large (not even add the mesh slowly, just starting up and loading the polygons into memory is kinda slow). Fortunately, you only have to have one chunk data, as you can use LibNew to make a new mesh from the first one. Currently, it’s pretty usable. So… Anybody want to try an open-source Minecraft from this? :rolleyes:
If you want to download the demo, you can do so here.