- Home /
Voxel Dictionary Lookup Issues
Thanks in advance for reading and any suggestions you have! I realize this problem is very specific, but I'm hoping I either don't understand dictionaries or I've overlooked something obvious. Any help will be greatly appreciated!
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]).
Here are some missing faces:
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):
if (x + 1 >= chunkSize) //ie, x > 15, the outer bound of the array
{
Vector3 newX = new Vector3(nX + 1, nY, nZ); //nX is the chunk's position + x
if (World.GetChunk(newX) != null && World.GetBlock(newX).index == 0) // It fails here.
DrawFace(x+1,y,z,Vector3.up,Vector3.forward, true, chunk[x,y,z].index);
}
else if (chunk[x+1,y,z].index == 0) // We are within array bounds, so just check the block
DrawFace(x+1,y,z,Vector3.up,Vector3.forward, true, chunk[x,y,z].index);
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.
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!
Your answer
![](https://koobas.hobune.stream/wayback/20220613191727im_/https://answers.unity.com/themes/thub/images/avi.jpg)