enu icon indicating copy to clipboard operation
enu copied to clipboard

"PackedChunk" format

Open dsrw opened this issue 4 months ago • 1 comments

Chunks are currently 16 * 16 * 16 [Vector3, VoxelInfo] zen tables. Each new voxel triggers a change/network message and can be slow. I believe this is a big part of the poor network performance we sometimes encounter.

I'd like continue using ZenTable[Vector3, VoxelInfo] as the chunk API, but the tables should no longer sync. Instead, each chunk should build a PackedChunk after all other changes are done, but before syncing changes to subscribers.

A PackedChunk is just a byte array. 0 represents an empty block, and values 1-240 (or something like that) represent present blocks. Currently only 8 of these will be used from our standard enu colors, but at some point we'll support custom blocks and these will likely be index values from a pallet. Values > 240 will be for command bytes. Currently the only commands are Repeat and Done.

The first byte will represent vector (0, 0, 0), the 2nd (0, 0, 1) etc until the 17th byte, which will represent (0, 1, 0). At most a PackedChunk will be 4,096 bytes (16 * 16 * 16). However, if there are 3 or more blocks with the same value in a row, they can be compressed with a Repeat byte followed by a count (1 - 256, represented as 0 - 255), which repeats the voxel proceeding the Repeat byte count times. If a block is repeated more than 256 times another Repeat byte can be placed with an additional count.

It may be worth adding Repeat2x, Repeat10x, etc bytes to better compress long stretches of repeated blocks. Hopefully the worst case scenario of a single voxel in a chunk can still be represented efficiently.

dsrw avatar Aug 25 '25 20:08 dsrw