WriteVoxels vs. PasteRegion

Hi, I’m currently working on a game where terrain maps are created from seeds. A big problem right now is that it takes forever to generate terrains larger than 1000x1000 with WriteVoxel. In fact, attempting to WriteVoxel() a 1000x1000 map will throw you an error, so I had program a chunk system, where 100x100 pieces are loaded one after the other.

However, when I run this code:

terrainRegion = workspace.Terrain:CopyRegion(workspace.Terrain.MaxExtents)
workspace.Terrain:Clear()
wait(15)
workspace.Terrain:PasteRegion(terrainRegion, workspace.Terrain.MaxExtents.Min, true)

The terrain will clear for a few seconds, and then load in just a few seconds again. I’m talking like 5 seconds, compared to 85 with WriteVoxels.

My question is how can better use PasteRegion to my advantage? I generate the map with a height map (with math.noise) and WriteVoxel(). Is it possible to save terrainRegion from above in a DataStore for example? Or somehow create a TerrainRegion from a height map? As I said, I’m comparing 5 seconds to 85 right now…

1 Like

It looks like you might be able to save and load (with datastores) terrain using these two functions: Documentation - Roblox Creator Hub & Documentation - Roblox Creator Hub

Here’s some code I was playing around with that should help you:

local terrain = game.Workspace.Terrain
local resolution = 4 --  I think this is redundant, trying to set a resolution other than 4 throws an error

local region = Region3.new(Vector3.new(-150, -150, -150), Vector3.new(150,150,150))
local region2 = Region3.new(Vector3.new(-150, 150, 350), Vector3.new(150,450,650))

region = region:ExpandToGrid(resolution)
region2 = region2:ExpandToGrid(resolution)

local terrainVoxels, terrainVoxelOccupancy = terrain:ReadVoxels(region, resolution)
--terrain:Clear()
terrain:WriteVoxels(region2, resolution, terrainVoxels, terrainVoxelOccupancy)
1 Like

I’m talking about CopyRegion and PasteRegion. My script uses WriteVoxels and it takes forever to load compared to when I try PasteRegion in the command bar. I want to be able to store a TerrainRegion though, or somehow turn a terrain material and occupancy array into a TerrainRegion

Prerequisites:

  • Reverse engineer the current Terrain format.
  • Write a 3rd-party server that receives seeds, generates voxels, and writes them in that format.

Steps:

  1. Send an HTTP request to the server with the desired seed.
  2. Wrap the formatted voxels in a serialized TerrainRegion.
  3. Upload the TerrainRegion as a model.
  4. Insert the model into the server via InsertService.
  5. PasteRegion the inserted model.

Hmmm…

1 Like

Are you sure it’s the actual write voxel function causing the lag and not the algorithm creating the heightmap? I’m able to create a map approximately 1k studs x 1k studs x 256 studs in under a second.

Repro: terrain.rbxl (81.1 KB)

1 Like

And hey, you have the specification right here!

2 Likes