Hello, I am wondering how I can make a chunk system like minecrafts? I have the chunk generation bit just find its just rendering and un rendering them relative to the cameras position. There is a tutorial by
okeanskiy on youtube but that method is slow and not very optimized for my means. Here is my chunk generation code:
ModuleScript
math.randomseed(tick())
local Module = {}
Module.__index = Module
Module.new = function()
local Chunk = {}
Chunk.Parts = {}
Chunk.Seed = math.random()
setmetatable(Chunk, Module)
return Chunk
end
Module.Generate = function(Chunk, X, Z)
local Folder = Instance.new("Folder")
Folder.Name = "Chunk: " .. X .. " " .. Z
Folder.Parent = workspace
local Seed = Chunk.Seed
print(Seed)
for Number = 1, 16 do
for Number2 = 1, 16 do
local Part = Instance.new("Part")
Part.Material = Enum.Material.SmoothPlastic
Part.CFrame = CFrame.new(X * 16 * 4 + Number * 4 - 4, 0, Z * 16 * 4 + Number2 * 4 - 4)
Part.Anchored = true
Part.Size = Vector3.new(4, 64 - Part.CFrame.Position.Y, 4)
Part.Parent = Folder
Part.CFrame = Part.CFrame + Vector3.new(0, math.noise(Part.CFrame.Position.X / 40, Part.CFrame.Position.Z / 40, Seed) * 25, 0)
table.insert(Chunk.Parts, Part)
end
wait()
end
end
Module.Destroy = function(Chunk)
for Number, Instance2 in pairs(Chunk.Parts) do
Instance2:Destroy()
end
end
return Module
Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ModuleScripts = ReplicatedStorage.ModuleScripts
local Algorithm = require(ModuleScripts.Algorithm)
local Chunk = Algorithm.new()
local Size = 30
for Number = -Size / 2, Size / 2 do
for Number2 = -Size / 2, Size / 2 do
Chunk:Generate(Number, Number2)
end
end
but if someone could help me out with generating them relative to the camera position I would be very grateful!