this is what i currently have, issue is the materials and occupancies lists have to be 3d lists when passing into WriteVoxels() but how would i know how they should be arranged
the original materials list is set up like this
[1] = {[1] = {materials}, [2] = {materials}, [3] = {materials}, [4] = {materials}},
[2] = {[1] = {materials}, [2] = {materials}, [3] = {materials}, [4] = {materials}},
[3] = {[1] = {materials}, [2] = {materials}, [3] = {materials}, [4] = {materials}},
[4] = {[1] = {materials}, [2] = {materials}, [3] = {materials}, [4] = {materials}},
Size = Vector3(4, 4, 4)
my loaded materials list is a 1d array of some order ranging from 1-64
local RESOLUTION = 2
local MATERIAL_TO_INT = {
[Enum.Material.Air] = 0,
[Enum.Material.Grass] = 1
}
local INT_TO_MATERIAL = {
[0] = Enum.Material.Air,
[1] = Enum.Material.Grass
}
local region = Region3.new(Vector3.one * -10, Vector3.one * 10)
local materials, occupancies = workspace.Terrain:ReadVoxels(region, RESOLUTION + 2)
local size = materials.Size
print(materials)
print(occupancies)
local chunkData = buffer.create(RESOLUTION * size.x * size.y * size.z)
local chunkOffset = 0
for x = 1, size.X, 1 do
for y = 1, size.Y, 1 do
for z = 1, size.Z, 1 do
local occupancy = occupancies[x][y][z] * 255
local material = MATERIAL_TO_INT[materials[x][y][z]]
buffer.writeu8(chunkData, chunkOffset, occupancy)
buffer.writeu8(chunkData, chunkOffset + 1, material)
chunkOffset += 2
end
end
end
print('saved')
task.wait(2)
print('loading')
local saved = buffer.tostring(chunkData)
workspace.Terrain:Clear()
local loaded = buffer.fromstring(saved)
local materials, occupancies = {}, {}
local size = buffer.len(chunkData)
for offset = 0, size - 1, 2 do
local occupancy = buffer.readu8(loaded, offset) / 255
local material = INT_TO_MATERIAL[buffer.readu8(loaded, offset + 1)]
table.insert(materials, material)
table.insert(occupancies, occupancy)
end
print(materials)
print(occupancies)
--workspace.Terrain:WriteVoxels(region, RESOLUTION + 2, materials, occupancies)