New to figuring out how procedural generation works using terrain. Trying to find methods for getting objects placed into the procedural generation.
End goal
To get structures in. (Trees, not buildings)
https://www.youtube.com/watch?v=1IQy_8g5SYc
The Attempt
local baseHeight = 5 – The main height factor for the terrain.
local chunkScale = 3 – The grid scale for terrain generation. Should be kept relatively low if used in real-time.
local renderDist = 320/4 – The length/width of chunks in voxels that should be around the player at all times
local xScale = 90/4 – How much we should strech the X scale of the generation noise
local zScale = 90/4 – How much we should strech the Z scale of the generation noise
local generationSeed = math.random() – Seed for determining the main height map of the terrain.
local minTreeGen = 30
local maxTreeGen = 80
local chunks = {}
function chunkExists(chunkX,chunkZ)
if not chunks[chunkX] then
chunks[chunkX] = {}
end
return chunks[chunkX][chunkZ]
endfunction mountLayer(x,heightY,z,material)
local begY = -baseHeight
local endY = heightY
workspace.Terrain:FillBlock(CFrame.new(x*4+2, (begY+endY)4/2, z4+2), Vector3.new(4, (endY-begY)*4, 4), material)
endfunction makeChunk(chunkX,chunkZ)
local rootPos = Vector3.new(chunkXchunkScale,0,chunkZchunkScale)
chunks[chunkX][chunkZ] = true – Acknowledge the chunk’s existance.
for x = 0,chunkScale-1 do
for z = 0,chunkScale-1 do
local cx = (chunkXchunkScale) + x
local cz = (chunkZchunkScale) + z
local noise = math.noise(generationSeed,cx/xScale,cz/zScale)
local cy = noise*baseHeight
mountLayer(cx,cy,cz,Enum.Material.Snow)
end
end
endfunction populateForest(chunkX,chunkZ)
local range = math.max(1,renderDist/chunkScale)
for x = -range,range,10 do
for z = -range,range,10 do
local spawnTrees = math.random(minTreeGen,maxTreeGen)
local treeTypes = game.ServerStorage.GenerationGuides.TreeTypes:GetChildren()
local pickedTreeType = treeTypes[math.random(1,#treeTypes)]
local treeSpawner = pickedTreeType:Clone()
treeSpawner.Parent = workspace.Trees
treeSpawner.Position = Vector3.new(x,0,z)
end
end
endfunction checkSurroundings(location)
local chunkX,chunkZ = math.floor(location.X/4/chunkScale),math.floor(location.Z/4/chunkScale)
local range = math.max(1,renderDist/chunkScale)
for x = -range,range do
for z = -range,range do
local cx,cz = chunkX + x,chunkZ + z
if not chunkExists(cx,cz) then
makeChunk(cx,cz)
populateForest(x,z)
end
end
end
endwhile true do
for _,player in pairs(game.Players:GetPlayers()) do
if player.Character then
local torso = player.Character:FindFirstChild(“UpperTorso”)
if torso then
checkSurroundings(torso.Position)
end
end
end
wait(1)
end
Issues: Not really sure what should be called. But I have been using Documentation - Roblox Creator Hub as the base script to get an understanding.
I’ve searched aroung the devForums and the Wiki to kinda find ideas, but no use.
Any help?