Using ReadVoxels with terrain previously generated with FillBlock

I am generating a terrain by first creating parts and assigning them positions and sizes, then using Terrain:FillBlock() to fill the parts with terrain.

It works fine, yet after I create the terrain I want to then select parts of the terrain with Terrain:ReadVoxels() and write over the materials I dont want and then write the voxels back. Effectively creating the paint tool in code.

The only problem I am having is that ReadVoxels is only returning air, and it is not detecting any terrain that I have generated with FillBlock.

When I use the terrain tool in studio, it does return the correct terrain. Is this a glitch or can I not use FillBlock to get the results that I want?

1 Like

It should work, but the grid scale of the terrain is 4:1 studs instead of 1:1. Are you using Terrain:WorldToCell() to find the terrain cells that correspond to the parts? If not, then you may be reading the wrong voxels which are actually air.

1 Like

Creating terrain with FillBlock is not a problem, but the gotcha is that 4:1 voxel size. ReadVoxels takes a Region3, but min and max Vector3 values you use to construct the Region3 need to be aligned with the voxel grid, meaning the x, y, and z values of all of these vectors need to be multiples of 4, and the resulting 3D arrays you get for materials and occupancies are indexed with x,y, and z values that are voxel coordinates, not studs, and because the arrays are 1-indexed like all Lua arrays, they are not the absolute voxel coordinates (like what you get from Terrain:WorldToCell), they are relative coordinates where the material[1][1][1] entry is whatever terrain voxel is at the corner of your Region3 with the minimum world coordinates.