Hello Devforum, I haven’t made a scripting support post in a while but I decided to rewrite my voxel terrain generator but I’m having some issues currently.
I have never used region3 before and Terrain:WriteVoxels() and I’m running in some issues.
I am running command
require(game.ReplicatedStorage.rubysvoxelworldgen.chunkmod).loadchunk(workspace.CurrentCamera.CFrame.Position)
in the command bar to test a chunk loading function,
but I keep getting this error.
(screenshot: https://gyazo.com/859aec268e7e919207fbb53a5742667e)
> require(game.ReplicatedStorage.rubysvoxelworldgen.chunkmod).loadchunk(workspace.CurrentCamera.CFrame.Position)
[Chunk load time: 0.095962285995483]
[23:17:28.545 - Region cannot be empty]
23:17:28.545 - Stack Begin
[23:17:28.546 - Script 'ReplicatedStorage.rubysvoxelworldgen.chunkmod', Line 68]
23:17:28.546 - Script 'require(game.ReplicatedStorage.rubysvoxelworldgen.chunkmod).loadchunk(workspace.CurrentCamera.CFrame.Position)', Line 1
23:17:28.546 - Stack End
local wdat = require(script.Parent.worlddata) --The mod with all data.
local genmod = require(script.worldgenmod) --The mod used for the noise function, etc.
local chunkdat = wdat.chunkdata
local matdat = wdat.chunkmatdat
local terrain = workspace.Terrain
local runservice = game:GetService("RunService")
--Advanced world configuration
local chunkres = 6
local chunkresy = 10
local voxelres = 4
local mod = {}
local v3 = Vector3.new
local reg3 = Region3.new
local floor = math.floor
local function chunksnap(pos)
return v3(
floor((pos.x / chunkres) + 0.5) * chunkres,
floor((pos.y / chunkresy) + 0.5) * chunkresy,
floor((pos.z / chunkres) + 0.5) * chunkres
) + v3(voxelres /2, voxelres / 2, voxelres / 2)
end
local function newreg(pos)
pos = chunksnap(pos)
local offset = v3(-(chunkres / 2) * voxelres, (chunkresy / 2) * voxelres, -(chunkres / 2) * voxelres)
--Return a region3 along with a start position for writing chunks.
return reg3(pos + offset, pos - offset):ExpandToGrid(voxelres), pos - offset
end
local noise = genmod.noise
mod.loadchunk = function(pos)
local starttick = tick()
local space, startpos = newreg(pos)
local voxtab = {}
local mattab = {}
for x = 1, chunkres do --x
for y = 1, chunkresy do --y
for z = 1, chunkres do --z
--Loop start
local voxelpos = startpos + v3(x * voxelres, y * voxelres, z * voxelres)
voxtab[x] = {}
voxtab[x][y] = {}
voxtab[x][y][z] = noise(voxelpos)
mattab[x] = {}
mattab[x][y] = {}
mattab[x][y][z] = Enum.Material.Grass
--Loop end
--print("Repeated")
end --z
end --y
runservice.Heartbeat:Wait()
end --x --Ends of all x, y, z loops
print("Chunk load time:", tick() - starttick)
terrain:WriteVoxels(space, voxelres, mattab, voxtab)
end
return mod
It keeps telling me regions can’t be empty and such but I am pretty sure they aren’t empty, first time using region3 tbh so I’m uncertain, but I also don’t know where the issue is at.
I feel like maybe at where I am trying to set the start position offset or so for the chunk to load (this is so when you load a chunk the “center” of the chunk isn’t at the corner), but again I am very unsure about that.
My previous terrain generator just spams the FillBlock() function of terrain and I want a terrain generator that generates more beautiful and costs less performance, figured out I had to use WriteVoxels() for that assuming that’s faster than just spamming the FillBlock() function in a loop, it also has the advantage of being able to generate caves and more impressive mountains, overhangs, etc, it made my script spike to 90% activity and heats up my computer since it’s supposed to generate a infinite world in run time so you can explore for yourself.