Hey, so I made this terrain generation using perlin noise. I was wondering how I could incorporate chunk-loading alongside the generation.
Here’s the code :
local TERRAIN_GENERATION = {}
--// Services
local S_RUN = game:GetService("RunService")
local S_RS = game:GetService("ReplicatedStorage")
--// Constants
local BLOCK_SIZE = 3
local GRID_SIZE = 16 -- blocks
local MAX_HEIGHT = 25
local MIN_HEIGHT = -25
local FREQUENCY = 10
local SCALE = 200
local AMPLITUDE = 8
local STONE_LEVEL = -10
--[PRIVATE]--
--// Generates blocks on surface level
local function generate_surface(seed)
local blocks = {}
local surface_y = {}
for x = 1, GRID_SIZE, 1 do
blocks[x] = {}
surface_y[x] = {}
for z = 1, GRID_SIZE, 1 do
blocks[x][z] = {}
local y = math.floor(
math.noise((x / SCALE) * FREQUENCY, (z / SCALE) * FREQUENCY, seed) * AMPLITUDE
)
local block = S_RS.ITEMS.BLOCKS["Grass Block"]:Clone()
block.Parent = workspace.MAP
block.Position = Vector3.new(x * BLOCK_SIZE, y * BLOCK_SIZE, z * BLOCK_SIZE)
surface_y[x][z] = y
blocks[x][z][y] = block
end
end
local values_returned = {blocks, surface_y}
return values_returned
end
local function generate_below_surface(blocks: {}, surface_y: {})
for x, x_blocks in blocks do
for z, z_blocks in x_blocks do
local surface_level = surface_y[x][z]
for y = MAX_HEIGHT, MIN_HEIGHT, -1 do
if y >= surface_level then continue end
local block = S_RS.ITEMS.BLOCKS["Dirt"]:Clone()
block.Parent = workspace.MAP
block.Position = Vector3.new(x * BLOCK_SIZE, y * BLOCK_SIZE, z * BLOCK_SIZE)
blocks[x][z][y] = block
--if y < math.random(STONE_LEVEL - 2, STONE_LEVEL + 2) then
--local block = S_RS.ITEMS["Stone"]:Clone()
--block.Parent = workspace.MAP
--block.Position = Vector3.new(x * BLOCK_SIZE, y * BLOCK_SIZE, z * BLOCK_SIZE)
--blocks[x][z][y] = block
--end
end
end
end
end
--[PUBLIC]--
function TERRAIN_GENERATION.generate(seed)
local surface_blocks = generate_surface(seed)
local below_surface_blocks = generate_below_surface(surface_blocks[1], surface_blocks[2])
end
return TERRAIN_GENERATION