Having Trouble Reading Voxels

I got this Script to help me better understand Voxels from the Wiki. It prints out a table of numbers and material names in a 3D space.

  • Also I’m getting the Regio3 from the mouse Hitpoint
    when I Click, the script prints out:
    Screenshot_14

All I’m asking for, is how do I understand what has been printed?

local materials, occupancies = terrain:ReadVoxels(region, 4)
			local size = materials.Size -- Same as occupancies.Size
			
			for x = 1, size.X, 1 do
				for y = 1, size.Y, 1 do
					for z = 1, size.Z, 1 do
						print(("(%2i, %2i, %2i): %.2f %s"):format(x, y, z, occupancies[x][y][z], materials[x][y][z].Name))
					end
				end
			end
1 Like

What do you mean? It seems to be working as intended.

This code loops through all the X, Y, and Z values in the Region3 you provided, and then prints out the occupancy and material for the voxel at each point.

I just dont know what the numbers mean, and why are they printing row after row?

Oh I getcha. If you are new to scripting, this code might be slightly overwhelming. I can try to break down what it’s doing.

terrain:ReadVoxels gives you two 3D tables. It’s not really important what that means. Just know that you can use materials and occupancies to get the material and percent-occupied of a voxel at a certain position like this:

-- get material at the voxel at (1, 2, 3):
local theMaterial = materials[1][2][3]
print("Material: " .. theMaterial.Name) -- print the material

-- get the percentage of space occupied at that same position
local theOccupancy = occupancies[1][2][3]
print("Occupancy" .. theOccupancy)

That’s all this code does, but it doesn’t explicitly say “1, 2, 3”. Instead, it uses a for loop to come up with those numbers for every voxel in the region you give it.

So it’s looping through all possible X,Y,Z combinations and, for each of those voxels, printing out on a separate line:

  1. the position of the voxel in the grid (the (1, 1, 2) part)
  2. the occupancy of that voxel (the 1.00 part – that means “fully occupied”)
  3. the material of that voxel (the Water or Sand part)

Bro thanks a million :slight_smile: