I’m trying to make a voxel engine just for fun. I have been extending Voxity (from the asset store) with concepts and modified code from http://alexstv.com/index.php/category/voxels
My dictionary of chunks uses the chunk’s transform position as its key. Finding a chunk uses this:
Any value greater than chunk size gets rounded up to the next possible chunk position. Any thing lower gets floored to the next lowest possible chunk position (works for negatives as well). This succeeds every time. I have never had it return the wrong value for a chunks position in the world.
The issue I’m having (as far as I can tell at the moment) is that the lookup fails, returning null chunks at incomprehensible times (to me at least. I tried debugging but I didn’t learn anything I didn’t already see: I’m getting null chunks returned at odd times [I used mono to debug, not Debug.Log]).
Despite the fact that the neighboring chunks exist (as verified in the editor by checking neighboring positions), the look up fails sometimes, but succeeds most of the time.
The blocks of a chunk are stored in a [16,16,16] array.
I use this to determine when to draw a face (its ugly):
The face doesn’t draw when if its neighbor is null. If i remove this check, EVERY face gets drawn every time. Since the chunk is null, asking a block for its index will return 0 (I assume), meaning the face is drawn.
removed null check on +x axis:
This confuses me even more. I’m not even sure whats going on. The Vector3 key always seems to be correct, yet the look up fails at odd time! Unless I don’t check for null chunks, then it always fails? Obviously I’ve got the wrong idea of what is actually happening.
If you can, please help me out!
Make sure that you generate the entire world’s data before generating the mesh.
I tried to make a voxel editor, and my problem was that it would generate a chunk and it’s data, find all the surrounding chunks (some of which didn’t even exist YET), and generate the mesh based on that.
So, if you’re generating the data & mesh at the same time, well, don’t. Use something like a coroutine to delay mesh generation until each chunk’s data is generated.
Rioku, I do generate the mesh right after the chunk has been created. It doesn’t seem to effect anything negatively (yet), especially since I make sure the neighbor chunk != null before checking its blocks (even if I don’t check for null, grabbing what would be a null block returns a default air block, causing the face in question to be built). Who knows, maybe this will cause issues once I get the look up working as intended. I plan on changing it soon regardless (instead of calling updateMesh, I’ll just add the chunk to a list of chunks to update once all the blocks have been filled in). Thanks for the tip!
Thermal, I think that is the problem. I made a new IntVector3 struct to deal with the it and altered all my functions to use that instead. The good thing is that look up always succeeds! But now I have a new problem: My chunks don’t generate at all due to rounding errors trying to access negative indices within the chunk’s block array. At least now I can accurately grab any chunk and try to write outside of it’s array I guess haha.
The IntVector constructor floors any floats it receives automatically. I hope that’s not the problem.
Trying to step through the code to find out what goes wrong.
Sigh. On a positive note I’m learning quite a lot about debugging ^.^