Hi, I am making a free-to-use community resource.
I have everything else working besides the chunks generating off-center a little bit
https://i.gyazo.com/65cc833d830125eff6e5458253a927a4.mp4
function Find_Block_By_ID(block_id)
--// Returns the game data block of the specified block_id
for _, block in GDM.Game_Data.Blocks:GetChildren() do
local id = block:GetAttribute('block_id')
if id == block_id then
return block
end
end
warn('No block with id of: '..block_id..' found!')
end
function Init_Chunk_Generation()
local render_distance = GDM.Config.generation_config.render_distance
local world_size = GDM.Config.generation_config.world_size * 3
local chunk_size = GDM.Config.generation_config.chunk_size
local function Render_Block(x, z, alpha, block_id)
--// Renders the block at the given x, y, and alpha positions
local block = Find_Block_By_ID('2'):Clone()
block:PivotTo(CFrame.new(Vector3.new(x, alpha, z)))
return block
end
local function Render_New_Chunk(chunk_position)
--// Renders a chunk at the given position
local chunk_data = {}
task.spawn(function()
for z = 1, chunk_size do
chunk_data[z] = {}
for x = 1, chunk_size do
local noise_X = ((((x * 3) + ((chunk_position.X - ((chunk_size * 3) / 2)) + 1.5)) / chunk_size) / world_size) * (28 * (world_size / 100))
local noise_Z = ((((z * 3) + ((chunk_position.Z - ((chunk_size * 3) / 2)) + 1.5)) / chunk_size) / world_size) * (37 * (world_size / 100))
local noise = math.noise(noise_X, noise_Z, GDM.Config.world_seed)
local alpha = ((math.floor(noise * 3) / 3) * 3) * 3
local x_pos = (x * 3) + ((chunk_position.X - ((chunk_size * 3) / 2)) + 1.5)
local z_pos = (z * 3) + ((chunk_position.Z - ((chunk_size * 3) / 2)) + 1.5)
chunk_data[z][x] = Vector3.new(x_pos, alpha, z_pos)
end
end
end)
table.insert(data.Chunk_Grid, {['chunk_position'] = chunk_position, ['chunk_data'] = chunk_data})
end
local function Render_Chunk(chunk_index, chunk_data)
--// Renders a chunk from the chunk_grid
local chunk_group = GDM.Bin.Chunk_Group:Clone()
chunk_group.Name = 'chunk_'..chunk_index
chunk_group.Parent = workspace.Chunk_Grid
for _, row in chunk_data do
for _, block_data in row do
local block = Render_Block(block_data.X, block_data.Z, block_data.Y, '2')
block.Parent = chunk_group
end
end
end
local function Render_Chunks_Near_Player(player)
local player_position = player.Character:WaitForChild('HumanoidRootPart').Position
for x = 1, render_distance do
for z = 1, render_distance do
local chunk_x = ((math.floor(player_position.X / (chunk_size * 3)) * (chunk_size * 3)) + ((chunk_size * 3) * x)) - ((chunk_size * 3) * render_distance) / 1.8
local chunk_z = ((math.floor(player_position.Z / (chunk_size * 3)) * (chunk_size * 3)) + ((chunk_size * 3) * z)) - ((chunk_size * 3) * render_distance) / 1.8
local chunk_identified = false
for _, chunk_data in data.Chunk_Grid do
if chunk_data.chunk_position == Vector3.new(chunk_x, 0, chunk_z) then
chunk_identified = true
break
end
end
if not chunk_identified then
Render_New_Chunk(Vector3.new(chunk_x, 0, chunk_z))
end
end
end
for chunk_index, chunk_data in pairs(data.Chunk_Grid) do
local physical_chunk = workspace.Chunk_Grid:FindFirstChild('chunk_'..chunk_index)
if (player_position - chunk_data.chunk_position).Magnitude <= (chunk_size * 3) * render_distance then
if not physical_chunk then
Render_Chunk(chunk_index, chunk_data.chunk_data)
end
else
if physical_chunk then
physical_chunk:Destroy()
end
end
end
end
task.spawn(function()
while true do
for _, player in GDM.Players:GetChildren() do
if player.Character then
Render_Chunks_Near_Player(player)
end
end
task.wait()
end
end)
end
And it would also be nice to know how to use parallel lua to render the surrounding chunks simultaneously.
Thanks