Chunk Generation Help

  1. What do you want to achieve?
    I wanted to create voxel terrain generation. For instance, minecraft terrain generation. I was following a few tutorials.

2. What is the issue?
The issue is that the chunk doesn’t spawn near the camera and that there are holes in my terrain. (I put my camera where the character is, before the chunk generated)

  1. What solutions have you tried so far?
    I tried looking on the devforum for solutions, but most of them were generating chunks at spawn or did not work out.
local NoiseUtil = {}

type int = number

function NoiseUtil.Generate2DFractalNoise(X: int, Y: int, Octaves: int, Lacunarity: int, Persistence: int, Scale: int, Seed: int)
	local Value = 0
	
	local x1 = X
	local y1 = Y
	
	local Amplitude = 1
	
	for i = 1, Octaves, 1 do
		Value += math.abs(math.noise(x1 / Scale, y1 / Scale, Seed)) * Amplitude
		
		y1 *= Lacunarity
		x1 *= Lacunarity
		
		Amplitude *= Persistence
	end
	
	Value = Value ^ 2
	
	return math.clamp(Value, -1, 1)
end

local MAP_SIZE							= 250.0
local MAP_SCALE							= MAP_SIZE/2.0

local NOISE_SCALE						= 50.0
local HEIGHT_SCALE						= 50.0
local OCTAVES							= 4.0
local LACUNARITY						= 3.0
local PERSISTENCE						= 0.35

local BLOCK_SIZE						= 3.0
local CHUNK_SIZE						= 30.0

local NUMBER_OF_CHUNKS					= math.ceil(MAP_SCALE/CHUNK_SIZE)

local ChunkPositionX					= 1
local ChunkPositionZ					= 1

local function updateChunkPositionVariables()
	local camPos = Camera.CFrame.Position
	
	ChunkPositionX = camPos.X // CHUNK_SIZE
	ChunkPositionZ = camPos.Z // CHUNK_SIZE 
end

local function generateBlock(x, y, z, color, chunkfolder)
	local grass = Template:Clone()
	local height = grass.PrimaryPart.Size.Y
	
	local realx = (x // BLOCK_SIZE) * BLOCK_SIZE
	local realY = (math.floor(y) * BLOCK_SIZE)
	local realz = (z // BLOCK_SIZE) * BLOCK_SIZE
	
	grass:PivotTo(CFrame.new(x, realY, z))
	grass.PrimaryPart.BrickColor = BrickColor.Green()
	grass.PrimaryPart.TopSurface = Enum.SurfaceType.Smooth
	grass.PrimaryPart.BottomSurface = Enum.SurfaceType.Smooth
	grass.PrimaryPart.Color = color
	grass.Parent = chunkfolder
	
	return grass
end

local function generateASingleChunk(chunkX: number, chunkZ: number)
	local PlayerPos = Camera.CFrame.Position
	
	for x = ChunkPositionX, ChunkPositionX + CHUNK_SIZE do
		for z = ChunkPositionZ, ChunkPositionZ + CHUNK_SIZE do
			local realX = (chunkX - 1) * CHUNK_SIZE + x --The problem might be here
			local realZ = (chunkZ - 1) * CHUNK_SIZE + z

			local height2d = NoiseUtil.Generate2DFractalNoise(realX, realZ,
				OCTAVES,
				LACUNARITY,
				PERSISTENCE,
				NOISE_SCALE,
				SEED
			) * HEIGHT_SCALE

			generateBlock(realX * BLOCK_SIZE, height2d, realZ * BLOCK_SIZE, Color3.new(1,1,1), WorldFolder)
		end
	end
end

task.wait(4)
updateChunkPositionVariables()
generateASingleChunk(ChunkPositionX, ChunkPositionZ)

Are you generating the chunk on the client?

Yes I’m generating the chunks on the client for testing

Ok so after a few tests, I noticed that the chunks overlap each other. I updated the code to fix this problem, but the problem isnt fixed yet.

local function generateASingleChunk(chunkX: number, chunkZ: number)
	local PlayerPos = Camera.CFrame.Position
	
	local chunkColor = Color3.new(math.random(1, 255), math.random(1, 255), math.random(1, 255))
	
	local chunkFolder = Instance.new("Folder", WorldFolder)
	chunkFolder.Name = "Chunk_".. chunkX .."_".. chunkZ
	
	for x = 1, CHUNK_SIZE do
		for z = 1, CHUNK_SIZE do
			local realX = (chunkX - 1) * (CHUNK_SIZE) + x
			local realZ = (chunkZ - 1) * (CHUNK_SIZE) + z

			local height2d = NoiseUtil.Generate2DFractalNoise(realX, realZ,
				OCTAVES,
				LACUNARITY,
				PERSISTENCE,
				NOISE_SCALE,
				SEED
			) * HEIGHT_SCALE

			local color = getColor(realX, realZ, height2d)

			generateBlock(realX * BLOCK_SIZE, height2d, realZ * BLOCK_SIZE, chunkColor, chunkFolder)
		end
	end
end

After a few hours of trials and errors, I finally found the solution!! I had to change these lines of code.

local function updateChunkPositionVariables()
	local camPos = Camera.CFrame.Position
	
	ChunkPositionX = math.floor(camPos.X / (CHUNK_SIZE * BLOCK_SIZE)) + 1
	ChunkPositionZ = math.floor(camPos.Z / (CHUNK_SIZE * BLOCK_SIZE)) + 1
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.