aiekick / MagicaVoxel_File_Writer

MagicaVoxel File Writer dependency free cpp class
MIT License
35 stars 10 forks source link

Improve handling of voxels with negative coordinates #8

Closed PJayB closed 1 year ago

PJayB commented 1 year ago

Consider a box that spans the following coordinates: (-200, 0, 0) (-100, 50, 20). This box would span two cubes because it's wider than 126. The output should be two cubes at (-252, 0, 0) and (-126, 0, 0) and the voxels should form a contiguous box.

However, because the wrapping of the voxels is broken right now, the voxels get written into the .vox file with coordinates that exceed 126 due to wrapping on the uint8_t. This has one of two effects: (1) the voxels exceed the bounds of the parent cube, and/or (2) MagicaVox crashes.

Wrapping the voxels correctly inside the cube produces the correct output and prevents the crash.

Without this fix, the only workaround is to force users of the library to shift all their voxels into a positive coordinate space, which seems unnecessary. This fix is especially convenient for people working with geometry that has its origin around (0,0,0), e.g. game models, as the geometry is likely to extend out from (0,0,0) in all directions.

Broken case. Note how the box is broken up, and the left box exceeds the cube bounds. Broken case

Fixed case. The voxels are correctly aligned contiguously. Fixed

aiekick commented 1 year ago

ok i see.

thanks for the explanation :)