Best way to get trees in procedural terrain generation?

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]
end

function 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)
end

function 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 = (chunkZ
chunkScale) + z
local noise = math.noise(generationSeed,cx/xScale,cz/zScale)
local cy = noise*baseHeight
mountLayer(cx,cy,cz,Enum.Material.Snow)
end
end
end

function 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
end

function 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
end

while 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 http://wiki.roblox.com/index.php?title=Smooth_terrain#Procedural_terrain_generation as the base script to get an understanding.

I’ve searched aroung the devForums and the Wiki to kinda find ideas, but no use. :frowning:

Any help?

1 Like

Updated: I managed to figure it out :slight_smile:

Check out https://wiki.roblox.com/index.php?title=Random if you haven’t yet. This is the PRNG that @Fractality_alt recently integrated, and unlike math.random() and math.randomseed(), seeding it actually works and gives repeatable results.

1 Like

I’ll check it out. I ended up using random through the procedural script so it got some random tree generation in there. :slight_smile: :+1: