-
What do you want to achieve?
I wanted to create voxel terrain generation. For instance, minecraft terrain generation. I was following a few tutorials.
2. What is the issue?
The issue is that the chunk doesn’t spawn near the camera and that there are holes in my terrain. (I put my camera where the character is, before the chunk generated)
-
What solutions have you tried so far?
I tried looking on the devforum for solutions, but most of them were generating chunks at spawn or did not work out.
local NoiseUtil = {}
type int = number
function NoiseUtil.Generate2DFractalNoise(X: int, Y: int, Octaves: int, Lacunarity: int, Persistence: int, Scale: int, Seed: int)
local Value = 0
local x1 = X
local y1 = Y
local Amplitude = 1
for i = 1, Octaves, 1 do
Value += math.abs(math.noise(x1 / Scale, y1 / Scale, Seed)) * Amplitude
y1 *= Lacunarity
x1 *= Lacunarity
Amplitude *= Persistence
end
Value = Value ^ 2
return math.clamp(Value, -1, 1)
end
local MAP_SIZE = 250.0
local MAP_SCALE = MAP_SIZE/2.0
local NOISE_SCALE = 50.0
local HEIGHT_SCALE = 50.0
local OCTAVES = 4.0
local LACUNARITY = 3.0
local PERSISTENCE = 0.35
local BLOCK_SIZE = 3.0
local CHUNK_SIZE = 30.0
local NUMBER_OF_CHUNKS = math.ceil(MAP_SCALE/CHUNK_SIZE)
local ChunkPositionX = 1
local ChunkPositionZ = 1
local function updateChunkPositionVariables()
local camPos = Camera.CFrame.Position
ChunkPositionX = camPos.X // CHUNK_SIZE
ChunkPositionZ = camPos.Z // CHUNK_SIZE
end
local function generateBlock(x, y, z, color, chunkfolder)
local grass = Template:Clone()
local height = grass.PrimaryPart.Size.Y
local realx = (x // BLOCK_SIZE) * BLOCK_SIZE
local realY = (math.floor(y) * BLOCK_SIZE)
local realz = (z // BLOCK_SIZE) * BLOCK_SIZE
grass:PivotTo(CFrame.new(x, realY, z))
grass.PrimaryPart.BrickColor = BrickColor.Green()
grass.PrimaryPart.TopSurface = Enum.SurfaceType.Smooth
grass.PrimaryPart.BottomSurface = Enum.SurfaceType.Smooth
grass.PrimaryPart.Color = color
grass.Parent = chunkfolder
return grass
end
local function generateASingleChunk(chunkX: number, chunkZ: number)
local PlayerPos = Camera.CFrame.Position
for x = ChunkPositionX, ChunkPositionX + CHUNK_SIZE do
for z = ChunkPositionZ, ChunkPositionZ + CHUNK_SIZE do
local realX = (chunkX - 1) * CHUNK_SIZE + x --The problem might be here
local realZ = (chunkZ - 1) * CHUNK_SIZE + z
local height2d = NoiseUtil.Generate2DFractalNoise(realX, realZ,
OCTAVES,
LACUNARITY,
PERSISTENCE,
NOISE_SCALE,
SEED
) * HEIGHT_SCALE
generateBlock(realX * BLOCK_SIZE, height2d, realZ * BLOCK_SIZE, Color3.new(1,1,1), WorldFolder)
end
end
end
task.wait(4)
updateChunkPositionVariables()
generateASingleChunk(ChunkPositionX, ChunkPositionZ)