Optimizing Terrain by thinning it out

To optimize my game, I am developing a script that thins out terrain such that all grass and sand terrain aside from the top surface will be deleted.

Visual Example:
terrain

The script I’ve developed so far to accomplish goes by this logic:

  1. Read the voxels of the terrain
  2. Loop through each 4x4x4 block in the voxel. If the block above is a 4x4x4 piece of sand or grass, then delete the current block.
local function deleteRegion(terrain, p1, p2)
	--assume the x,y,z of p2-p1 are multiple of 4s 
	local region = Region3.new(p1, p2) 
	local materials, occupancies = terrain:ReadVoxels(region, 4)
	local toDelete = {"Grass", "Sand"}
	local size = materials.Size -- Same as occupancies.Size
	for x = 1, size.X, 1 do
		for z = 1, size.Z, 1 do
			for y = 1, size.Y - 1, 1 do
				if occupancies[x][y][z] == 1  and occupancies[x][y+1][z] == 1  and table.find(toDelete, materials[x][y][z].Name) and table.find(toDelete, materials[x][y+1][z].Name)  then
					local regionToDelete = Region3.new(p1 + Vector3.new((x-1) * 4, (y-1) * 4, (z-1) * 4), p1 + Vector3.new(x * 4, y * 4, z * 4))
					terrain:FillRegion(regionToDelete, 4, Enum.Material.Air)
				end
			end
		end
	end
end

For the most part, the code works as intended, but ends up deleting terrain that I wouldn’t it to delete.
An example being this hill, where random holes are missing:
image

So my questions are:

  1. What am I doing wrong? Is this not how you get the region to delete?
  2. Is there a better way to approach this. I cannot do this manually as my map is very large.

Each voxel affects the surrounding ones as well. If you use steep Terrain then just checking the one above would delete all your vertical walls.
Try deleting the voxels leaving a 2 voxel thickness (including the surface ones) by checking if there are Voxels on each side as well.

1 Like

I’ve done it before on large maps just by using the Region>Select tool and drag it until it’s pretty big and it almost shows through the top surface of the Terrain, then Delete that region. It works well for large flat areas, but if you have a Mountain you can make a tall vertical Select box and hollow it out pretty well in chunks.
I’m don’t know whether there’s a performance difference if you have a rough surface underneath with a minimum number of voxels overall, or if you use a lot of flat 90 degree surfaces like I suggested but had more voxels.
That might take some experimenting…

Problem with this is that my map is extremely large, and the region selection tool has a set limited size. My maps also has a lot of hills and valleys and I’d have to manually hollow those ones out too.

I did however try using a 2 voxel thickness like you said previously, it worked for that region. I’ll keep you updated on the rest of the map.

How did it work for vertical wall areas?

Pretty well in general, no random holes or patches so far

Do you have any areas where it’s completely vertical?
If you are only checking for and deleting voxels that are 2 voxels down from the surface then vertical walls or walls that are tilted back will have holes.

Hello,

You’re not actually optimizing it by making it thinner, in fact you’re making it less performant according to a reply I got a while ago when I asked if is hollow terrain better

You could have flat areas thin but hollow mountains would be more expensive because the engine will calculate physics for both of the sides

1 Like

I believe this is talking about completely hollow objects. For example, if you have a ball of terrain it’s better to just make it solid. Same goes for pillars and columns. A mountain is different though, since it only has one side (from a voxel perspective) and isn’t hollow.

Example source:

If you like there is a plugin that can generate a thin layer of terrain for you and also has built in editing tools

you can find out more here