Hi,
For some context, I’m currently developing my own terrain
class. This class is used to copy, and paste terrain (The voxel kind) at certain places.
So what’s the problem? Well, once a terrain piece is copied, I can’t re-paste it in a spot I’d like. The only thing I managed to get working is copying the terrain, and pasting it within the exact same spot
Below is an image; the blue region is where I copy the terrain from, and the red region is where I’d like it to be pasted:
A demonstration of the issue at hand:
As for the code. Below is the module Terrain
, and a server-script Server
:
Terrain module:
debug.setmemorycategory("TerrainClass")
-- variables
local terrainModel = workspace:WaitForChild("Terrain")
local terrain = {}
terrain.__index = terrain
-- functions
function terrain.new(size: Vector3, position: Vector3)
local self = setmetatable({}, terrain)
self.minSignedVector = Vector3int16.new(position.X - size.X / 2, position.Y - size.Y / 2, position.Z - size.Z / 2)
self.maxSignedVector = Vector3int16.new(position.X + size.X / 2, position.Y + size.Y / 2, position.Z + size.Z / 2)
self.minVector = Vector3.new(position.X - size.X / 2, position.Y - size.Y / 2, position.Z - size.Z / 2)
self.maxVector = Vector3.new(position.X + size.X / 2, position.Y + size.Y / 2, position.Z + size.Z / 2)
self._region3int16 = Region3int16.new(self.minSignedVector, self.maxSignedVector)
self._region3 = Region3.new(self.minVector, self.maxVector)
self._terrainRegion = terrainModel:CopyRegion(self._region3int16)
return self
end
function terrain:Clear()
terrainModel:FillRegion(self._region3, 4, Enum.Material.Air)
end
function terrain:PasteAt(pasteEmptyCells: boolean, corner: Vector3int16)
terrainModel:PasteRegion(self._terrainRegion, corner, pasteEmptyCells)
end
#
return terrain
Server script:
-- variables
local regionToCopy, regionToPasteTo = workspace:WaitForChild("Region"), workspace:WaitForChild("Region2")
-- modules
local terrainModule = require(script:WaitForChild("Terrain"))
-- functions
local newTerrain = terrainModule.new(regionToCopy.Size, regionToCopy.Position)
newTerrain:Clear()
task.wait(3)
local regionToPasteToPosition, regionToPasteToSize = regionToPasteTo.Position, regionToPasteTo.Size
local newCorner = Vector3int16.new(regionToPasteToPosition.X - regionToPasteToSize.X / 2, regionToPasteToPosition.Y - regionToPasteToSize.Y / 2, regionToPasteToPosition.Z - regionToPasteToSize.Z / 2)
newTerrain:PasteAt(false, newCorner)
And the instance hierarchy in workspace is as such:
My main question would be, how would I get the correct “corner” with the size and position of the region? Any assistance, or guidance in the right direction helps a lot.
Thanks in advance!