I am making a voxel-based game. I want to optimize memory usage.
I generate the world in chunks, like in Minecraft, and I have voxels, which have vertices and UVs and stuff. I decided to start optimization from vertices, positions of which are stored in Vector3s, like this:
public static readonly Vector3[] voxelVerts = new Vector3[8]
{
new Vector3(0, 0, 0),
new Vector3(1, 0, 0),
new Vector3(1, 1, 0),
new Vector3(0, 1, 0),
new Vector3(0, 0, 1),
new Vector3(1, 0, 1),
new Vector3(1, 1, 1),
new Vector3(0, 1, 1)
};
The one thing that is obvious to me is that Vector3's store 3 floats, which is a lot of bits. The chunks are not bigger than 16 blocks wide, even though I planned to increase that to 32 someday if possible. Anyway, 32 can be stored nicely in 6 bits (18 bits for a coordinate), which is small and just what I need. After asking around and reading forums, I came to a conclusion that an int can store 4 bytes which is 32 bits. This might sound like a silly question, but can I store those 18 bits in an int and then read them? From what I read, I can't store bit info directly without ints or other memory-consuming methods. So, is it possible? If not, is there another way? Someone did it, so there is definiely a way.
but I found this