Currently, I am using THREEJS to create a dynamically generated 'minecraft' world utilizing a perlin noise generator.
Check out the progress so far: Block World
Everything is going smoothly except that I am facing significant performance issues once the number of 'objects' reaches around 7,000.
My world usually consists of approximately 12-14 thousand objects when the dimensions are set to a maximum of 64*64*6 (the Perlin map only uses a portion of these blocks).
I believe what I need to do is only render the blocks that are visible to the camera and implement "Occlusion Culling," but I have been unable to find any working examples for this in ThreeJS.
One idea I had was to perform Raytracing for each pixel on the screen to check for intersections with the first object:
raycaster.setFromCamera( mouse, camera );
var intersects = raycaster.intersectObjects( scene.children );
However, I suspect this approach may be too slow.
Another thought was to merge all meshes together into one, which did improve frame rates significantly. Unfortunately, I lost the ability to manipulate individual blocks and their textures. So, merging may not be the ideal solution.
I am hoping someone can guide me on how to hide meshes that are out of view due to being obstructed by other objects.
Thank you
EDIT: Regarding suggestions to implement frustum culling, I have already tried this
var counter = 0;
//
// Only draw objects that you can see... I think
frustum.setFromMatrix(new THREE.Matrix4().multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse));
for (var i=0; i<world.length; i++) {
for (var j=0; j<world[i].length; j++) {
for (var d=0; d<world[i][j].length; d++) {
if ( world[i][j][d].type !== -1 ) {
if ( frustum.intersectsObject( world[i][j][d].block ) ) {
world[i][j][d].block.visible = true;
counter++;
}
}
}
}
}
console.log(counter);
The counter displayed the number of visible blocks at any given moment, significantly lower than the total number of blocks. However, my framerate decreased drastically with this method, making movement within the scene unbearable.