Issues with offsetting terrain generation per chunk

I am working on a base for a world generation script I may utilize in multiple games, so I am immersing myself for a first* time. However, my script seems to create unsatisfactory results.

The perlin results look choppy amongst chunks

image

The ModuleScript that generates the perlin
local TerrainGenerator = {}

-- Define the properties of the Perlin noise visualization
local gridSize = 16 -- Size of a chunk
local cellSize = 5 -- Adjust the size of each cell
local scale = 0.1 -- Adjust the Perlin noise scale

-- Function to generate Perlin noise values and map to grayscale
function TerrainGenerator.generatePerlinNoise(x, y)
	local perlinValue = math.noise(x * scale, y * scale, 0)
	-- Remap the Perlin noise value to a narrower range for smoother grayscale
	local noiseValue = (perlinValue + 1) / 2
	return noiseValue
end

-- Function to create a chunk of terrain
function TerrainGenerator.createChunk(chunkX, chunkY)
	local chunk = {}
	for x = 1, gridSize do
		for y = 1, gridSize do
			local noiseValue = TerrainGenerator.generatePerlinNoise(x + (chunkX - 1) * gridSize, y + (chunkY - 1) * gridSize)
			local color = Color3.new(noiseValue, noiseValue, noiseValue) -- Uniform grayscale
			table.insert(chunk, color)
		end
	end
	return chunk
end

return TerrainGenerator
The part of the localscript that loads the perlin in client-side
local TerrainGenerator = require(game.ReplicatedStorage.TerrainGenerator) -- Get the modulescript

local chunkSize = 16
local cellSize = 5
local viewDistance = 3 -- Adjust the view distance as needed

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local rootPart = character:WaitForChild("HumanoidRootPart")

local loadedChunks = {}

function createTerrainChunk(chunkX, chunkY)
	local terrainModel = Instance.new("Model")
	terrainModel.Name = "TerrainChunk"
	terrainModel.Parent = workspace

	local chunkData = TerrainGenerator.createChunk(chunkX, chunkY)

	for x = 1, chunkSize do
		for y = 1, chunkSize do
			local part = Instance.new("Part")
			part.Size = Vector3.new(cellSize, 1, cellSize)
			part.Position = Vector3.new((x + (chunkX - 1) * chunkSize) * cellSize, 0, (y + (chunkY - 1) * chunkSize) * cellSize)
			part.Color = chunkData[x + (y - 1) * chunkSize]
			part.Anchored = true
			part.Parent = terrainModel
		end
	end

	loadedChunks[chunkX] = loadedChunks[chunkX] or {}
	loadedChunks[chunkX][chunkY] = terrainModel
end

Note, this is not the full script.
Also, you only need to generate a chunk like createTerrainChunk(1, 1) instead of putting the entire world position.

Basically, my problem is that the offset is not applied correctly when a chunk is generated, this might be due to an issue in the offset calculation…

I have tried looking around on the devforum, tinkered around, tried youtube guides, and have used the assistant, and I am yet to find a clear answer.

1 Like

Vector3.new((x + (chunkX - 1) * chunkSize) * cellSize, 0, (y + (chunkY - 1) * chunkSize) * cellSize)
x axis is:

(x + (chunkX - 1) * chunkSize) * cellSize

z axis is:

(y + (chunkY - 1) * chunkSize) * cellSize

y axis is 0
is that the problem or it is intended?

1 Like

That is intended, since I am generating a flat perlin noise and that variable is in reality the Z axis

If im right, it got something going on with the noise system if you can see the height level of the white and black. This is usually caused by the noise value.


function TerrainGenerator.generatePerlinNoise(x, y)
	local perlinValue = math.noise(x * scale, y * scale, 0)
	-- Remap the Perlin noise value to a narrower range for smoother grayscale
	local noiseValue = (perlinValue + 1) / 2
	return noiseValue
end

Im pretty sure it got to do with this.

1 Like

I believe it is something to do with the lack of an offset in that part, but I cannot seem to find the correct formula to give a correct offset according to the chunk

If you are generating a perlin every chunk that is a likely answer to him because every chunk is different

Note: I’ve never really used perlin or anything like that and I don’t know how it works but I guess it makes some sense.

Used very little offset and, the perlin always yields the same result, so the problem really is just the offsetting.