I am trying to create a Minecraft like program. I am running out of Direct Memory when I try to increase the size of the world.
The program uses an instance of the Box class for each “cube”. I am finding that e.g. 1 million boxes needs around 1 gigabyte of Direct Memory. That works out to around 1000 bytes per Box.
Note that this is Direct Memory (these are DirectFloatBuffer instances as far as I can tell). JVM heap usage is fine.
Does this sound right?
The following code example illustrates the issue I am getting. Run this with a heap size of 2GB to reproduce the behaviour.
public static void main(String[] args) {
// Run with -Xmx2048m
int rows = 2000;
List<Spatial> spatials=new ArrayList<>();
for(int i=0;i<rows;i++){
if(i%100==0)
System.out.println("Created "+i+" rows");
Node rowNode = new Node("parent");
for(int j=0;j<rows;j++){
Box box = new Box(1, 1, 1);
Geometry geometry = new Geometry("geometry", box);
rowNode.attachChild(geometry);
}
Spatial optimised = GeometryBatchFactory.optimize(rowNode);
spatials.add(optimised);
}
}
When I run this I get an exception
Created 1100 nodes
Exception in thread “main” java.lang.OutOfMemoryError: Direct buffer memory
Note that I am using GeometryBatchFactory.optimize() to reduce heap usage enough to show the problem with Direct Memory. If I just create Box instances, the direct memory usage is the same, but the program runs out of heap before it runs out of Direct memory.
If you are not set on rolling your own framework, you can use Blocks. You can checkout the page on the store or take a loot at the topic on the forum.
It will take care of the chunk management and mesh generation for you.
If not, you should rethink the way you are solving the issue. As paul said, a block world is not made of boxes. It’s made of different meshes that look like a lot of boxes stacked on top of each other.
6 sides * 4 vertices per side = 24 vertices.
24 verteices * 3 floats for position * 3 floats for normal * 2 floats for texture coordinates = 432 floats = 1728 bytes.
…and that’s not including the 6 shorts per side. 36 shorts * 2 = 72 bytes.
Or the overhead just for the object, etc…
So I guess a single Box mesh is taking up even more than 1000 bytes.
@remy_vd My worlds are based on a horizontal grid, but vertically I do not want to be constrained to a grid. I want to e.g. have a block that is 1x1x0.3 high. Does Blocks support this? The sample code I looked at implies blocks are all 1x1x1 in size.
Using Box objects allowed positioning and sizing the blocks to arbitrary positions.
This was one of the limitations of Minecraft I was hoping to avoid by writing my own application.
Even supporting blocks with heights less than 1 would be OK. For example, Minecraft has some blocks that are “slabs” that have a height of 0.5. I was using this but it didn’t look great.
Yes, but there is a very good reason that block world games do not do this. It’s effectively impossible to optimize beyond some simple “doesn’t matter” level stuff.
The reason block worlds work at all is because they are on a grid. You can get away with subdividing the grid in some cases but you will never get away from the grid.
I know a little bit about what I’m talking about here as this is my game-in-progress for some time: http://mythruna.com/
…and is the least blocky of all of the block-worlds that I’ve seen.
I was thinking 8 vertices and 6 faces. Like the “upper” quad and the “lower” quad, everything else could be with shared ones, couldn’t that work?
Either way I underestimated the normals, texture coordinates and indices etc.
The default cube shaped block is indeed 1x1x1. But you can change this if you want. Using the Slab shape, you have a cube shape with a modifiable height.
By default, Blocks has a slab and a ‘double’ slab block. The first one has a height of 1/3, the second one a height of 2/3.
@remy_vd I ended up creating 20 different Slab blocks at 0.05 intervals, between 0.05 and 0.95 units high. It looks fantastic and performance doesn’t seem impacted, as far as I can tell. The terrain looks almost rounded now. Much better than I ever got from Minecraft!