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
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.