Five months ago I started messing around with voxel-engine and it has thoroughly re-kindled my love for measuring and optimizing code. You can see a demo of my project in my previous video post. However, I’ve taken the three.js-based engine as far as it can go. I want to increase the draw distance, want world chunks to load and draw faster, to make changes to the physics engine and reign-in the garbage collection pauses (caused by excessive memory allocation). So I took the plunge, and I’m now 1.5 months into a full re-write of the voxel-engine code. The good news is that I finally have something to show (the image above).
The road has been rough. Very rough. For 2 weeks I slogged through confusing collision detection logic. For three weeks I iterated on the right way to handle meshing, gl buffer creation, and first-person camera matrices. I was also stumped for a week trying to figure out why every texture looked so horribly distorted. Who knows why it took a week for me to break down and ask about it on Stack Overflow, but I finally did. Within an hour I had the answer: I was setting the canvas size using CSS, which merely scales up the canvas. All canvas drawing was being done at the default width of 300px, stretched to fill my whole screen. What!? Thank jibbers for Stack Overflow.
A run-down of the features and benefits of the new setup:
- Mostly-working physics engine
- Can draw further and maintain a steadier, higher framerate
- Mostly-working meshing algorithm (combines like faces of adjacent cubes into a single, long face … less stuff to draw)
- Stable first-person camera setup (though, admittedly, it still feels like black magic)
- An object pool that I can extend beyond arrays
- More reliable websocket emitter (we can now handle disconnects properly!)
- Fewer “streams” abstractions when there’s no need for them
- Block creation
- Block destruction
- Material picker
- Portions of the multiplayer experience
I’m so damn stoked. Feels good, man.
There’s stuff I’m leaving out, but it’s getting late. I’ll be pushing the code to github in the coming weeks, though I’ll probably re-work the server component to use my new websocket emitter first. Take care.