How can I optimize a procedurally generated game?

One way to do that is to make the density fall as the Y coordinate increases. Using that approach you can get something like

You can get some really cool overhangs

But also floating islands

If you tune it right you won’t get floaties but there’s a limit to the mountain intensity you can have with this approach.

If you want more extreme terrain with no floaties, check out this post:

And here’s some info on Perlin worm cave generation:

… although I wouldn’t recommend the Perlin worm thing, it’s complicated and doesn’t work that great unless you spend a lot of time tuning it.

EDIT: Here’s the code


local BLOCK_SIZE = 4
local MAP_SIZE = Vector3.new(128, 64, 128)
local MOUNTAIN_INTENSITY = 8

seed = 0
noisescale = 1/64
amplitude = 10

function get_density(v3)
	local noise_value = math.noise(v3.X * noisescale, v3.Y * noisescale, v3.Z * noisescale + seed)*amplitude
	local height_bonus = -v3.Y / MOUNTAIN_INTENSITY + 8
	
	return noise_value + height_bonus
end

function is_on_boundary(v3)
	if get_density(v3) <= 0 then
		return false
	end

	for _, dirEnum in pairs(Enum.NormalId:GetEnumItems()) do
		local dir = Vector3.FromNormalId(dirEnum)
		local neighbor_pos = v3 + dir * BLOCK_SIZE
		local neighbor_density = get_density(neighbor_pos)

		if neighbor_density <= 0 then
			return true
		end
	end

	return false
end

for x = 0, MAP_SIZE.X do
	for y = 0, MAP_SIZE.Y do
		for z = 0, MAP_SIZE.Z do
			local block_pos = Vector3.new(x, y, z) * BLOCK_SIZE
			local density = get_density(block_pos)
			
			if density > 0 and is_on_boundary(block_pos) then
				local block = script.Block:Clone()
				block.Parent = game.Workspace
				block.CFrame = CFrame.new(block_pos)
				
				if y > 25 then
					block.BrickColor = BrickColor.White()
				elseif y < 23 then
					block.BrickColor = BrickColor.Green()
				else
					block.Color = Color3.fromRGB(108, 88, 75)
				end
			end
		end
	end
	wait()
end
6 Likes