Hi. I've been playing with Perlin Noise for a while now and my code is able to generate a square piece of terrain, that I can change the size of by changing map scale. If I wanted to make a big world, I firstly tried by just increasing my mapscale. This makes the script very laggy and unstable as sometimes it works and sometimes it doesn't, depending on my connection. I then looked at games like Minecraft, and realised that it generates the world in chunks, rather than just generating the whole terrain already. Here is my code :
local mapScale = 50
local mapHeight = 15
local groundLvl = 10
local smoothness = 20
local octaves = 8
local lancunarity = 2
local gain = 0.5
local waterLvl = 8
local chunks = {}
function setCell(x, y, z, material)
local part = Instance.new("Part", game.Workspace)
part.Anchored = true
part.FormFactor = "Custom"
part.Size = Vector3.new(4,4,4)
part.CFrame = CFrame.new(x,y,z)
part.TopSurface = "Smooth"
part.BottomSurface = "Smooth"
part.Material = "SmoothPlastic"
if material == "grass" then
part.BrickColor = BrickColor.Green()
part.Name = "Grass"
end
if material == "water" then
part.BrickColor = BrickColor.Blue()
part.Name = "Water"
end
if material == "stone" then
part.BrickColor = BrickColor.Gray()
part.Name = "Stone"
end
end
function noise(x, y, z)
local X = math.floor(x % 255)
local Y = math.floor(y % 255)
local Z = math.floor(z % 255)
x = x - math.floor(x)
y = y - math.floor(y)
z = z - math.floor(z)
local u = fade(x)
local v = fade(y)
local w = fade(z)
local A = p[X ]+Y
local AA = p[A]+Z
local AB = p[A+1]+Z
local B = p[X+1]+Y
local BA = p[B]+Z
local BB = p[B+1]+Z
return lerp(w, lerp(v, lerp(u, grad(p[AA ], x , y , z ),
grad(p[BA ], x-1, y , z )),
lerp(u, grad(p[AB ], x , y-1, z ),
grad(p[BB ], x-1, y-1, z ))),
lerp(v, lerp(u, grad(p[AA+1], x , y , z-1 ),
grad(p[BA+1], x-1, y , z-1 )),
lerp(u, grad(p[AB+1], x , y-1, z-1 ),
grad(p[BB+1], x-1, y-1, z-1 )))
)
end
function fade(t)
return t * t * t * (t * (t * 6 - 15) + 10)
end
function lerp(t,a,b)
return a + t * (b - a)
end
function grad(hash,x,y,z)
local h = hash % 16
local u
local v
if (h<8) then u = x else u = y end
if (h<4) then v = y elseif (h==12 or h==14) then v=x else v=z end
local r
if ((h%2) == 0) then r=u else r=-u end
if ((h%4) == 0) then r=r+v else r=r-v end
return r
end
p = {}
local permutation = {151,160,137,91,90,15,
131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180
}
for i=0,255 do
p[i] = permutation[i+1]
p[256+i] = permutation[i+1]
end
function fBm(x,y,z,octaves,lacunarity,gain)
local amplitude = 1
local frequency = 1
local sum = 0
for i = 1, octaves do
sum = sum + amplitude * noise(x * frequency, y * frequency, z * frequency);
amplitude = amplitude * gain
frequency = frequency * lancunarity
return sum
end
end
local scale = mapScale
local a = 1
local b = 1
local c = scale
local d = scale
local x
local z
local centre = (scale/2)+1
while true do
for i = a,c do
x = i
z = b
local height = fBm(x/smoothness, z/smoothness,0,octaves,lancunarity,gain)
for y = 1, (height*mapHeight)+10 do
local density = fBm(x/smoothness, y/smoothness ,z/smoothness,octaves,lancunarity,gain)
if y > groundLvl then
if density*10 > 0 then
setCell(x*4, y*4, z*4,"stone")
end
else
setCell(x*4, y*4, z*4,"grass")
end
end
end
b = b + 1
for i = b,d do
x = c
z = i
local height = fBm(x/smoothness, z/smoothness,0,octaves,lancunarity,gain)
for y = 1, (height*mapHeight)+10 do
local density = fBm(x/smoothness, y/smoothness ,z/smoothness,octaves,lancunarity,gain)
if y > groundLvl then
if density*10 > 0 then
setCell(x*4, y*4, z*4,"stone")
end
else
setCell(x*4, y*4, z*4,"grass")
end
end
end
c = c - 1
for i = a,c do
x = (scale-i)
z = d
local height = fBm(x/smoothness, z/smoothness,0,octaves,lancunarity,gain)
for y = 1, (height*mapHeight)+10 do
local density = fBm(x/smoothness, y/smoothness ,z/smoothness,octaves,lancunarity,gain)
if y > groundLvl then
if density*10 > 0 then
setCell(x*4, y*4, z*4,"stone")
end
else
setCell(x*4, y*4, z*4,"grass")
end
end
end
d = d - 1
for i = b,d do
x = a
z = (scale-i)+1
local height = fBm(x/smoothness, z/smoothness,0,octaves,lancunarity,gain)
for y = 1, (height*mapHeight)+10 do
local density = fBm(x/smoothness, y/smoothness ,z/smoothness,octaves,lancunarity,gain)
if y > groundLvl then
if density*10 > 0 then
setCell(x*4, y*4, z*4,"stone")
end
else
setCell(x*4, y*4, z*4,"grass")
end
end
end
a = a + 1
if b >= centre then
break
end
wait()
end
for x = 1, scale do
for z = 1, scale do
setCell(x*4,0,z*4,"stone")
for y = 1, waterLvl do
setCell(x*4,(y*4)+0.05,z*4,"water")
end
end
end
I've tried turning this into a chunk generation function, but what happens is it would generate the same terrain in every chunk, which of course is not what I want. Can anybody point me in the right direction to make this generate in chunks?

1 comment
Try giving snippets of code where you're interfering a problem with that way it will be easier for Answerers to help you. Thanks!