Hey. So instead of working on my games or tutorials, I messed around in the game engine, heh. This time, the subject that I was interested in was voxels. I tried to use them before, with pretty poor results, as I couldn't get a large number of voxels to render in the BGE. Well, I then had an idea, which I tried to implement into the BGE, with pretty good results.
Usually, other engines that deal with voxels don't just cull the blocks so that blocks behind
other blocks aren't drawn. 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 give a fairly huge speed increase). They also draw the blocks
in as few batches as possible - they effectively merge the blocks added into a single mesh.
Unfortunately, the BGE can't do this, merging meshes together. 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 a single 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 (or whatever number) so that they're outside the camera's
view frustum (and so, the camera won't draw those faces).
There's one major upside to this method - it runs very quickly. Unfortunately, there are also two downsides to this method.
1. 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.
2. It requires that you have enough faces available in a 'fodder' object 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.
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.
So... Anybody want to try an open-source Minecraft from this?
You can download the demo for yourself here. It's made for Blender 2.6. By the way, if you do use the scripts or assets, you may not distribute them outside of personal projects without prior consent from me, the author.
This is pretty much :)
ReplyDeleteI got error,
ReplyDelete-----------
Python module can't be import - object 'ChunkSpawner' , controller 'Python':
Traceback :
..\BGEVoxelTest\System.py", line 15, in
import BGHelper
ImportError: No module named BGHelper
----------
I used Blender 2.62
@Anonymous - D'oh. Didn't realize it used my BGHelper module. I uploaded a different version with the module contained. Try the download again.
ReplyDeleteThanks SolarLune, it works
ReplyDeleteVery nice example ;)
ReplyDeleteBy the way, do you know some way to alter the point of gravity in blender?
Imagine we create voxel-world-sphere and want our characters to move on it, jumping and being atracted into the center of the sphere.
thx in advance.
@Sergio - I don't know of one off-hand, but I do know that such scripts exist on the BlenderArtists forum. Such a script wouldn't really work for this particular application, though, as the cubes should be non-collision enabled, as to have physics enabled on each individual cube would be too hard on the physics engine. You would code the movement using a list of positions where cubes are.
ReplyDelete@Sergio Alexio I wrote such a script a couple weeks back: http://blenderartists.org/forum/showthread.php?261417-Gravity-simulation-script I hope it helps! :)
ReplyDelete