EditableMesh - 3D perlin with cubic chunks

Hello! I would like to generate 3D perlin noise, using a basic cubic chunk system, that utilizes 3d perlin noise, that uses EditableMesh for geometry.

I have been able to create a basic, 2 dimensional variant of this system:

local ChunksFolder = workspace.Chunks

local worldSize, chunkSize = 8, 64
local seed = math.random(999)

local function generateTerrainNoise(x, y)
	return math.noise(x / 64 + seed, y / 64 + seed) * 32
end

local function generateChunk(cX, cY)
	local Mesh = Instance.new("MeshPart")
	local EditableMesh = Instance.new("EditableMesh", Mesh)
	Mesh.Name = cX .. "," .. cY
	Mesh.Anchored = true
	Mesh.Size = Vector3.one
	Mesh.Position = Vector3.new(cX * chunkSize, 0, cY * chunkSize)
	Mesh.Parent = ChunksFolder

	local vertices = {}

	for y = 0, chunkSize do
		for x = 0, chunkSize do
			local worldX, worldY = x + cX * chunkSize, y + cY * chunkSize
			local height = generateTerrainNoise(worldX, worldY)
			local newPos = Vector3.yAxis * height + Vector3.new(x, 0, y)
			local vertID = EditableMesh:AddVertex(newPos)
			vertices[#vertices + 1] = vertID
		end
	end

	for y = 0, chunkSize - 1 do
		for x = 0, chunkSize - 1 do
			local i1 = y * (chunkSize + 1) + x + 1
			local i2 = (y + 1) * (chunkSize + 1) + x + 1
			local i3 = y * (chunkSize + 1) + x + 2
			local i4 = (y + 1) * (chunkSize + 1) + x + 2
			EditableMesh:AddTriangle(vertices[i1], vertices[i2], vertices[i3])
			EditableMesh:AddTriangle(vertices[i2], vertices[i4], vertices[i3])
		end
	end
end

local function generateChunksInSquareRadius(centerX, centerY, radius)
	for cX = centerX - radius, centerX + radius do
		for cY = centerY - radius, centerY + radius do
			generateChunk(cX, cY)
		end
	end
end

generateChunksInSquareRadius(0, 0, 5)

Problem is, all of the vertex handling was obtained from an outside source, and because I myself have very little knowledge on how to properly generate vertices, and operate with them in general, I haven’t found any success in converting this system to the requested system above.

How could I go about handling vertex placement?

1 Like