Voxel Terrain Generation Issue

Hello, I am trying to make a voxel terrain generator and I ran into an issue.

This is my first time working with perlin noise but I got the perlin noise working fine. I made a chunk generator function that generates 1 chunk (16x16) but, when I spawn another chunk next to it its supposed to be inline with eachother. This is the outcome of my attempts…

As you can see, they do not line up.

I have tried offsetting the perlin noise texture to where the chunk is in the world but that hasnt worked either, here is my code for the generation…

local Knit = require(game.ReplicatedStorage["@game/modules_"]["@game/modules/knit_"].Packages.Knit)
Knit.Start():catch(warn):await()

local PerlinService = Knit.GetService("PerlinNoiseService")

local LowestY = 2 -- How many layers are spawned underneath the first (Lowered for performance sake)

local OffsetZ,OffsetX = 0,0

function GenerateChunk(At)
	if At.Z ~= 0 then 
		OffsetZ = OffsetZ + math.abs(At.Z) --  I try to offset the perlin texture
	end
	if At.X ~= 0 then 
		OffsetX = OffsetX + math.abs(At.X) -- I try to offset the perlin texture here aswell
	end
	print(OffsetX, OffsetZ) -- Debug to see if it was offsetting or not
	local Options = {
		PerlinSize = 50,
		PerlinAmplitude = 30,
		PerlinGain = 0.5,
		PerlinLacunarity = 1.5,
		PerlinScale = 50,
		PerlinOctaves = 4,
		PerlinZOffset = OffsetZ,
		PerlinXOffset = OffsetX
	}
	
	local ChunkModel = Instance.new("Model")
	ChunkModel.Name = "Chunk" .. math.random(1,10)
	ChunkModel.Parent = workspace
	
	PerlinService:CreateNoise(Options):andThen(function(NoiseTable)
		------
		for Index,NoiseNode in pairs(NoiseTable) do 
			if typeof(NoiseNode) == 'Vector3' then
				local Position = (At + NoiseNode)
				
				local Color = Color3.fromRGB(255, 255, 255)
				local ColorLerp = Color3.fromRGB(0, 0, 0)
				
				local block2 = Instance.new("Part")
				block2.Parent = ChunkModel
				block2.Size = Vector3.new(3,3,3)
				block2.Position = Position
				block2.Anchored = true
				block2.Color  = Color:Lerp(ColorLerp, math.clamp(Position.Y/100*2, -1, 1))
				
				for i = 1,math.abs(LowestY) do 
					local block = Instance.new("Part")
					block.Parent = ChunkModel
					block.Size = Vector3.new(3,3,3)
					block.Position = Position + Vector3.new(0,-(3 * i),0)
					block.Anchored = true
					block.Color  = block2.Color
				end
			end
		end
		-----
	end)
end

GenerateChunk(Vector3.new(0,0,0))
GenerateChunk(Vector3.new(0,0,-(50 * 3))) -- Second chunk generates next to the first

And here is the code for the perlin generator…

local Knit = require(game.ReplicatedStorage["@game/modules_"]["@game/modules/knit_"].Packages.Knit)

local Perlin = Knit.CreateService {
	Name = "PerlinNoiseService",
}

function Perlin:CreateNoise(Options)
	local PerlinSize,Amplitude,Gain,Lacunarity,Scale,Octaves,ZOffset,XOffset 
	= Options.PerlinSize, Options.PerlinAmplitude, Options.PerlinGain, Options.PerlinLacunarity, Options.PerlinScale, Options.PerlinOctaves, Options.PerlinZOffset, Options.PerlinXOffset
	
	local PerlinTable = {}
	
	function PerlinTable:andThen(Function)
		Function(PerlinTable)
	end
	
	for x = 0,PerlinSize do
		task.wait()
		for z = 0,PerlinSize do
			local amplitude = 1
			local frequency = 1
			local total = 0
			local normalizer = 0
			--------
			for octave=1, Octaves do
				local noise = math.noise(((x + XOffset) / Scale) * frequency, ((z + ZOffset)  / Scale) * frequency)
				noise = math.clamp(noise, -1, 1)
				total += noise * amplitude
				amplitude *= Gain
				frequency *= Lacunarity
				normalizer += amplitude
			end
			total /= normalizer
			total *= Amplitude
			---------
			table.insert(PerlinTable, Vector3.new(x * 3, math.floor(total + 10) * 3, z * 3))
		end
	end
	
	return PerlinTable
end

Knit.Start():catch(warn)

Any help is appreciated, thanks.

How come you pass a Vector3 of where you want to generate the chunk, then use a completely different value (that vector’s components added to global OffsetX and OffsetZ) to do the actual generation?

To get the different block positions, I call my perlin noise service and that returns a array of positions, the At variable is the origin of the chunk, and the OffsetZ and OffsetX are the numbers that determine where on the perlin noise texture I want it to grab the block data from. The actual generation is from the service, not the texture offset. I add the components to the offset because I want it to be in line with the center chunk.

I have fixed it, the texture displacement was off. Thanks!

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