[Week 9.5] three.js notes and links

I've been stuck at performance for the past few weeks, but here are a few notes I've made about three.js:
  • three.js has a function called THREE.GeometryUtils.merge(geometry, otherGeometry). As noted here, merging geometry will reduce the number of WebGL calls leading to better performance. I've tried it out myself, and it certain does allow for rendering more cubes more efficiently. However, I've been at a loss about how to remove cubes from a merged geometry. It seems that the merge function essentially concatenates the vertices, faces and uv arrays of both geometries, so reversal might be hard as it requires knowing which array indices are relevant to the deleted cube.
  • Textures can be done by having a texture array (MeshFaceMaterial), and each face having a materialIndex which indexes into the array. Unfortunately, materialIndex's can't be changed after an object has been rendered once, but the texture referenced by the index can be (reference).
  • Vertex buffers cannot be resized after an object has been rendered once (reference). It seems that you can preallocate enough space in the buffer, but for a voxel environment that could be quite memory-intensive.
  • three.js has support for shaders (reference).

As for my program, I initial envisioned that the player would start out on a plane of cubes, and this would be their starting platform. The plane would extend as the player moved around. However, since I'm having trouble rendering the cubes efficiently I'm currently trying to just render a single flat plane with a repeated texture (example here), and allow players to build off that.

Cubes are currently stored within 16x16x16 chunks. The chunks are stored in a hash table, and the blocks within each chunk are stored in a 3D array. So far the hash table has been working out fine, but then again all tests so far have only gone up to as much as 500 chunks.

With the new program there are currently a few bugs:
  • Chunks sometimes disappear depending on where the player is and where they are looking
  • Blocks are sometimes non-selectable, instead selecting a block behind it
  • The wrong texture is being rendered