I made world generation, and now I need to render it.
The issue I encountered is that rendering the entire world at once is going to be painfully slow, plus this times out the script. My map is 200x200, but only because it is in test version.
Then, I decided to make world rendering be similiar to minecraft, instead of rendering the entire world, I will slowly load the world in as the player walks around. Though this causes lag spikes.
I wonder how can I fix it, or maybe I need another way of rendering the world.
My script:
local Players = game:GetService("Players")
local SS = game:GetService("ServerStorage")
local RunService = game:GetService("RunService")
local blocksFolder = SS:WaitForChild("Blocks")
local blocks = {
Grass = blocksFolder:WaitForChild("Grass"),
Dirt = blocksFolder:WaitForChild("Dirt"),
Stone = blocksFolder:WaitForChild("Stone"),
Bedrock = blocksFolder:WaitForChild("Bedrock")
}
local nearestBlockPosses = {}
local blockSize = 3
for i = 0, 9 do
table.insert(nearestBlockPosses, i*blockSize)
end
local blocksWorkspace = workspace:WaitForChild("Blocks")
local X = 200
local Z = 200
local smoothness = 25
local grid = {}
for x = -(X/2), X/2 do
grid[x] = {}
task.spawn(function()
for z = -(Z/2), Z/2 do
local y = math.floor(math.noise(x/smoothness, z/smoothness) * 15)
y += blockSize*10
repeat y -= blockSize*10 until y < blockSize*10
local nearestPos = {0, math.huge}
for i = 1, #nearestBlockPosses do
local n = nearestBlockPosses[i]
local difference = math.abs(math.abs(y)-n)
if difference < nearestPos[2] then
nearestPos[1] = n
nearestPos[2] = difference
end
end
y = nearestPos[1]
grid[x][z] = y
end
end)
end
local generatedCords = {}
local function generateAroundPos(position:Vector3, radius:number)
for x = (position.X - math.round(radius/2)), (position.X + math.round(radius/2)) do
task.spawn(function()
for z = (position.Z - math.round(radius/2)), (position.Z + math.round(radius/2)) do
local xgrid = grid[x]
if not xgrid then continue end
local y = xgrid[z]
if not y then continue end
if table.find(generatedCords, tostring(x).."|"..tostring(z)) then continue end
local block = blocks["Grass"]:Clone()
block.Position = Vector3.new(x*blockSize, y, z*blockSize)
block.Parent = blocksWorkspace
task.spawn(function()
for i = y-blockSize, -(blockSize*3), -blockSize do
local dirt = blocks["Dirt"]:Clone()
dirt.Position = Vector3.new(x*blockSize, i, z*blockSize)
dirt.Parent = blocksWorkspace
end
for i = -(blockSize*4), -(blockSize*6), -blockSize do
local stone = blocks["Stone"]:Clone()
stone.Position = Vector3.new(x*blockSize, i, z*blockSize)
stone.Parent = blocksWorkspace
end
local bedrock = blocks["Bedrock"]:Clone()
bedrock.Position = Vector3.new(x*blockSize, -(blockSize*7), z*blockSize)
bedrock.Parent = blocksWorkspace
end)
table.insert(generatedCords, tostring(x).."|"..tostring(z))
end
end)
end
end
generateAroundPos(Vector3.new(0, 0, 0), 50)
local last_position = {}
local function onPlayer(plr:Player)
last_position[plr] = Vector3.new(0, 0, 0)
end
Players.PlayerAdded:Connect(onPlayer)
for i, v in ipairs(Players:GetChildren()) do
onPlayer(v)
end
RunService.Stepped:Connect(function(t, dt)
for i, plr:Player in ipairs(Players:GetChildren()) do
local char = plr.Character
if not char then continue end
local root:BasePart = char:FindFirstChild("HumanoidRootPart")
if not root then continue end
local last_pos = last_position[plr]
if not last_pos then continue end
local pos = Vector3.new(math.round(root.Position.X/blockSize), 0, math.round(root.Position.Z/blockSize))
if (pos - last_pos).Magnitude > blockSize then
last_position[plr] = pos
task.spawn(function()
generateAroundPos(pos, 50)
end)
end
end
end)
Thanks in advance.