My terrain generator has gaps

When I use my terrain generator it sometimes has some gaps like this:

I want there to be no gaps but I can’t figure it out.

Here’s some of the script that I’m using:

for x = 0, self.XSize do
		for z = 0, self.XSize do
			local yn = math.floor(math.noise(x/30, z/30, increment) * 15 + 0.5) * self.BlockSize
			local part = Instance.new("Part", workspace.Terrain)
			part.Anchored = true
			part.Size = Vector3.new(self.BlockSize, self.BlockSize, self.BlockSize)
			part.Material = self.BlockMaterial
			if typeof(self.BlockColor) == "Color3" then
				part.Color = self.BlockColor
			elseif typeof(self.BlockColor) == "BrickColor" then
				part.BrickColor = self.BlockColor
			end
			if yn % self.BlockSize == 0 then
				part.Position = Vector3.new(x * self.BlockSize, yn, z * self.BlockSize)
			elseif (yn + self.BlockSize / 2) % self.BlockSize == 0 then
				part.Position = Vector3.new(x * self.BlockSize, yn + self.BlockSize / 2, z * self.BlockSize)
			end
		end
		if self.BeautifulRender then
			wait(self.BeautifulRenderTime)
		end
	end
2 Likes

The issue is pretty obvious. Since you are generating a cube based voxel terrain, but only the top layer, any height difference between neighbours of more than 1 will cause holes in the sides.
The easiest way to fix this is to check the height of the neigbours. Find the neighbour with the lowest height value and subtract it from the current position’s height (height0-height1 = n). If n-1>1 then place additional blocks below.

2 Likes

Ok I will look into that thanks.

Well that’s a solution but since my terrain is only generated on the surface, new blocks will be created.

Here’s a picture:

So basically a new block will be created for every single block there is.

I wrote a code myself and it seems to work just fine.

local sizex,sizez = self.XSize,self.ZSize
local mapHeight = table.create(sizex*sizez)
for z=0,sizez-1 do
	for x=0,sizex-1 do
		local i = z*sizex+x+1
		mapHeight[i] = math.floor(math.noise(x/30, z/30, increment) * 15 + 0.5)
	end
end

local defpart = Instance.new("Part", workspace.Terrain)
defpart.Anchored = true
defpart.Size = Vector3.new(self.BlockSize, self.BlockSize, self.BlockSize)
defpart.Material = self.BlockMaterial
if typeof(self.BlockColor) == "Color3" then
	defpart.Color = self.BlockColor
elseif typeof(self.BlockColor) == "BrickColor" then
	defpart.BrickColor = self.BlockColor
end

local terrain = workspace.Terrain
local blocksize = self.BlockSize
for x0 = 0, sizex-1 do
	for z0 = 0, sizez-1 do
		local i0 = z0*sizex+x0+1
		local yn0 = mapHeight[i0]
		local part0 = defpart:Clone()
		part0.Position = Vector3.new(blocksize*x0,yn0*blocksize,blocksize*z0)
		part0.Parent = terrain
		local ynmin = math.huge
		if x0>0 then
			local x1,z1 = x0-1,z0
			local i1 = z1*sizex+x1+1
			local yn1 = mapHeight[i1]
			ynmin = ynmin<yn1 and ynmin or yn1
		end
		if x0<sizex-1 then
			local x1,z1 = x0+1,z0
			local i1 = z1*sizex+x1+1
			local yn1 = mapHeight[i1]
			ynmin = ynmin<yn1 and ynmin or yn1
		end
		if z0>0 then
			local x1,z1 = x0,z0-1
			local i1 = z1*sizex+x1+1
			local yn1 = mapHeight[i1]
			ynmin = ynmin<yn1 and ynmin or yn1
		end
		if z0<sizez-1 then
			local x1,z1 = x0,z0+1
			local i1 = z1*sizex+x1+1
			local yn1 = mapHeight[i1]
			ynmin = ynmin<yn1 and ynmin or yn1
		end
		local yoff = yn0-ynmin
		if yoff>1 then
			for i=1,yoff-1 do
				local yn1 = yn0-i
				local part1 = defpart:Clone()
				part1.Position = Vector3.new(blocksize*x0,yn1*blocksize,blocksize*z0)
				part1.Parent = terrain
			end
		end
	end
	if self.BeautifulRender then
		wait(self.BeautifulRenderTime)
	end
end
2 Likes

Also, an image with an amplified noise to emphasize the edges.

2 Likes

I used your code and something weird happened.


It looked like it bent or something.

I noticed it too, for some reason the last Z row is copied from the first row. You could just do one Z row less in part generation.

1 Like

NVM, the issue is that for x0 = 0, sizex do and for z0 = 0, sizez do have to be replaced with for x0 = 0, sizex-1 do and for z0 = 0, sizez-1 do
Edited my original post to include the fix.

1 Like